Search code examples
pythonpython-3.xapiput

How can I use the new password generated in next call of same function?


I have the following function which updates password of a username AFTER authentication

def test_put_profile(self):
    username = '[email protected]'
    password = 'evkna7'
    adv = {'email': username, 'password': password}

    # genearate auth code for authorization
    auth_code = {'Authorization': f'Bearer {self.get_auth_token(adv)}'}

    #genearate new password
    np = DataGenerator.generate_random_password_string(6)
    param = f'old_password={password}&'f'new_password={np}'

    #update password
    response = self.send_adv_api_request(
                                         data=param,
                                         auth=auth_code)

Now, How can I store the new password and replace that with password of the same function at the beginning when I call it next time?. Basically, this is going to be the recursive test of update password.

Any help would be appreciated. Should I write it in a text?


Solution

  • You need to store the password outside of the scope of this function so that you can access it from your recursive calls.

    curr_password = 'evkna7'
    
    def test_put_profile(self):
        global curr_password
        username = '[email protected]'
        adv = {'email': username, 'password': curr_password}
    
        # genearate auth code for authorization
        auth_code = {'Authorization': f'Bearer {self.get_auth_token(adv)}'}
    
        #genearate new password
        np = DataGenerator.generate_random_password_string(6)
        param = f'old_password={password}&'f'new_password={np}'
        curr_password = np #Change global password to newly created password
    
        #update password
        response = self.send_adv_api_request(
                                             data=param,
                                             auth=auth_code)