Skip to content

Commit 6309f7c

Browse files
committed
Add support for Registration Tokens
1 parent 8add939 commit 6309f7c

File tree

1 file changed

+98
-1
lines changed

1 file changed

+98
-1
lines changed

tests/foreman/api/test_user.py

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import pytest
2424
from requests.exceptions import HTTPError
2525

26-
from robottelo.config import settings
26+
from robottelo.config import settings, user_nailgun_config
2727
from robottelo.constants import LDAP_ATTR, LDAP_SERVER_TYPE, DataFile
2828
from robottelo.utils import gen_ssh_keypairs
2929
from robottelo.utils.datafactory import (
@@ -981,3 +981,100 @@ def test_expired_personal_access_token(self):
981981
:CaseImportance: Medium
982982
983983
"""
984+
985+
986+
@pytest.mark.no_containers
987+
@pytest.mark.parametrize('admin_enable', [True, False])
988+
@pytest.mark.rhel_ver_list([settings.content_host.default_rhel_version])
989+
def test_positive_invalidate_users_tokens(
990+
target_sat, request, admin_enable, module_org, module_location, rhel_contenthost, module_activation_key
991+
):
992+
"""Verify invalidating single and multiple users tokens.
993+
994+
:id: ee45cd69-d993-494c-8a14-c977096c1f52
995+
996+
:steps:
997+
1. Create an admin user and a non-admin user with "edit_users" and "register_hosts" permission.
998+
2. Generate a token with admin user and register a host with it, it should be successful.
999+
3. Invalidate the token and try to use the generated token again to register the host, it should fail.
1000+
4. Invalidate tokens for multiple users with "invalidate-multiple" command, it should invalidate all the tokens for provided users.
1001+
5. Repeat Steps 2,3 and 4 with non-admin user and it should work the same way.
1002+
1003+
:expectedresults: Tokens invalidated cannot be used for registration of hosts.
1004+
1005+
:CaseImportance: Critical
1006+
1007+
:Verifies: SAT-30383
1008+
"""
1009+
admin_login = gen_string('alpha')
1010+
non_admin_login = gen_string('alpha')
1011+
password = gen_string('alpha')
1012+
1013+
admin_user = target_sat.api.User(
1014+
login=admin_login,
1015+
password=password,
1016+
organization=[module_org],
1017+
location=[module_location],
1018+
admin=True,
1019+
).create()
1020+
user = admin_user
1021+
login = admin_login
1022+
1023+
# Non-Admin user with "edit_users" permission and "Register hosts" role
1024+
roles = [target_sat.api.Role().create()]
1025+
host_register_role = target_sat.api.Role().search(query={'search': 'name="Register hosts"'})[0]
1026+
roles.append(host_register_role)
1027+
user_permissions = {
1028+
'User': ['edit_users'],
1029+
'Host': ['create_hosts'],
1030+
}
1031+
target_sat.api_factory.create_role_permissions(roles[0], user_permissions)
1032+
1033+
non_admin_user = target_sat.api.User(
1034+
login=non_admin_login,
1035+
password=password,
1036+
organization=[module_org],
1037+
location=[module_location],
1038+
role=roles,
1039+
).create()
1040+
1041+
# delete the users
1042+
@request.addfinalizer
1043+
def _finalize():
1044+
target_sat.api.Host(name=rhel_contenthost.hostname).search()[0].delete()
1045+
admin_user.delete()
1046+
non_admin_user.delete()
1047+
1048+
if not admin_enable:
1049+
login = non_admin_login
1050+
user = non_admin_user
1051+
1052+
# Generate token and verify token invalidation
1053+
user_cfg = user_nailgun_config(login, password)
1054+
cmd = target_sat.api.RegistrationCommand(
1055+
server_config=user_cfg,
1056+
organization=module_org,
1057+
location=module_location,
1058+
activation_keys=[module_activation_key.name],
1059+
insecure=True,
1060+
).create()
1061+
result = rhel_contenthost.execute(cmd.strip('\n'))
1062+
assert result.status == 0, f'Failed to register host: {result.stderr}'
1063+
1064+
# Invalidate JWTs for a single user
1065+
result = target_sat.api.RegistrationTokens(server_config=user_cfg, user=user.id).invalidate()
1066+
assert 'Successfully invalidated registration tokens' in result['message']
1067+
assert user.login in result['user']
1068+
1069+
rhel_contenthost.unregister()
1070+
# Re-register the host with invalidated token
1071+
result = rhel_contenthost.execute(cmd.strip('\n'))
1072+
assert result.status == 1
1073+
assert 'ERROR: unauthorized' in result.stdout
1074+
1075+
# Invalidate JWTs for multiple users
1076+
result = target_sat.api.RegistrationTokens(server_config=user_cfg).invalidate_multiple(
1077+
search=f'id ^ ({user.id})'
1078+
)
1079+
assert 'Successfully invalidated registration tokens' in result['message']
1080+

0 commit comments

Comments
 (0)