Best Python code snippet using tempest_python
test_users.py
Source:test_users.py  
...59                          self.data.tenant['id'], self.alt_email)60    @attr(type='negative')61    def test_create_user_with_duplicate_name(self):62        """Duplicate user should not be created"""63        self.data.setup_test_user()64        self.assertRaises(exceptions.Duplicate, self.client.create_user,65                          self.data.test_user, self.data.test_password,66                          self.data.tenant['id'], self.data.test_email)67    @attr(type='negative')68    @unittest.skip("Until Bug 999084 is fixed")69    def test_create_user_with_empty_password(self):70        """User with an empty password should not be created"""71        self.data.setup_test_tenant()72        self.assertRaises(exceptions.BadRequest, self.client.create_user,73                          self.alt_user, '', self.data.tenant['id'],74                          self.alt_email)75    @attr(type='nagative')76    @unittest.skip("Until Bug 999084 is fixed")77    def test_create_user_with_long_password(self):78        """User having password exceeding max length should not be created"""79        self.data.setup_test_tenant()80        self.assertRaises(exceptions.BadRequest, self.client.create_user,81                          self.alt_user, 'a' * 65, self.data.tenant['id'],82                          self.alt_email)83    @attr(type='negative')84    @unittest.skip("Until Bug 999084 is fixed")85    def test_create_user_with_invalid_email_format(self):86        """Email format should be validated while creating a user"""87        self.data.setup_test_tenant()88        self.assertRaises(exceptions.BadRequest, self.client.create_user,89                          self.alt_user, '', self.data.tenant['id'], '12345')90    @attr(type='negative')91    def test_create_user_for_non_existant_tenant(self):92        """Attempt to create a user in a non-existent tenant should fail"""93        self.assertRaises(exceptions.NotFound, self.client.create_user,94                          self.alt_user, self.alt_password, '49ffgg99999',95                          self.alt_email)96    @attr(type='negative')97    def test_create_user_request_without_a_token(self):98        """Request to create a user without a valid token should fail"""99        self.data.setup_test_tenant()100        # Get the token of the current client101        token = self.client.get_auth()102        # Delete the token from database103        self.client.delete_token(token)104        self.assertRaises(exceptions.Unauthorized, self.client.create_user,105                          self.alt_user, self.alt_password,106                          self.data.tenant['id'], self.alt_email)107        # Unset the token to allow further tests to generate a new token108        self.client.clear_auth()109    @attr(type='smoke')110    def test_delete_user(self):111        """Delete a user"""112        self.data.setup_test_tenant()113        resp, user = self.client.create_user('user_1234', self.alt_password,114                                             self.data.tenant['id'],115                                             self.alt_email)116        resp, body = self.client.delete_user(user['id'])117        self.assertEquals('204', resp['status'])118    @attr(type='negative')119    def test_delete_users_by_unauthorized_user(self):120        """Non admin user should not be authorized to delete a user"""121        self.data.setup_test_user()122        self.assertRaises(exceptions.Unauthorized,123                          self.non_admin_client.delete_user,124                          self.data.user['id'])125    @attr(type='negative')126    def test_delete_non_existant_user(self):127        """Attempt to delete a non-existent user should fail"""128        self.assertRaises(exceptions.NotFound, self.client.delete_user,129                          'junk12345123')130    @attr(type='smoke')131    def test_user_authentication(self):132        """Valid user's token is authenticated"""133        self.data.setup_test_user()134        # Get a token135        self.token_client.auth(self.data.test_user, self.data.test_password,136                               self.data.test_tenant)137        # Re-auth138        resp, body = self.token_client.auth(self.data.test_user,139                                            self.data.test_password,140                                            self.data.test_tenant)141        self.assertEqual('200', resp['status'])142    @attr(type='negative')143    def test_authentication_for_disabled_user(self):144        """Disabled user's token should not get authenticated"""145        self.data.setup_test_user()146        self.disable_user(self.data.test_user)147        self.assertRaises(exceptions.Unauthorized, self.token_client.auth,148                          self.data.test_user,149                          self.data.test_password,150                          self.data.test_tenant)151    @attr(type='negative')152    @unittest.skip('Until Bug 988920 is fixed')153    def test_authentication_when_tenant_is_disabled(self):154        """User's token for a disabled tenant should not be authenticated"""155        self.data.setup_test_user()156        self.disable_tenant(self.data.test_tenant)157        self.assertRaises(exceptions.Unauthorized, self.token_client.auth,158                          self.data.test_user,159                          self.data.test_password,160                          self.data.test_tenant)161    @attr(type='negative')162    @unittest.skip('Until Bug 988920 is fixed')163    def test_authentication_with_invalid_tenant(self):164        """User's token for an invalid tenant should not be authenticated"""165        self.data.setup_one_user()166        self.assertRaises(exceptions.Unauthorized, self.token_client.auth,167                          self.data.test_user,168                          self.data.test_password,169                          'junktenant1234')170    @attr(type='negative')171    def test_authentication_with_invalid_username(self):172        """Non-existent user's token should not get authenticated"""173        self.assertRaises(exceptions.Unauthorized, self.token_client.auth,174                          'junkuser123', self.data.test_password,175                          self.data.test_tenant)176    @attr(type='negative')177    def test_authentication_with_invalid_password(self):178        """User's token with invalid password should not be authenticated"""179        self.data.setup_test_user()180        self.assertRaises(exceptions.Unauthorized, self.token_client.auth,181                          self.data.test_user, 'junkpass1234',182                          self.data.test_tenant)183    @attr(type='positive')184    def test_authentication_request_without_token(self):185        """Request for token authentication with a valid token in header"""186        self.data.setup_test_user()187        self.token_client.auth(self.data.test_user, self.data.test_password,188                               self.data.test_tenant)189        # Get the token of the current client190        token = self.client.get_auth()191        # Delete the token from database192        self.client.delete_token(token)193        # Re-auth194        resp, body = self.token_client.auth(self.data.test_user,195                                            self.data.test_password,196                                            self.data.test_tenant)197        self.assertEqual('200', resp['status'])198        self.client.clear_auth()199    @attr(type='smoke')200    def test_get_users(self):201        """Get a list of users and find the test user"""202        self.data.setup_test_user()203        resp, users = self.client.get_users()204        self.assertIn(self.data.test_user, [u['name'] for u in users],205                      "Could not find %s" % self.data.test_user)206    @attr(type='negative')207    def test_get_users_by_unauthorized_user(self):208        """Non admin user should not be authorized to get user list"""209        self.data.setup_test_user()210        self.assertRaises(exceptions.Unauthorized,211                          self.non_admin_client.get_users)212    @attr(type='negative')213    def test_get_users_request_without_token(self):214        """Request to get list of users without a valid token should fail"""215        token = self.client.get_auth()216        self.client.delete_token(token)217        self.assertRaises(exceptions.Unauthorized, self.client.get_users)218        self.client.clear_auth()219    @attr(type='positive')220    def test_list_users_for_tenant(self):221        """Return a list of all users for a tenant"""222        self.data.setup_test_tenant()223        user_ids = list()224        fetched_user_ids = list()225        resp, user1 = self.client.create_user('tenant_user1', 'password1',226                                              self.data.tenant['id'],227                                              'user1@123')228        user_ids.append(user1['id'])229        self.data.users.append(user1)230        resp, user2 = self.client.create_user('tenant_user2', 'password2',231                                              self.data.tenant['id'],232                                              'user2@123')233        user_ids.append(user2['id'])234        self.data.users.append(user2)235        #List of users for the respective tenant ID236        resp, body = self.client.list_users_for_tenant(self.data.tenant['id'])237        self.assertTrue(resp['status'].startswith('2'))238        for i in body:239            fetched_user_ids.append(i['id'])240        #verifying the user Id in the list241        missing_users =\242            [user for user in user_ids if user not in fetched_user_ids]243        self.assertEqual(0, len(missing_users),244                         "Failed to find user %s in fetched list" %245                         ', '.join(m_user for m_user in missing_users))246    @attr(type='positive')247    def test_list_users_with_roles_for_tenant(self):248        """Return list of users on tenant when roles are assigned to users"""249        self.data.setup_test_user()250        self.data.setup_test_role()251        user = self.get_user_by_name(self.data.test_user)252        tenant = self.get_tenant_by_name(self.data.test_tenant)253        role = self.get_role_by_name(self.data.test_role)254        #Assigning roles to two users255        user_ids = list()256        fetched_user_ids = list()257        user_ids.append(user['id'])258        self.client.assign_user_role(tenant['id'], user['id'], role['id'])259        resp, second_user = self.client.create_user('second_user', 'password1',260                                                    self.data.tenant['id'],261                                                    'user1@123')262        user_ids.append(second_user['id'])263        self.data.users.append(second_user)...base_test_classes.py
Source:base_test_classes.py  
2from django.test import TestCase3class BaseTestClassMethods(TestCase):4    def setUp(self):5        self.created_documents = []6        self.user = self.setup_test_user()7        super(BaseTestClassMethods, self).setUp()8    def tearDown(self):9        for doc in self.created_documents:10            doc.delete()11        super(BaseTestClassMethods, self).tearDown()12    def setup_test_user(13        self, username='test', password='12345', first_name='John', last_name='Doe', email='john.doe@example.com'14    ):15        return get_user_model().objects.create_user(16            username=username, password=password, first_name=first_name, last_name=last_name, email=email...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!!
