Best Python code snippet using tempest_python
test_oauth1_tokens.py
Source:test_oauth1_tokens.py
...38 """Authorize request token to provide access to specific roles."""39 user_id = self.oauth_token_client.auth_provider.credentials.user_id40 project_id = (41 self.oauth_token_client.auth_provider.credentials.project_id)42 roles = self.roles_client.list_user_roles_on_project(43 project_id, user_id)44 role_ids = [role['id'] for role in roles['roles']]45 oauth_verifier = self.oauth_token_client.authorize_request_token(46 request_token['oauth_token'], role_ids)47 return oauth_verifier['token']48 def _create_access_token(self, consumer):49 """Create access token for a consumer."""50 request_token = self._create_request_token(consumer)51 oauth_verifier = self._authorize_request_token(request_token)52 access_token = self.oauth_token_client.create_access_token(53 consumer['id'], consumer['secret'],54 request_token['oauth_token'],55 request_token['oauth_token_secret'],56 oauth_verifier['oauth_verifier'])57 # cleans up access tokens after tests58 user_id = self.oauth_token_client.auth_provider.credentials.user_id59 self.addCleanup(test_utils.call_and_ignore_notfound_exc,60 self.oauth_token_client.revoke_access_token,61 user_id, access_token['oauth_token'])62 return access_token63 @decorators.idempotent_id('23d2fe8d-fc8d-4bef-8aaa-289400732c3f')64 def test_create_and_show_consumer(self):65 """Tests to make sure that a consumer with parameters is made."""66 consumer = self._create_consumer()67 # fetch created consumer from client68 fetched_consumer = self.consumers_client.show_consumer(69 consumer['id'])['consumer']70 # assert that the fetched consumer matches the created one and71 # has all parameters72 for key in ['description', 'id', 'links']:73 self.assertEqual(consumer[key], fetched_consumer[key])74 @decorators.idempotent_id('3820f3d0-9b06-4d15-8f01-c7dd4eea30a2')75 def test_delete_consumer(self):76 """Tests the delete functionality for resource consumer."""77 consumer = self._create_consumer()78 # fetch consumer from client to confirm it exists79 fetched_consumer = self.consumers_client.show_consumer(80 consumer['id'])['consumer']81 self.assertEqual(consumer['id'], fetched_consumer['id'])82 # delete existing consumer83 self.consumers_client.delete_consumer(consumer['id'])84 # check that consumer no longer exists85 self.assertRaises(lib_exc.NotFound,86 self.consumers_client.show_consumer,87 consumer['id'])88 @decorators.idempotent_id('5a03fa78-3a03-449b-a04c-ef9de80eb6c4')89 def test_update_consumer(self):90 """Tests the update functionality for resource consumer."""91 # create a new consumer to update92 consumer = self._create_consumer()93 # create new description94 new_description = data_utils.rand_name('test_update_consumer')95 # update consumer96 self.consumers_client.update_consumer(consumer['id'],97 new_description)98 # check that the same consumer now has the new description99 updated_consumer = self.consumers_client.show_consumer(100 consumer['id'])['consumer']101 self.assertEqual(new_description, updated_consumer['description'])102 @decorators.idempotent_id('6da689b1-39a0-44ee-9624-445159119c57')103 def test_list_consumers(self):104 """Test for listing consumers."""105 # create two consumers to populate list106 new_consumer_one = self._create_consumer()107 new_consumer_two = self._create_consumer()108 # fetch the list of consumers109 consumer_list = self.consumers_client.list_consumers()['consumers']110 # add fetched consumer ids to a list111 id_list = [consumer['id'] for consumer in consumer_list]112 # check if created consumers are in the list113 self.assertIn(new_consumer_one['id'], id_list)114 self.assertIn(new_consumer_two['id'], id_list)115 @decorators.idempotent_id('a17d60e4-7cb5-4e06-9e16-b044f3ee6d94')116 def test_create_request_token(self):117 """Test to create request token for consumer."""118 consumer = self._create_consumer()119 request_token = self._create_request_token(consumer)120 # check that oauth token id and secret exists121 self.assertIsNotNone(request_token['oauth_token'])122 self.assertIsNotNone(request_token['oauth_token_secret'])123 @decorators.idempotent_id('607aecc4-a623-4566-a3a5-bb0e2a6fc9c5')124 def test_authorize_request_token(self):125 """Test to authorize a request token for specific role."""126 consumer = self._create_consumer()127 request_token = self._create_request_token(consumer)128 oauth_verifier = self._authorize_request_token(request_token)129 # check that oauth verifier exists130 self.assertIsNotNone(oauth_verifier['oauth_verifier'])131 @decorators.idempotent_id('7d488fc9-342c-4c12-b6b8-b158e2183925')132 def test_create_access_token(self):133 """Test to create access token for consumer."""134 consumer = self._create_consumer()135 access_token = self._create_access_token(consumer)136 user_id = self.oauth_token_client.auth_provider.credentials.user_id137 project_id = (138 self.oauth_token_client.auth_provider.credentials.project_id)139 fetched_access_token = self.oauth_token_client.get_access_token(140 user_id, access_token['oauth_token'])['access_token']141 # check that access token details matches142 self.assertEqual(access_token['oauth_token'],143 fetched_access_token['id'])144 self.assertEqual(consumer['id'], fetched_access_token['consumer_id'])145 self.assertEqual(access_token['oauth_expires_at'],146 fetched_access_token['expires_at'])147 self.assertEqual(project_id, fetched_access_token['project_id'])148 self.assertEqual(user_id, fetched_access_token['authorizing_user_id'])149 @decorators.idempotent_id('1b802896-91a0-4cbb-a8b9-860c7087fad8')150 def test_revoke_access_token(self):151 """Test to delete the access token provided for consumer."""152 consumer = self._create_consumer()153 access_token = self._create_access_token(consumer)154 user_id = self.oauth_token_client.auth_provider.credentials.user_id155 # delete existing access token156 self.oauth_token_client.revoke_access_token(157 user_id, access_token['oauth_token'])158 # check that access token no longer exist159 self.assertRaises(lib_exc.NotFound,160 self.oauth_token_client.get_access_token,161 user_id,162 access_token['oauth_token'])163 @decorators.idempotent_id('5929055d-7c0f-4661-a9a5-15c4b95082dc')164 def test_list_access_tokens(self):165 """Test to list access tokens provided to consumer."""166 # create two consumers and access token for each167 new_consumer_one = self._create_consumer()168 new_consumer_two = self._create_consumer()169 access_token_one = self._create_access_token(new_consumer_one)170 access_token_two = self._create_access_token(new_consumer_two)171 user_id = self.oauth_token_client.auth_provider.credentials.user_id172 # fetch the list of access tokens173 access_token_list = self.oauth_token_client.list_access_tokens(174 user_id)['access_tokens']175 # add fetch access token ids to a list176 id_list = [access_token['id'] for access_token in access_token_list]177 # check if created access tokens are in the list178 self.assertIn(access_token_one['oauth_token'], id_list)179 self.assertIn(access_token_two['oauth_token'], id_list)180 @decorators.idempotent_id('0075f413-e249-42e5-9bc9-d6e3aecf6cbc')181 def test_list_roles_for_access_token(self):182 """Test to list roles for an access token."""183 consumer = self._create_consumer()184 access_token = self._create_access_token(consumer)185 user_id = self.oauth_token_client.auth_provider.credentials.user_id186 project_id = (187 self.oauth_token_client.auth_provider.credentials.project_id)188 fetched_roles = self.oauth_token_client.list_access_token_roles(189 user_id, access_token['oauth_token'])['roles']190 fetched_role_ids = [role['id'] for role in fetched_roles]191 roles = self.roles_client.list_user_roles_on_project(192 project_id, user_id)193 role_ids = [role['id'] for role in roles['roles']]194 # check that role ids matches195 self.assertCountEqual(fetched_role_ids, role_ids)196 @decorators.idempotent_id('28aee994-86b1-4596-a652-572f558045e7')197 def test_show_role_for_access_token(self):198 """Test to show role details for an access token."""199 consumer = self._create_consumer()200 access_token = self._create_access_token(consumer)201 user_id = self.oauth_token_client.auth_provider.credentials.user_id202 project_id = (203 self.oauth_token_client.auth_provider.credentials.project_id)204 roles = self.roles_client.list_user_roles_on_project(205 project_id, user_id)206 fetched_role = self.oauth_token_client.get_access_token_role(207 user_id,208 access_token['oauth_token'],209 roles['roles'][0]['id'])210 # check that role id matches...
base.py
Source:base.py
...44 filters={'task_id': {"exact": taskid}}45 )46 return tokens['tokens'][0]['token']47 def assert_user_has_role(self, project_id, user_id, role):48 ks_role_list = self.roles_client.list_user_roles_on_project(49 project_id,50 user_id)51 actual_roles = set([r['name'] for r in ks_role_list['roles']])52 self.assertIn(role, actual_roles)53 def assert_user_roles(self, project_id, user_id, expected_roles):54 # NOTE(dalees): Between tags 12.0.0 and 12.1.0 Tempest has renamed the55 # function list_user_roles to list_user_roles_on_project,56 # so we'll support both until deprecation of 12.0.0.57 list_roles_func = getattr(58 self.roles_client,59 'list_user_roles_on_project',60 None)61 if list_roles_func is None:62 list_roles_func = getattr(self.roles_client, 'list_user_roles')...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!