2016-01-15 15:15:35 +00:00
|
|
|
from flask import url_for
|
2016-03-09 15:12:33 +00:00
|
|
|
from bs4 import BeautifulSoup
|
2015-12-01 13:23:54 +00:00
|
|
|
|
2016-01-19 15:50:31 +00:00
|
|
|
|
2016-01-27 16:30:33 +00:00
|
|
|
def test_render_register_returns_template_with_form(app_):
|
2016-01-22 17:24:14 +00:00
|
|
|
response = app_.test_client().get('/register')
|
2016-01-15 17:46:09 +00:00
|
|
|
|
2016-01-22 17:24:14 +00:00
|
|
|
assert response.status_code == 200
|
|
|
|
|
assert 'Create an account' in response.get_data(as_text=True)
|
2015-12-01 13:23:54 +00:00
|
|
|
|
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_send_verify_code,
|
|
|
|
|
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.register'))
|
|
|
|
|
assert response.status_code == 302
|
2015-12-01 13:23:54 +00:00
|
|
|
|
2016-01-22 17:24:14 +00:00
|
|
|
response = client.get(url_for('main.sign_in', follow_redirects=True))
|
|
|
|
|
assert response.location == url_for('main.choose_service', _external=True)
|
2015-12-01 13:23:54 +00:00
|
|
|
|
|
|
|
|
|
2016-01-15 17:46:09 +00:00
|
|
|
def test_process_register_creates_new_user(app_,
|
2016-01-27 12:22:32 +00:00
|
|
|
mock_send_verify_code,
|
2016-01-21 11:33:53 +00:00
|
|
|
mock_register_user,
|
2016-01-27 16:30:33 +00:00
|
|
|
mock_get_user_by_email_not_found,
|
2016-01-27 12:22:32 +00:00
|
|
|
mock_login):
|
2016-01-19 15:50:31 +00:00
|
|
|
user_data = {
|
|
|
|
|
'name': 'Some One Valid',
|
2016-01-23 23:14:50 +00:00
|
|
|
'email_address': 'notfound@example.gov.uk',
|
2016-01-19 15:50:31 +00:00
|
|
|
'mobile_number': '+4407700900460',
|
|
|
|
|
'password': 'validPassword!'
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-15 17:46:09 +00:00
|
|
|
with app_.test_request_context():
|
2016-01-27 16:30:33 +00:00
|
|
|
response = app_.test_client().post(url_for('main.register'),
|
2016-01-19 15:50:31 +00:00
|
|
|
data=user_data)
|
2016-01-15 17:46:09 +00:00
|
|
|
assert response.status_code == 302
|
|
|
|
|
assert response.location == url_for('main.verify', _external=True)
|
2016-01-23 23:14:50 +00:00
|
|
|
assert mock_register_user.called
|
2015-12-01 13:23:54 +00:00
|
|
|
|
|
|
|
|
|
2016-03-10 11:53:29 +00:00
|
|
|
def test_process_register_returns_200_when_mobile_number_is_invalid(app_,
|
2016-01-27 12:22:32 +00:00
|
|
|
mock_send_verify_code,
|
2016-01-27 16:30:33 +00:00
|
|
|
mock_get_user_by_email_not_found,
|
2016-01-27 12:22:32 +00:00
|
|
|
mock_login):
|
2016-01-27 16:30:33 +00:00
|
|
|
with app_.test_request_context():
|
|
|
|
|
response = app_.test_client().post(url_for('main.register'),
|
|
|
|
|
data={'name': 'Bad Mobile',
|
|
|
|
|
'email_address': 'bad_mobile@example.gov.uk',
|
|
|
|
|
'mobile_number': 'not good',
|
|
|
|
|
'password': 'validPassword!'})
|
2016-01-21 11:33:53 +00:00
|
|
|
|
2016-01-21 12:12:06 +00:00
|
|
|
assert response.status_code == 200
|
2016-03-07 18:47:05 +00:00
|
|
|
assert 'Must not contain letters or symbols' in response.get_data(as_text=True)
|
2016-01-21 11:33:53 +00:00
|
|
|
|
|
|
|
|
|
2016-01-21 12:12:06 +00:00
|
|
|
def test_should_return_400_when_email_is_not_gov_uk(app_,
|
2016-01-27 12:22:32 +00:00
|
|
|
mock_send_verify_code,
|
2016-01-27 16:30:33 +00:00
|
|
|
mock_get_user_by_email,
|
2016-01-27 12:22:32 +00:00
|
|
|
mock_login):
|
2016-01-27 16:30:33 +00:00
|
|
|
with app_.test_request_context():
|
|
|
|
|
response = app_.test_client().post(url_for('main.register'),
|
|
|
|
|
data={'name': 'Bad Mobile',
|
|
|
|
|
'email_address': 'bad_mobile@example.not.right',
|
|
|
|
|
'mobile_number': '+44123412345',
|
|
|
|
|
'password': 'validPassword!'})
|
2016-01-21 11:33:53 +00:00
|
|
|
|
2016-01-21 12:12:06 +00:00
|
|
|
assert response.status_code == 200
|
|
|
|
|
assert 'Enter a gov.uk email address' in response.get_data(as_text=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_add_verify_codes_on_session(app_,
|
2016-01-27 12:22:32 +00:00
|
|
|
mock_send_verify_code,
|
2016-01-21 12:12:06 +00:00
|
|
|
mock_register_user,
|
2016-01-27 16:30:33 +00:00
|
|
|
mock_get_user,
|
|
|
|
|
mock_get_user_by_email_not_found,
|
2016-01-27 12:22:32 +00:00
|
|
|
mock_login):
|
2016-01-21 12:12:06 +00:00
|
|
|
user_data = {
|
|
|
|
|
'name': 'Test Codes',
|
2016-01-23 23:14:50 +00:00
|
|
|
'email_address': 'notfound@example.gov.uk',
|
2016-01-21 12:12:06 +00:00
|
|
|
'mobile_number': '+4407700900460',
|
|
|
|
|
'password': 'validPassword!'
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-27 16:30:33 +00:00
|
|
|
with app_.test_request_context():
|
|
|
|
|
with app_.test_client() as client:
|
|
|
|
|
response = client.post(url_for('main.register'),
|
|
|
|
|
data=user_data)
|
|
|
|
|
assert response.status_code == 302
|
|
|
|
|
assert 'notify_admin_session' in response.headers.get('Set-Cookie')
|
2016-01-21 12:12:06 +00:00
|
|
|
|
|
|
|
|
|
2016-01-27 12:22:32 +00:00
|
|
|
def test_should_return_400_if_password_is_blacklisted(app_,
|
2016-01-27 16:30:33 +00:00
|
|
|
mock_get_user_by_email,
|
2016-01-27 12:22:32 +00:00
|
|
|
mock_login):
|
2016-01-27 16:30:33 +00:00
|
|
|
with app_.test_request_context():
|
|
|
|
|
response = app_.test_client().post(url_for('main.register'),
|
|
|
|
|
data={'name': 'Bad Mobile',
|
|
|
|
|
'email_address': 'bad_mobile@example.not.right',
|
|
|
|
|
'mobile_number': '+44123412345',
|
|
|
|
|
'password': 'password1234'})
|
2016-01-21 12:12:06 +00:00
|
|
|
|
|
|
|
|
response.status_code == 200
|
|
|
|
|
assert 'That password is blacklisted, too common' in response.get_data(as_text=True)
|
2016-03-09 15:12:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_register_with_existing_email_returns_error(app_,
|
|
|
|
|
api_user_active,
|
|
|
|
|
mock_get_user_by_email):
|
|
|
|
|
user_data = {
|
|
|
|
|
'name': 'Already Hasaccount',
|
|
|
|
|
'email_address': api_user_active.email_address,
|
|
|
|
|
'mobile_number': '+4407700900460',
|
|
|
|
|
'password': 'validPassword!'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
with app_.test_request_context():
|
|
|
|
|
response = app_.test_client().post(url_for('main.register'),
|
|
|
|
|
data=user_data)
|
2016-03-09 15:46:45 +00:00
|
|
|
assert response.status_code == 400
|
2016-03-09 15:12:33 +00:00
|
|
|
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
|
|
|
|
|
element = page.find('h1')
|
|
|
|
|
assert element.text == 'Create an account'
|
|
|
|
|
flash_banner = page.find('div', class_='banner-dangerous').string.strip()
|
|
|
|
|
assert flash_banner == 'There was an error registering your account'
|