Best Python code snippet using tempest_python
test_users.py
Source:test_users.py
...25 def resource_setup(cls):26 super(IdentityV3UsersTest, cls).resource_setup()27 cls.creds = cls.os_primary.credentials28 cls.user_id = cls.creds.user_id29 def _update_password(self, original_password, password):30 self.non_admin_users_client.update_user_password(31 self.user_id,32 password=password,33 original_password=original_password)34 # NOTE(morganfainberg): Fernet tokens are not subsecond aware and35 # Keystone should only be precise to the second. Sleep to ensure36 # we are passing the second boundary.37 time.sleep(1)38 # check authorization with new password39 self.non_admin_token.auth(user_id=self.user_id, password=password)40 # Reset auth to get a new token with the new password41 self.non_admin_users_client.auth_provider.clear_auth()42 self.non_admin_users_client.auth_provider.credentials.password = (43 password)44 def _restore_password(self, old_pass, new_pass):45 if CONF.identity_feature_enabled.security_compliance:46 # First we need to clear the password history47 unique_count = CONF.identity.user_unique_last_password_count48 for _ in range(unique_count):49 random_pass = data_utils.rand_password()50 self._update_password(51 original_password=new_pass, password=random_pass)52 new_pass = random_pass53 self._update_password(original_password=new_pass, password=old_pass)54 # Reset auth again to verify the password restore does work.55 # Clear auth restores the original credentials and deletes56 # cached auth data57 self.non_admin_users_client.auth_provider.clear_auth()58 # NOTE(lbragstad): Fernet tokens are not subsecond aware and59 # Keystone should only be precise to the second. Sleep to ensure we60 # are passing the second boundary before attempting to61 # authenticate.62 time.sleep(1)63 self.non_admin_users_client.auth_provider.set_auth()64 @decorators.idempotent_id('ad71bd23-12ad-426b-bb8b-195d2b635f27')65 def test_user_update_own_password(self):66 old_pass = self.creds.password67 old_token = self.non_admin_client.token68 new_pass = data_utils.rand_password()69 # to change password back. important for allow_tenant_isolation = false70 self.addCleanup(self._restore_password, old_pass, new_pass)71 # user updates own password72 self._update_password(original_password=old_pass, password=new_pass)73 # authorize with old token should lead to IdentityError (404 code)74 self.assertRaises(exceptions.IdentityError,75 self.non_admin_token.auth,76 token=old_token)77 # authorize with old password should lead to Unauthorized78 self.assertRaises(exceptions.Unauthorized,79 self.non_admin_token.auth,80 user_id=self.user_id,81 password=old_pass)82 @testtools.skipUnless(CONF.identity_feature_enabled.security_compliance,83 'Security compliance not available.')84 @decorators.idempotent_id('941784ee-5342-4571-959b-b80dd2cea516')85 def test_password_history_check_self_service_api(self):86 old_pass = self.creds.password87 new_pass1 = data_utils.rand_password()88 new_pass2 = data_utils.rand_password()89 self.addCleanup(self._restore_password, old_pass, new_pass2)90 # Update password91 self._update_password(original_password=old_pass, password=new_pass1)92 if CONF.identity.user_unique_last_password_count > 1:93 # Can not reuse a previously set password94 self.assertRaises(exceptions.BadRequest,95 self.non_admin_users_client.update_user_password,96 self.user_id,97 password=new_pass1,98 original_password=new_pass1)99 self.assertRaises(exceptions.BadRequest,100 self.non_admin_users_client.update_user_password,101 self.user_id,102 password=old_pass,103 original_password=new_pass1)104 # A different password can be set105 self._update_password(original_password=new_pass1, password=new_pass2)106 @testtools.skipUnless(CONF.identity_feature_enabled.security_compliance,107 'Security compliance not available.')108 @decorators.idempotent_id('a7ad8bbf-2cff-4520-8c1d-96332e151658')109 def test_user_account_lockout(self):110 password = self.creds.password111 # First, we login using the correct credentials112 self.non_admin_token.auth(user_id=self.user_id, password=password)113 # Lock user account by using the wrong password to login114 bad_password = data_utils.rand_password()115 for _ in range(CONF.identity.user_lockout_failure_attempts):116 self.assertRaises(exceptions.Unauthorized,117 self.non_admin_token.auth,118 user_id=self.user_id,119 password=bad_password)...
password.py
Source:password.py
...32 validate_json(data, {'foris_current_password': str})33 if not check_password(data['foris_current_password']):34 raise APIError(_('Wrong current password.'), 400)35 if data.get('foris_password', False):36 response['foris_password'] = _update_password('foris', data['foris_password'])37 if data.get('root_password', False):38 response['root_password'] = _update_password('system', data['root_password'])39 return jsonify(response)40def _update_password(password_type: str, password: str) -> dict:41 new_password = _decode_password_to_base64(password)42 request_data = {'type': password_type, 'password': new_password}43 return current_app.backend.perform('password', 'set', request_data)44# pylint: disable=invalid-name45views = [{46 'rule': '/password',47 'view_func': password,48 'methods': ['GET', 'POST']...
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!!