Send 2fa email and move user to waiting page when they need to re-validate email access

This commit is contained in:
Pea Tyczynska
2020-01-27 18:10:45 +00:00
parent a4fb1b6c06
commit caf77341b3
8 changed files with 100 additions and 8 deletions

View File

@@ -1,5 +1,6 @@
from bs4 import BeautifulSoup
from flask import url_for
from freezegun import freeze_time
from tests.conftest import (
SERVICE_ONE_ID,
@@ -13,6 +14,7 @@ def test_should_render_two_factor_page(
client,
api_user_active,
mock_get_user_by_email,
mocker
):
# TODO this lives here until we work out how to
# reassign the session after it is lost mid register process
@@ -20,6 +22,7 @@ def test_should_render_two_factor_page(
session['user_details'] = {
'id': api_user_active['id'],
'email': api_user_active['email_address']}
mocker.patch('app.user_api_client.get_user', return_value=api_user_active)
response = client.get(url_for('main.two_factor'))
assert response.status_code == 200
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
@@ -33,6 +36,7 @@ def test_should_render_two_factor_page(
assert page.select_one('input')['pattern'] == '[0-9]*'
@freeze_time('2020-01-27T12:00:00')
def test_should_login_user_and_should_redirect_to_next_url(
client,
api_user_active,
@@ -45,6 +49,8 @@ def test_should_login_user_and_should_redirect_to_next_url(
session['user_details'] = {
'id': api_user_active['id'],
'email': api_user_active['email_address']}
api_user_active['email_access_validated_at'] = 'Sun, 23 Jan 2020 11:28:25 GMT'
response = client.post(url_for('main.two_factor', next='/services/{}'.format(SERVICE_ONE_ID)),
data={'sms_code': '12345'})
assert response.status_code == 302
@@ -55,6 +61,31 @@ def test_should_login_user_and_should_redirect_to_next_url(
)
@freeze_time('2020-01-27T12:00:00')
def test_should_send_email_and_redirect_to_info_page_if_user_needs_to_revalidate_email(
client,
api_user_active,
mock_get_user,
mock_check_verify_code,
mock_create_event,
mock_send_verify_code,
mocker
):
mocker.patch('app.user_api_client.get_user', return_value=api_user_active)
api_user_active['email_access_validated_at'] = 'Sun, 03 Mar 2019 11:28:25 GMT'
with client.session_transaction() as session:
session['user_details'] = {
'id': api_user_active['id'],
'email': api_user_active['email_address']}
response = client.post(url_for('main.two_factor', next='/services/{}'.format(SERVICE_ONE_ID)),
data={'sms_code': '12345'})
assert response.status_code == 302
assert response.location == url_for('main.revalidate_email_sent', _external=True)
mock_send_verify_code.assert_called_with(api_user_active['id'], 'email', None, mocker.ANY)
@freeze_time('2020-01-27T12:00:00')
def test_should_login_user_and_not_redirect_to_external_url(
client,
api_user_active,
@@ -68,12 +99,15 @@ def test_should_login_user_and_not_redirect_to_external_url(
session['user_details'] = {
'id': api_user_active['id'],
'email': api_user_active['email_address']}
api_user_active['email_access_validated_at'] = 'Sun, 23 Jan 2020 11:28:25 GMT'
response = client.post(url_for('main.two_factor', next='http://www.google.com'),
data={'sms_code': '12345'})
assert response.status_code == 302
assert response.location == url_for('main.show_accounts_or_dashboard', _external=True)
@freeze_time('2020-01-27T12:00:00')
def test_should_login_user_and_redirect_to_show_accounts(
client,
api_user_active,
@@ -86,6 +120,8 @@ def test_should_login_user_and_redirect_to_show_accounts(
session['user_details'] = {
'id': api_user_active['id'],
'email': api_user_active['email_address']}
api_user_active['email_access_validated_at'] = 'Sun, 23 Jan 2020 11:28:25 GMT'
response = client.post(url_for('main.two_factor'),
data={'sms_code': '12345'})
@@ -98,17 +134,21 @@ def test_should_return_200_with_sms_code_error_when_sms_code_is_wrong(
api_user_active,
mock_get_user_by_email,
mock_check_verify_code_code_not_found,
mocker
):
with client.session_transaction() as session:
session['user_details'] = {
'id': api_user_active['id'],
'email': api_user_active['email_address']}
mocker.patch('app.user_api_client.get_user', return_value=api_user_active)
response = client.post(url_for('main.two_factor'),
data={'sms_code': '23456'})
assert response.status_code == 200
assert 'Code not found' in response.get_data(as_text=True)
@freeze_time('2020-01-27T12:00:00')
def test_should_login_user_when_multiple_valid_codes_exist(
client,
api_user_active,
@@ -122,11 +162,14 @@ def test_should_login_user_when_multiple_valid_codes_exist(
session['user_details'] = {
'id': api_user_active['id'],
'email': api_user_active['email_address']}
api_user_active['email_access_validated_at'] = 'Sun, 23 Jan 2020 11:28:25 GMT'
response = client.post(url_for('main.two_factor'),
data={'sms_code': '23456'})
assert response.status_code == 302
@freeze_time('2020-01-27T12:00:00')
def test_two_factor_should_set_password_when_new_password_exists_in_session(
client,
api_user_active,
@@ -141,13 +184,14 @@ def test_two_factor_should_set_password_when_new_password_exists_in_session(
'id': api_user_active['id'],
'email': api_user_active['email_address'],
'password': 'changedpassword'}
api_user_active['email_access_validated_at'] = 'Sun, 23 Jan 2020 11:28:25 GMT'
response = client.post(url_for('main.two_factor'),
data={'sms_code': '12345'})
assert response.status_code == 302
assert response.location == url_for('main.show_accounts_or_dashboard', _external=True)
mock_update_user_password.assert_called_once_with(api_user_active['id'], 'changedpassword')
mock_update_user_password.assert_called_once_with(api_user_active['id'], 'changedpassword', from_email=True)
def test_two_factor_returns_error_when_user_is_locked(
@@ -179,6 +223,7 @@ def test_two_factor_should_redirect_to_sign_in_if_user_not_in_session(
assert response.location == url_for('main.sign_in', _external=True)
@freeze_time('2020-01-27T12:00:00')
def test_two_factor_should_activate_pending_user(
client,
mocker,
@@ -189,6 +234,7 @@ def test_two_factor_should_activate_pending_user(
):
mocker.patch('app.user_api_client.get_user', return_value=api_user_pending)
mocker.patch('app.service_api_client.get_services', return_value={'data': []})
api_user_pending['email_access_validated_at'] = 'Sun, 23 Jan 2020 11:28:25 GMT'
with client.session_transaction() as session:
session['user_details'] = {
'id': api_user_pending['id'],