"""Functional tests for lost password.""" import json import faker from auth import handlers # noqa - we need this so the handlers can exist. from auth.app import app from auth.logic import user from auth.models.client import clients def test_forgot_password_success(): """Functional test for forgot password handler success. Test hits the handler and provides a valid username (login). The system looks up the user and generates a LOST_PASSWORD type token. The token is returned by the handler. """ app.testing = True navigator = app.test_client() # information about the user. fake = faker.Faker() login = fake.user_name() password = fake.password() email = fake.email() user.create_user(login, password, email) # set the navigator. client = list(clients.values())[0] forgot_password_request = navigator.post( '/forgot_password', data=dict(login=login, client_id=client.id)) assert forgot_password_request.status_code == 201 def test_forgot_password_invalid_user(): """Functional test for forgot password failure. Test hits the handler and provides an invalid username (login). The system is unable to find a valid user and a 404 error code is raised. No token is generated. """ app.testing = True navigator = app.test_client() # set the navigator. client = list(clients.values())[0] forgot_password_request = navigator.post( '/forgot_password', data=dict(login='bad_login', client_id=client.id)) assert not json.loads(forgot_password_request.data.decode('utf8')) assert forgot_password_request.status_code == 404