"""Functional test for update password.""" import base64 import json import faker from flask import jsonify import rsa from auth import handlers # noqa from auth.app import app from auth.logic import passwords from auth.logic import token from auth.logic import user from auth.models.client import clients from tests.logic import test_logic_user def test_update_password_success(monkeypatch): """Functional test for successfully updating a password. Test creates a user and generates a forgot password token for them. It then uses this token to change the password and logs in. """ app.testing = True navigator = app.test_client() fake = faker.Faker() new_password = fake.password() current_user, password = test_logic_user.random_user() # set the navigator. client = list(clients.values())[0] current_state = 'state' # Use this to bypass authorization request returning a template def render_template( name, keys=None, client_id=None, state=None, redirect=None): """Mock for rendering template. Args: name (string): template name. client_id (str): the client id. state (str): the state of the token. redirect (str): url to redirect the user to. """ assert state == current_state assert str(client.id) == client_id return jsonify(keys) monkeypatch.setattr(handlers, 'render_template', render_template) authorize_request = navigator.get( '/authorize?client_id={}&state={}'.format(client.id, current_state)) authorize_response = json.loads(authorize_request.data.decode('utf8')) assert authorize_request.status_code == 200 public_key = authorize_response['public_key'] request_id = authorize_response['token'] public_key = rsa.PublicKey.load_pkcs1_openssl_pem( authorize_response.get('public_key').encode('utf8')) encrypted_password = base64.b64encode( rsa.encrypt(new_password.encode('utf8'), public_key)).decode('utf8') password_token_response = token.create_password_token( current_user.login, current_user.id, client.id) update_pwd_request = navigator.post( '/user/password', data=dict( request_id=request_id, password_token=password_token_response.token, password=encrypted_password, client_id=client.id)) assert update_pwd_request.status_code == 200 current_user = user.get_user(current_user.login) pwd_check = passwords.check( new_password.encode('utf8'), current_user.password) redirect = json.loads( update_pwd_request.data.decode('utf8')).get('redirect') assert redirect == client.redirect_url assert pwd_check def test_update_password_invalid_token(monkeypatch): """Functional test for triggering a password update with an invalid token. Test attempts to use an invalid password token and expects to receive a 404 error. """ app.testing = True navigator = app.test_client() fake = faker.Faker() new_password = fake.password() current_user, password = test_logic_user.random_user() # set the navigator. client = list(clients.values())[0] current_state = 'state' # Use this to bypass authorization request returning a template def render_template( tpl, keys=None, client_id=None, state=None, redirect=None): assert state == current_state assert str(client.id) == client_id return jsonify(keys) monkeypatch.setattr(handlers, 'render_template', render_template) authorize_request = navigator.get( '/authorize?client_id={}&state={}'.format(client.id, current_state)) authorize_response = json.loads(authorize_request.data.decode('utf8')) assert authorize_request.status_code == 200 public_key = authorize_response['public_key'] request_id = authorize_response['token'] public_key = rsa.PublicKey.load_pkcs1_openssl_pem( authorize_response.get('public_key').encode('utf8')) encrypted_password = base64.b64encode( rsa.encrypt(new_password.encode('utf8'), public_key)).decode('utf8') update_pwd_request = navigator.post( '/user/password', data=dict( request_id=request_id, password_token='foobar123', password=encrypted_password, client_id=client.id)) assert update_pwd_request.status_code == 404 def test_update_password_invalid_password(monkeypatch): """Functional test for triggering a password update with invalid password. Test attempts to use an invalid password and expects to receive a 400 error. """ app.testing = True navigator = app.test_client() current_user, password = test_logic_user.random_user() # set the navigator. client = list(clients.values())[0] current_state = 'state' # Use this to bypass authorization request returning a template def render_template( tpl, keys=None, client_id=None, state=None, redirect=None): assert state == current_state assert str(client.id) == client_id return jsonify(keys) monkeypatch.setattr(handlers, 'render_template', render_template) authorize_request = navigator.get( '/authorize?client_id={}&state={}'.format(client.id, current_state)) authorize_response = json.loads(authorize_request.data.decode('utf8')) assert authorize_request.status_code == 200 request_id = authorize_response['token'] password_token_response = token.create_password_token( current_user.login, current_user.id, client.id) update_pwd_request = navigator.post( '/user/password', data=dict( request_id=request_id, password_token=password_token_response.token, password=None, client_id=client.id)) assert update_pwd_request.status_code == 400