2015-12-10 14:48:01 +00:00
|
|
|
import sqlalchemy
|
|
|
|
|
from pytest import fail
|
|
|
|
|
|
|
|
|
|
from app.main.dao import verify_codes_dao
|
2015-12-10 15:21:06 +00:00
|
|
|
from app.main.encryption import check_hash
|
2015-12-10 14:48:01 +00:00
|
|
|
|
|
|
|
|
|
2016-01-15 15:15:35 +00:00
|
|
|
def test_insert_new_code_and_get_it_back(app_, db_, db_session):
|
2015-12-10 14:48:01 +00:00
|
|
|
|
2016-01-19 22:47:42 +00:00
|
|
|
verify_codes_dao.add_code(user_id=1, code='12345', code_type='email')
|
|
|
|
|
saved_codes = verify_codes_dao.get_codes(user_id=1, code_type='email')
|
2015-12-16 12:20:25 +00:00
|
|
|
assert len(saved_codes) == 1
|
|
|
|
|
saved_code = saved_codes[0]
|
2016-01-19 22:47:42 +00:00
|
|
|
assert saved_code.user_id == 1
|
2015-12-10 15:21:06 +00:00
|
|
|
assert check_hash('12345', saved_code.code)
|
2015-12-10 14:48:01 +00:00
|
|
|
assert saved_code.code_type == 'email'
|
|
|
|
|
assert saved_code.code_used is False
|
|
|
|
|
|
|
|
|
|
|
2016-01-15 15:15:35 +00:00
|
|
|
def test_insert_new_code_should_thrw_exception_when_type_does_not_exist(app_,
|
|
|
|
|
db_,
|
|
|
|
|
db_session):
|
2015-12-10 14:48:01 +00:00
|
|
|
try:
|
2016-01-19 22:47:42 +00:00
|
|
|
verify_codes_dao.add_code(user_id=1, code='23545', code_type='not_real')
|
2015-12-10 14:48:01 +00:00
|
|
|
fail('Should have thrown an exception')
|
|
|
|
|
except sqlalchemy.exc.DataError as e:
|
|
|
|
|
assert 'invalid input value for enum verify_code_types: "not_real"' in e.orig.pgerror
|
|
|
|
|
|
|
|
|
|
|
2016-01-15 15:15:35 +00:00
|
|
|
def test_should_return_none_if_code_is_used(app_,
|
|
|
|
|
db_,
|
|
|
|
|
db_session):
|
2015-12-16 12:20:25 +00:00
|
|
|
|
2016-01-19 22:47:42 +00:00
|
|
|
code = verify_codes_dao.add_code(user_id=1, code='12345', code_type='email')
|
|
|
|
|
verify_codes_dao.use_code(code.id)
|
|
|
|
|
saved_code = verify_codes_dao.get_code_by_code(user_id=1, code_type='email', code='12345')
|
|
|
|
|
assert not saved_code
|