2016-01-27 16:30:33 +00:00
|
|
|
|
2015-11-27 16:25:56 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
from app.main.dao import users_dao
|
2015-11-27 09:47:29 +00:00
|
|
|
|
2016-01-05 17:08:50 +00:00
|
|
|
from flask import url_for
|
2016-01-21 12:31:09 +00:00
|
|
|
|
2015-11-27 09:47:29 +00:00
|
|
|
|
2016-01-15 15:15:35 +00:00
|
|
|
def test_render_sign_in_returns_sign_in_template(app_):
|
|
|
|
|
with app_.test_request_context():
|
|
|
|
|
response = app_.test_client().get(url_for('main.sign_in'))
|
2015-11-27 09:47:29 +00:00
|
|
|
assert response.status_code == 200
|
|
|
|
|
assert 'Sign in' in response.get_data(as_text=True)
|
|
|
|
|
assert 'Email address' in response.get_data(as_text=True)
|
|
|
|
|
assert 'Password' in response.get_data(as_text=True)
|
|
|
|
|
assert 'Forgotten password?' in response.get_data(as_text=True)
|
|
|
|
|
|
|
|
|
|
|
2016-01-22 17:24:14 +00:00
|
|
|
def test_logged_in_user_redirects_to_choose_service(app_,
|
2016-01-27 16:30:33 +00:00
|
|
|
api_user_active,
|
|
|
|
|
mock_get_user_by_email,
|
|
|
|
|
mock_login):
|
2016-01-22 17:24:14 +00:00
|
|
|
with app_.test_request_context():
|
|
|
|
|
with app_.test_client() as client:
|
2016-01-27 16:30:33 +00:00
|
|
|
client.login(api_user_active)
|
2016-01-22 17:24:14 +00:00
|
|
|
response = client.get(url_for('main.sign_in'))
|
|
|
|
|
assert response.status_code == 302
|
|
|
|
|
|
|
|
|
|
response = client.get(url_for('main.sign_in', follow_redirects=True))
|
|
|
|
|
assert response.location == url_for('main.choose_service', _external=True)
|
|
|
|
|
|
|
|
|
|
|
2016-01-15 15:15:35 +00:00
|
|
|
def test_process_sign_in_return_2fa_template(app_,
|
2016-01-28 12:31:24 +00:00
|
|
|
api_user_active,
|
2016-01-27 12:22:32 +00:00
|
|
|
mock_send_verify_code,
|
|
|
|
|
mock_get_user,
|
|
|
|
|
mock_get_user_by_email,
|
|
|
|
|
mock_verify_password):
|
2016-01-27 16:30:33 +00:00
|
|
|
|
2016-01-15 15:15:35 +00:00
|
|
|
with app_.test_request_context():
|
|
|
|
|
response = app_.test_client().post(
|
2016-01-05 17:08:50 +00:00
|
|
|
url_for('main.sign_in'), data={
|
|
|
|
|
'email_address': 'valid@example.gov.uk',
|
|
|
|
|
'password': 'val1dPassw0rd!'})
|
2016-01-28 16:36:36 +00:00
|
|
|
assert response.status_code == 302
|
|
|
|
|
assert response.location == url_for('.two_factor', _external=True)
|
2016-01-28 12:31:24 +00:00
|
|
|
mock_verify_password.assert_called_with(api_user_active.id, 'val1dPassw0rd!')
|
2015-11-27 16:25:56 +00:00
|
|
|
|
|
|
|
|
|
2016-01-15 15:15:35 +00:00
|
|
|
def test_should_return_locked_out_true_when_user_is_locked(app_,
|
2016-01-27 16:30:33 +00:00
|
|
|
mock_get_user_by_email_locked):
|
2016-01-15 15:15:35 +00:00
|
|
|
with app_.test_request_context():
|
2016-01-27 16:30:33 +00:00
|
|
|
resp = app_.test_client().post(
|
2016-01-05 17:08:50 +00:00
|
|
|
url_for('main.sign_in'), data={
|
|
|
|
|
'email_address': 'valid@example.gov.uk',
|
|
|
|
|
'password': 'whatIsMyPassword!'})
|
2016-01-27 16:30:33 +00:00
|
|
|
assert resp.status_code == 200
|
|
|
|
|
assert 'Username or password is incorrect' in resp.get_data(as_text=True)
|
2015-11-30 16:52:28 +00:00
|
|
|
|
2015-11-30 16:33:45 +00:00
|
|
|
|
2016-01-27 16:30:33 +00:00
|
|
|
def test_should_return_200_when_user_does_not_exist(app_, mock_get_user_by_email_not_found):
|
2016-01-22 17:24:14 +00:00
|
|
|
with app_.test_request_context():
|
|
|
|
|
response = app_.test_client().post(
|
2016-01-05 17:08:50 +00:00
|
|
|
url_for('main.sign_in'), data={
|
2016-01-23 23:14:50 +00:00
|
|
|
'email_address': 'notfound@gov.uk',
|
2016-01-22 17:24:14 +00:00
|
|
|
'password': 'doesNotExist!'})
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
assert 'Username or password is incorrect' in response.get_data(as_text=True)
|
|
|
|
|
|
|
|
|
|
|
2016-01-28 16:36:36 +00:00
|
|
|
def test_should_return_redirect_when_user_is_pending(app_,
|
|
|
|
|
mock_get_user_by_email_pending,
|
|
|
|
|
mock_verify_password):
|
2016-01-22 17:24:14 +00:00
|
|
|
with app_.test_request_context():
|
|
|
|
|
response = app_.test_client().post(
|
|
|
|
|
url_for('main.sign_in'), data={
|
2016-01-23 23:14:50 +00:00
|
|
|
'email_address': 'pending_user@example.gov.uk',
|
2016-01-22 17:24:14 +00:00
|
|
|
'password': 'val1dPassw0rd!'})
|
2016-01-28 16:36:36 +00:00
|
|
|
assert response.status_code == 302
|
|
|
|
|
assert response.location == url_for('main.verify', _external=True)
|
2016-02-23 15:45:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_not_fresh_session_login(app_,
|
|
|
|
|
api_user_active,
|
|
|
|
|
mock_login,
|
|
|
|
|
mock_get_user_by_email,
|
|
|
|
|
mock_verify_password,
|
|
|
|
|
mock_get_services_with_one_service):
|
|
|
|
|
with app_.test_request_context():
|
|
|
|
|
with app_.test_client() as client:
|
|
|
|
|
client.login(api_user_active)
|
|
|
|
|
with client.session_transaction() as session:
|
|
|
|
|
assert session['_fresh']
|
|
|
|
|
session['_fresh'] = False
|
|
|
|
|
# This should skip the two factor
|
|
|
|
|
response = client.post(
|
|
|
|
|
url_for('main.sign_in'), data={
|
|
|
|
|
'email_address': api_user_active.email_address,
|
|
|
|
|
'password': 'val1dPassw0rd!'})
|
|
|
|
|
assert response.status_code == 302
|
|
|
|
|
service_dct = mock_get_services_with_one_service(api_user_active.id)['data'][0]
|
|
|
|
|
assert response.location == url_for(
|
|
|
|
|
'main.service_dashboard', service_id=service_dct['id'], _external=True)
|