Skip to content

Commit d5ea2a4

Browse files
committed
Add support for Registration Tokens
1 parent b4e1c14 commit d5ea2a4

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

tests/foreman/api/test_registration.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
from robottelo import constants
2222
from robottelo.config import (
23+
admin_nailgun_config,
2324
settings,
2425
user_nailgun_config,
2526
)
@@ -447,3 +448,90 @@ def test_positive_katello_ca_crt_refresh(
447448
# check if the certificate file is refreshed
448449
ca_file_after_refresh = len(str(rhel_contenthost.execute(f'cat {katello_ca_crt_path}')))
449450
assert ca_cert_file == ca_file_after_refresh
451+
452+
453+
@pytest.mark.no_containers
454+
@pytest.mark.rhel_ver_list([settings.content_host.default_rhel_version])
455+
def test_positive_invalidate_users_tokens(
456+
target_sat, request, module_org, module_location, rhel_contenthost, module_activation_key
457+
):
458+
"""Verify invalidating single and multiple users tokens.
459+
460+
:id: ee45cd69-d993-494c-8a14-c977096c1f52
461+
462+
:steps:
463+
1. Create an admin user and a non-admin user with "edit_users" and "register_hosts" permission.
464+
2. Generate a token with admin user and register a host with it, it should be successful.
465+
3. Invalidate the token and try to use the generated token again to register the host, it should fail.
466+
4. Invalidate tokens for multiple users with "invalidate-multiple" command, it should invalidate all the tokens for provided users.
467+
5. Repeat Steps 2,3 and 4 with non-admin user and it should work the same way.
468+
469+
:expectedresults: Host registration will not be possible after/with invalidated tokens.
470+
471+
:CaseImportance: Critical
472+
473+
:Verifies: SAT-30383
474+
"""
475+
password = settings.server.admin_password
476+
477+
# Admin User
478+
admin_user = target_sat.api.User().search(
479+
query={'search': f'login={settings.server.admin_username}'}
480+
)[0]
481+
482+
# Non-Admin user with "edit_users" permission and "Register hosts" role
483+
roles = [target_sat.api.Role().create()]
484+
host_register_role = target_sat.api.Role().search(query={'search': 'name="Register hosts"'})[0]
485+
roles.append(host_register_role)
486+
user_permissions = {
487+
'User': ['edit_users'],
488+
}
489+
target_sat.api_factory.create_role_permissions(roles[0], user_permissions)
490+
491+
non_admin_user = target_sat.api.User(
492+
login=gen_string('alpha'),
493+
password=password,
494+
organization=[module_org],
495+
location=[module_location],
496+
role=roles,
497+
).create()
498+
499+
# delete the users
500+
@request.addfinalizer
501+
def _finalize():
502+
target_sat.api.Host(name=rhel_contenthost.hostname).search()[0].delete()
503+
non_admin_user.delete()
504+
505+
# Generate token and verify token invalidation
506+
for usertype in (admin_user, non_admin_user):
507+
user = admin_user if usertype.admin else non_admin_user
508+
user_cfg = user_nailgun_config(user.login, password)
509+
510+
cmd = target_sat.api.RegistrationCommand(
511+
server_config=user_cfg,
512+
organization=module_org,
513+
location=module_location,
514+
activation_keys=[module_activation_key.name],
515+
insecure=True,
516+
).create()
517+
result = rhel_contenthost.execute(cmd.strip('\n'))
518+
assert result.status == 0, f'Failed to register host: {result.stderr}'
519+
520+
# Invalidate JWTs for a single user
521+
result = target_sat.api.RegistrationTokens(
522+
server_config=user_cfg, user=user.id
523+
).invalidate()
524+
assert 'Successfully invalidated registration tokens' in result['message']
525+
assert user.login in result['user']
526+
527+
rhel_contenthost.unregister()
528+
# Re-register the host with invalidated token
529+
result = rhel_contenthost.execute(cmd.strip('\n'))
530+
assert result.status == 1
531+
assert 'ERROR: unauthorized' in result.stdout
532+
533+
# Invalidate JWTs for multiple users
534+
result = target_sat.api.RegistrationTokens(server_config=user_cfg).invalidate_multiple(
535+
search=f'id ^ ({admin_user.id, non_admin_user.id})'
536+
)
537+
assert 'Successfully invalidated registration tokens' in result['message']

0 commit comments

Comments
 (0)