How to use assign_user_role method in tempest

Best Python code snippet using tempest_python

test_roles.py

Source:test_roles.py Github

copy

Full Screen

...81 self.assertTrue(resp['status'].startswith('2'))82 self.addCleanup(self.client.delete_role, role1_id)83 self.assertRaises(exceptions.Duplicate, self.client.create_role,84 role_name)85 def test_assign_user_role(self):86 # Assign a role to a user on a tenant87 (user, tenant, role) = self._get_role_params()88 self.client.assign_user_role(tenant['id'], user['id'], role['id'])89 resp, roles = self.client.list_user_roles(tenant['id'], user['id'])90 self.assert_role_in_role_list(role, roles)91 def test_assign_user_role_by_unauthorized_user(self):92 # Non admin user should not be authorized to assign a role to user93 (user, tenant, role) = self._get_role_params()94 self.assertRaises(exceptions.Unauthorized,95 self.non_admin_client.assign_user_role,96 tenant['id'], user['id'], role['id'])97 def test_assign_user_role_request_without_token(self):98 # Request to assign a role to a user without a valid token99 (user, tenant, role) = self._get_role_params()100 token = self.client.get_auth()101 self.client.delete_token(token)102 self.assertRaises(exceptions.Unauthorized,103 self.client.assign_user_role, tenant['id'],104 user['id'], role['id'])105 self.client.clear_auth()106 def test_assign_user_role_for_non_existent_user(self):107 # Attempt to assign a role to a non existent user should fail108 (user, tenant, role) = self._get_role_params()109 self.assertRaises(exceptions.NotFound, self.client.assign_user_role,110 tenant['id'], 'junk-user-id-999', role['id'])111 def test_assign_user_role_for_non_existent_role(self):112 # Attempt to assign a non existent role to user should fail113 (user, tenant, role) = self._get_role_params()114 self.assertRaises(exceptions.NotFound, self.client.assign_user_role,115 tenant['id'], user['id'], 'junk-role-id-12345')116 def test_assign_user_role_for_non_existent_tenant(self):117 # Attempt to assign a role on a non existent tenant should fail118 (user, tenant, role) = self._get_role_params()119 self.assertRaises(exceptions.NotFound, self.client.assign_user_role,120 'junk-tenant-1234', user['id'], role['id'])121 def test_assign_duplicate_user_role(self):122 # Duplicate user role should not get assigned123 (user, tenant, role) = self._get_role_params()124 self.client.assign_user_role(tenant['id'], user['id'], role['id'])125 self.assertRaises(exceptions.Duplicate, self.client.assign_user_role,126 tenant['id'], user['id'], role['id'])127 def test_remove_user_role(self):128 # Remove a role assigned to a user on a tenant129 (user, tenant, role) = self._get_role_params()130 resp, user_role = self.client.assign_user_role(tenant['id'],131 user['id'], role['id'])132 resp, body = self.client.remove_user_role(tenant['id'], user['id'],133 user_role['id'])134 self.assertEquals(resp['status'], '204')135 def test_remove_user_role_by_unauthorized_user(self):136 # Non admin user should not be authorized to remove a user's role137 (user, tenant, role) = self._get_role_params()138 resp, user_role = self.client.assign_user_role(tenant['id'],139 user['id'],140 role['id'])141 self.assertRaises(exceptions.Unauthorized,142 self.non_admin_client.remove_user_role,143 tenant['id'], user['id'], role['id'])144 def test_remove_user_role_request_without_token(self):145 # Request to remove a user's role without a valid token146 (user, tenant, role) = self._get_role_params()147 resp, user_role = self.client.assign_user_role(tenant['id'],148 user['id'],149 role['id'])150 token = self.client.get_auth()151 self.client.delete_token(token)152 self.assertRaises(exceptions.Unauthorized,153 self.client.remove_user_role, tenant['id'],154 user['id'], role['id'])155 self.client.clear_auth()156 def test_remove_user_role_non_existant_user(self):157 # Attempt to remove a role from a non existent user should fail158 (user, tenant, role) = self._get_role_params()159 resp, user_role = self.client.assign_user_role(tenant['id'],160 user['id'],161 role['id'])162 self.assertRaises(exceptions.NotFound, self.client.remove_user_role,163 tenant['id'], 'junk-user-id-123', role['id'])164 def test_remove_user_role_non_existant_role(self):165 # Attempt to delete a non existent role from a user should fail166 (user, tenant, role) = self._get_role_params()167 resp, user_role = self.client.assign_user_role(tenant['id'],168 user['id'],169 role['id'])170 self.assertRaises(exceptions.NotFound, self.client.remove_user_role,171 tenant['id'], user['id'], 'junk-user-role-123')172 def test_remove_user_role_non_existant_tenant(self):173 # Attempt to remove a role from a non existent tenant should fail174 (user, tenant, role) = self._get_role_params()175 resp, user_role = self.client.assign_user_role(tenant['id'],176 user['id'],177 role['id'])178 self.assertRaises(exceptions.NotFound, self.client.remove_user_role,179 'junk-tenant-id-123', user['id'], role['id'])180 def test_list_user_roles(self):181 # List roles assigned to a user on tenant182 (user, tenant, role) = self._get_role_params()183 self.client.assign_user_role(tenant['id'], user['id'], role['id'])184 resp, roles = self.client.list_user_roles(tenant['id'], user['id'])185 self.assert_role_in_role_list(role, roles)186 def test_list_user_roles_by_unauthorized_user(self):187 # Non admin user should not be authorized to list a user's roles188 (user, tenant, role) = self._get_role_params()189 self.client.assign_user_role(tenant['id'], user['id'], role['id'])190 self.assertRaises(exceptions.Unauthorized,191 self.non_admin_client.list_user_roles, tenant['id'],192 user['id'])193 def test_list_user_roles_request_without_token(self):194 # Request to list user's roles without a valid token should fail195 (user, tenant, role) = self._get_role_params()196 token = self.client.get_auth()197 self.client.delete_token(token)198 try:199 self.assertRaises(exceptions.Unauthorized,200 self.client.list_user_roles, tenant['id'],201 user['id'])202 finally:203 self.client.clear_auth()...

Full Screen

Full Screen

Main.py

Source:Main.py Github

copy

Full Screen

...17 action3 = controller.add_action('read')18 action4 = controller.add_action('delete')19 # user1 has admin & teacher role20 # user2 has student role21 controller.assign_user_role(user1.get_id(), role1.get_name())22 controller.assign_user_role(user1.get_id(), role2.get_name())23 controller.assign_user_role(user2.get_id(), role3.get_name())24 # Assign role and action type to resources25 controller.add_rule(role1.get_name(), action1.get_name(), res1.get_name()) # admin can create exam paper26 controller.add_rule(role2.get_name(), action2.get_name(), res1.get_name()) # teacher can write questions on paper27 controller.add_rule(role3.get_name(), action3.get_name(), res1.get_name()) # students can read from paper28 print "Users: "+str(controller.list_all_user_roles())29 print "Resources: "+str(controller.list_resource_access_roles())30 # verify user access31 print controller.verify_user_access(user1.get_id(), action1.get_name(), res1.get_name()) # True32 print controller.verify_user_access(user2.get_id(), action3.get_name(), res1.get_name()) # True33 # user2 pass out, user2 doesn't have access to student role34 print "----- Removed student role from user2 -----"35 controller.remove_user_role(user2.get_id(), role3.get_name())36 print controller.verify_user_access(user2.get_id(), action3.get_name(), res1.get_name()) # False37 print controller.list_all_user_roles()

Full Screen

Full Screen

0007_auto_20180421_0739.py

Source:0007_auto_20180421_0739.py Github

copy

Full Screen

1# Generated by Django 2.0.3 on 2018-04-21 11:392from django.db import migrations3from core.choices import USER_ROLE4def assign_user_role(apps, schema_editor):5 User = apps.get_model('core', 'User')6 for user in User.objects.all():7 if user.is_superuser or user.is_staff:8 user.role = USER_ROLE.staff9 else:10 user.role = USER_ROLE.stylist11 user.save(update_fields=['role', ])12class Migration(migrations.Migration):13 dependencies = [14 ('core', '0006_user_role'),15 ]16 operations = [17 migrations.RunPython(assign_user_role, migrations.RunPython.noop),18 ]

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run tempest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful