2017-02-17 14:06:09 +00:00
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
|
|
|
|
import pytest
|
2016-03-29 12:13:36 +01:00
|
|
|
|
from bs4 import BeautifulSoup
|
2018-02-20 11:22:17 +00:00
|
|
|
|
from flask import url_for
|
2016-01-21 12:31:09 +00:00
|
|
|
|
|
2015-11-27 09:47:29 +00:00
|
|
|
|
|
2017-02-17 14:06:09 +00:00
|
|
|
|
def test_render_sign_in_template_for_new_user(
|
2017-02-03 12:07:21 +00:00
|
|
|
|
client
|
2017-02-03 10:42:01 +00:00
|
|
|
|
):
|
2017-02-17 14:06:09 +00:00
|
|
|
|
response = client.get(url_for('main.sign_in', next=None))
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
resp = response.get_data(as_text=True)
|
|
|
|
|
|
assert 'Sign in' in resp
|
|
|
|
|
|
assert 'Email address' in resp
|
|
|
|
|
|
assert 'Password' in resp
|
|
|
|
|
|
assert 'Forgot your password?' in resp
|
|
|
|
|
|
assert 'If you do not have an account, you can' in resp
|
|
|
|
|
|
assert 'Sign in again' not in resp
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_sign_in_explains_session_timeout(client):
|
|
|
|
|
|
response = client.get(url_for('main.sign_in', next='/foo'))
|
2015-11-27 09:47:29 +00:00
|
|
|
|
assert response.status_code == 200
|
2017-02-17 14:06:09 +00:00
|
|
|
|
assert 'We signed you out because you haven’t used Notify for a while.' in response.get_data(as_text=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_sign_in_explains_other_browser(logged_in_client, api_user_active, mocker):
|
|
|
|
|
|
api_user_active.current_session_id = str(uuid.UUID(int=1))
|
|
|
|
|
|
mocker.patch('app.user_api_client.get_user', return_value=api_user_active)
|
|
|
|
|
|
|
|
|
|
|
|
with logged_in_client.session_transaction() as session:
|
|
|
|
|
|
session['current_session_id'] = str(uuid.UUID(int=2))
|
|
|
|
|
|
|
|
|
|
|
|
response = logged_in_client.get(url_for('main.sign_in', next='/foo'))
|
|
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
assert 'We signed you out because you logged in to Notify on another device' in response.get_data(as_text=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-03-08 16:51:53 +00:00
|
|
|
|
def test_doesnt_redirect_to_sign_in_if_no_session_info(
|
|
|
|
|
|
logged_in_client, api_user_active
|
|
|
|
|
|
):
|
2017-02-17 14:06:09 +00:00
|
|
|
|
assert api_user_active.current_session_id is None
|
2018-03-08 16:51:53 +00:00
|
|
|
|
|
2017-02-17 14:06:09 +00:00
|
|
|
|
with logged_in_client.session_transaction() as session:
|
|
|
|
|
|
session['current_session_id'] = None
|
|
|
|
|
|
|
2018-03-08 16:51:53 +00:00
|
|
|
|
response = logged_in_client.get(url_for('main.add_service'))
|
2017-02-17 14:06:09 +00:00
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('db_sess_id, cookie_sess_id', [
|
2018-12-31 13:10:19 +00:00
|
|
|
|
pytest.param(None, None, marks=pytest.mark.xfail), # OK - not used notify since browser signout was implemented
|
2017-02-17 14:06:09 +00:00
|
|
|
|
|
|
|
|
|
|
(uuid.UUID(int=1), None), # BAD - has used other browsers before but this is a brand new browser with no cookie
|
|
|
|
|
|
(uuid.UUID(int=1), uuid.UUID(int=2)), # BAD - this person has just signed in on a different browser
|
|
|
|
|
|
])
|
|
|
|
|
|
def test_redirect_to_sign_in_if_logged_in_from_other_browser(
|
|
|
|
|
|
logged_in_client,
|
|
|
|
|
|
api_user_active,
|
|
|
|
|
|
mocker,
|
|
|
|
|
|
db_sess_id,
|
|
|
|
|
|
cookie_sess_id
|
|
|
|
|
|
):
|
|
|
|
|
|
api_user_active.current_session_id = db_sess_id
|
|
|
|
|
|
mocker.patch('app.user_api_client.get_user', return_value=api_user_active)
|
|
|
|
|
|
with logged_in_client.session_transaction() as session:
|
|
|
|
|
|
session['current_session_id'] = str(cookie_sess_id)
|
|
|
|
|
|
|
2018-03-08 16:51:53 +00:00
|
|
|
|
response = logged_in_client.get(url_for('main.choose_account'))
|
2017-02-17 14:06:09 +00:00
|
|
|
|
assert response.status_code == 302
|
2018-03-08 16:51:53 +00:00
|
|
|
|
assert response.location == url_for('main.sign_in', next='/accounts', _external=True)
|
2015-11-27 09:47:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
2018-11-15 15:38:43 +00:00
|
|
|
|
def test_logged_in_user_redirects_to_account(
|
|
|
|
|
|
client_request
|
2017-02-03 10:42:01 +00:00
|
|
|
|
):
|
2018-11-15 15:38:43 +00:00
|
|
|
|
client_request.get(
|
|
|
|
|
|
'main.sign_in',
|
|
|
|
|
|
_expected_status=302,
|
|
|
|
|
|
_expected_redirect=url_for('main.show_accounts_or_dashboard', _external=True),
|
|
|
|
|
|
)
|
2016-01-22 17:24:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-12-11 16:09:19 +00:00
|
|
|
|
@pytest.mark.parametrize('email_address, password', [
|
|
|
|
|
|
('valid@example.gov.uk', 'val1dPassw0rd!'),
|
|
|
|
|
|
(' valid@example.gov.uk ', ' val1dPassw0rd! '),
|
2017-12-06 20:24:25 +00:00
|
|
|
|
])
|
2017-11-07 16:11:31 +00:00
|
|
|
|
def test_process_sms_auth_sign_in_return_2fa_template(
|
2017-02-03 12:07:21 +00:00
|
|
|
|
client,
|
2017-02-03 10:42:01 +00:00
|
|
|
|
api_user_active,
|
|
|
|
|
|
mock_send_verify_code,
|
|
|
|
|
|
mock_get_user,
|
|
|
|
|
|
mock_get_user_by_email,
|
|
|
|
|
|
mock_verify_password,
|
2017-12-06 20:24:25 +00:00
|
|
|
|
email_address,
|
2017-12-11 16:09:19 +00:00
|
|
|
|
password,
|
2017-02-03 10:42:01 +00:00
|
|
|
|
):
|
2017-02-03 12:07:21 +00:00
|
|
|
|
response = client.post(
|
|
|
|
|
|
url_for('main.sign_in'), data={
|
2017-12-06 20:24:25 +00:00
|
|
|
|
'email_address': email_address,
|
2017-12-11 16:09:19 +00:00
|
|
|
|
'password': password})
|
2017-02-03 12:07:21 +00:00
|
|
|
|
assert response.status_code == 302
|
|
|
|
|
|
assert response.location == url_for('.two_factor', _external=True)
|
2017-12-11 16:09:19 +00:00
|
|
|
|
mock_verify_password.assert_called_with(api_user_active.id, password)
|
2017-12-06 20:24:25 +00:00
|
|
|
|
mock_get_user_by_email.assert_called_with('valid@example.gov.uk')
|
2015-11-27 16:25:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-11-07 16:11:31 +00:00
|
|
|
|
def test_process_email_auth_sign_in_return_2fa_template(
|
|
|
|
|
|
client,
|
|
|
|
|
|
api_user_active_email_auth,
|
|
|
|
|
|
mock_send_verify_code,
|
|
|
|
|
|
mock_verify_password,
|
|
|
|
|
|
mocker
|
|
|
|
|
|
):
|
|
|
|
|
|
mocker.patch('app.user_api_client.get_user', return_value=api_user_active_email_auth)
|
|
|
|
|
|
mocker.patch('app.user_api_client.get_user_by_email', return_value=api_user_active_email_auth)
|
|
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
|
|
|
|
|
url_for('main.sign_in'), data={
|
|
|
|
|
|
'email_address': 'valid@example.gov.uk',
|
|
|
|
|
|
'password': 'val1dPassw0rd!'})
|
|
|
|
|
|
assert response.status_code == 302
|
|
|
|
|
|
assert response.location == url_for('.two_factor_email_sent', _external=True)
|
2018-02-09 15:01:20 +00:00
|
|
|
|
mock_send_verify_code.assert_called_with(api_user_active_email_auth.id, 'email', None)
|
2017-11-07 16:11:31 +00:00
|
|
|
|
mock_verify_password.assert_called_with(api_user_active_email_auth.id, 'val1dPassw0rd!')
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-02-03 10:42:01 +00:00
|
|
|
|
def test_should_return_locked_out_true_when_user_is_locked(
|
2017-02-03 12:07:21 +00:00
|
|
|
|
client,
|
2017-02-03 10:42:01 +00:00
|
|
|
|
mock_get_user_by_email_locked,
|
|
|
|
|
|
):
|
2017-02-03 12:07:21 +00:00
|
|
|
|
resp = client.post(
|
|
|
|
|
|
url_for('main.sign_in'), data={
|
|
|
|
|
|
'email_address': 'valid@example.gov.uk',
|
|
|
|
|
|
'password': 'whatIsMyPassword!'})
|
|
|
|
|
|
assert resp.status_code == 200
|
|
|
|
|
|
assert 'The email address or password you entered 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
|
|
|
|
|
2017-02-03 10:42:01 +00:00
|
|
|
|
def test_should_return_200_when_user_does_not_exist(
|
2017-02-03 12:07:21 +00:00
|
|
|
|
client,
|
2017-02-03 10:42:01 +00:00
|
|
|
|
mock_get_user_by_email_not_found,
|
|
|
|
|
|
):
|
2017-02-03 12:07:21 +00:00
|
|
|
|
response = client.post(
|
|
|
|
|
|
url_for('main.sign_in'), data={
|
|
|
|
|
|
'email_address': 'notfound@gov.uk',
|
|
|
|
|
|
'password': 'doesNotExist!'})
|
2016-01-22 17:24:14 +00:00
|
|
|
|
assert response.status_code == 200
|
2016-04-26 12:26:41 +01:00
|
|
|
|
assert 'The email address or password you entered is incorrect' in response.get_data(as_text=True)
|
2016-01-22 17:24:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-02-03 10:42:01 +00:00
|
|
|
|
def test_should_return_redirect_when_user_is_pending(
|
2017-02-03 12:07:21 +00:00
|
|
|
|
client,
|
2017-02-03 10:42:01 +00:00
|
|
|
|
mock_get_user_by_email_pending,
|
|
|
|
|
|
mock_verify_password,
|
|
|
|
|
|
):
|
2017-02-03 12:07:21 +00:00
|
|
|
|
response = client.post(
|
|
|
|
|
|
url_for('main.sign_in'), data={
|
|
|
|
|
|
'email_address': 'pending_user@example.gov.uk',
|
|
|
|
|
|
'password': 'val1dPassw0rd!'}, follow_redirects=True)
|
2016-09-06 15:44:33 +01:00
|
|
|
|
|
2017-02-03 12:07:21 +00:00
|
|
|
|
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
|
|
|
|
|
|
assert page.h1.string == 'Sign in'
|
|
|
|
|
|
assert response.status_code == 200
|
2016-09-06 15:44:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
2017-02-03 10:42:01 +00:00
|
|
|
|
def test_should_attempt_redirect_when_user_is_pending(
|
2017-02-03 12:07:21 +00:00
|
|
|
|
client,
|
2017-02-03 10:42:01 +00:00
|
|
|
|
mock_get_user_by_email_pending,
|
|
|
|
|
|
mock_verify_password,
|
|
|
|
|
|
):
|
2017-02-03 12:07:21 +00:00
|
|
|
|
response = client.post(
|
|
|
|
|
|
url_for('main.sign_in'), data={
|
|
|
|
|
|
'email_address': 'pending_user@example.gov.uk',
|
|
|
|
|
|
'password': 'val1dPassw0rd!'})
|
|
|
|
|
|
assert response.location == url_for('main.resend_email_verification', _external=True)
|
|
|
|
|
|
assert response.status_code == 302
|
2017-12-21 16:42:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_email_address_is_treated_case_insensitively_when_signing_in_as_invited_user(
|
|
|
|
|
|
client,
|
|
|
|
|
|
mocker,
|
|
|
|
|
|
mock_verify_password,
|
|
|
|
|
|
api_user_active,
|
|
|
|
|
|
sample_invite,
|
|
|
|
|
|
mock_accept_invite,
|
|
|
|
|
|
mock_send_verify_code
|
|
|
|
|
|
):
|
|
|
|
|
|
sample_invite['email_address'] = 'TEST@user.gov.uk'
|
|
|
|
|
|
|
|
|
|
|
|
mocker.patch('app.user_api_client.get_user_by_email_or_none', return_value=api_user_active)
|
|
|
|
|
|
mocker.patch('app.main.views.sign_in._get_and_verify_user', return_value=api_user_active)
|
|
|
|
|
|
|
|
|
|
|
|
with client.session_transaction() as session:
|
|
|
|
|
|
session['invited_user'] = sample_invite
|
|
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
|
|
|
|
|
url_for('main.sign_in'), data={
|
|
|
|
|
|
'email_address': 'test@user.gov.uk',
|
|
|
|
|
|
'password': 'val1dPassw0rd!'})
|
|
|
|
|
|
|
|
|
|
|
|
assert mock_accept_invite.called
|
|
|
|
|
|
assert response.status_code == 302
|
|
|
|
|
|
assert mock_send_verify_code.called
|