"""Function tests for reseting a password.""" import json import faker from auth import handlers # noqa from auth.app import app from auth.logic import user from auth.logic import token def test_reset_password_success(): """Functional test for successfully triggering a password reset. Test creates a user and attempts to reset the password with the provided token from the link given in the reset email. It then verifies that the password token is valid and returns the client_id that corresponds to the password reset request. """ app.testing = True navigator = app.test_client() # information about the user. fake = faker.Faker() login = fake.user_name() password = fake.password() email = fake.email() current_user = user.create_user(login, password, email) # set the navigator. password_token_response = token.create_password_token( current_user.login, current_user.id, 28873) reset_request = navigator.get('/reset/' + password_token_response.token) assert reset_request.status_code == 200 assert json.loads( reset_request.data.decode('utf8')).get('client_id') == 28873 def test_password_expires_in_reduced(): """Functional test for expiring tokens sooner when hit with a reset. Test creates a user and attempts to reset the password. It expects that after this route has been successfully hit, the token expires in 20 minutes. """ app.testing = True navigator = app.test_client() # information about the user. fake = faker.Faker() login = fake.user_name() password = fake.password() email = fake.email() current_user = user.create_user(login, password, email) # set the navigator. password_token_response = token.create_password_token( current_user.login, current_user.id, 28873) reset_request = navigator.get('/reset/' + password_token_response.token) assert reset_request.status_code == 200 current_token = token.fetch_token(password_token_response.token) assert current_token.expires_in == 1200 def test_reset_password_invalid_token(): """Functional test for triggering a password reset with an invalid token. Test attempts to use an invalid token in the URL and expects to receive a 404 error. """ app.testing = True navigator = app.test_client() reset_request = navigator.get('/reset/abcdefg1234') assert reset_request.status_code == 404 def test_reset_password_expired_token(): """Functional test for triggering a password reset with an expired token. Test attempts to use an expired token in the URL and expects to receive a 404. """ app.testing = True navigator = app.test_client() # information about the user. fake = faker.Faker() login = fake.user_name() password = fake.password() email = fake.email() current_user = user.create_user(login, password, email) # set the navigator. password_token_response = token.create_password_token( current_user.login, current_user.id, 28873, '', 0) reset_request = navigator.get('/reset/' + password_token_response.token) assert reset_request.status_code == 404