109898688: All codes are valid until one code is used, then they are all marked used.

Fixed the is_active() method on the Users model, if the user was pending they would come back as active, allowing a user to sign in before being active.
There is still a problem with the validate_sms_code and validate_email_code method.
This commit is contained in:
Rebecca Law
2015-12-16 12:20:25 +00:00
parent bd8bb3c926
commit 64812c1614
18 changed files with 259 additions and 203 deletions

View File

@@ -7,10 +7,12 @@ from tests.app.main import create_test_user
def test_insert_new_code_and_get_it_back(notifications_admin, notifications_admin_db, notify_db_session):
user = create_test_user()
user = create_test_user('pending')
verify_codes_dao.add_code(user_id=user.id, code='12345', code_type='email')
saved_code = verify_codes_dao.get_code(user_id=user.id, code_type='email')
saved_codes = verify_codes_dao.get_codes(user_id=user.id, code_type='email')
assert len(saved_codes) == 1
saved_code = saved_codes[0]
assert saved_code.user_id == user.id
assert check_hash('12345', saved_code.code)
assert saved_code.code_type == 'email'
@@ -20,7 +22,7 @@ def test_insert_new_code_and_get_it_back(notifications_admin, notifications_admi
def test_insert_new_code_should_thrw_exception_when_type_does_not_exist(notifications_admin,
notifications_admin_db,
notify_db_session):
user = create_test_user()
user = create_test_user('pending')
try:
verify_codes_dao.add_code(user_id=user.id, code='23545', code_type='not_real')
fail('Should have thrown an exception')
@@ -42,7 +44,7 @@ def test_should_throw_exception_when_user_does_not_exist(notifications_admin,
def test_should_return_none_if_code_is_used(notifications_admin,
notifications_admin_db,
notify_db_session):
user = create_test_user()
user = create_test_user('pending')
verify_codes_dao.add_code(user_id=user.id, code='12345', code_type='email')
verify_codes_dao.use_code(user_id=user.id, code='12345', code_type='email')
@@ -53,10 +55,32 @@ def test_should_return_none_if_code_is_used(notifications_admin,
def test_should_return_none_if_code_is_used(notifications_admin,
notifications_admin_db,
notify_db_session):
user = create_test_user()
user = create_test_user('pending')
verify_codes_dao.add_code(user_id=user.id, code='12345', code_type='sms')
code = verify_codes_dao.get_code(user_id=user.id, code_type='sms')
verify_codes_dao.use_code(code.id)
code = verify_codes_dao.get_code(user_id=user.id, code_type='sms')
assert code is None
code = verify_codes_dao.get_codes(user_id=user.id, code_type='sms')
verify_codes_dao.use_code(code[0].id)
used_code = verify_codes_dao.get_codes(user_id=user.id, code_type='sms')
assert used_code == []
def test_should_return_all_unused_code_when_there_are_many(notifications_admin,
notifications_admin_db,
notify_db_session):
user = create_test_user('pending')
another_user = create_test_user('active')
verify_codes_dao.add_code(user_id=user.id, code='12345', code_type='sms')
id = verify_codes_dao.add_code(user_id=user.id, code='09876', code_type='email')
verify_codes_dao.use_code(id)
verify_codes_dao.add_code(user_id=user.id, code='12345', code_type='email')
verify_codes_dao.add_code(user_id=user.id, code='23456', code_type='email')
verify_codes_dao.add_code(user_id=another_user.id, code='12345', code_type='sms')
verify_codes_dao.add_code(user_id=another_user.id, code='12345', code_type='email')
user_codes = verify_codes_dao.get_codes(user_id=user.id, code_type='email')
assert len(user_codes) == 2
s = sorted(user_codes, key=lambda r: r.expiry_datetime)
assert check_hash('12345', s[0].code)
assert check_hash('23456', s[1].code)
assert [(code.code_used, code.code_type, code.user_id) for code in user_codes] == \
[(False, 'email', user.id), (False, 'email', user.id)]