mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-11 09:28:27 -04:00
Tests added for dao.
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
from flask.testing import FlaskClient
|
||||
from flask import url_for
|
||||
from app.models import User
|
||||
from app.main.dao import (users_dao, verify_codes_dao)
|
||||
|
||||
|
||||
class TestClient(FlaskClient):
|
||||
def login(self, user):
|
||||
# Skipping authentication here and just log them in
|
||||
with self.session_transaction() as session:
|
||||
session['user_email'] = user.email_address
|
||||
verify_codes_dao.add_code(user_id=user.id, code='12345', code_type='sms')
|
||||
response = self.post(
|
||||
url_for('main.two_factor'), data={'sms_code': '12345'})
|
||||
assert response.status_code == 200
|
||||
|
||||
def logout(self, user):
|
||||
self.get(url_for("main.logout"))
|
||||
|
||||
|
||||
def service_json(id_, name, users, limit=1000, active=False, restricted=True):
|
||||
return {
|
||||
'id': id_,
|
||||
'name': name,
|
||||
'users': users,
|
||||
'limit': limit,
|
||||
'active': active,
|
||||
'restricted': restricted
|
||||
}
|
||||
|
||||
|
||||
def create_test_user(state):
|
||||
user = User(name='Test User',
|
||||
password='somepassword',
|
||||
email_address='test@user.gov.uk',
|
||||
mobile_number='+441234123412',
|
||||
role_id=1,
|
||||
state=state)
|
||||
users_dao.insert_user(user)
|
||||
return user
|
||||
|
||||
|
||||
def create_another_test_user(state):
|
||||
user = User(name='Another Test User',
|
||||
password='someOtherpassword',
|
||||
email_address='another_test@user.gov.uk',
|
||||
mobile_number='+442233123412',
|
||||
role_id=1,
|
||||
state=state)
|
||||
users_dao.insert_user(user)
|
||||
return user
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
from datetime import datetime
|
||||
|
||||
from app.main.dao import users_dao
|
||||
from app.models import User
|
||||
|
||||
|
||||
def create_test_user(state):
|
||||
user = User(name='Test User',
|
||||
password='somepassword',
|
||||
email_address='test@user.gov.uk',
|
||||
mobile_number='+441234123412',
|
||||
created_at=datetime.now(),
|
||||
role_id=1,
|
||||
state=state)
|
||||
users_dao.insert_user(user)
|
||||
return user
|
||||
|
||||
|
||||
def create_another_test_user(state):
|
||||
user = User(name='Another Test User',
|
||||
password='someOtherpassword',
|
||||
email_address='another_test@user.gov.uk',
|
||||
mobile_number='+442233123412',
|
||||
created_at=datetime.now(),
|
||||
role_id=1,
|
||||
state=state)
|
||||
users_dao.insert_user(user)
|
||||
return user
|
||||
|
||||
@@ -5,7 +5,7 @@ from app.models import Roles
|
||||
from app.main.dao import roles_dao
|
||||
|
||||
|
||||
def test_insert_role_should_be_able_to_get_role(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
def test_insert_role_should_be_able_to_get_role(app_, db_, db_session):
|
||||
role = Roles(id=1000, role='some role for test')
|
||||
roles_dao.insert_role(role)
|
||||
|
||||
@@ -13,9 +13,9 @@ def test_insert_role_should_be_able_to_get_role(notifications_admin, notificatio
|
||||
assert saved_role == role
|
||||
|
||||
|
||||
def test_insert_role_will_throw_error_if_role_already_exists(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session):
|
||||
def test_insert_role_will_throw_error_if_role_already_exists(app_,
|
||||
db_,
|
||||
db_session):
|
||||
role1 = roles_dao.get_role_by_id(1)
|
||||
assert role1.id == 1
|
||||
|
||||
|
||||
@@ -1,73 +1,63 @@
|
||||
import pytest
|
||||
import sqlalchemy
|
||||
|
||||
from app.main.dao import services_dao
|
||||
from tests.app.main import create_test_user
|
||||
|
||||
|
||||
def test_can_insert_and_retrieve_new_service(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
user = create_test_user('active')
|
||||
id = services_dao.insert_new_service('testing service', user)
|
||||
saved_service = services_dao.get_service_by_id(id)
|
||||
assert id == saved_service.id
|
||||
assert saved_service.users == [user]
|
||||
assert saved_service.name == 'testing service'
|
||||
def test_can_insert_new_service(db_,
|
||||
db_session,
|
||||
active_user,
|
||||
mock_create_service):
|
||||
service_name = 'testing service'
|
||||
id_ = services_dao.insert_new_service(service_name, active_user.id)
|
||||
mock_create_service.assert_called_once_with(
|
||||
service_name, False, 1000, True, active_user.id)
|
||||
|
||||
|
||||
def test_unrestrict_service_updates_the_service(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
user = create_test_user('active')
|
||||
id = services_dao.insert_new_service('unrestricted service', user)
|
||||
saved_service = services_dao.get_service_by_id(id)
|
||||
assert saved_service.restricted is True
|
||||
services_dao.unrestrict_service(id)
|
||||
unrestricted_service = services_dao.get_service_by_id(id)
|
||||
assert unrestricted_service.restricted is False
|
||||
def test_unrestrict_service_updates_the_service(db_,
|
||||
db_session,
|
||||
mock_get_service,
|
||||
mock_update_service,
|
||||
service_one):
|
||||
mock_get_service.return_value = {'data': service_one}
|
||||
services_dao.unrestrict_service(service_one['id'])
|
||||
mock_update_service.assert_called_once_with(service_one['id'],
|
||||
service_one['name'],
|
||||
service_one['active'],
|
||||
service_one['limit'],
|
||||
False,
|
||||
service_one['users'])
|
||||
|
||||
|
||||
def test_activate_service_update_service(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
user = create_test_user('active')
|
||||
id = services_dao.insert_new_service('activated service', user)
|
||||
service = services_dao.get_service_by_id(id)
|
||||
assert service.active is False
|
||||
services_dao.activate_service(id)
|
||||
activated_service = services_dao.get_service_by_id(id)
|
||||
assert activated_service.active is True
|
||||
def test_activate_service_update_service(db_,
|
||||
db_session,
|
||||
mock_get_service,
|
||||
mock_update_service,
|
||||
service_one):
|
||||
mock_get_service.return_value = {'data': service_one}
|
||||
services_dao.activate_service(service_one['id'])
|
||||
mock_update_service.assert_called_once_with(service_one['id'],
|
||||
service_one['name'],
|
||||
True,
|
||||
service_one['limit'],
|
||||
service_one['restricted'],
|
||||
service_one['users'])
|
||||
|
||||
|
||||
def test_get_service_returns_none_if_service_does_not_exist(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session):
|
||||
def test_get_service_returns_none_if_service_does_not_exist(db_, db_session, mock_get_service):
|
||||
mock_get_service.return_value = None
|
||||
service = services_dao.get_service_by_id(1)
|
||||
assert service is None
|
||||
|
||||
|
||||
def test_find_by_service_name_returns_right_service(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session):
|
||||
user = create_test_user('active')
|
||||
id = services_dao.insert_new_service('testing service', user)
|
||||
another = services_dao.insert_new_service('Testing the Service', user)
|
||||
found = services_dao.find_service_by_service_name('testing service')
|
||||
assert found.id == id
|
||||
assert found.name == 'testing service'
|
||||
found_another = services_dao.find_service_by_service_name('Testing the Service')
|
||||
assert found_another == services_dao.get_service_by_id(another)
|
||||
def test_find_by_service_name_returns_right_service(db_, db_session, mock_get_services):
|
||||
service_name = "service_one"
|
||||
service = services_dao.find_service_by_service_name(service_name)
|
||||
assert mock_get_services.called
|
||||
assert service['name'] == service_name
|
||||
|
||||
|
||||
def test_should_not_allow_two_services_of_the_same_name(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
user = create_test_user('active')
|
||||
services_dao.insert_new_service('duplicate service', user)
|
||||
with pytest.raises(sqlalchemy.exc.IntegrityError) as error:
|
||||
services_dao.insert_new_service('duplicate service', user)
|
||||
assert 'duplicate key value violates unique constraint "services_name_key' in error.value
|
||||
|
||||
|
||||
def test_should_return_list_of_service_names(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
user = create_test_user('active')
|
||||
services_dao.insert_new_service('first service', user)
|
||||
services_dao.insert_new_service('second service', user)
|
||||
services_dao.insert_new_service('third service', user)
|
||||
expected = ['first service', 'second service', 'third service']
|
||||
|
||||
def test_should_return_list_of_service_names(db_, db_session, mock_get_services):
|
||||
expected = ['service_one', 'service_two']
|
||||
actual = services_dao.find_all_service_names()
|
||||
assert mock_get_services.called
|
||||
assert actual == expected
|
||||
|
||||
@@ -6,12 +6,11 @@ from app.models import User
|
||||
from app.main.dao import users_dao
|
||||
|
||||
|
||||
def test_insert_user_should_add_user(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
def test_insert_user_should_add_user(db_, db_session):
|
||||
user = User(name='test insert',
|
||||
password='somepassword',
|
||||
email_address='test@insert.gov.uk',
|
||||
mobile_number='+441234123412',
|
||||
created_at=datetime.now(),
|
||||
role_id=1)
|
||||
|
||||
users_dao.insert_user(user)
|
||||
@@ -19,26 +18,22 @@ def test_insert_user_should_add_user(notifications_admin, notifications_admin_db
|
||||
assert saved_user == user
|
||||
|
||||
|
||||
def test_insert_user_with_role_that_does_not_exist_fails(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session):
|
||||
def test_insert_user_with_role_that_does_not_exist_fails(db_, db_session):
|
||||
user = User(name='role does not exist',
|
||||
password='somepassword',
|
||||
email_address='test@insert.gov.uk',
|
||||
mobile_number='+441234123412',
|
||||
created_at=datetime.now(),
|
||||
role_id=100)
|
||||
with pytest.raises(sqlalchemy.exc.IntegrityError) as error:
|
||||
users_dao.insert_user(user)
|
||||
assert 'insert or update on table "users" violates foreign key constraint "users_role_id_fkey"' in str(error.value)
|
||||
|
||||
|
||||
def test_get_user_by_email(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
def test_get_user_by_email(db_, db_session):
|
||||
user = User(name='test_get_by_email',
|
||||
password='somepassword',
|
||||
email_address='email@example.gov.uk',
|
||||
mobile_number='+441234153412',
|
||||
created_at=datetime.now(),
|
||||
role_id=1)
|
||||
|
||||
users_dao.insert_user(user)
|
||||
@@ -46,24 +41,21 @@ def test_get_user_by_email(notifications_admin, notifications_admin_db, notify_d
|
||||
assert retrieved == user
|
||||
|
||||
|
||||
def test_get_all_users_returns_all_users(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
def test_get_all_users_returns_all_users(db_, db_session):
|
||||
user1 = User(name='test one',
|
||||
password='somepassword',
|
||||
email_address='test1@get_all.gov.uk',
|
||||
mobile_number='+441234123412',
|
||||
created_at=datetime.now(),
|
||||
role_id=1)
|
||||
user2 = User(name='test two',
|
||||
password='some2ndpassword',
|
||||
email_address='test2@get_all.gov.uk',
|
||||
mobile_number='+441234123412',
|
||||
created_at=datetime.now(),
|
||||
role_id=1)
|
||||
user3 = User(name='test three',
|
||||
password='some2ndpassword',
|
||||
email_address='test3@get_all.gov.uk',
|
||||
mobile_number='+441234123412',
|
||||
created_at=datetime.now(),
|
||||
role_id=1)
|
||||
|
||||
users_dao.insert_user(user1)
|
||||
@@ -74,14 +66,11 @@ def test_get_all_users_returns_all_users(notifications_admin, notifications_admi
|
||||
assert users == [user1, user2, user3]
|
||||
|
||||
|
||||
def test_increment_failed_lockout_count_should_increade_count_by_1(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session):
|
||||
def test_increment_failed_lockout_count_should_increade_count_by_1(db_, db_session):
|
||||
user = User(name='cannot remember password',
|
||||
password='somepassword',
|
||||
email_address='test1@get_all.gov.uk',
|
||||
mobile_number='+441234123412',
|
||||
created_at=datetime.now(),
|
||||
role_id=1)
|
||||
users_dao.insert_user(user)
|
||||
|
||||
@@ -91,13 +80,11 @@ def test_increment_failed_lockout_count_should_increade_count_by_1(notifications
|
||||
assert users_dao.get_user_by_id(user.id).failed_login_count == 1
|
||||
|
||||
|
||||
def test_user_is_locked_if_failed_login_count_is_10_or_greater(notifications_admin,
|
||||
notifications_admin_db, notify_db_session):
|
||||
def test_user_is_locked_if_failed_login_count_is_10_or_greater(db_, db_session):
|
||||
user = User(name='cannot remember password',
|
||||
password='somepassword',
|
||||
email_address='test1@get_all.gov.uk',
|
||||
mobile_number='+441234123412',
|
||||
created_at=datetime.now(),
|
||||
role_id=1)
|
||||
users_dao.insert_user(user)
|
||||
saved_user = users_dao.get_user_by_id(user.id)
|
||||
@@ -111,12 +98,11 @@ def test_user_is_locked_if_failed_login_count_is_10_or_greater(notifications_adm
|
||||
assert saved_user.is_locked() is True
|
||||
|
||||
|
||||
def test_user_is_active_is_false_if_state_is_inactive(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
def test_user_is_active_is_false_if_state_is_inactive(db_, db_session):
|
||||
user = User(name='inactive user',
|
||||
password='somepassword',
|
||||
email_address='test1@get_all.gov.uk',
|
||||
mobile_number='+441234123412',
|
||||
created_at=datetime.now(),
|
||||
role_id=1,
|
||||
state='inactive')
|
||||
users_dao.insert_user(user)
|
||||
@@ -125,12 +111,11 @@ def test_user_is_active_is_false_if_state_is_inactive(notifications_admin, notif
|
||||
assert saved_user.is_active() is False
|
||||
|
||||
|
||||
def test_should_update_user_to_active(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
def test_should_update_user_to_active(db_, db_session):
|
||||
user = User(name='Make user active',
|
||||
password='somepassword',
|
||||
email_address='activate@user.gov.uk',
|
||||
mobile_number='+441234123412',
|
||||
created_at=datetime.now(),
|
||||
role_id=1,
|
||||
state='pending')
|
||||
users_dao.insert_user(user)
|
||||
@@ -139,18 +124,17 @@ def test_should_update_user_to_active(notifications_admin, notifications_admin_d
|
||||
assert updated_user.state == 'active'
|
||||
|
||||
|
||||
def test_should_throws_error_when_id_does_not_exist(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
def test_should_throws_error_when_id_does_not_exist(db_, db_session):
|
||||
with pytest.raises(AttributeError) as error:
|
||||
users_dao.activate_user(123)
|
||||
assert '''object has no attribute 'state''''' in str(error.value)
|
||||
|
||||
|
||||
def test_should_update_email_address(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
def test_should_update_email_address(db_, db_session):
|
||||
user = User(name='Update Email',
|
||||
password='somepassword',
|
||||
email_address='test@it.gov.uk',
|
||||
mobile_number='+441234123412',
|
||||
created_at=datetime.now(),
|
||||
role_id=1,
|
||||
state='inactive')
|
||||
users_dao.insert_user(user)
|
||||
@@ -162,12 +146,11 @@ def test_should_update_email_address(notifications_admin, notifications_admin_db
|
||||
assert updated.email_address == 'new_email@testit.gov.uk'
|
||||
|
||||
|
||||
def test_should_update_password(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
def test_should_update_password(db_, db_session):
|
||||
user = User(name='Update Email',
|
||||
password='somepassword',
|
||||
email_address='test@it.gov.uk',
|
||||
mobile_number='+441234123412',
|
||||
created_at=datetime.now(),
|
||||
role_id=1,
|
||||
state='active')
|
||||
start = datetime.now()
|
||||
@@ -183,19 +166,17 @@ def test_should_update_password(notifications_admin, notifications_admin_db, not
|
||||
assert updated.password_changed_at > start
|
||||
|
||||
|
||||
def test_should_return_list_of_all_email_addresses(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
def test_should_return_list_of_all_email_addresses(db_, db_session):
|
||||
first = User(name='First Person',
|
||||
password='somepassword',
|
||||
email_address='first@it.gov.uk',
|
||||
mobile_number='+441234123412',
|
||||
created_at=datetime.now(),
|
||||
role_id=1,
|
||||
state='active')
|
||||
second = User(name='Second Person',
|
||||
password='somepassword',
|
||||
email_address='second@it.gov.uk',
|
||||
mobile_number='+441234123412',
|
||||
created_at=datetime.now(),
|
||||
role_id=1,
|
||||
state='active')
|
||||
users_dao.insert_user(first)
|
||||
@@ -206,12 +187,11 @@ def test_should_return_list_of_all_email_addresses(notifications_admin, notifica
|
||||
assert expected == [x.email_address for x in email_addresses]
|
||||
|
||||
|
||||
def test_should_update_state_to_request_password_reset(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
def test_should_update_state_to_request_password_reset(db_, db_session):
|
||||
user = User(name='Requesting Password Resest',
|
||||
password='somepassword',
|
||||
email_address='request@new_password.gov.uk',
|
||||
mobile_number='+441234123412',
|
||||
created_at=datetime.now(),
|
||||
role_id=1,
|
||||
state='active')
|
||||
users_dao.insert_user(user)
|
||||
|
||||
@@ -6,7 +6,7 @@ from app.main.encryption import check_hash
|
||||
from tests.app.main import create_test_user, create_another_test_user
|
||||
|
||||
|
||||
def test_insert_new_code_and_get_it_back(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
def test_insert_new_code_and_get_it_back(app_, db_, db_session):
|
||||
user = create_test_user('pending')
|
||||
|
||||
verify_codes_dao.add_code(user_id=user.id, code='12345', code_type='email')
|
||||
@@ -19,9 +19,9 @@ def test_insert_new_code_and_get_it_back(notifications_admin, notifications_admi
|
||||
assert saved_code.code_used is False
|
||||
|
||||
|
||||
def test_insert_new_code_should_thrw_exception_when_type_does_not_exist(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session):
|
||||
def test_insert_new_code_should_thrw_exception_when_type_does_not_exist(app_,
|
||||
db_,
|
||||
db_session):
|
||||
user = create_test_user('pending')
|
||||
try:
|
||||
verify_codes_dao.add_code(user_id=user.id, code='23545', code_type='not_real')
|
||||
@@ -30,9 +30,9 @@ def test_insert_new_code_should_thrw_exception_when_type_does_not_exist(notifica
|
||||
assert 'invalid input value for enum verify_code_types: "not_real"' in e.orig.pgerror
|
||||
|
||||
|
||||
def test_should_throw_exception_when_user_does_not_exist(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session):
|
||||
def test_should_throw_exception_when_user_does_not_exist(app_,
|
||||
db_,
|
||||
db_session):
|
||||
try:
|
||||
verify_codes_dao.add_code(user_id=1, code='12345', code_type='email')
|
||||
fail('Should throw exception')
|
||||
@@ -41,9 +41,9 @@ def test_should_throw_exception_when_user_does_not_exist(notifications_admin,
|
||||
'foreign key constraint "verify_codes_user_id_fkey"' in e.orig.pgerror
|
||||
|
||||
|
||||
def test_should_return_none_if_code_is_used(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session):
|
||||
def test_should_return_none_if_code_is_used(app_,
|
||||
db_,
|
||||
db_session):
|
||||
user = create_test_user('pending')
|
||||
|
||||
verify_codes_dao.add_code(user_id=user.id, code='12345', code_type='email')
|
||||
@@ -52,9 +52,9 @@ def test_should_return_none_if_code_is_used(notifications_admin,
|
||||
assert saved_code.code_used is True
|
||||
|
||||
|
||||
def test_should_return_none_if_code_is_used(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session):
|
||||
def test_should_return_none_if_code_is_used(app_,
|
||||
db_,
|
||||
db_session):
|
||||
user = create_test_user('pending')
|
||||
|
||||
verify_codes_dao.add_code(user_id=user.id, code='12345', code_type='sms')
|
||||
@@ -64,9 +64,9 @@ def test_should_return_none_if_code_is_used(notifications_admin,
|
||||
assert used_code == []
|
||||
|
||||
|
||||
def test_should_return_all_unused_code_when_there_are_many(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session):
|
||||
def test_should_return_all_unused_code_when_there_are_many(app_,
|
||||
db_,
|
||||
db_session):
|
||||
user = create_test_user('pending')
|
||||
another_user = create_another_test_user('active')
|
||||
verify_codes_dao.add_code(user_id=user.id, code='12345', code_type='sms')
|
||||
|
||||
@@ -4,17 +4,17 @@ from pytest import fail
|
||||
from app.notify_client.sender import generate_token, check_token
|
||||
|
||||
|
||||
def test_should_return_email_from_signed_token(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session):
|
||||
def test_should_return_email_from_signed_token(app_,
|
||||
db_,
|
||||
db_session):
|
||||
email = 'email@something.com'
|
||||
token = generate_token(email)
|
||||
assert email == check_token(token)
|
||||
|
||||
|
||||
def test_should_throw_exception_when_token_is_tampered_with(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session):
|
||||
def test_should_throw_exception_when_token_is_tampered_with(app_,
|
||||
db_,
|
||||
db_session):
|
||||
email = 'email@something.com'
|
||||
token = generate_token(email)
|
||||
try:
|
||||
@@ -24,12 +24,12 @@ def test_should_throw_exception_when_token_is_tampered_with(notifications_admin,
|
||||
pass
|
||||
|
||||
|
||||
def test_return_none_when_token_is_expired(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
notifications_admin.config['TOKEN_MAX_AGE_SECONDS'] = -1000
|
||||
def test_return_none_when_token_is_expired(app_,
|
||||
db_,
|
||||
db_session):
|
||||
with app_.test_request_context():
|
||||
app_.config['TOKEN_MAX_AGE_SECONDS'] = -1000
|
||||
email = 'email@something.com'
|
||||
token = generate_token(email)
|
||||
assert check_token(token) is None
|
||||
notifications_admin.config['TOKEN_MAX_AGE_SECONDS'] = 120000
|
||||
app_.config['TOKEN_MAX_AGE_SECONDS'] = 120000
|
||||
|
||||
@@ -2,10 +2,10 @@ from app.main.forms import AddServiceForm
|
||||
from werkzeug.datastructures import MultiDict
|
||||
|
||||
|
||||
def test_form_should_have_errors_when_duplicate_service_is_added(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
def test_form_should_have_errors_when_duplicate_service_is_added(app_,
|
||||
db_,
|
||||
db_session):
|
||||
with app_.test_request_context():
|
||||
form = AddServiceForm(['some service', 'more names'],
|
||||
formdata=MultiDict([('service_name', 'some service')]))
|
||||
form.validate()
|
||||
|
||||
@@ -5,8 +5,8 @@ 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',
|
||||
def test_form_is_valid_returns_no_errors(app_, db_, db_session):
|
||||
with app_.test_request_context(method='POST',
|
||||
data={'sms_code': '12345'}) as req:
|
||||
user = set_up_test_data()
|
||||
codes = verify_codes_dao.get_codes(user.id)
|
||||
@@ -15,8 +15,8 @@ def test_form_is_valid_returns_no_errors(notifications_admin, notifications_admi
|
||||
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',
|
||||
def test_returns_errors_when_code_is_too_short(app_, db_, db_session):
|
||||
with app_.test_request_context(method='POST',
|
||||
data={'sms_code': '145'}) as req:
|
||||
user = set_up_test_data()
|
||||
codes = verify_codes_dao.get_codes(user.id)
|
||||
@@ -26,8 +26,8 @@ def test_returns_errors_when_code_is_too_short(notifications_admin, notification
|
||||
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',
|
||||
def test_returns_errors_when_code_is_missing(app_, db_, db_session):
|
||||
with app_.test_request_context(method='POST',
|
||||
data={}) as req:
|
||||
user = set_up_test_data()
|
||||
codes = verify_codes_dao.get_codes(user.id)
|
||||
@@ -37,8 +37,8 @@ def test_returns_errors_when_code_is_missing(notifications_admin, notifications_
|
||||
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',
|
||||
def test_returns_errors_when_code_contains_letters(app_, db_, db_session):
|
||||
with app_.test_request_context(method='POST',
|
||||
data={'sms_code': 'asdfg'}) as req:
|
||||
user = set_up_test_data()
|
||||
codes = verify_codes_dao.get_codes(user.id)
|
||||
@@ -48,8 +48,8 @@ def test_returns_errors_when_code_contains_letters(notifications_admin, notifica
|
||||
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',
|
||||
def test_should_return_errors_when_code_is_expired(app_, db_, db_session):
|
||||
with app_.test_request_context(method='POST',
|
||||
data={'sms_code': '23456'}) as req:
|
||||
user = create_test_user('active')
|
||||
verify_codes_dao.add_code_with_expiry(user_id=user.id,
|
||||
|
||||
@@ -2,7 +2,7 @@ from app.main.dao import users_dao
|
||||
from app.main.forms import RegisterUserForm
|
||||
|
||||
|
||||
def test_should_raise_validation_error_for_password(notifications_admin):
|
||||
def test_should_raise_validation_error_for_password(app_):
|
||||
form = RegisterUserForm(users_dao.get_user_by_email)
|
||||
form.name.data = 'test'
|
||||
form.email_address.data = 'teset@example.gov.uk'
|
||||
|
||||
@@ -4,8 +4,8 @@ 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',
|
||||
def test_form_should_have_error_when_code_is_not_valid(app_, db_, db_session):
|
||||
with app_.test_request_context(method='POST',
|
||||
data={'sms_code': '12345aa', 'email_code': 'abcde'}) as req:
|
||||
user = set_up_test_data()
|
||||
codes = verify_codes_dao.get_codes(user.id)
|
||||
@@ -19,8 +19,8 @@ def test_form_should_have_error_when_code_is_not_valid(notifications_admin, noti
|
||||
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',
|
||||
def test_should_return_errors_when_code_missing(app_, db_, db_session):
|
||||
with app_.test_request_context(method='POST',
|
||||
data={}) as req:
|
||||
user = set_up_test_data()
|
||||
codes = verify_codes_dao.get_codes(user.id)
|
||||
@@ -33,8 +33,8 @@ def test_should_return_errors_when_code_missing(notifications_admin, notificatio
|
||||
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',
|
||||
def test_should_return_errors_when_code_is_too_short(app_, db_, db_session):
|
||||
with app_.test_request_context(method='POST',
|
||||
data={'sms_code': '123', 'email_code': '123'}) as req:
|
||||
user = set_up_test_data()
|
||||
codes = verify_codes_dao.get_codes(user.id)
|
||||
@@ -47,8 +47,8 @@ def test_should_return_errors_when_code_is_too_short(notifications_admin, notifi
|
||||
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',
|
||||
def test_should_return_errors_when_code_does_not_match(app_, db_, db_session):
|
||||
with app_.test_request_context(method='POST',
|
||||
data={'sms_code': '34567', 'email_code': '34567'}) as req:
|
||||
user = set_up_test_data()
|
||||
codes = verify_codes_dao.get_codes(user.id)
|
||||
@@ -61,8 +61,8 @@ def test_should_return_errors_when_code_does_not_match(notifications_admin, noti
|
||||
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',
|
||||
def test_should_return_errors_when_code_is_expired(app_, db_, db_session):
|
||||
with app_.test_request_context(method='POST',
|
||||
data={'sms_code': '23456',
|
||||
'email_code': '23456'}) as req:
|
||||
user = create_test_user('pending')
|
||||
@@ -85,10 +85,10 @@ def test_should_return_errors_when_code_is_expired(notifications_admin, notifica
|
||||
assert set(errors) == set(expected)
|
||||
|
||||
|
||||
def test_should_return_valid_form_when_many_codes_exist(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session):
|
||||
with notifications_admin.test_request_context(method='POST',
|
||||
def test_should_return_valid_form_when_many_codes_exist(app_,
|
||||
db_,
|
||||
db_session):
|
||||
with app_.test_request_context(method='POST',
|
||||
data={'sms_code': '23456',
|
||||
'email_code': '23456'}) as req:
|
||||
user = set_up_test_data()
|
||||
|
||||
@@ -2,9 +2,9 @@ from app.main.dao import verify_codes_dao, services_dao
|
||||
from tests.app.main import create_test_user
|
||||
|
||||
|
||||
def test_get_should_render_add_service_template(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_get_should_render_add_service_template(app_, db_, db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
user = create_test_user('active')
|
||||
client.login(user)
|
||||
verify_codes_dao.add_code(user_id=user.id, code='12345', code_type='sms')
|
||||
@@ -14,9 +14,9 @@ def test_get_should_render_add_service_template(notifications_admin, notificatio
|
||||
assert 'Set up notifications for your service' in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_should_add_service_and_redirect_to_next_page(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_add_service_and_redirect_to_next_page(app_, db_, db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
user = create_test_user('active')
|
||||
client.login(user)
|
||||
verify_codes_dao.add_code(user_id=user.id, code='12345', code_type='sms')
|
||||
@@ -28,11 +28,11 @@ def test_should_add_service_and_redirect_to_next_page(notifications_admin, notif
|
||||
assert saved_service is not None
|
||||
|
||||
|
||||
def test_should_return_form_errors_when_service_name_is_empty(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_return_form_errors_when_service_name_is_empty(app_,
|
||||
db_,
|
||||
db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
user = create_test_user('active')
|
||||
client.login(user)
|
||||
verify_codes_dao.add_code(user_id=user.id, code='12345', code_type='sms')
|
||||
|
||||
@@ -2,11 +2,11 @@ from tests.app.main import create_test_user
|
||||
from flask import url_for
|
||||
|
||||
|
||||
def test_should_show_choose_services_page(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_show_choose_services_page(app_,
|
||||
db_,
|
||||
db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
user = create_test_user('active')
|
||||
client.login(user)
|
||||
response = client.get('/services')
|
||||
|
||||
@@ -3,14 +3,13 @@ from tests.app.main import create_test_user
|
||||
from flask import url_for
|
||||
|
||||
|
||||
def test_should_render_email_code_not_received_template_and_populate_email_address(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session,
|
||||
mocker):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_render_email_code_not_received_template_and_populate_email_address(db_,
|
||||
db_session,
|
||||
mock_send_sms,
|
||||
mock_send_email):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
with client.session_transaction() as session:
|
||||
_set_up_mocker(mocker)
|
||||
user = create_test_user('pending')
|
||||
session['user_email'] = user.email_address
|
||||
response = client.get(url_for('main.check_and_resend_email_code'))
|
||||
@@ -20,14 +19,13 @@ def test_should_render_email_code_not_received_template_and_populate_email_addre
|
||||
assert 'value="test@user.gov.uk"' in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_should_check_and_resend_email_code_redirect_to_verify(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session,
|
||||
mocker):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_check_and_resend_email_code_redirect_to_verify(db_,
|
||||
db_session,
|
||||
mock_send_sms,
|
||||
mock_send_email):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
with client.session_transaction() as session:
|
||||
_set_up_mocker(mocker)
|
||||
user = create_test_user('pending')
|
||||
session['user_email'] = user.email_address
|
||||
verify_codes_dao.add_code(user.id, code='12345', code_type='email')
|
||||
@@ -37,12 +35,12 @@ def test_should_check_and_resend_email_code_redirect_to_verify(notifications_adm
|
||||
assert response.location == url_for('main.verify', _external=True)
|
||||
|
||||
|
||||
def test_should_render_text_code_not_received_template(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session,
|
||||
mocker):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_render_text_code_not_received_template(db_,
|
||||
db_session,
|
||||
mock_send_sms,
|
||||
mock_send_email):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
with client.session_transaction() as session:
|
||||
_set_up_mocker(mocker)
|
||||
user = create_test_user('pending')
|
||||
@@ -55,14 +53,13 @@ def test_should_render_text_code_not_received_template(notifications_admin,
|
||||
assert 'value="+441234123412"'
|
||||
|
||||
|
||||
def test_should_check_and_redirect_to_verify(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session,
|
||||
mocker):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_check_and_redirect_to_verify(db_,
|
||||
db_session,
|
||||
mock_send_sms,
|
||||
mock_send_email):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
with client.session_transaction() as session:
|
||||
_set_up_mocker(mocker)
|
||||
user = create_test_user('pending')
|
||||
session['user_email'] = user.email_address
|
||||
verify_codes_dao.add_code(user.id, code='12345', code_type='sms')
|
||||
@@ -72,14 +69,13 @@ def test_should_check_and_redirect_to_verify(notifications_admin,
|
||||
assert response.location == url_for('main.verify', _external=True)
|
||||
|
||||
|
||||
def test_should_update_email_address_resend_code(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session,
|
||||
mocker):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_update_email_address_resend_code(db_,
|
||||
db_session,
|
||||
mock_send_sms,
|
||||
mock_send_email):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
with client.session_transaction() as session:
|
||||
_set_up_mocker(mocker)
|
||||
user = create_test_user('pending')
|
||||
session['user_email'] = user.email_address
|
||||
verify_codes_dao.add_code(user_id=user.id, code='12345', code_type='email')
|
||||
@@ -91,14 +87,13 @@ def test_should_update_email_address_resend_code(notifications_admin,
|
||||
assert updated_user.email_address == 'new@address.gov.uk'
|
||||
|
||||
|
||||
def test_should_update_mobile_number_resend_code(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session,
|
||||
mocker):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_update_mobile_number_resend_code(db_,
|
||||
db_session,
|
||||
mock_send_sms,
|
||||
mock_send_email):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
with client.session_transaction() as session:
|
||||
_set_up_mocker(mocker)
|
||||
user = create_test_user('pending')
|
||||
session['user_email'] = user.email_address
|
||||
verify_codes_dao.add_code(user_id=user.id, code='12345', code_type='sms')
|
||||
@@ -110,11 +105,10 @@ def test_should_update_mobile_number_resend_code(notifications_admin,
|
||||
assert updated_user.mobile_number == '+44 7700 900 460'
|
||||
|
||||
|
||||
def test_should_render_verification_code_not_received(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_render_verification_code_not_received(db_,
|
||||
db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
with client.session_transaction() as session:
|
||||
user = create_test_user('active')
|
||||
session['user_email'] = user.email_address
|
||||
@@ -125,32 +119,30 @@ def test_should_render_verification_code_not_received(notifications_admin,
|
||||
'speak to your service manager to reset the number.' in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_check_and_redirect_to_two_factor(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session,
|
||||
mocker):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_check_and_redirect_to_two_factor(db_,
|
||||
db_session,
|
||||
mock_send_sms,
|
||||
mock_send_email):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
with client.session_transaction() as session:
|
||||
user = create_test_user('active')
|
||||
session['user_email'] = user.email_address
|
||||
_set_up_mocker(mocker)
|
||||
response = client.get(url_for('main.check_and_resend_verification_code'))
|
||||
assert response.status_code == 302
|
||||
assert response.location == url_for('main.two_factor', _external=True)
|
||||
|
||||
|
||||
def test_should_create_new_code_for_user(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session,
|
||||
mocker):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_create_new_code_for_user(db_,
|
||||
db_session,
|
||||
mock_send_sms,
|
||||
mock_send_email):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
with client.session_transaction() as session:
|
||||
user = create_test_user('active')
|
||||
session['user_email'] = user.email_address
|
||||
verify_codes_dao.add_code(user_id=user.id, code='12345', code_type='sms')
|
||||
_set_up_mocker(mocker)
|
||||
response = client.get(url_for('main.check_and_resend_verification_code'))
|
||||
assert response.status_code == 302
|
||||
assert response.location == url_for('main.two_factor', _external=True)
|
||||
@@ -158,9 +150,3 @@ def test_should_create_new_code_for_user(notifications_admin,
|
||||
assert len(codes) == 2
|
||||
for x in ([used.code_used for used in codes]):
|
||||
assert x is False
|
||||
|
||||
|
||||
def _set_up_mocker(mocker):
|
||||
# mocker.patch("app.admin_api_client.send_sms")
|
||||
# mocker.patch("app.admin_api_client.send_email")
|
||||
pass
|
||||
|
||||
@@ -2,11 +2,11 @@ from tests.app.main import create_test_user
|
||||
from flask import url_for
|
||||
|
||||
|
||||
def test_should_show_recent_jobs_on_dashboard(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_show_recent_jobs_on_dashboard(app_,
|
||||
db_,
|
||||
db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
user = create_test_user('active')
|
||||
client.login(user)
|
||||
response = client.get('/services/123/dashboard')
|
||||
|
||||
@@ -3,24 +3,25 @@ from app.main.dao import users_dao
|
||||
from tests.app.main import create_test_user
|
||||
|
||||
|
||||
def test_should_render_forgot_password(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
response = notifications_admin.test_client().get(url_for('.forgot_password'))
|
||||
def test_should_render_forgot_password(app_, db_, db_session):
|
||||
with app_.test_request_context():
|
||||
response = app_.test_client().get(url_for('.forgot_password'))
|
||||
assert response.status_code == 200
|
||||
assert 'If you have forgotten your password, we can send you an email to create a new password.' \
|
||||
in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_should_redirect_to_password_reset_sent_and_state_updated(notifications_admin,
|
||||
notifications_admin_db,
|
||||
mocker,
|
||||
notify_db_session):
|
||||
# mocker.patch("app.admin_api_client.send_email")
|
||||
with notifications_admin.test_request_context():
|
||||
user = create_test_user('active')
|
||||
response = notifications_admin.test_client().post(url_for('.forgot_password'),
|
||||
data={'email_address': user.email_address})
|
||||
def test_should_redirect_to_password_reset_sent_and_state_updated(app_,
|
||||
db_,
|
||||
db_session,
|
||||
active_user,
|
||||
mock_send_email):
|
||||
with app_.test_request_context():
|
||||
response = app_.test_client().post(
|
||||
url_for('.forgot_password'),
|
||||
data={'email_address': active_user.email_address})
|
||||
assert response.status_code == 200
|
||||
assert 'You have been sent an email containing a link to reset your password.' in response.get_data(
|
||||
as_text=True)
|
||||
assert users_dao.get_user_by_id(user.id).state == 'request_password_reset'
|
||||
assert (
|
||||
'You have been sent an email containing a link'
|
||||
' to reset your password.') in response.get_data(as_text=True)
|
||||
assert users_dao.get_user_by_id(active_user.id).state == 'request_password_reset'
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
|
||||
def test_owasp_useful_headers_set(notifications_admin):
|
||||
with notifications_admin.test_request_context():
|
||||
response = notifications_admin.test_client().get('/')
|
||||
def test_owasp_useful_headers_set(app_):
|
||||
with app_.test_request_context():
|
||||
response = app_.test_client().get('/')
|
||||
assert response.status_code == 200
|
||||
assert response.headers['X-Frame-Options'] == 'deny'
|
||||
assert response.headers['X-Content-Type-Options'] == 'nosniff'
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
from tests.app.main import create_test_user
|
||||
|
||||
|
||||
def test_should_return_list_of_all_jobs(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_return_list_of_all_jobs(app_, db_, db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
user = create_test_user('active')
|
||||
client.login(user)
|
||||
response = client.get('/services/123/jobs')
|
||||
@@ -13,9 +13,9 @@ def test_should_return_list_of_all_jobs(notifications_admin, notifications_admin
|
||||
assert 'Final reminder' in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_should_show_page_for_one_job(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_show_page_for_one_job(app_, db_, db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
# TODO filename will be part of job metadata not in session
|
||||
with client.session_transaction() as s:
|
||||
s[456] = 'dispatch_20151114.csv'
|
||||
@@ -28,9 +28,9 @@ def test_should_show_page_for_one_job(notifications_admin, notifications_admin_d
|
||||
assert 'Test message 1' in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_should_show_page_for_one_notification(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_show_page_for_one_notification(app_, db_, db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
user = create_test_user('active')
|
||||
client.login(user)
|
||||
response = client.get('/services/123/jobs/456/notification/3')
|
||||
|
||||
@@ -6,9 +6,9 @@ from app.notify_client.sender import generate_token
|
||||
from tests.app.main import create_test_user
|
||||
|
||||
|
||||
def test_should_render_new_password_template(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_render_new_password_template(db_, db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
user = create_test_user('request_password_reset')
|
||||
token = generate_token(user.email_address)
|
||||
response = client.get(url_for('.new_password', token=token))
|
||||
@@ -16,10 +16,9 @@ def test_should_render_new_password_template(notifications_admin, notifications_
|
||||
assert ' You can now create a new password for your account.' in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_should_render_new_password_template_with_message_of_bad_token(notifications_admin, notifications_admin_db,
|
||||
notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_render_new_password_template_with_message_of_bad_token(db_, db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
create_test_user('request_password_reset')
|
||||
token = generate_token('no_user@d.gov.uk')
|
||||
response = client.get(url_for('.new_password', token=token))
|
||||
@@ -28,13 +27,11 @@ def test_should_render_new_password_template_with_message_of_bad_token(notificat
|
||||
response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_should_redirect_to_two_factor_when_password_reset_is_successful(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session,
|
||||
mocker):
|
||||
_set_up_mocker(mocker)
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_redirect_to_two_factor_when_password_reset_is_successful(db_,
|
||||
db_session,
|
||||
mock_send_sms):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
user = create_test_user('request_password_reset')
|
||||
token = generate_token(user.email_address)
|
||||
response = client.post(url_for('.new_password', token=token), data={'new_password': 'a-new_password'})
|
||||
@@ -45,32 +42,25 @@ def test_should_redirect_to_two_factor_when_password_reset_is_successful(notific
|
||||
assert saved_user.state == 'active'
|
||||
|
||||
|
||||
def test_should_redirect_to_forgot_password_with_flash_message_when_token_is_expired(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
notifications_admin.config['TOKEN_MAX_AGE_SECONDS'] = -1000
|
||||
def test_should_redirect_to_forgot_password_with_flash_message_when_token_is_expired(db_,
|
||||
db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
app_.config['TOKEN_MAX_AGE_SECONDS'] = -1000
|
||||
user = create_test_user('request_password_reset')
|
||||
token = generate_token(user.email_address)
|
||||
response = client.post(url_for('.new_password', token=token), data={'new_password': 'a-new_password'})
|
||||
assert response.status_code == 302
|
||||
assert response.location == url_for('.forgot_password', _external=True)
|
||||
notifications_admin.config['TOKEN_MAX_AGE_SECONDS'] = 3600
|
||||
app_.config['TOKEN_MAX_AGE_SECONDS'] = 3600
|
||||
|
||||
|
||||
def test_should_redirect_to_forgot_password_when_user_is_active_should_be_request_password_reset(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_redirect_to_forgot_password_when_user_is_active_should_be_request_password_reset(db_,
|
||||
db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
user = create_test_user('active')
|
||||
token = generate_token(user.email_address)
|
||||
response = client.post(url_for('.new_password', token=token), data={'new_password': 'a-new_password'})
|
||||
assert response.status_code == 302
|
||||
assert response.location == url_for('.index', _external=True)
|
||||
|
||||
|
||||
def _set_up_mocker(mocker):
|
||||
# mocker.patch("app.admin_api_client.send_sms")
|
||||
pass
|
||||
|
||||
@@ -1,57 +1,58 @@
|
||||
from flask import url_for
|
||||
|
||||
|
||||
def test_render_register_returns_template_with_form(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
response = notifications_admin.test_client().get('/register')
|
||||
def test_render_register_returns_template_with_form(db_, db_session):
|
||||
response = app_.test_client().get('/register')
|
||||
|
||||
assert response.status_code == 200
|
||||
assert 'Create an account' in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_process_register_creates_new_user(notifications_admin, notifications_admin_db, mocker, notify_db_session):
|
||||
_set_up_mocker(mocker)
|
||||
|
||||
response = notifications_admin.test_client().post('/register',
|
||||
data={'name': 'Some One Valid',
|
||||
'email_address': 'someone@example.gov.uk',
|
||||
'mobile_number': '+4407700900460',
|
||||
'password': 'validPassword!'})
|
||||
def test_process_register_creates_new_user(db_,
|
||||
db_session,
|
||||
mock_send_sms,
|
||||
mock_send_email):
|
||||
response = app_.test_client().post('/register',
|
||||
data={'name': 'Some One Valid',
|
||||
'email_address': 'someone@example.gov.uk',
|
||||
'mobile_number': '+4407700900460',
|
||||
'password': 'validPassword!'})
|
||||
assert response.status_code == 302
|
||||
assert response.location == 'http://localhost/verify'
|
||||
assert response.location == url_for('main.verify', _external=True)
|
||||
|
||||
|
||||
def test_process_register_returns_400_when_mobile_number_is_invalid(notifications_admin,
|
||||
notifications_admin_db,
|
||||
mocker,
|
||||
notify_db_session):
|
||||
_set_up_mocker(mocker)
|
||||
response = notifications_admin.test_client().post('/register',
|
||||
data={'name': 'Bad Mobile',
|
||||
'email_address': 'bad_mobile@example.gov.uk',
|
||||
'mobile_number': 'not good',
|
||||
'password': 'validPassword!'})
|
||||
def test_process_register_returns_400_when_mobile_number_is_invalid(db_,
|
||||
db_session,
|
||||
mock_send_sms,
|
||||
mock_send_email):
|
||||
response = app_.test_client().post('/register',
|
||||
data={'name': 'Bad Mobile',
|
||||
'email_address': 'bad_mobile@example.gov.uk',
|
||||
'mobile_number': 'not good',
|
||||
'password': 'validPassword!'})
|
||||
|
||||
assert response.status_code == 200
|
||||
assert 'Must be a UK mobile number (eg 07700 900460)' in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_should_return_400_when_email_is_not_gov_uk(notifications_admin,
|
||||
notifications_admin_db,
|
||||
mocker,
|
||||
notify_db_session):
|
||||
_set_up_mocker(mocker)
|
||||
response = notifications_admin.test_client().post('/register',
|
||||
data={'name': 'Bad Mobile',
|
||||
'email_address': 'bad_mobile@example.not.right',
|
||||
'mobile_number': '+44123412345',
|
||||
'password': 'validPassword!'})
|
||||
def test_should_return_400_when_email_is_not_gov_uk(db_,
|
||||
db_session,
|
||||
mock_send_sms,
|
||||
mock_send_email):
|
||||
response = app_.test_client().post('/register',
|
||||
data={'name': 'Bad Mobile',
|
||||
'email_address': 'bad_mobile@example.not.right',
|
||||
'mobile_number': '+44123412345',
|
||||
'password': 'validPassword!'})
|
||||
|
||||
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(notifications_admin, notifications_admin_db, mocker, notify_db_session):
|
||||
_set_up_mocker(mocker)
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_add_verify_codes_on_session(db_,
|
||||
db_session,
|
||||
mock_send_sms,
|
||||
mock_send_email):
|
||||
with app_.test_client() as client:
|
||||
response = client.post('/register',
|
||||
data={'name': 'Test Codes',
|
||||
'email_address': 'test_codes@example.gov.uk',
|
||||
@@ -61,18 +62,12 @@ def test_should_add_verify_codes_on_session(notifications_admin, notifications_a
|
||||
assert 'notify_admin_session' in response.headers.get('Set-Cookie')
|
||||
|
||||
|
||||
def _set_up_mocker(mocker):
|
||||
# mocker.patch("app.admin_api_client.send_sms")
|
||||
# mocker.patch("app.admin_api_client.send_email")
|
||||
pass
|
||||
|
||||
|
||||
def test_should_return_400_if_password_is_blacklisted(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
response = notifications_admin.test_client().post('/register',
|
||||
data={'name': 'Bad Mobile',
|
||||
'email_address': 'bad_mobile@example.not.right',
|
||||
'mobile_number': '+44123412345',
|
||||
'password': 'password1234'})
|
||||
def test_should_return_400_if_password_is_blacklisted(db_, db_session):
|
||||
response = app_.test_client().post('/register',
|
||||
data={'name': 'Bad Mobile',
|
||||
'email_address': 'bad_mobile@example.not.right',
|
||||
'mobile_number': '+44123412345',
|
||||
'password': 'password1234'})
|
||||
|
||||
response.status_code == 200
|
||||
assert 'That password is blacklisted, too common' in response.get_data(as_text=True)
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
from tests.app.main import create_test_user
|
||||
|
||||
|
||||
def test_should_show_overview(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_show_overview(app_, db_, db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
user = create_test_user('active')
|
||||
client.login(user)
|
||||
response = client.get('/services/123/service-settings')
|
||||
@@ -11,9 +11,9 @@ def test_should_show_overview(notifications_admin, notifications_admin_db, notif
|
||||
assert 'Service settings' in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_should_show_service_name(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_show_service_name(app_, db_, db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
user = create_test_user('active')
|
||||
client.login(user)
|
||||
response = client.get('/services/123/service-settings/name')
|
||||
@@ -21,9 +21,9 @@ def test_should_show_service_name(notifications_admin, notifications_admin_db, n
|
||||
assert 'Change your service name' in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_should_redirect_after_change_service_name(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_redirect_after_change_service_name(app_, db_, db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
user = create_test_user('active')
|
||||
client.login(user)
|
||||
response = client.post('/services/123/service-settings/request-to-go-live')
|
||||
@@ -32,9 +32,9 @@ def test_should_redirect_after_change_service_name(notifications_admin, notifica
|
||||
assert 'http://localhost/services/123/service-settings' == response.location
|
||||
|
||||
|
||||
def test_should_show_service_name_confirmation(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_show_service_name_confirmation(app_, db_, db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
user = create_test_user('active')
|
||||
client.login(user)
|
||||
response = client.get('/services/123/service-settings/name/confirm')
|
||||
@@ -43,10 +43,10 @@ def test_should_show_service_name_confirmation(notifications_admin, notification
|
||||
assert 'Change your service name' in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_should_redirect_after_service_name_confirmation(notifications_admin, notifications_admin_db,
|
||||
notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_redirect_after_service_name_confirmation(app_, db_,
|
||||
db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
user = create_test_user('active')
|
||||
client.login(user)
|
||||
response = client.post('/services/123/service-settings/name/confirm')
|
||||
@@ -55,9 +55,9 @@ def test_should_redirect_after_service_name_confirmation(notifications_admin, no
|
||||
assert 'http://localhost/services/123/service-settings' == response.location
|
||||
|
||||
|
||||
def test_should_show_request_to_go_live(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_show_request_to_go_live(app_, db_, db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
user = create_test_user('active')
|
||||
client.login(user)
|
||||
response = client.get('/services/123/service-settings/request-to-go-live')
|
||||
@@ -66,9 +66,9 @@ def test_should_show_request_to_go_live(notifications_admin, notifications_admin
|
||||
assert 'Request to go live' in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_should_redirect_after_request_to_go_live(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_redirect_after_request_to_go_live(app_, db_, db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
user = create_test_user('active')
|
||||
client.login(user)
|
||||
response = client.post('/services/123/service-settings/request-to-go-live')
|
||||
@@ -77,9 +77,9 @@ def test_should_redirect_after_request_to_go_live(notifications_admin, notificat
|
||||
assert 'http://localhost/services/123/service-settings' == response.location
|
||||
|
||||
|
||||
def test_should_show_status_page(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_show_status_page(app_, db_, db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
user = create_test_user('active')
|
||||
client.login(user)
|
||||
response = client.get('/services/123/service-settings/status')
|
||||
@@ -88,9 +88,9 @@ def test_should_show_status_page(notifications_admin, notifications_admin_db, no
|
||||
assert 'Turn off all outgoing notifications' in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_should_show_redirect_after_status_change(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_show_redirect_after_status_change(app_, db_, db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
user = create_test_user('active')
|
||||
client.login(user)
|
||||
response = client.post('/services/123/service-settings/status')
|
||||
@@ -99,9 +99,9 @@ def test_should_show_redirect_after_status_change(notifications_admin, notificat
|
||||
assert 'http://localhost/services/123/service-settings/status/confirm' == response.location
|
||||
|
||||
|
||||
def test_should_show_status_confirmation(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_show_status_confirmation(app_, db_, db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
user = create_test_user('active')
|
||||
client.login(user)
|
||||
response = client.get('/services/123/service-settings/status/confirm')
|
||||
@@ -110,9 +110,9 @@ def test_should_show_status_confirmation(notifications_admin, notifications_admi
|
||||
assert 'Turn off all outgoing notifications' in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_should_redirect_after_status_confirmation(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_redirect_after_status_confirmation(app_, db_, db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
user = create_test_user('active')
|
||||
client.login(user)
|
||||
response = client.post('/services/123/service-settings/status/confirm')
|
||||
@@ -121,9 +121,9 @@ def test_should_redirect_after_status_confirmation(notifications_admin, notifica
|
||||
assert 'http://localhost/services/123/service-settings' == response.location
|
||||
|
||||
|
||||
def test_should_show_delete_page(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_show_delete_page(app_, db_, db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
user = create_test_user('active')
|
||||
client.login(user)
|
||||
response = client.get('/services/123/service-settings/delete')
|
||||
@@ -132,9 +132,9 @@ def test_should_show_delete_page(notifications_admin, notifications_admin_db, no
|
||||
assert 'Delete this service from Notify' in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_should_show_redirect_after_deleting_service(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_show_redirect_after_deleting_service(app_, db_, db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
user = create_test_user('active')
|
||||
client.login(user)
|
||||
response = client.post('/services/123/service-settings/delete')
|
||||
@@ -143,9 +143,9 @@ def test_should_show_redirect_after_deleting_service(notifications_admin, notifi
|
||||
assert 'http://localhost/services/123/service-settings/delete/confirm' == response.location
|
||||
|
||||
|
||||
def test_should_show_delete_confirmation(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_show_delete_confirmation(app_, db_, db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
user = create_test_user('active')
|
||||
client.login(user)
|
||||
response = client.get('/services/123/service-settings/delete/confirm')
|
||||
@@ -154,9 +154,9 @@ def test_should_show_delete_confirmation(notifications_admin, notifications_admi
|
||||
assert 'Delete this service from Notify' in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_should_redirect_delete_confirmation(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_redirect_delete_confirmation(app_, db_, db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
user = create_test_user('active')
|
||||
client.login(user)
|
||||
response = client.post('/services/123/service-settings/delete/confirm')
|
||||
|
||||
@@ -5,9 +5,9 @@ from app.models import User
|
||||
from flask import url_for
|
||||
|
||||
|
||||
def test_render_sign_in_returns_sign_in_template(notifications_admin):
|
||||
with notifications_admin.test_request_context():
|
||||
response = notifications_admin.test_client().get(url_for('main.sign_in'))
|
||||
def test_render_sign_in_returns_sign_in_template(app_):
|
||||
with app_.test_request_context():
|
||||
response = app_.test_client().get(url_for('main.sign_in'))
|
||||
assert response.status_code == 200
|
||||
assert 'Sign in' in response.get_data(as_text=True)
|
||||
assert 'Email address' in response.get_data(as_text=True)
|
||||
@@ -15,8 +15,11 @@ def test_render_sign_in_returns_sign_in_template(notifications_admin):
|
||||
assert 'Forgotten password?' in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_process_sign_in_return_2fa_template(notifications_admin, notifications_admin_db, mocker, notify_db_session):
|
||||
_set_up_mocker(mocker)
|
||||
def test_process_sign_in_return_2fa_template(app_,
|
||||
db_,
|
||||
db_session,
|
||||
mock_send_sms,
|
||||
mock_send_email):
|
||||
user = User(email_address='valid@example.gov.uk',
|
||||
password='val1dPassw0rd!',
|
||||
mobile_number='+441234123123',
|
||||
@@ -25,8 +28,8 @@ def test_process_sign_in_return_2fa_template(notifications_admin, notifications_
|
||||
role_id=1,
|
||||
state='active')
|
||||
users_dao.insert_user(user)
|
||||
with notifications_admin.test_request_context():
|
||||
response = notifications_admin.test_client().post(
|
||||
with app_.test_request_context():
|
||||
response = app_.test_client().post(
|
||||
url_for('main.sign_in'), data={
|
||||
'email_address': 'valid@example.gov.uk',
|
||||
'password': 'val1dPassw0rd!'})
|
||||
@@ -34,9 +37,9 @@ def test_process_sign_in_return_2fa_template(notifications_admin, notifications_
|
||||
assert response.location == 'http://localhost/two-factor'
|
||||
|
||||
|
||||
def test_should_return_locked_out_true_when_user_is_locked(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session):
|
||||
def test_should_return_locked_out_true_when_user_is_locked(app_,
|
||||
db_,
|
||||
db_session):
|
||||
user = User(email_address='valid@example.gov.uk',
|
||||
password='val1dPassw0rd!',
|
||||
mobile_number='+441234123123',
|
||||
@@ -45,14 +48,14 @@ def test_should_return_locked_out_true_when_user_is_locked(notifications_admin,
|
||||
role_id=1,
|
||||
state='active')
|
||||
users_dao.insert_user(user)
|
||||
with notifications_admin.test_request_context():
|
||||
with app_.test_request_context():
|
||||
for _ in range(10):
|
||||
notifications_admin.test_client().post(
|
||||
app_.test_client().post(
|
||||
url_for('main.sign_in'), data={
|
||||
'email_address': 'valid@example.gov.uk',
|
||||
'password': 'whatIsMyPassword!'})
|
||||
|
||||
response = notifications_admin.test_client().post(
|
||||
response = app_.test_client().post(
|
||||
url_for('main.sign_in'), data={
|
||||
'email_address': 'valid@example.gov.uk',
|
||||
'password': 'val1dPassw0rd!'})
|
||||
@@ -60,7 +63,7 @@ def test_should_return_locked_out_true_when_user_is_locked(notifications_admin,
|
||||
assert response.status_code == 200
|
||||
assert 'Username or password is incorrect' in response.get_data(as_text=True)
|
||||
|
||||
another_bad_attempt = notifications_admin.test_client().post(
|
||||
another_bad_attempt = app_.test_client().post(
|
||||
url_for('main.sign_in'), data={
|
||||
'email_address': 'valid@example.gov.uk',
|
||||
'password': 'whatIsMyPassword!'})
|
||||
@@ -68,9 +71,9 @@ def test_should_return_locked_out_true_when_user_is_locked(notifications_admin,
|
||||
assert 'Username or password is incorrect' in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_should_return_active_user_is_false_if_user_is_inactive(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session):
|
||||
def test_should_return_active_user_is_false_if_user_is_inactive(app_,
|
||||
db_,
|
||||
db_session):
|
||||
user = User(email_address='inactive_user@example.gov.uk',
|
||||
password='val1dPassw0rd!',
|
||||
mobile_number='+441234123123',
|
||||
@@ -80,8 +83,8 @@ def test_should_return_active_user_is_false_if_user_is_inactive(notifications_ad
|
||||
state='inactive')
|
||||
users_dao.insert_user(user)
|
||||
|
||||
with notifications_admin.test_request_context():
|
||||
response = notifications_admin.test_client().post(
|
||||
with app_.test_request_context():
|
||||
response = app_.test_client().post(
|
||||
url_for('main.sign_in'), data={
|
||||
'email_address': 'inactive_user@example.gov.uk',
|
||||
'password': 'val1dPassw0rd!'})
|
||||
@@ -90,9 +93,9 @@ def test_should_return_active_user_is_false_if_user_is_inactive(notifications_ad
|
||||
assert 'Username or password is incorrect' in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_should_return_200_when_user_does_not_exist(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
response = notifications_admin.test_client().post(
|
||||
def test_should_return_200_when_user_does_not_exist(app_, db_, db_session):
|
||||
with app_.test_request_context():
|
||||
response = app_.test_client().post(
|
||||
url_for('main.sign_in'), data={
|
||||
'email_address': 'does_not_exist@gov.uk',
|
||||
'password': 'doesNotExist!'})
|
||||
@@ -100,7 +103,7 @@ def test_should_return_200_when_user_does_not_exist(notifications_admin, notific
|
||||
assert 'Username or password is incorrect' in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_should_return_200_when_user_is_not_active(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
def test_should_return_200_when_user_is_not_active(app_, db_, db_session):
|
||||
user = User(email_address='PendingUser@example.gov.uk',
|
||||
password='val1dPassw0rd!',
|
||||
mobile_number='+441234123123',
|
||||
@@ -109,16 +112,10 @@ def test_should_return_200_when_user_is_not_active(notifications_admin, notifica
|
||||
role_id=1,
|
||||
state='pending')
|
||||
users_dao.insert_user(user)
|
||||
with notifications_admin.test_request_context():
|
||||
response = notifications_admin.test_client().post(
|
||||
with app_.test_request_context():
|
||||
response = app_.test_client().post(
|
||||
url_for('main.sign_in'), data={
|
||||
'email_address': 'PendingUser@example.gov.uk',
|
||||
'password': 'val1dPassw0rd!'})
|
||||
assert response.status_code == 200
|
||||
assert 'Username or password is incorrect' in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def _set_up_mocker(mocker):
|
||||
# mocker.patch("app.admin_api_client.send_sms")
|
||||
# mocker.patch("app.admin_api_client.send_email")
|
||||
pass
|
||||
|
||||
@@ -6,21 +6,21 @@ from app.models import User
|
||||
from .test_sign_in import _set_up_mocker
|
||||
|
||||
|
||||
def test_render_sign_out_redirects_to_sign_in(notifications_admin):
|
||||
with notifications_admin.test_request_context():
|
||||
response = notifications_admin.test_client().get(
|
||||
def test_render_sign_out_redirects_to_sign_in(app_):
|
||||
with app_.test_request_context():
|
||||
response = app_.test_client().get(
|
||||
url_for('main.sign_out'))
|
||||
assert response.status_code == 302
|
||||
assert response.location == url_for(
|
||||
'main.sign_in', _external=True, next=url_for('main.sign_out'))
|
||||
|
||||
|
||||
def test_sign_out_user(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session,
|
||||
mocker):
|
||||
with notifications_admin.test_request_context():
|
||||
_set_up_mocker(mocker)
|
||||
def test_sign_out_user(app_,
|
||||
db_,
|
||||
db_session,
|
||||
mock_send_sms,
|
||||
mock_send_email):
|
||||
with app_.test_request_context():
|
||||
email = 'valid@example.gov.uk'
|
||||
password = 'val1dPassw0rd!'
|
||||
user = User(email_address=email,
|
||||
@@ -31,7 +31,7 @@ def test_sign_out_user(notifications_admin,
|
||||
role_id=1,
|
||||
state='active')
|
||||
users_dao.insert_user(user)
|
||||
with notifications_admin.test_client() as client:
|
||||
with app_.test_client() as client:
|
||||
client.login(user)
|
||||
# Check we are logged in
|
||||
response = client.get('/services/123/dashboard')
|
||||
|
||||
@@ -6,12 +6,12 @@ from tests.app.main import create_test_user
|
||||
|
||||
|
||||
def test_upload_empty_csvfile_returns_to_upload_page(
|
||||
notifications_admin, notifications_admin_db, notify_db_session,
|
||||
app_, db_, db_session,
|
||||
mocker):
|
||||
|
||||
_setup_mocker_for_empty_file(mocker)
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
user = create_test_user('active')
|
||||
client.login(user)
|
||||
upload_data = {'file': (BytesIO(''.encode('utf-8')), 'emtpy.csv')}
|
||||
@@ -24,7 +24,7 @@ def test_upload_empty_csvfile_returns_to_upload_page(
|
||||
|
||||
|
||||
def test_upload_csvfile_with_invalid_phone_shows_check_page_with_errors(
|
||||
notifications_admin, notifications_admin_db, notify_db_session,
|
||||
app_, db_, db_session,
|
||||
mocker):
|
||||
|
||||
contents = 'phone\n+44 123\n+44 456'
|
||||
@@ -32,8 +32,8 @@ def test_upload_csvfile_with_invalid_phone_shows_check_page_with_errors(
|
||||
m_open = mock_open(read_data=contents)
|
||||
_setup_mocker_for_nonemtpy_file(mocker)
|
||||
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
user = create_test_user('active')
|
||||
client.login(user)
|
||||
upload_data = {'file': file_data}
|
||||
@@ -52,7 +52,7 @@ def test_upload_csvfile_with_invalid_phone_shows_check_page_with_errors(
|
||||
|
||||
|
||||
def test_upload_csvfile_with_valid_phone_shows_first3_and_last3_numbers(
|
||||
notifications_admin, notifications_admin_db, notify_db_session,
|
||||
app_, db_, db_session,
|
||||
mocker):
|
||||
|
||||
contents = 'phone\n+44 7700 900981\n+44 7700 900982\n+44 7700 900983\n+44 7700 900984\n+44 7700 900985\n+44 7700 900986\n+44 7700 900987\n+44 7700 900988\n+44 7700 900989' # noqa
|
||||
@@ -61,8 +61,8 @@ def test_upload_csvfile_with_valid_phone_shows_first3_and_last3_numbers(
|
||||
m_open = mock_open(read_data=contents)
|
||||
_setup_mocker_for_nonemtpy_file(mocker)
|
||||
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
user = create_test_user('active')
|
||||
client.login(user)
|
||||
upload_data = {'file': file_data}
|
||||
@@ -89,7 +89,7 @@ def test_upload_csvfile_with_valid_phone_shows_first3_and_last3_numbers(
|
||||
|
||||
|
||||
def test_upload_csvfile_with_valid_phone_shows_all_if_6_or_less_numbers(
|
||||
notifications_admin, notifications_admin_db, notify_db_session,
|
||||
app_, db_, db_session,
|
||||
mocker):
|
||||
|
||||
contents = 'phone\n+44 7700 900981\n+44 7700 900982\n+44 7700 900983\n+44 7700 900984\n+44 7700 900985\n+44 7700 900986' # noqa
|
||||
@@ -98,8 +98,8 @@ def test_upload_csvfile_with_valid_phone_shows_all_if_6_or_less_numbers(
|
||||
m_open = mock_open(read_data=contents)
|
||||
_setup_mocker_for_nonemtpy_file(mocker)
|
||||
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
user = create_test_user('active')
|
||||
client.login(user)
|
||||
upload_data = {'file': file_data}
|
||||
@@ -121,11 +121,11 @@ def test_upload_csvfile_with_valid_phone_shows_all_if_6_or_less_numbers(
|
||||
assert '+44 7700 900986' in content
|
||||
|
||||
|
||||
def test_should_redirect_to_job(notifications_admin, notifications_admin_db,
|
||||
notify_db_session, mocker):
|
||||
def test_should_redirect_to_job(app_, db_,
|
||||
db_session, mocker):
|
||||
_setup_mocker_for_check(mocker)
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
user = create_test_user('active')
|
||||
client.login(user)
|
||||
with client.session_transaction() as s:
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
def test_styleguide_can_render(notifications_admin):
|
||||
response = notifications_admin.test_client().get('/_styleguide')
|
||||
def test_styleguide_can_render(app_):
|
||||
response = app_.test_client().get('/_styleguide')
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
from tests.app.main import create_test_user
|
||||
|
||||
|
||||
def test_should_return_list_of_all_templates(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_return_list_of_all_templates(app_, db_, db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
user = create_test_user('active')
|
||||
client.login(user)
|
||||
response = client.get('/services/123/templates')
|
||||
@@ -11,9 +11,9 @@ def test_should_return_list_of_all_templates(notifications_admin, notifications_
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_should_show_page_for_one_templates(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_show_page_for_one_templates(app_, db_, db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
user = create_test_user('active')
|
||||
client.login(user)
|
||||
response = client.get('/services/123/templates/template')
|
||||
@@ -21,9 +21,9 @@ def test_should_show_page_for_one_templates(notifications_admin, notifications_a
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_should_redirect_when_saving_a_template(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_redirect_when_saving_a_template(app_, db_, db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
user = create_test_user('active')
|
||||
client.login(user)
|
||||
response = client.post('/services/123/templates/template')
|
||||
|
||||
@@ -4,9 +4,9 @@ from app.main.dao import verify_codes_dao
|
||||
from tests.app.main import create_test_user
|
||||
|
||||
|
||||
def test_should_render_two_factor_page(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_render_two_factor_page(app_, db_, db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
# TODO this lives here until we work out how to
|
||||
# reassign the session after it is lost mid register process
|
||||
with client.session_transaction() as session:
|
||||
@@ -17,9 +17,9 @@ def test_should_render_two_factor_page(notifications_admin, notifications_admin_
|
||||
assert '''We've sent you a text message with a verification code.''' in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_should_login_user_and_redirect_to_dashboard(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_login_user_and_redirect_to_dashboard(app_, db_, db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
with client.session_transaction() as session:
|
||||
user = create_test_user('active')
|
||||
session['user_email'] = user.email_address
|
||||
@@ -31,11 +31,11 @@ def test_should_login_user_and_redirect_to_dashboard(notifications_admin, notifi
|
||||
assert response.location == url_for('main.choose_service', _external=True)
|
||||
|
||||
|
||||
def test_should_return_200_with_sms_code_error_when_sms_code_is_wrong(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_return_200_with_sms_code_error_when_sms_code_is_wrong(app_,
|
||||
db_,
|
||||
db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
with client.session_transaction() as session:
|
||||
user = create_test_user('active')
|
||||
session['user_email'] = user.email_address
|
||||
@@ -46,11 +46,11 @@ def test_should_return_200_with_sms_code_error_when_sms_code_is_wrong(notificati
|
||||
assert 'Code does not match' in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_should_login_user_when_multiple_valid_codes_exist(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_login_user_when_multiple_valid_codes_exist(app_,
|
||||
db_,
|
||||
db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
with client.session_transaction() as session:
|
||||
user = create_test_user('active')
|
||||
session['user_email'] = user.email_address
|
||||
|
||||
@@ -1,121 +1,121 @@
|
||||
def test_should_show_overview_page(notifications_admin):
|
||||
response = notifications_admin.test_client().get('/user-profile')
|
||||
def test_should_show_overview_page(app_):
|
||||
response = app_.test_client().get('/user-profile')
|
||||
|
||||
assert 'Your profile' in response.get_data(as_text=True)
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_should_show_name_page(notifications_admin):
|
||||
response = notifications_admin.test_client().get('/user-profile/name')
|
||||
def test_should_show_name_page(app_):
|
||||
response = app_.test_client().get('/user-profile/name')
|
||||
|
||||
assert 'Change your name' in response.get_data(as_text=True)
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_should_redirect_after_name_change(notifications_admin):
|
||||
response = notifications_admin.test_client().post('/user-profile/name')
|
||||
def test_should_redirect_after_name_change(app_):
|
||||
response = app_.test_client().post('/user-profile/name')
|
||||
|
||||
assert response.status_code == 302
|
||||
assert response.location == 'http://localhost/user-profile'
|
||||
|
||||
|
||||
def test_should_show_email_page(notifications_admin):
|
||||
response = notifications_admin.test_client().get('/user-profile/email')
|
||||
def test_should_show_email_page(app_):
|
||||
response = app_.test_client().get('/user-profile/email')
|
||||
|
||||
assert 'Change your email address' in response.get_data(as_text=True)
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_should_redirect_after_email_change(notifications_admin):
|
||||
response = notifications_admin.test_client().post('/user-profile/email')
|
||||
def test_should_redirect_after_email_change(app_):
|
||||
response = app_.test_client().post('/user-profile/email')
|
||||
|
||||
assert response.status_code == 302
|
||||
assert response.location == 'http://localhost/user-profile/email/authenticate'
|
||||
|
||||
|
||||
def test_should_show_authenticate_after_email_change(notifications_admin):
|
||||
response = notifications_admin.test_client().get('/user-profile/email/authenticate')
|
||||
def test_should_show_authenticate_after_email_change(app_):
|
||||
response = app_.test_client().get('/user-profile/email/authenticate')
|
||||
|
||||
assert 'Change your email address' in response.get_data(as_text=True)
|
||||
assert 'Confirm' in response.get_data(as_text=True)
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_should_redirect_after_email_change_confirm(notifications_admin):
|
||||
response = notifications_admin.test_client().post('/user-profile/email/authenticate')
|
||||
def test_should_redirect_after_email_change_confirm(app_):
|
||||
response = app_.test_client().post('/user-profile/email/authenticate')
|
||||
|
||||
assert response.status_code == 302
|
||||
assert response.location == 'http://localhost/user-profile/email/confirm'
|
||||
|
||||
|
||||
def test_should_show_confirm_after_email_change(notifications_admin):
|
||||
response = notifications_admin.test_client().get('/user-profile/email/confirm')
|
||||
def test_should_show_confirm_after_email_change(app_):
|
||||
response = app_.test_client().get('/user-profile/email/confirm')
|
||||
|
||||
assert 'Change your email address' in response.get_data(as_text=True)
|
||||
assert 'Confirm' in response.get_data(as_text=True)
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_should_redirect_after_email_change_confirm(notifications_admin):
|
||||
response = notifications_admin.test_client().post('/user-profile/email/confirm')
|
||||
def test_should_redirect_after_email_change_confirm(app_):
|
||||
response = app_.test_client().post('/user-profile/email/confirm')
|
||||
|
||||
assert response.status_code == 302
|
||||
assert response.location == 'http://localhost/user-profile'
|
||||
|
||||
|
||||
def test_should_show_mobile_number_page(notifications_admin):
|
||||
response = notifications_admin.test_client().get('/user-profile/mobile-number')
|
||||
def test_should_show_mobile_number_page(app_):
|
||||
response = app_.test_client().get('/user-profile/mobile-number')
|
||||
|
||||
assert 'Change your mobile number' in response.get_data(as_text=True)
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_should_redirect_after_mobile_number_change(notifications_admin):
|
||||
response = notifications_admin.test_client().post('/user-profile/email')
|
||||
def test_should_redirect_after_mobile_number_change(app_):
|
||||
response = app_.test_client().post('/user-profile/email')
|
||||
|
||||
assert response.status_code == 302
|
||||
assert response.location == 'http://localhost/user-profile/email/authenticate'
|
||||
|
||||
|
||||
def test_should_show_authenticate_after_mobile_number_change(notifications_admin):
|
||||
response = notifications_admin.test_client().get('/user-profile/mobile-number/authenticate')
|
||||
def test_should_show_authenticate_after_mobile_number_change(app_):
|
||||
response = app_.test_client().get('/user-profile/mobile-number/authenticate')
|
||||
|
||||
assert 'Change your mobile number' in response.get_data(as_text=True)
|
||||
assert 'Confirm' in response.get_data(as_text=True)
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_should_redirect_after_mobile_number_authenticate(notifications_admin):
|
||||
response = notifications_admin.test_client().post('/user-profile/mobile-number/authenticate')
|
||||
def test_should_redirect_after_mobile_number_authenticate(app_):
|
||||
response = app_.test_client().post('/user-profile/mobile-number/authenticate')
|
||||
|
||||
assert response.status_code == 302
|
||||
assert response.location == 'http://localhost/user-profile/mobile-number/confirm'
|
||||
|
||||
|
||||
def test_should_show_confirm_after_mobile_number_change(notifications_admin):
|
||||
response = notifications_admin.test_client().get('/user-profile/mobile-number/confirm')
|
||||
def test_should_show_confirm_after_mobile_number_change(app_):
|
||||
response = app_.test_client().get('/user-profile/mobile-number/confirm')
|
||||
|
||||
assert 'Change your mobile number' in response.get_data(as_text=True)
|
||||
assert 'Confirm' in response.get_data(as_text=True)
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_should_redirect_after_mobile_number_confirm(notifications_admin):
|
||||
response = notifications_admin.test_client().post('/user-profile/mobile-number/confirm')
|
||||
def test_should_redirect_after_mobile_number_confirm(app_):
|
||||
response = app_.test_client().post('/user-profile/mobile-number/confirm')
|
||||
|
||||
assert response.status_code == 302
|
||||
assert response.location == 'http://localhost/user-profile'
|
||||
|
||||
|
||||
def test_should_show_password_page(notifications_admin):
|
||||
response = notifications_admin.test_client().get('/user-profile/password')
|
||||
def test_should_show_password_page(app_):
|
||||
response = app_.test_client().get('/user-profile/password')
|
||||
|
||||
assert 'Change your password' in response.get_data(as_text=True)
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_should_redirect_after_password_change(notifications_admin):
|
||||
response = notifications_admin.test_client().post('/user-profile/password')
|
||||
def test_should_redirect_after_password_change(app_):
|
||||
response = app_.test_client().post('/user-profile/password')
|
||||
|
||||
assert response.status_code == 302
|
||||
assert response.location == 'http://localhost/user-profile'
|
||||
|
||||
@@ -3,9 +3,9 @@ from app.main.dao import users_dao, verify_codes_dao
|
||||
from tests.app.main import create_test_user
|
||||
|
||||
|
||||
def test_should_return_verify_template(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_return_verify_template(app_, db_, db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
# TODO this lives here until we work out how to
|
||||
# reassign the session after it is lost mid register process
|
||||
with client.session_transaction() as session:
|
||||
@@ -18,11 +18,11 @@ def test_should_return_verify_template(notifications_admin, notifications_admin_
|
||||
" You need to enter both codes here.") in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_should_redirect_to_add_service_when_code_are_correct(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_redirect_to_add_service_when_code_are_correct(app_,
|
||||
db_,
|
||||
db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
with client.session_transaction() as session:
|
||||
user = create_test_user('pending')
|
||||
session['user_email'] = user.email_address
|
||||
@@ -35,9 +35,9 @@ def test_should_redirect_to_add_service_when_code_are_correct(notifications_admi
|
||||
assert response.location == url_for('main.add_service', _external=True)
|
||||
|
||||
|
||||
def test_should_activate_user_after_verify(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_activate_user_after_verify(app_, db_, db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
with client.session_transaction() as session:
|
||||
user = create_test_user('pending')
|
||||
session['user_email'] = user.email_address
|
||||
@@ -51,9 +51,9 @@ def test_should_activate_user_after_verify(notifications_admin, notifications_ad
|
||||
assert after_verify.state == 'active'
|
||||
|
||||
|
||||
def test_should_return_200_when_codes_are_wrong(notifications_admin, notifications_admin_db, notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_return_200_when_codes_are_wrong(app_, db_, db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
with client.session_transaction() as session:
|
||||
user = create_test_user('pending')
|
||||
session['user_email'] = user.email_address
|
||||
@@ -68,11 +68,11 @@ def test_should_return_200_when_codes_are_wrong(notifications_admin, notificatio
|
||||
assert resp_data.count('Code does not match') == 2
|
||||
|
||||
|
||||
def test_should_mark_all_codes_as_used_when_many_codes_exist(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session):
|
||||
with notifications_admin.test_request_context():
|
||||
with notifications_admin.test_client() as client:
|
||||
def test_should_mark_all_codes_as_used_when_many_codes_exist(app_,
|
||||
db_,
|
||||
db_session):
|
||||
with app_.test_request_context():
|
||||
with app_.test_client() as client:
|
||||
with client.session_transaction() as session:
|
||||
user = create_test_user('pending')
|
||||
session['user_email'] = user.email_address
|
||||
|
||||
@@ -1,34 +1,17 @@
|
||||
import os
|
||||
|
||||
import pytest
|
||||
from alembic.command import upgrade
|
||||
from alembic.config import Config
|
||||
from flask import url_for
|
||||
from flask.ext.migrate import Migrate, MigrateCommand
|
||||
from flask.ext.script import Manager
|
||||
from flask_login import login_user
|
||||
from sqlalchemy.schema import MetaData
|
||||
from flask.testing import FlaskClient
|
||||
from app.main.dao import verify_codes_dao
|
||||
|
||||
from . import (
|
||||
create_test_user, create_another_test_user, service_json, TestClient)
|
||||
from app import create_app, db
|
||||
|
||||
|
||||
class TestClient(FlaskClient):
|
||||
def login(self, user):
|
||||
# Skipping authentication here and just log them in
|
||||
with self.session_transaction() as session:
|
||||
session['user_email'] = user.email_address
|
||||
verify_codes_dao.add_code(user_id=user.id, code='12345', code_type='sms')
|
||||
response = self.post(
|
||||
url_for('main.two_factor'), data={'sms_code': '12345'})
|
||||
|
||||
def logout(self, user):
|
||||
self.get(url_for("main.logout"))
|
||||
|
||||
|
||||
@pytest.fixture(scope='session')
|
||||
def notifications_admin(request):
|
||||
def app_(request):
|
||||
app = create_app('test')
|
||||
|
||||
ctx = app.app_context()
|
||||
@@ -43,28 +26,28 @@ def notifications_admin(request):
|
||||
|
||||
|
||||
@pytest.fixture(scope='session')
|
||||
def notifications_admin_db(notifications_admin, request):
|
||||
Migrate(notifications_admin, db)
|
||||
def db_(app_, request):
|
||||
Migrate(app_, db)
|
||||
Manager(db, MigrateCommand)
|
||||
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
|
||||
ALEMBIC_CONFIG = os.path.join(BASE_DIR, 'migrations')
|
||||
config = Config(ALEMBIC_CONFIG + '/alembic.ini')
|
||||
config.set_main_option("script_location", ALEMBIC_CONFIG)
|
||||
|
||||
with notifications_admin.app_context():
|
||||
with app_.app_context():
|
||||
upgrade(config, 'head')
|
||||
|
||||
def teardown():
|
||||
db.session.remove()
|
||||
db.drop_all()
|
||||
db.engine.execute("drop table alembic_version")
|
||||
db.get_engine(notifications_admin).dispose()
|
||||
db.get_engine(app_).dispose()
|
||||
|
||||
request.addfinalizer(teardown)
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def notify_db_session(request):
|
||||
def db_session(request):
|
||||
def teardown():
|
||||
db.session.remove()
|
||||
for tbl in reversed(meta.sorted_tables):
|
||||
@@ -73,3 +56,73 @@ def notify_db_session(request):
|
||||
|
||||
meta = MetaData(bind=db.engine, reflect=True)
|
||||
request.addfinalizer(teardown)
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def service_one(request):
|
||||
usr = create_test_user('active')
|
||||
return service_json(1, 'service one', [usr])
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def active_user(request, db_, db_session):
|
||||
return create_test_user('active')
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def mock_send_sms(request, mocker):
|
||||
return mocker.patch("app.notifications_api_client.send_sms")
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def mock_send_email(request, mocker):
|
||||
return mocker.patch("app.notifications_api_client.send_email")
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def mock_get_service(mocker):
|
||||
return mocker.patch('app.notifications_api_client.get_service')
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def mock_create_service(mocker):
|
||||
# TODO fix token generation
|
||||
def _create(service_name, active, limit, restricted, user_id):
|
||||
service = service_json(
|
||||
101, service_name, [user_id], limit=limit,
|
||||
active=active, restricted=restricted)
|
||||
return {'data': service, 'token': 1}
|
||||
mock_class = mocker.patch(
|
||||
'app.notifications_api_client.create_service', side_effect=_create)
|
||||
return mock_class
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def mock_update_service(mocker):
|
||||
def _update(service_id,
|
||||
service_name,
|
||||
active,
|
||||
limit,
|
||||
restricted,
|
||||
users):
|
||||
service = service_json(
|
||||
service_id, service_name, users, limit=limit,
|
||||
active=active, restricted=restricted)
|
||||
return {'data': service}
|
||||
mock_class = mocker.patch(
|
||||
'app.notifications_api_client.update_service', side_effect=_update)
|
||||
return mock_class
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def mock_get_services(mocker, active_user):
|
||||
def _create():
|
||||
service_one = service_json(
|
||||
1, "service_one", [active_user.id], 1000, True, False)
|
||||
service_two = service_json(
|
||||
2, "service_two", [active_user.id], 1000, True, False)
|
||||
return {'data': [service_one, service_two]}
|
||||
mock_class = mocker.patch(
|
||||
'app.notifications_api_client.get_services', side_effect=_create)
|
||||
return mock_class
|
||||
|
||||
|
||||
Reference in New Issue
Block a user