2016-01-08 17:51:46 +00:00
|
|
|
from sqlalchemy.exc import DataError
|
|
|
|
|
from sqlalchemy.orm.exc import NoResultFound
|
2016-01-25 11:14:23 +00:00
|
|
|
|
|
|
|
|
import pytest
|
2016-01-12 10:39:49 +00:00
|
|
|
from app.dao.users_dao import (
|
2016-01-25 11:14:23 +00:00
|
|
|
save_model_user,
|
|
|
|
|
get_model_users,
|
|
|
|
|
delete_model_user,
|
|
|
|
|
increment_failed_login_count
|
|
|
|
|
)
|
|
|
|
|
|
2016-01-08 12:18:12 +00:00
|
|
|
from tests.app.conftest import sample_user as create_sample_user
|
|
|
|
|
from app.models import User
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_user(notify_api, notify_db, notify_db_session):
|
|
|
|
|
email = 'notify@digital.cabinet-office.gov.uk'
|
2016-01-19 11:38:29 +00:00
|
|
|
data = {
|
|
|
|
|
'name': 'Test User',
|
|
|
|
|
'email_address': email,
|
|
|
|
|
'password': 'password',
|
2016-01-22 14:43:30 +00:00
|
|
|
'mobile_number': '+447700900986'
|
2016-01-19 11:38:29 +00:00
|
|
|
}
|
|
|
|
|
user = User(**data)
|
2016-01-11 17:19:06 +00:00
|
|
|
save_model_user(user)
|
2016-01-08 12:18:12 +00:00
|
|
|
assert User.query.count() == 1
|
|
|
|
|
assert User.query.first().email_address == email
|
2016-01-11 15:07:13 +00:00
|
|
|
assert User.query.first().id == user.id
|
2016-01-08 12:18:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_all_users(notify_api, notify_db, notify_db_session, sample_user):
|
|
|
|
|
assert User.query.count() == 1
|
2016-01-11 15:07:13 +00:00
|
|
|
assert len(get_model_users()) == 1
|
2016-01-08 12:18:12 +00:00
|
|
|
email = "another.notify@digital.cabinet-office.gov.uk"
|
|
|
|
|
another_user = create_sample_user(notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
email=email)
|
|
|
|
|
assert User.query.count() == 2
|
2016-01-11 15:07:13 +00:00
|
|
|
assert len(get_model_users()) == 2
|
2016-01-08 12:18:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_user(notify_api, notify_db, notify_db_session):
|
|
|
|
|
email = "another.notify@digital.cabinet-office.gov.uk"
|
|
|
|
|
another_user = create_sample_user(notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
email=email)
|
2016-01-11 15:07:13 +00:00
|
|
|
assert get_model_users(user_id=another_user.id).email_address == email
|
2016-01-08 17:51:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_user_not_exists(notify_api, notify_db, notify_db_session):
|
|
|
|
|
try:
|
2016-01-11 15:07:13 +00:00
|
|
|
get_model_users(user_id="12345")
|
2016-01-08 17:51:46 +00:00
|
|
|
pytest.fail("NoResultFound exception not thrown.")
|
|
|
|
|
except:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_user_invalid_id(notify_api, notify_db, notify_db_session):
|
|
|
|
|
try:
|
2016-01-11 15:07:13 +00:00
|
|
|
get_model_users(user_id="blah")
|
2016-01-08 17:51:46 +00:00
|
|
|
pytest.fail("DataError exception not thrown.")
|
|
|
|
|
except DataError:
|
|
|
|
|
pass
|
2016-01-12 10:39:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_delete_users(notify_api, notify_db, notify_db_session, sample_user):
|
|
|
|
|
assert User.query.count() == 1
|
|
|
|
|
delete_model_user(sample_user)
|
|
|
|
|
assert User.query.count() == 0
|
2016-01-25 11:14:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_increment_failed_login_should_increment_failed_logins(notify_api, notify_db, notify_db_session, sample_user):
|
|
|
|
|
assert User.query.count() == 1
|
|
|
|
|
assert sample_user.failed_login_count == 0
|
|
|
|
|
increment_failed_login_count(sample_user)
|
|
|
|
|
assert sample_user.failed_login_count == 1
|