mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-07-12 09:24:15 -04:00
Merge pull request #28 from alphagov/refator_unit_tests
Refator unit tests
This commit is contained in:
@@ -23,7 +23,7 @@ class LoginForm(Form):
|
||||
|
||||
gov_uk_email = "(^[^@^\\s]+@[^@^\\.^\\s]+(\\.[^@^\\.^\\s]*)*.gov.uk)"
|
||||
mobile_number = "^\\+44[\\d]{10}$"
|
||||
verify_code = "[\\d]{5}$"
|
||||
verify_code = '^\d{5}$'
|
||||
|
||||
|
||||
class RegisterUserForm(Form):
|
||||
@@ -71,7 +71,7 @@ class VerifyForm(Form):
|
||||
|
||||
|
||||
def validate_code(field, code):
|
||||
if code.expiry_datetime < datetime.now():
|
||||
if code.expiry_datetime <= datetime.now():
|
||||
field.errors.append('Code has expired')
|
||||
return False
|
||||
if field.data is not None:
|
||||
|
||||
@@ -20,5 +20,4 @@ def process_verify():
|
||||
login_user(user)
|
||||
return redirect('/add-service')
|
||||
else:
|
||||
print(form.errors)
|
||||
return jsonify(form.errors), 400
|
||||
|
||||
71
tests/app/main/test_two_factor_form.py
Normal file
71
tests/app/main/test_two_factor_form.py
Normal file
@@ -0,0 +1,71 @@
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from app.main.dao import verify_codes_dao
|
||||
from app.main.forms import TwoFactorForm
|
||||
from tests.app.main import create_test_user
|
||||
|
||||
|
||||
def test_form_is_valid_returns_no_errors(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context(method='POST',
|
||||
data={'sms_code': '12345'}) as req:
|
||||
user = set_up_test_data()
|
||||
req.session['user_id'] = user.id
|
||||
form = TwoFactorForm(req.request.form)
|
||||
assert form.validate() is True
|
||||
assert len(form.errors) == 0
|
||||
|
||||
|
||||
def test_returns_errors_when_code_is_too_short(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context(method='POST',
|
||||
data={'sms_code': '145'}) as req:
|
||||
user = set_up_test_data()
|
||||
req.session['user_id'] = user.id
|
||||
form = TwoFactorForm(req.request.form)
|
||||
assert form.validate() is False
|
||||
assert len(form.errors) == 1
|
||||
assert set(form.errors) == set({'sms_code': ['Code must be 5 digits', 'Code does not match']})
|
||||
|
||||
|
||||
def test_returns_errors_when_code_is_missing(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context(method='POST',
|
||||
data={}) as req:
|
||||
user = set_up_test_data()
|
||||
req.session['user_id'] = user.id
|
||||
form = TwoFactorForm(req.request.form)
|
||||
assert form.validate() is False
|
||||
assert len(form.errors) == 1
|
||||
assert set(form.errors) == set({'sms_code': ['Code must not be empty']})
|
||||
|
||||
|
||||
def test_returns_errors_when_code_contains_letters(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context(method='POST',
|
||||
data={'sms_code': 'asdfg'}) as req:
|
||||
user = set_up_test_data()
|
||||
req.session['user_id'] = user.id
|
||||
form = TwoFactorForm(req.request.form)
|
||||
assert form.validate() is False
|
||||
assert len(form.errors) == 1
|
||||
assert set(form.errors) == set({'sms_code': ['Code must be 5 digits', 'Code does not match']})
|
||||
|
||||
|
||||
def test_should_return_errors_when_code_is_expired(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context(method='POST',
|
||||
data={'sms_code': '23456'}) as req:
|
||||
user = create_test_user()
|
||||
req.session['user_id'] = user.id
|
||||
verify_codes_dao.add_code_with_expiry(user_id=user.id,
|
||||
code='23456',
|
||||
code_type='sms',
|
||||
expiry=datetime.now() + timedelta(hours=-2))
|
||||
req.session['user_id'] = user.id
|
||||
form = TwoFactorForm(req.request.form)
|
||||
assert form.validate() is False
|
||||
errors = form.errors
|
||||
assert len(errors) == 1
|
||||
assert errors == {'sms_code': ['Code has expired']}
|
||||
|
||||
|
||||
def set_up_test_data():
|
||||
user = create_test_user()
|
||||
verify_codes_dao.add_code(user_id=user.id, code='12345', code_type='sms')
|
||||
return user
|
||||
93
tests/app/main/test_verify_form.py
Normal file
93
tests/app/main/test_verify_form.py
Normal file
@@ -0,0 +1,93 @@
|
||||
from datetime import datetime, timedelta
|
||||
from app.main.dao import verify_codes_dao
|
||||
from app.main.forms import VerifyForm
|
||||
from tests.app.main import create_test_user
|
||||
|
||||
|
||||
def test_form_should_have_error_when_code_is_not_valid(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context(method='POST',
|
||||
data={'sms_code': '12345aa', 'email_code': 'abcde'}) as req:
|
||||
user = set_up_test_data()
|
||||
req.session['user_id'] = user.id
|
||||
form = VerifyForm(req.request.form)
|
||||
assert form.validate() is False
|
||||
errors = form.errors
|
||||
assert len(errors) == 2
|
||||
expected = {'email_code': ['Code must be 5 digits', 'Code does not match'],
|
||||
'sms_code': ['Code does not match', 'Code must be 5 digits']}
|
||||
assert 'sms_code' in errors
|
||||
assert set(errors) == set(expected)
|
||||
|
||||
|
||||
def test_should_return_errors_when_code_missing(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context(method='POST',
|
||||
data={}) as req:
|
||||
user = set_up_test_data()
|
||||
req.session['user_id'] = user.id
|
||||
form = VerifyForm(req.request.form)
|
||||
assert form.validate() is False
|
||||
errors = form.errors
|
||||
expected = {'sms_code': ['SMS code can not be empty'],
|
||||
'email_code': ['Email code can not be empty']}
|
||||
assert len(errors) == 2
|
||||
assert set(errors) == set(expected)
|
||||
|
||||
|
||||
def test_should_return_errors_when_code_is_too_short(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context(method='POST',
|
||||
data={'sms_code': '123', 'email_code': '123'}) as req:
|
||||
user = set_up_test_data()
|
||||
req.session['user_id'] = user.id
|
||||
form = VerifyForm(req.request.form)
|
||||
assert form.validate() is False
|
||||
errors = form.errors
|
||||
expected = {'sms_code': ['Code must be 5 digits', 'Code does not match'],
|
||||
'email_code': ['Code must be 5 digits', 'Code does not match']}
|
||||
assert len(errors) == 2
|
||||
assert set(errors) == set(expected)
|
||||
|
||||
|
||||
def test_should_return_errors_when_code_does_not_match(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context(method='POST',
|
||||
data={'sms_code': '34567', 'email_code': '34567'}) as req:
|
||||
user = set_up_test_data()
|
||||
req.session['user_id'] = user.id
|
||||
form = VerifyForm(req.request.form)
|
||||
assert form.validate() is False
|
||||
errors = form.errors
|
||||
expected = {'sms_code': ['Code does not match'],
|
||||
'email_code': ['Code does not match']}
|
||||
assert len(errors) == 2
|
||||
assert set(errors) == set(expected)
|
||||
|
||||
|
||||
def test_should_return_errors_when_code_is_expired(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context(method='POST',
|
||||
data={'sms_code': '23456',
|
||||
'email_code': '23456'}) as req:
|
||||
user = create_test_user()
|
||||
req.session['user_id'] = user.id
|
||||
verify_codes_dao.add_code_with_expiry(user_id=user.id,
|
||||
code='23456',
|
||||
code_type='sms',
|
||||
expiry=datetime.now() + timedelta(hours=-2))
|
||||
|
||||
verify_codes_dao.add_code_with_expiry(user_id=user.id,
|
||||
code='23456',
|
||||
code_type='email',
|
||||
expiry=datetime.now() + timedelta(hours=-2))
|
||||
req.session['user_id'] = user.id
|
||||
form = VerifyForm(req.request.form)
|
||||
assert form.validate() is False
|
||||
errors = form.errors
|
||||
expected = {'sms_code': ['Code has expired'],
|
||||
'email_code': ['Code has expired']}
|
||||
assert len(errors) == 2
|
||||
assert set(errors) == set(expected)
|
||||
|
||||
|
||||
def set_up_test_data():
|
||||
user = create_test_user()
|
||||
verify_codes_dao.add_code(user_id=user.id, code='12345', code_type='email')
|
||||
verify_codes_dao.add_code(user_id=user.id, code='12345', code_type='sms')
|
||||
return user
|
||||
@@ -35,28 +35,3 @@ def test_should_return_400_with_sms_code_error_when_sms_code_is_wrong(notificati
|
||||
data={'sms_code': '23456'})
|
||||
assert response.status_code == 400
|
||||
assert {'sms_code': ['Code does not match']} == json.loads(response.get_data(as_text=True))
|
||||
|
||||
|
||||
def test_should_return_400_when_sms_code_is_empty(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_client() as client:
|
||||
with client.session_transaction() as session:
|
||||
user = create_test_user()
|
||||
session['user_id'] = user.id
|
||||
verify_codes_dao.add_code(user_id=user.id, code='12345', code_type='sms')
|
||||
response = client.post('/two-factor')
|
||||
assert response.status_code == 400
|
||||
assert {'sms_code': ['Please enter your code']} == json.loads(response.get_data(as_text=True))
|
||||
|
||||
|
||||
def test_should_return_400_when_sms_code_is_too_short(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_client() as client:
|
||||
with client.session_transaction() as session:
|
||||
user = create_test_user()
|
||||
session['user_id'] = user.id
|
||||
verify_codes_dao.add_code(user_id=user.id, code='23467', code_type='sms')
|
||||
response = client.post('/two-factor', data={'sms_code': '2346'})
|
||||
assert response.status_code == 400
|
||||
data = json.loads(response.get_data(as_text=True))
|
||||
assert len(data.keys()) == 1
|
||||
assert 'sms_code' in data
|
||||
assert data['sms_code'].sort() == ['Code must be 5 digits', 'Code does not match'].sort()
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from flask import json
|
||||
|
||||
from app.main.dao import users_dao, verify_codes_dao
|
||||
from tests.app.main import create_test_user
|
||||
|
||||
@@ -43,136 +40,19 @@ def test_should_activate_user_after_verify(notifications_admin, notifications_ad
|
||||
assert after_verify.state == 'active'
|
||||
|
||||
|
||||
def test_should_return_400_when_sms_code_is_wrong(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
def test_should_return_400_when_codes_are_wrong(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_client() as client:
|
||||
with client.session_transaction() as session:
|
||||
user = create_test_user()
|
||||
session['user_id'] = user.id
|
||||
verify_codes_dao.add_code(user_id=user.id, code='12345', code_type='sms')
|
||||
verify_codes_dao.add_code(user_id=user.id, code='23456', code_type='email')
|
||||
response = client.post('/verify',
|
||||
data={'sms_code': '98765',
|
||||
'email_code': '23456'})
|
||||
assert response.status_code == 400
|
||||
assert {'sms_code': ['Code does not match']} == json.loads(response.get_data(as_text=True))
|
||||
|
||||
|
||||
def test_should_return_400_when_email_code_is_wrong(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_client() as client:
|
||||
with client.session_transaction() as session:
|
||||
user = create_test_user()
|
||||
session['user_id'] = user.id
|
||||
verify_codes_dao.add_code(user_id=user.id, code='12345', code_type='sms')
|
||||
verify_codes_dao.add_code(user_id=user.id, code='23345', code_type='sms')
|
||||
verify_codes_dao.add_code(user_id=user.id, code='98456', code_type='email')
|
||||
response = client.post('/verify',
|
||||
data={'sms_code': '12345',
|
||||
'email_code': '23456'})
|
||||
assert response.status_code == 400
|
||||
assert {'email_code': ['Code does not match']} == json.loads(response.get_data(as_text=True))
|
||||
|
||||
|
||||
def test_should_return_400_when_sms_code_is_missing(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_client() as client:
|
||||
with client.session_transaction() as session:
|
||||
user = create_test_user()
|
||||
session['user_id'] = user.id
|
||||
verify_codes_dao.add_code(user_id=user.id, code='12345', code_type='sms')
|
||||
verify_codes_dao.add_code(user_id=user.id, code='98456', code_type='email')
|
||||
response = client.post('/verify',
|
||||
data={'email_code': '98456'})
|
||||
assert response.status_code == 400
|
||||
assert {'sms_code': ['SMS code can not be empty']} == json.loads(response.get_data(as_text=True))
|
||||
|
||||
|
||||
def test_should_return_400_when_email_code_is_missing(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_client() as client:
|
||||
with client.session_transaction() as session:
|
||||
user = create_test_user()
|
||||
session['user_id'] = user.id
|
||||
verify_codes_dao.add_code(user_id=user.id, code='23456', code_type='email')
|
||||
verify_codes_dao.add_code(user_id=user.id, code='23456', code_type='sms')
|
||||
response = client.post('/verify',
|
||||
data={'sms_code': '23456'})
|
||||
assert response.status_code == 400
|
||||
assert {'email_code': ['Email code can not be empty']} == json.loads(response.get_data(as_text=True))
|
||||
|
||||
|
||||
def test_should_return_400_when_email_code_has_letter(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_client() as client:
|
||||
with client.session_transaction() as session:
|
||||
user = create_test_user()
|
||||
session['user_id'] = user.id
|
||||
verify_codes_dao.add_code(user_id=user.id, code='23456', code_type='email')
|
||||
verify_codes_dao.add_code(user_id=user.id, code='23456', code_type='sms')
|
||||
response = client.post('/verify',
|
||||
data={'sms_code': '23456',
|
||||
'email_code': 'abcde'})
|
||||
assert response.status_code == 400
|
||||
data = json.loads(response.get_data(as_text=True))
|
||||
expected = {'email_code': ['Code does not match', 'Code must be 5 digits']}
|
||||
assert len(data.keys()) == 1
|
||||
assert 'email_code' in data
|
||||
assert data['email_code'].sort() == expected['email_code'].sort()
|
||||
|
||||
|
||||
def test_should_return_400_when_sms_code_is_too_short(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_client() as client:
|
||||
with client.session_transaction() as session:
|
||||
user = create_test_user()
|
||||
session['user_id'] = user.id
|
||||
verify_codes_dao.add_code(user_id=user.id, code='23456', code_type='email')
|
||||
verify_codes_dao.add_code(user_id=user.id, code='23456', code_type='sms')
|
||||
response = client.post('/verify',
|
||||
data={'sms_code': '2345',
|
||||
'email_code': '23456'})
|
||||
assert response.status_code == 400
|
||||
data = json.loads(response.get_data(as_text=True))
|
||||
expected = {'sms_code': ['Code must be 5 digits', 'Code does not match']}
|
||||
assert len(data.keys()) == 1
|
||||
assert 'sms_code' in data
|
||||
assert data['sms_code'].sort() == expected['sms_code'].sort()
|
||||
|
||||
|
||||
def test_should_return_302_when_email_code_starts_with_zero(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session):
|
||||
with notifications_admin.test_client() as client:
|
||||
with client.session_transaction() as session:
|
||||
user = create_test_user()
|
||||
session['user_id'] = user.id
|
||||
verify_codes_dao.add_code(user_id=user.id, code='23456', code_type='sms')
|
||||
verify_codes_dao.add_code(user_id=user.id, code='09765', code_type='email')
|
||||
response = client.post('/verify',
|
||||
data={'sms_code': '23456',
|
||||
'email_code': '09765'})
|
||||
assert response.status_code == 302
|
||||
assert response.location == 'http://localhost/add-service'
|
||||
|
||||
|
||||
def test_should_return_400_when_verify_code_has_expired(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session):
|
||||
with notifications_admin.test_client() as client:
|
||||
with client.session_transaction() as session:
|
||||
user = create_test_user()
|
||||
session['user_id'] = user.id
|
||||
verify_codes_dao.add_code_with_expiry(user_id=user.id,
|
||||
code='23456',
|
||||
code_type='email',
|
||||
expiry=datetime.now() + timedelta(hours=-2))
|
||||
verify_codes_dao.add_code_with_expiry(user_id=user.id,
|
||||
code='23456',
|
||||
code_type='sms',
|
||||
expiry=datetime.now() + timedelta(hours=-2))
|
||||
response = client.post('/verify',
|
||||
data={'sms_code': '23456',
|
||||
'email_code': '23456'})
|
||||
assert response.status_code == 400
|
||||
data = json.loads(response.get_data(as_text=True))
|
||||
expected = {'sms_code': ['Code has expired'],
|
||||
'email_code': ['Code has expired']}
|
||||
assert len(data.keys()) == 2
|
||||
assert 'sms_code' in data
|
||||
assert data['sms_code'].sort() == expected['sms_code'].sort()
|
||||
assert 'email_code' in data
|
||||
assert data['email_code'].sort() == expected['email_code'].sort()
|
||||
expected = {'sms_code': ['Code must be 5 digits', 'Code does not match'],
|
||||
'email_code': ['Code must be 5 digits', 'Code does not match']}
|
||||
errors = json.loads(response.get_data(as_text=True))
|
||||
assert len(errors) == 2
|
||||
assert set(errors) == set(expected)
|
||||
|
||||
Reference in New Issue
Block a user