2017-05-22 15:05:05 +01:00
|
|
|
|
import json
|
2016-06-01 15:59:44 +01:00
|
|
|
|
import uuid
|
2018-09-18 15:22:08 +01:00
|
|
|
|
from datetime import datetime, timedelta
|
2017-06-13 15:22:31 +01:00
|
|
|
|
|
2017-05-22 15:05:05 +01:00
|
|
|
|
import pytest
|
2017-06-13 15:22:31 +01:00
|
|
|
|
import pytz
|
|
|
|
|
|
import requests_mock
|
2018-09-18 15:22:08 +01:00
|
|
|
|
from flask import current_app, url_for
|
2016-12-19 16:53:23 +00:00
|
|
|
|
from sqlalchemy.orm.session import make_transient
|
2016-06-01 15:59:44 +01:00
|
|
|
|
|
2016-03-31 17:46:18 +01:00
|
|
|
|
from app import db
|
2018-09-18 15:22:08 +01:00
|
|
|
|
from app.clients.sms.firetext import FiretextClient
|
|
|
|
|
|
from app.dao.api_key_dao import save_model_api_key
|
|
|
|
|
|
from app.dao.invited_user_dao import save_invited_user
|
|
|
|
|
|
from app.dao.jobs_dao import dao_create_job
|
|
|
|
|
|
from app.dao.notifications_dao import dao_create_notification
|
|
|
|
|
|
from app.dao.organisation_dao import dao_create_organisation
|
|
|
|
|
|
from app.dao.services_dao import (dao_create_service, dao_add_user_to_service)
|
|
|
|
|
|
from app.dao.templates_dao import dao_create_template
|
|
|
|
|
|
from app.dao.users_dao import create_secret_code, create_user_code
|
|
|
|
|
|
from app.history_meta import create_history
|
2016-02-26 12:00:16 +00:00
|
|
|
|
from app.models import (
|
2016-04-28 12:01:27 +01:00
|
|
|
|
Service,
|
|
|
|
|
|
Template,
|
2017-11-09 14:42:43 +00:00
|
|
|
|
TemplateHistory,
|
2016-04-28 12:01:27 +01:00
|
|
|
|
ApiKey,
|
|
|
|
|
|
Job,
|
2018-02-10 01:34:37 +00:00
|
|
|
|
Organisation,
|
2016-04-28 12:01:27 +01:00
|
|
|
|
Notification,
|
2016-07-26 11:00:03 +01:00
|
|
|
|
NotificationHistory,
|
2016-04-28 12:01:27 +01:00
|
|
|
|
InvitedUser,
|
|
|
|
|
|
Permission,
|
2016-05-10 09:13:02 +01:00
|
|
|
|
ProviderDetails,
|
2016-12-19 16:53:23 +00:00
|
|
|
|
ProviderDetailsHistory,
|
|
|
|
|
|
ProviderRates,
|
2020-07-28 10:22:13 +01:00
|
|
|
|
ServiceGuestList,
|
2017-11-28 11:05:56 +00:00
|
|
|
|
KEY_TYPE_NORMAL,
|
|
|
|
|
|
KEY_TYPE_TEST,
|
|
|
|
|
|
KEY_TYPE_TEAM,
|
|
|
|
|
|
EMAIL_TYPE,
|
|
|
|
|
|
SMS_TYPE,
|
|
|
|
|
|
LETTER_TYPE,
|
|
|
|
|
|
SERVICE_PERMISSION_TYPES,
|
|
|
|
|
|
ServiceEmailReplyTo
|
2017-11-27 12:30:50 +00:00
|
|
|
|
)
|
2017-05-22 15:05:05 +01:00
|
|
|
|
from tests import create_authorization_header
|
2017-08-04 19:10:22 +01:00
|
|
|
|
from tests.app.db import (
|
|
|
|
|
|
create_user,
|
|
|
|
|
|
create_template,
|
|
|
|
|
|
create_notification,
|
|
|
|
|
|
create_service,
|
|
|
|
|
|
create_api_key,
|
2017-10-03 13:35:09 +01:00
|
|
|
|
create_inbound_number,
|
2018-02-19 17:14:01 +00:00
|
|
|
|
create_letter_contact,
|
|
|
|
|
|
create_invited_org_user,
|
2019-06-11 16:45:35 +01:00
|
|
|
|
create_job
|
2017-08-04 19:10:22 +01:00
|
|
|
|
)
|
2016-12-28 12:30:21 +00:00
|
|
|
|
|
2016-02-16 17:42:04 +00:00
|
|
|
|
|
2016-05-31 16:55:26 +01:00
|
|
|
|
@pytest.yield_fixture
|
|
|
|
|
|
def rmock():
|
|
|
|
|
|
with requests_mock.mock() as rmock:
|
|
|
|
|
|
yield rmock
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-16 14:06:56 +00:00
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def service_factory(notify_db, notify_db_session):
|
|
|
|
|
|
class ServiceFactory(object):
|
2016-03-31 17:46:18 +01:00
|
|
|
|
def get(self, service_name, user=None, template_type=None, email_from=None):
|
2016-02-19 15:54:11 +00:00
|
|
|
|
if not user:
|
2016-12-28 12:30:21 +00:00
|
|
|
|
user = create_user()
|
2016-07-26 11:00:03 +01:00
|
|
|
|
if not email_from:
|
|
|
|
|
|
email_from = service_name
|
2018-12-31 13:01:24 +00:00
|
|
|
|
|
|
|
|
|
|
service = create_service(
|
|
|
|
|
|
email_from=email_from,
|
|
|
|
|
|
service_name=service_name,
|
|
|
|
|
|
service_permissions=None,
|
|
|
|
|
|
user=user,
|
2019-01-02 17:15:27 +00:00
|
|
|
|
check_if_service_exists=True,
|
2018-12-31 13:01:24 +00:00
|
|
|
|
)
|
2016-02-22 17:17:29 +00:00
|
|
|
|
if template_type == 'email':
|
2018-12-28 16:15:12 +00:00
|
|
|
|
create_template(
|
|
|
|
|
|
service,
|
|
|
|
|
|
template_name="Template Name",
|
2016-02-22 17:17:29 +00:00
|
|
|
|
template_type=template_type,
|
2018-12-28 16:15:12 +00:00
|
|
|
|
subject=service.email_from,
|
2016-02-22 17:17:29 +00:00
|
|
|
|
)
|
|
|
|
|
|
else:
|
2018-12-28 16:15:12 +00:00
|
|
|
|
create_template(
|
|
|
|
|
|
service,
|
|
|
|
|
|
template_name="Template Name",
|
|
|
|
|
|
template_type='sms',
|
2016-02-22 17:17:29 +00:00
|
|
|
|
)
|
2016-02-16 14:06:56 +00:00
|
|
|
|
return service
|
|
|
|
|
|
|
|
|
|
|
|
return ServiceFactory()
|
|
|
|
|
|
|
2016-01-08 12:18:12 +00:00
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
2016-12-28 12:30:21 +00:00
|
|
|
|
def sample_user(notify_db_session):
|
|
|
|
|
|
return create_user()
|
2016-01-21 17:29:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
2018-02-23 13:30:35 +00:00
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def notify_user(notify_db_session):
|
|
|
|
|
|
return create_user(
|
|
|
|
|
|
email="notify-service-user@digital.cabinet-office.gov.uk",
|
|
|
|
|
|
id_=current_app.config['NOTIFY_USER_ID']
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-11-01 14:34:15 +00:00
|
|
|
|
def create_code(notify_db_session, code_type):
|
|
|
|
|
|
code = create_secret_code()
|
|
|
|
|
|
usr = create_user()
|
2016-01-21 17:29:24 +00:00
|
|
|
|
return create_user_code(usr, code, code_type), code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
2019-11-01 14:34:15 +00:00
|
|
|
|
def sample_sms_code(notify_db_session):
|
|
|
|
|
|
code, txt_code = create_code(notify_db_session, code_type="sms")
|
2016-01-21 17:29:24 +00:00
|
|
|
|
code.txt_code = txt_code
|
|
|
|
|
|
return code
|
2016-01-08 12:18:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
2019-11-01 14:34:15 +00:00
|
|
|
|
def sample_service(notify_db_session):
|
|
|
|
|
|
user = create_user()
|
|
|
|
|
|
service_name = 'Sample service'
|
|
|
|
|
|
email_from = service_name.lower().replace(' ', '.')
|
2017-08-31 12:44:06 +01:00
|
|
|
|
|
2016-01-11 15:07:13 +00:00
|
|
|
|
data = {
|
|
|
|
|
|
'name': service_name,
|
2019-11-01 14:34:15 +00:00
|
|
|
|
'message_limit': 1000,
|
|
|
|
|
|
'restricted': False,
|
2016-04-14 15:09:59 +01:00
|
|
|
|
'email_from': email_from,
|
2019-07-10 18:17:33 +01:00
|
|
|
|
'created_by': user,
|
|
|
|
|
|
'crown': True
|
2016-02-19 15:54:11 +00:00
|
|
|
|
}
|
2016-01-22 14:43:30 +00:00
|
|
|
|
service = Service.query.filter_by(name=service_name).first()
|
|
|
|
|
|
if not service:
|
|
|
|
|
|
service = Service(**data)
|
2019-11-01 14:34:15 +00:00
|
|
|
|
dao_create_service(service, user, service_permissions=None)
|
2016-03-22 13:14:23 +00:00
|
|
|
|
else:
|
|
|
|
|
|
if user not in service.users:
|
|
|
|
|
|
dao_add_user_to_service(service, user)
|
2017-08-16 12:27:42 +01:00
|
|
|
|
|
2016-01-11 15:07:13 +00:00
|
|
|
|
return service
|
2016-01-13 11:04:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
2018-10-17 16:12:57 +01:00
|
|
|
|
@pytest.fixture(scope='function', name='sample_service_full_permissions')
|
|
|
|
|
|
def _sample_service_full_permissions(notify_db_session):
|
|
|
|
|
|
service = create_service(
|
2017-09-21 11:50:49 +01:00
|
|
|
|
service_name="sample service full permissions",
|
2019-01-02 17:15:27 +00:00
|
|
|
|
service_permissions=set(SERVICE_PERMISSION_TYPES),
|
|
|
|
|
|
check_if_service_exists=True
|
2017-09-21 11:50:49 +01:00
|
|
|
|
)
|
2018-10-17 16:12:57 +01:00
|
|
|
|
create_inbound_number('12345', service_id=service.id)
|
|
|
|
|
|
return service
|
2017-06-29 15:33:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
2018-10-17 16:12:57 +01:00
|
|
|
|
@pytest.fixture(scope='function', name='sample_service_custom_letter_contact_block')
|
|
|
|
|
|
def _sample_service_custom_letter_contact_block(sample_service):
|
|
|
|
|
|
create_letter_contact(sample_service, contact_block='((contact block))')
|
|
|
|
|
|
return sample_service
|
2017-09-20 10:27:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
2016-01-13 11:04:13 +00:00
|
|
|
|
@pytest.fixture(scope='function')
|
2019-11-01 14:34:15 +00:00
|
|
|
|
def sample_template(notify_db_session):
|
|
|
|
|
|
user = create_user()
|
|
|
|
|
|
service = create_service(service_permissions=[EMAIL_TYPE, SMS_TYPE], check_if_service_exists=True)
|
2017-02-14 17:59:18 +00:00
|
|
|
|
|
2017-02-24 13:39:58 +00:00
|
|
|
|
data = {
|
2019-11-01 14:34:15 +00:00
|
|
|
|
'name': 'Template Name',
|
|
|
|
|
|
'template_type': 'sms',
|
|
|
|
|
|
'content': 'This is a template:\nwith a newline',
|
2017-02-24 13:39:58 +00:00
|
|
|
|
'service': service,
|
2019-11-01 14:34:15 +00:00
|
|
|
|
'created_by': user,
|
|
|
|
|
|
'archived': False,
|
|
|
|
|
|
'hidden': False,
|
|
|
|
|
|
'process_type': 'normal'
|
2017-02-24 13:39:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
template = Template(**data)
|
|
|
|
|
|
dao_create_template(template)
|
2017-08-31 12:44:06 +01:00
|
|
|
|
|
2016-02-22 17:17:29 +00:00
|
|
|
|
return template
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-06-26 14:18:43 +01:00
|
|
|
|
@pytest.fixture(scope='function')
|
2019-10-28 16:31:47 +00:00
|
|
|
|
def sample_template_without_sms_permission(notify_db_session):
|
|
|
|
|
|
service = create_service(service_permissions=[EMAIL_TYPE], check_if_service_exists=True)
|
|
|
|
|
|
return create_template(service, template_type=SMS_TYPE)
|
2017-06-26 14:18:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
2016-02-29 11:23:34 +00:00
|
|
|
|
@pytest.fixture(scope='function')
|
2019-09-18 12:01:20 +01:00
|
|
|
|
def sample_template_with_placeholders(sample_service):
|
2016-11-01 15:19:56 +00:00
|
|
|
|
# deliberate space and title case in placeholder
|
2019-09-18 12:01:20 +01:00
|
|
|
|
return create_template(sample_service, content="Hello (( Name))\nYour thing is due soon")
|
2016-02-29 11:23:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-01-19 12:05:28 +00:00
|
|
|
|
@pytest.fixture(scope='function')
|
2019-09-18 12:01:20 +01:00
|
|
|
|
def sample_sms_template_with_html(sample_service):
|
2017-01-19 12:05:28 +00:00
|
|
|
|
# deliberate space and title case in placeholder
|
2019-09-18 12:01:20 +01:00
|
|
|
|
return create_template(sample_service, content="Hello (( Name))\nHere is <em>some HTML</em> & entities")
|
2017-01-19 12:05:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
2016-02-22 17:17:29 +00:00
|
|
|
|
@pytest.fixture(scope='function')
|
2019-11-01 14:34:15 +00:00
|
|
|
|
def sample_email_template(notify_db_session):
|
|
|
|
|
|
user = create_user()
|
|
|
|
|
|
service = create_service(user=user, service_permissions=[EMAIL_TYPE, SMS_TYPE], check_if_service_exists=True)
|
2016-02-22 17:17:29 +00:00
|
|
|
|
data = {
|
2019-11-01 14:34:15 +00:00
|
|
|
|
'name': 'Email Template Name',
|
|
|
|
|
|
'template_type': EMAIL_TYPE,
|
|
|
|
|
|
'content': 'This is a template',
|
2016-04-25 10:38:37 +01:00
|
|
|
|
'service': service,
|
2016-12-22 14:40:33 +00:00
|
|
|
|
'created_by': user,
|
2019-11-01 14:34:15 +00:00
|
|
|
|
'subject': 'Email Subject'
|
2016-02-22 17:17:29 +00:00
|
|
|
|
}
|
2016-01-13 11:04:13 +00:00
|
|
|
|
template = Template(**data)
|
2016-02-24 11:51:02 +00:00
|
|
|
|
dao_create_template(template)
|
2016-01-13 11:04:13 +00:00
|
|
|
|
return template
|
2016-01-14 11:30:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-06-26 14:18:43 +01:00
|
|
|
|
@pytest.fixture(scope='function')
|
2019-10-28 16:31:47 +00:00
|
|
|
|
def sample_template_without_email_permission(notify_db_session):
|
|
|
|
|
|
service = create_service(service_permissions=[SMS_TYPE], check_if_service_exists=True)
|
|
|
|
|
|
return create_template(service, template_type=EMAIL_TYPE)
|
2017-06-26 14:18:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
persist_letter saves address correctly to database
the `to` field stores either the phone number or the email address
of the recipient - it's a bit more complicated for letters, since
there are address lines 1 through 6, and a postcode. In utils, they're
stored alongside the personalisation, and we have to ensure that when
we persist to the database we keep as much parity with utils to make
our work easier. Aside from sending, the `to` field is also used to
show recipients on the front end report pages - we've decided that the
best thing to store here is address_line_1 - which is probably going to
be either a person's name, company name, or PO box number
Also, a lot of tests and test cleanup - I added create_template and
create_notification functions in db.py, so if you're creating new
fixtures you can use these functions, and you won't need to pass
notify_db and notify_db_session around, huzzah!
also removed create param from sample_notification since it's not used
anywhere
2017-01-19 12:10:32 +00:00
|
|
|
|
@pytest.fixture
|
2019-11-01 14:34:15 +00:00
|
|
|
|
def sample_letter_template(sample_service_full_permissions):
|
|
|
|
|
|
return create_template(sample_service_full_permissions, template_type=LETTER_TYPE, postage='second')
|
persist_letter saves address correctly to database
the `to` field stores either the phone number or the email address
of the recipient - it's a bit more complicated for letters, since
there are address lines 1 through 6, and a postcode. In utils, they're
stored alongside the personalisation, and we have to ensure that when
we persist to the database we keep as much parity with utils to make
our work easier. Aside from sending, the `to` field is also used to
show recipients on the front end report pages - we've decided that the
best thing to store here is address_line_1 - which is probably going to
be either a person's name, company name, or PO box number
Also, a lot of tests and test cleanup - I added create_template and
create_notification functions in db.py, so if you're creating new
fixtures you can use these functions, and you won't need to pass
notify_db and notify_db_session around, huzzah!
also removed create param from sample_notification since it's not used
anywhere
2017-01-19 12:10:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-08-25 16:11:39 +01:00
|
|
|
|
@pytest.fixture
|
|
|
|
|
|
def sample_trial_letter_template(sample_service_full_permissions):
|
|
|
|
|
|
sample_service_full_permissions.restricted = True
|
|
|
|
|
|
return create_template(sample_service_full_permissions, template_type=LETTER_TYPE)
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-29 14:43:44 +00:00
|
|
|
|
@pytest.fixture(scope='function')
|
2019-10-31 09:42:07 +00:00
|
|
|
|
def sample_email_template_with_placeholders(sample_service):
|
|
|
|
|
|
return create_template(
|
|
|
|
|
|
sample_service,
|
|
|
|
|
|
template_type=EMAIL_TYPE,
|
|
|
|
|
|
subject="((name))",
|
2016-07-08 11:02:19 +01:00
|
|
|
|
content="Hello ((name))\nThis is an email from GOV.UK",
|
2019-10-31 09:42:07 +00:00
|
|
|
|
)
|
2016-02-29 14:43:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-01-19 12:05:28 +00:00
|
|
|
|
@pytest.fixture(scope='function')
|
2019-10-31 09:42:07 +00:00
|
|
|
|
def sample_email_template_with_html(sample_service):
|
|
|
|
|
|
return create_template(
|
|
|
|
|
|
sample_service,
|
|
|
|
|
|
template_type=EMAIL_TYPE,
|
|
|
|
|
|
subject="((name)) <em>some HTML</em>",
|
2017-01-19 12:05:28 +00:00
|
|
|
|
content="Hello ((name))\nThis is an email from GOV.UK with <em>some HTML</em>",
|
2019-10-31 09:42:07 +00:00
|
|
|
|
)
|
2017-01-19 12:05:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
2016-01-14 11:30:45 +00:00
|
|
|
|
@pytest.fixture(scope='function')
|
2019-11-01 14:34:15 +00:00
|
|
|
|
def sample_api_key(notify_db_session):
|
|
|
|
|
|
service = create_service(check_if_service_exists=True)
|
|
|
|
|
|
data = {'service': service, 'name': uuid.uuid4(), 'created_by': service.created_by, 'key_type': KEY_TYPE_NORMAL}
|
2016-01-19 12:07:00 +00:00
|
|
|
|
api_key = ApiKey(**data)
|
|
|
|
|
|
save_model_api_key(api_key)
|
|
|
|
|
|
return api_key
|
2016-01-15 12:16:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
2016-09-15 16:00:04 +01:00
|
|
|
|
@pytest.fixture(scope='function')
|
2019-10-29 14:33:01 +00:00
|
|
|
|
def sample_test_api_key(sample_api_key):
|
|
|
|
|
|
service = create_service(check_if_service_exists=True)
|
|
|
|
|
|
|
|
|
|
|
|
return create_api_key(
|
|
|
|
|
|
service,
|
|
|
|
|
|
key_type=KEY_TYPE_TEST
|
|
|
|
|
|
)
|
2016-09-15 16:00:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
2019-10-29 14:33:01 +00:00
|
|
|
|
def sample_team_api_key(sample_api_key):
|
|
|
|
|
|
service = create_service(check_if_service_exists=True)
|
|
|
|
|
|
|
|
|
|
|
|
return create_api_key(
|
|
|
|
|
|
service,
|
|
|
|
|
|
key_type=KEY_TYPE_TEAM
|
|
|
|
|
|
)
|
2016-09-15 16:00:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
2016-01-15 12:16:07 +00:00
|
|
|
|
@pytest.fixture(scope='function')
|
2019-11-01 14:34:15 +00:00
|
|
|
|
def sample_job(notify_db_session):
|
|
|
|
|
|
service = create_service(check_if_service_exists=True)
|
|
|
|
|
|
template = create_template(service=service)
|
2016-01-15 12:16:07 +00:00
|
|
|
|
data = {
|
|
|
|
|
|
'id': uuid.uuid4(),
|
|
|
|
|
|
'service_id': service.id,
|
2016-04-07 13:44:04 +01:00
|
|
|
|
'service': service,
|
2016-01-15 12:16:07 +00:00
|
|
|
|
'template_id': template.id,
|
2016-05-13 16:25:05 +01:00
|
|
|
|
'template_version': template.version,
|
2019-11-01 14:34:15 +00:00
|
|
|
|
'original_file_name': 'some.csv',
|
|
|
|
|
|
'notification_count': 1,
|
|
|
|
|
|
'created_at': datetime.utcnow(),
|
2016-08-24 16:24:55 +01:00
|
|
|
|
'created_by': service.created_by,
|
2019-11-01 14:34:15 +00:00
|
|
|
|
'job_status': 'pending',
|
|
|
|
|
|
'scheduled_for': None,
|
|
|
|
|
|
'processing_started': None,
|
|
|
|
|
|
'archived': False
|
2016-01-15 12:16:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
job = Job(**data)
|
2016-02-24 17:12:30 +00:00
|
|
|
|
dao_create_job(job)
|
2016-01-15 12:16:07 +00:00
|
|
|
|
return job
|
2016-01-15 16:22:03 +00:00
|
|
|
|
|
2016-01-15 17:11:02 +00:00
|
|
|
|
|
2016-03-09 07:27:26 +00:00
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def sample_job_with_placeholdered_template(
|
2019-09-18 12:01:20 +01:00
|
|
|
|
sample_job,
|
|
|
|
|
|
sample_template_with_placeholders,
|
2016-03-09 07:27:26 +00:00
|
|
|
|
):
|
2019-09-18 12:01:20 +01:00
|
|
|
|
sample_job.template = sample_template_with_placeholders
|
|
|
|
|
|
|
|
|
|
|
|
return sample_job
|
2016-03-09 07:27:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
2016-09-01 14:31:01 +01:00
|
|
|
|
@pytest.fixture(scope='function')
|
2019-09-18 12:01:20 +01:00
|
|
|
|
def sample_scheduled_job(sample_template_with_placeholders):
|
|
|
|
|
|
return create_job(
|
|
|
|
|
|
sample_template_with_placeholders,
|
|
|
|
|
|
job_status='scheduled',
|
|
|
|
|
|
scheduled_for=(datetime.utcnow() + timedelta(minutes=60)).isoformat()
|
2016-09-01 14:31:01 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
persist_letter saves address correctly to database
the `to` field stores either the phone number or the email address
of the recipient - it's a bit more complicated for letters, since
there are address lines 1 through 6, and a postcode. In utils, they're
stored alongside the personalisation, and we have to ensure that when
we persist to the database we keep as much parity with utils to make
our work easier. Aside from sending, the `to` field is also used to
show recipients on the front end report pages - we've decided that the
best thing to store here is address_line_1 - which is probably going to
be either a person's name, company name, or PO box number
Also, a lot of tests and test cleanup - I added create_template and
create_notification functions in db.py, so if you're creating new
fixtures you can use these functions, and you won't need to pass
notify_db and notify_db_session around, huzzah!
also removed create param from sample_notification since it's not used
anywhere
2017-01-19 12:10:32 +00:00
|
|
|
|
@pytest.fixture
|
2017-07-27 11:10:22 +01:00
|
|
|
|
def sample_letter_job(sample_letter_template):
|
|
|
|
|
|
service = sample_letter_template.service
|
persist_letter saves address correctly to database
the `to` field stores either the phone number or the email address
of the recipient - it's a bit more complicated for letters, since
there are address lines 1 through 6, and a postcode. In utils, they're
stored alongside the personalisation, and we have to ensure that when
we persist to the database we keep as much parity with utils to make
our work easier. Aside from sending, the `to` field is also used to
show recipients on the front end report pages - we've decided that the
best thing to store here is address_line_1 - which is probably going to
be either a person's name, company name, or PO box number
Also, a lot of tests and test cleanup - I added create_template and
create_notification functions in db.py, so if you're creating new
fixtures you can use these functions, and you won't need to pass
notify_db and notify_db_session around, huzzah!
also removed create param from sample_notification since it's not used
anywhere
2017-01-19 12:10:32 +00:00
|
|
|
|
data = {
|
|
|
|
|
|
'id': uuid.uuid4(),
|
2017-07-27 11:10:22 +01:00
|
|
|
|
'service_id': service.id,
|
|
|
|
|
|
'service': service,
|
persist_letter saves address correctly to database
the `to` field stores either the phone number or the email address
of the recipient - it's a bit more complicated for letters, since
there are address lines 1 through 6, and a postcode. In utils, they're
stored alongside the personalisation, and we have to ensure that when
we persist to the database we keep as much parity with utils to make
our work easier. Aside from sending, the `to` field is also used to
show recipients on the front end report pages - we've decided that the
best thing to store here is address_line_1 - which is probably going to
be either a person's name, company name, or PO box number
Also, a lot of tests and test cleanup - I added create_template and
create_notification functions in db.py, so if you're creating new
fixtures you can use these functions, and you won't need to pass
notify_db and notify_db_session around, huzzah!
also removed create param from sample_notification since it's not used
anywhere
2017-01-19 12:10:32 +00:00
|
|
|
|
'template_id': sample_letter_template.id,
|
|
|
|
|
|
'template_version': sample_letter_template.version,
|
|
|
|
|
|
'original_file_name': 'some.csv',
|
|
|
|
|
|
'notification_count': 1,
|
|
|
|
|
|
'created_at': datetime.utcnow(),
|
2017-07-27 11:10:22 +01:00
|
|
|
|
'created_by': service.created_by,
|
persist_letter saves address correctly to database
the `to` field stores either the phone number or the email address
of the recipient - it's a bit more complicated for letters, since
there are address lines 1 through 6, and a postcode. In utils, they're
stored alongside the personalisation, and we have to ensure that when
we persist to the database we keep as much parity with utils to make
our work easier. Aside from sending, the `to` field is also used to
show recipients on the front end report pages - we've decided that the
best thing to store here is address_line_1 - which is probably going to
be either a person's name, company name, or PO box number
Also, a lot of tests and test cleanup - I added create_template and
create_notification functions in db.py, so if you're creating new
fixtures you can use these functions, and you won't need to pass
notify_db and notify_db_session around, huzzah!
also removed create param from sample_notification since it's not used
anywhere
2017-01-19 12:10:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
job = Job(**data)
|
|
|
|
|
|
dao_create_job(job)
|
|
|
|
|
|
return job
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-09-15 16:00:04 +01:00
|
|
|
|
@pytest.fixture(scope='function')
|
2019-11-01 14:34:15 +00:00
|
|
|
|
def sample_notification_with_job(notify_db_session):
|
|
|
|
|
|
service = create_service(check_if_service_exists=True)
|
|
|
|
|
|
template = create_template(service=service)
|
|
|
|
|
|
job = create_job(template=template)
|
|
|
|
|
|
return create_notification(
|
|
|
|
|
|
template=template,
|
|
|
|
|
|
job=job,
|
2016-09-15 16:00:04 +01:00
|
|
|
|
job_row_number=None,
|
|
|
|
|
|
to_field=None,
|
|
|
|
|
|
status='created',
|
|
|
|
|
|
reference=None,
|
|
|
|
|
|
created_at=None,
|
|
|
|
|
|
sent_at=None,
|
|
|
|
|
|
billable_units=1,
|
|
|
|
|
|
personalisation=None,
|
2017-08-02 15:35:56 +01:00
|
|
|
|
api_key=None,
|
2016-09-15 16:00:04 +01:00
|
|
|
|
key_type=KEY_TYPE_NORMAL
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-09 12:01:17 +00:00
|
|
|
|
@pytest.fixture(scope='function')
|
2019-11-01 14:34:15 +00:00
|
|
|
|
def sample_notification(notify_db_session):
|
|
|
|
|
|
created_at = datetime.utcnow()
|
|
|
|
|
|
service = create_service(check_if_service_exists=True)
|
|
|
|
|
|
template = create_template(service=service)
|
2017-08-02 15:35:56 +01:00
|
|
|
|
|
2019-11-01 14:34:15 +00:00
|
|
|
|
api_key = ApiKey.query.filter(ApiKey.service == template.service, ApiKey.key_type == KEY_TYPE_NORMAL).first()
|
|
|
|
|
|
if not api_key:
|
|
|
|
|
|
api_key = create_api_key(template.service, key_type=KEY_TYPE_NORMAL)
|
2016-03-01 14:58:27 +00:00
|
|
|
|
|
2019-11-01 14:34:15 +00:00
|
|
|
|
notification_id = uuid.uuid4()
|
|
|
|
|
|
to = '+447700900855'
|
2016-02-09 12:01:17 +00:00
|
|
|
|
|
|
|
|
|
|
data = {
|
2016-03-01 13:30:10 +00:00
|
|
|
|
'id': notification_id,
|
2016-02-09 12:01:17 +00:00
|
|
|
|
'to': to,
|
2019-11-01 14:34:15 +00:00
|
|
|
|
'job_id': None,
|
|
|
|
|
|
'job': None,
|
2016-03-08 15:23:19 +00:00
|
|
|
|
'service_id': service.id,
|
2016-02-09 12:48:27 +00:00
|
|
|
|
'service': service,
|
2017-11-09 14:31:31 +00:00
|
|
|
|
'template_id': template.id,
|
2016-05-11 17:04:51 +01:00
|
|
|
|
'template_version': template.version,
|
2019-11-01 14:34:15 +00:00
|
|
|
|
'status': 'created',
|
|
|
|
|
|
'reference': None,
|
2016-04-21 11:37:38 +01:00
|
|
|
|
'created_at': created_at,
|
2019-11-01 14:34:15 +00:00
|
|
|
|
'sent_at': None,
|
|
|
|
|
|
'billable_units': 1,
|
|
|
|
|
|
'personalisation': None,
|
2016-06-30 18:43:15 +01:00
|
|
|
|
'notification_type': template.template_type,
|
2017-08-02 15:35:56 +01:00
|
|
|
|
'api_key': api_key,
|
|
|
|
|
|
'api_key_id': api_key and api_key.id,
|
2019-11-01 14:34:15 +00:00
|
|
|
|
'key_type': api_key.key_type,
|
|
|
|
|
|
'sent_by': None,
|
|
|
|
|
|
'updated_at': None,
|
|
|
|
|
|
'client_reference': None,
|
|
|
|
|
|
'rate_multiplier': 1.0,
|
|
|
|
|
|
'normalised_to': None,
|
|
|
|
|
|
'postage': None,
|
2016-02-09 12:01:17 +00:00
|
|
|
|
}
|
2019-11-01 14:34:15 +00:00
|
|
|
|
|
2016-02-09 12:01:17 +00:00
|
|
|
|
notification = Notification(**data)
|
persist_letter saves address correctly to database
the `to` field stores either the phone number or the email address
of the recipient - it's a bit more complicated for letters, since
there are address lines 1 through 6, and a postcode. In utils, they're
stored alongside the personalisation, and we have to ensure that when
we persist to the database we keep as much parity with utils to make
our work easier. Aside from sending, the `to` field is also used to
show recipients on the front end report pages - we've decided that the
best thing to store here is address_line_1 - which is probably going to
be either a person's name, company name, or PO box number
Also, a lot of tests and test cleanup - I added create_template and
create_notification functions in db.py, so if you're creating new
fixtures you can use these functions, and you won't need to pass
notify_db and notify_db_session around, huzzah!
also removed create param from sample_notification since it's not used
anywhere
2017-01-19 12:10:32 +00:00
|
|
|
|
dao_create_notification(notification)
|
2017-05-15 17:27:38 +01:00
|
|
|
|
|
2016-02-09 12:01:17 +00:00
|
|
|
|
return notification
|
2016-02-17 15:41:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
persist_letter saves address correctly to database
the `to` field stores either the phone number or the email address
of the recipient - it's a bit more complicated for letters, since
there are address lines 1 through 6, and a postcode. In utils, they're
stored alongside the personalisation, and we have to ensure that when
we persist to the database we keep as much parity with utils to make
our work easier. Aside from sending, the `to` field is also used to
show recipients on the front end report pages - we've decided that the
best thing to store here is address_line_1 - which is probably going to
be either a person's name, company name, or PO box number
Also, a lot of tests and test cleanup - I added create_template and
create_notification functions in db.py, so if you're creating new
fixtures you can use these functions, and you won't need to pass
notify_db and notify_db_session around, huzzah!
also removed create param from sample_notification since it's not used
anywhere
2017-01-19 12:10:32 +00:00
|
|
|
|
@pytest.fixture
|
|
|
|
|
|
def sample_letter_notification(sample_letter_template):
|
|
|
|
|
|
address = {
|
2017-09-11 14:16:04 +01:00
|
|
|
|
'address_line_1': 'A1',
|
|
|
|
|
|
'address_line_2': 'A2',
|
|
|
|
|
|
'address_line_3': 'A3',
|
|
|
|
|
|
'address_line_4': 'A4',
|
|
|
|
|
|
'address_line_5': 'A5',
|
|
|
|
|
|
'address_line_6': 'A6',
|
persist_letter saves address correctly to database
the `to` field stores either the phone number or the email address
of the recipient - it's a bit more complicated for letters, since
there are address lines 1 through 6, and a postcode. In utils, they're
stored alongside the personalisation, and we have to ensure that when
we persist to the database we keep as much parity with utils to make
our work easier. Aside from sending, the `to` field is also used to
show recipients on the front end report pages - we've decided that the
best thing to store here is address_line_1 - which is probably going to
be either a person's name, company name, or PO box number
Also, a lot of tests and test cleanup - I added create_template and
create_notification functions in db.py, so if you're creating new
fixtures you can use these functions, and you won't need to pass
notify_db and notify_db_session around, huzzah!
also removed create param from sample_notification since it's not used
anywhere
2017-01-19 12:10:32 +00:00
|
|
|
|
'postcode': 'A_POST'
|
|
|
|
|
|
}
|
2017-12-08 17:32:57 +00:00
|
|
|
|
return create_notification(sample_letter_template, reference='foo', personalisation=address)
|
persist_letter saves address correctly to database
the `to` field stores either the phone number or the email address
of the recipient - it's a bit more complicated for letters, since
there are address lines 1 through 6, and a postcode. In utils, they're
stored alongside the personalisation, and we have to ensure that when
we persist to the database we keep as much parity with utils to make
our work easier. Aside from sending, the `to` field is also used to
show recipients on the front end report pages - we've decided that the
best thing to store here is address_line_1 - which is probably going to
be either a person's name, company name, or PO box number
Also, a lot of tests and test cleanup - I added create_template and
create_notification functions in db.py, so if you're creating new
fixtures you can use these functions, and you won't need to pass
notify_db and notify_db_session around, huzzah!
also removed create param from sample_notification since it's not used
anywhere
2017-01-19 12:10:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
2016-08-30 15:51:56 +01:00
|
|
|
|
@pytest.fixture(scope='function')
|
2019-10-29 14:33:01 +00:00
|
|
|
|
def sample_email_notification(notify_db_session):
|
2016-08-30 15:51:56 +01:00
|
|
|
|
created_at = datetime.utcnow()
|
2019-01-02 17:15:27 +00:00
|
|
|
|
service = create_service(check_if_service_exists=True)
|
2019-10-29 14:33:01 +00:00
|
|
|
|
template = create_template(service, template_type=EMAIL_TYPE)
|
|
|
|
|
|
job = create_job(template)
|
2016-08-30 15:51:56 +01:00
|
|
|
|
|
|
|
|
|
|
notification_id = uuid.uuid4()
|
|
|
|
|
|
|
|
|
|
|
|
to = 'foo@bar.com'
|
|
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'id': notification_id,
|
|
|
|
|
|
'to': to,
|
|
|
|
|
|
'job_id': job.id,
|
|
|
|
|
|
'job': job,
|
|
|
|
|
|
'service_id': service.id,
|
|
|
|
|
|
'service': service,
|
2017-11-09 14:31:31 +00:00
|
|
|
|
'template_id': template.id,
|
2016-08-30 15:51:56 +01:00
|
|
|
|
'template_version': template.version,
|
|
|
|
|
|
'status': 'created',
|
|
|
|
|
|
'reference': None,
|
|
|
|
|
|
'created_at': created_at,
|
|
|
|
|
|
'billable_units': 0,
|
|
|
|
|
|
'personalisation': None,
|
|
|
|
|
|
'notification_type': template.template_type,
|
|
|
|
|
|
'api_key_id': None,
|
|
|
|
|
|
'key_type': KEY_TYPE_NORMAL,
|
|
|
|
|
|
'job_row_number': 1
|
|
|
|
|
|
}
|
|
|
|
|
|
notification = Notification(**data)
|
|
|
|
|
|
dao_create_notification(notification)
|
|
|
|
|
|
return notification
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-26 11:00:03 +01:00
|
|
|
|
@pytest.fixture(scope='function')
|
2019-11-01 14:34:15 +00:00
|
|
|
|
def sample_notification_history(notify_db, notify_db_session, sample_template):
|
|
|
|
|
|
created_at = datetime.utcnow()
|
|
|
|
|
|
sent_at = datetime.utcnow()
|
|
|
|
|
|
notification_type = sample_template.template_type
|
|
|
|
|
|
api_key = create_api_key(sample_template.service, key_type=KEY_TYPE_NORMAL)
|
2017-08-31 12:44:06 +01:00
|
|
|
|
|
2016-07-26 11:00:03 +01:00
|
|
|
|
notification_history = NotificationHistory(
|
|
|
|
|
|
id=uuid.uuid4(),
|
|
|
|
|
|
service=sample_template.service,
|
2017-11-09 16:04:43 +00:00
|
|
|
|
template_id=sample_template.id,
|
2016-07-26 11:00:03 +01:00
|
|
|
|
template_version=sample_template.version,
|
2019-11-01 14:34:15 +00:00
|
|
|
|
status='created',
|
2016-07-26 11:00:03 +01:00
|
|
|
|
created_at=created_at,
|
2017-01-27 12:22:36 +00:00
|
|
|
|
notification_type=notification_type,
|
2019-11-01 14:34:15 +00:00
|
|
|
|
key_type=KEY_TYPE_NORMAL,
|
2017-08-31 12:44:06 +01:00
|
|
|
|
api_key=api_key,
|
|
|
|
|
|
api_key_id=api_key and api_key.id,
|
|
|
|
|
|
sent_at=sent_at
|
2016-07-26 11:00:03 +01:00
|
|
|
|
)
|
|
|
|
|
|
notify_db.session.add(notification_history)
|
|
|
|
|
|
notify_db.session.commit()
|
|
|
|
|
|
|
|
|
|
|
|
return notification_history
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-25 11:22:36 +00:00
|
|
|
|
@pytest.fixture(scope='function')
|
2019-11-01 14:34:15 +00:00
|
|
|
|
def sample_invited_user(notify_db_session):
|
|
|
|
|
|
service = create_service(check_if_service_exists=True)
|
|
|
|
|
|
to_email_address = 'invited_user@digital.gov.uk'
|
2016-02-25 11:22:36 +00:00
|
|
|
|
|
|
|
|
|
|
from_user = service.users[0]
|
|
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'service': service,
|
|
|
|
|
|
'email_address': to_email_address,
|
2016-02-29 09:49:12 +00:00
|
|
|
|
'from_user': from_user,
|
2019-03-11 14:33:51 +00:00
|
|
|
|
'permissions': 'send_messages,manage_service,manage_api_keys',
|
|
|
|
|
|
'folder_permissions': ['folder_1_id', 'folder_2_id'],
|
2016-02-25 11:22:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
invited_user = InvitedUser(**data)
|
|
|
|
|
|
save_invited_user(invited_user)
|
|
|
|
|
|
return invited_user
|
2016-02-26 12:00:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
2018-02-19 17:14:01 +00:00
|
|
|
|
@pytest.fixture(scope='function')
|
2019-11-01 14:34:15 +00:00
|
|
|
|
def sample_invited_org_user(sample_user, sample_organisation):
|
2018-02-19 17:14:01 +00:00
|
|
|
|
return create_invited_org_user(sample_organisation, sample_user)
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-26 15:57:24 +00:00
|
|
|
|
@pytest.fixture(scope='function')
|
2019-11-01 14:34:15 +00:00
|
|
|
|
def sample_user_service_permission(notify_db_session):
|
|
|
|
|
|
user = create_user()
|
|
|
|
|
|
service = create_service(user=user, check_if_service_exists=True)
|
|
|
|
|
|
permission = 'manage_settings'
|
|
|
|
|
|
|
2016-02-26 15:57:24 +00:00
|
|
|
|
data = {
|
|
|
|
|
|
'user': user,
|
|
|
|
|
|
'service': service,
|
|
|
|
|
|
'permission': permission
|
|
|
|
|
|
}
|
2016-03-02 11:10:52 +00:00
|
|
|
|
p_model = Permission.query.filter_by(
|
|
|
|
|
|
user=user,
|
|
|
|
|
|
service=service,
|
|
|
|
|
|
permission=permission).first()
|
|
|
|
|
|
if not p_model:
|
|
|
|
|
|
p_model = Permission(**data)
|
|
|
|
|
|
db.session.add(p_model)
|
|
|
|
|
|
db.session.commit()
|
2016-02-26 15:57:24 +00:00
|
|
|
|
return p_model
|
2016-04-08 13:34:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def fake_uuid():
|
|
|
|
|
|
return "6ce466d0-fd6a-11e5-82f5-e0accb9d11a6"
|
2016-04-21 11:37:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
2016-05-06 09:09:47 +01:00
|
|
|
|
def ses_provider():
|
|
|
|
|
|
return ProviderDetails.query.filter_by(identifier='ses').one()
|
2016-04-21 11:37:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
2016-04-28 12:01:27 +01:00
|
|
|
|
@pytest.fixture(scope='function')
|
2016-05-06 09:09:47 +01:00
|
|
|
|
def mmg_provider():
|
|
|
|
|
|
return ProviderDetails.query.filter_by(identifier='mmg').one()
|
2016-04-28 12:01:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
2016-06-01 15:59:44 +01:00
|
|
|
|
@pytest.fixture(scope='function')
|
2019-11-01 14:34:15 +00:00
|
|
|
|
def mock_firetext_client(mocker):
|
2016-06-01 15:59:44 +01:00
|
|
|
|
client = FiretextClient()
|
2019-11-01 14:34:15 +00:00
|
|
|
|
statsd_client = mocker.Mock()
|
2016-06-01 15:59:44 +01:00
|
|
|
|
current_app = mocker.Mock(config={
|
2019-04-12 12:03:58 +01:00
|
|
|
|
'FIRETEXT_URL': 'https://example.com/firetext',
|
2016-06-01 15:59:44 +01:00
|
|
|
|
'FIRETEXT_API_KEY': 'foo',
|
2016-06-06 09:49:51 +01:00
|
|
|
|
'FROM_NUMBER': 'bar'
|
2016-06-01 15:59:44 +01:00
|
|
|
|
})
|
|
|
|
|
|
client.init_app(current_app, statsd_client)
|
|
|
|
|
|
return client
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-06-06 11:51:12 +01:00
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def sms_code_template(notify_db,
|
|
|
|
|
|
notify_db_session):
|
2016-10-12 13:06:39 +01:00
|
|
|
|
service, user = notify_service(notify_db, notify_db_session)
|
2017-03-08 13:03:44 +00:00
|
|
|
|
return create_custom_template(
|
|
|
|
|
|
service=service,
|
|
|
|
|
|
user=user,
|
|
|
|
|
|
template_config_name='SMS_CODE_TEMPLATE_ID',
|
|
|
|
|
|
content='((verify_code))',
|
|
|
|
|
|
template_type='sms'
|
|
|
|
|
|
)
|
2016-06-09 16:41:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
2017-11-03 16:00:22 +00:00
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def email_2fa_code_template(notify_db, notify_db_session):
|
|
|
|
|
|
service, user = notify_service(notify_db, notify_db_session)
|
|
|
|
|
|
return create_custom_template(
|
|
|
|
|
|
service=service,
|
|
|
|
|
|
user=user,
|
|
|
|
|
|
template_config_name='EMAIL_2FA_TEMPLATE_ID',
|
|
|
|
|
|
content=(
|
|
|
|
|
|
'Hi ((name)),'
|
|
|
|
|
|
''
|
|
|
|
|
|
'To sign in to GOV.UK Notify please open this link:'
|
|
|
|
|
|
'((url))'
|
|
|
|
|
|
),
|
|
|
|
|
|
subject='Sign in to GOV.UK Notify',
|
|
|
|
|
|
template_type='email'
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-06-09 16:41:20 +01:00
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def email_verification_template(notify_db,
|
|
|
|
|
|
notify_db_session):
|
2016-10-12 13:06:39 +01:00
|
|
|
|
service, user = notify_service(notify_db, notify_db_session)
|
2017-03-08 13:03:44 +00:00
|
|
|
|
return create_custom_template(
|
|
|
|
|
|
service=service,
|
|
|
|
|
|
user=user,
|
2017-11-03 14:38:30 +00:00
|
|
|
|
template_config_name='NEW_USER_EMAIL_VERIFICATION_TEMPLATE_ID',
|
2017-03-08 13:03:44 +00:00
|
|
|
|
content='((user_name)) use ((url)) to complete registration',
|
|
|
|
|
|
template_type='email'
|
|
|
|
|
|
)
|
2016-06-16 17:34:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def invitation_email_template(notify_db,
|
|
|
|
|
|
notify_db_session):
|
2016-10-12 13:06:39 +01:00
|
|
|
|
service, user = notify_service(notify_db, notify_db_session)
|
|
|
|
|
|
content = '((user_name)) is invited to Notify by ((service_name)) ((url)) to complete registration',
|
2017-03-08 13:03:44 +00:00
|
|
|
|
return create_custom_template(
|
|
|
|
|
|
service=service,
|
|
|
|
|
|
user=user,
|
|
|
|
|
|
template_config_name='INVITATION_EMAIL_TEMPLATE_ID',
|
|
|
|
|
|
content=content,
|
|
|
|
|
|
subject='Invitation to ((service_name))',
|
|
|
|
|
|
template_type='email'
|
|
|
|
|
|
)
|
2016-06-16 17:34:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
2018-02-19 17:14:01 +00:00
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def org_invite_email_template(notify_db, notify_db_session):
|
|
|
|
|
|
service, user = notify_service(notify_db, notify_db_session)
|
|
|
|
|
|
|
|
|
|
|
|
return create_custom_template(
|
|
|
|
|
|
service=service,
|
|
|
|
|
|
user=user,
|
|
|
|
|
|
template_config_name='ORGANISATION_INVITATION_EMAIL_TEMPLATE_ID',
|
|
|
|
|
|
content='((user_name)) ((organisation_name)) ((url))',
|
|
|
|
|
|
subject='Invitation to ((organisation_name))',
|
|
|
|
|
|
template_type='email'
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-06-16 17:34:33 +01:00
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def password_reset_email_template(notify_db,
|
|
|
|
|
|
notify_db_session):
|
2016-10-12 13:06:39 +01:00
|
|
|
|
service, user = notify_service(notify_db, notify_db_session)
|
|
|
|
|
|
|
2017-03-08 13:03:44 +00:00
|
|
|
|
return create_custom_template(
|
|
|
|
|
|
service=service,
|
|
|
|
|
|
user=user,
|
|
|
|
|
|
template_config_name='PASSWORD_RESET_TEMPLATE_ID',
|
|
|
|
|
|
content='((user_name)) you can reset password by clicking ((url))',
|
|
|
|
|
|
subject='Reset your password',
|
|
|
|
|
|
template_type='email'
|
|
|
|
|
|
)
|
2016-10-12 13:06:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
2019-05-10 15:32:24 +01:00
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def verify_reply_to_address_email_template(notify_db, notify_db_session):
|
|
|
|
|
|
service, user = notify_service(notify_db, notify_db_session)
|
|
|
|
|
|
|
|
|
|
|
|
return create_custom_template(
|
|
|
|
|
|
service=service,
|
|
|
|
|
|
user=user,
|
|
|
|
|
|
template_config_name='REPLY_TO_EMAIL_ADDRESS_VERIFICATION_TEMPLATE_ID',
|
|
|
|
|
|
content="Hi,This address has been provided as the reply-to email address so we are verifying if it's working",
|
|
|
|
|
|
subject='Your GOV.UK Notify reply-to email address',
|
|
|
|
|
|
template_type='email'
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-02-26 16:25:04 +00:00
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def team_member_email_edit_template(notify_db, notify_db_session):
|
|
|
|
|
|
service, user = notify_service(notify_db, notify_db_session)
|
|
|
|
|
|
|
|
|
|
|
|
return create_custom_template(
|
|
|
|
|
|
service=service,
|
|
|
|
|
|
user=user,
|
|
|
|
|
|
template_config_name='TEAM_MEMBER_EDIT_EMAIL_TEMPLATE_ID',
|
|
|
|
|
|
content='Hi ((name)) ((servicemanagername)) changed your email to ((email address))',
|
|
|
|
|
|
subject='Your GOV.UK Notify email address has changed',
|
|
|
|
|
|
template_type='email'
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def team_member_mobile_edit_template(notify_db, notify_db_session):
|
|
|
|
|
|
service, user = notify_service(notify_db, notify_db_session)
|
|
|
|
|
|
|
|
|
|
|
|
return create_custom_template(
|
|
|
|
|
|
service=service,
|
|
|
|
|
|
user=user,
|
|
|
|
|
|
template_config_name='TEAM_MEMBER_EDIT_MOBILE_TEMPLATE_ID',
|
|
|
|
|
|
content='Your mobile number was changed by ((servicemanagername)).',
|
|
|
|
|
|
template_type='sms'
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-10-12 13:06:39 +01:00
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def already_registered_template(notify_db,
|
|
|
|
|
|
notify_db_session):
|
|
|
|
|
|
service, user = notify_service(notify_db, notify_db_session)
|
|
|
|
|
|
|
|
|
|
|
|
content = """Sign in here: ((signin_url)) If you’ve forgotten your password,
|
|
|
|
|
|
you can reset it here: ((forgot_password_url)) feedback:((feedback_url))"""
|
2017-03-08 13:03:44 +00:00
|
|
|
|
return create_custom_template(
|
|
|
|
|
|
service=service, user=user,
|
|
|
|
|
|
template_config_name='ALREADY_REGISTERED_EMAIL_TEMPLATE_ID',
|
|
|
|
|
|
content=content,
|
|
|
|
|
|
template_type='email'
|
|
|
|
|
|
)
|
2016-10-12 13:06:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def change_email_confirmation_template(notify_db,
|
|
|
|
|
|
notify_db_session):
|
|
|
|
|
|
service, user = notify_service(notify_db, notify_db_session)
|
|
|
|
|
|
content = """Hi ((name)),
|
|
|
|
|
|
Click this link to confirm your new email address:
|
|
|
|
|
|
((url))
|
|
|
|
|
|
If you didn’t try to change the email address for your GOV.UK Notify account, let us know here:
|
|
|
|
|
|
((feedback_url))"""
|
2017-03-08 13:03:44 +00:00
|
|
|
|
template = create_custom_template(
|
|
|
|
|
|
service=service,
|
|
|
|
|
|
user=user,
|
|
|
|
|
|
template_config_name='CHANGE_EMAIL_CONFIRMATION_TEMPLATE_ID',
|
|
|
|
|
|
content=content,
|
|
|
|
|
|
template_type='email'
|
|
|
|
|
|
)
|
2016-10-12 13:06:39 +01:00
|
|
|
|
return template
|
|
|
|
|
|
|
2016-06-16 17:34:33 +01:00
|
|
|
|
|
2019-07-08 12:33:53 +01:00
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def mou_signed_templates(notify_db, notify_db_session):
|
|
|
|
|
|
service, user = notify_service(notify_db, notify_db_session)
|
|
|
|
|
|
import importlib
|
|
|
|
|
|
alembic_script = importlib.import_module('migrations.versions.0298_add_mou_signed_receipt')
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
config_name: create_custom_template(
|
|
|
|
|
|
service,
|
|
|
|
|
|
user,
|
|
|
|
|
|
config_name,
|
|
|
|
|
|
'email',
|
|
|
|
|
|
content='\n'.join(
|
|
|
|
|
|
next(
|
|
|
|
|
|
x
|
|
|
|
|
|
for x in alembic_script.templates
|
|
|
|
|
|
if x['id'] == current_app.config[config_name]
|
|
|
|
|
|
)['content_lines']
|
|
|
|
|
|
),
|
|
|
|
|
|
)
|
|
|
|
|
|
for config_name in [
|
|
|
|
|
|
'MOU_SIGNER_RECEIPT_TEMPLATE_ID',
|
|
|
|
|
|
'MOU_SIGNED_ON_BEHALF_SIGNER_RECEIPT_TEMPLATE_ID',
|
|
|
|
|
|
'MOU_SIGNED_ON_BEHALF_ON_BEHALF_RECEIPT_TEMPLATE_ID',
|
|
|
|
|
|
'MOU_NOTIFY_TEAM_ALERT_TEMPLATE_ID',
|
|
|
|
|
|
]
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-03-08 13:03:44 +00:00
|
|
|
|
def create_custom_template(service, user, template_config_name, template_type, content='', subject=None):
|
2016-10-12 13:06:39 +01:00
|
|
|
|
template = Template.query.get(current_app.config[template_config_name])
|
2016-06-16 17:34:33 +01:00
|
|
|
|
if not template:
|
|
|
|
|
|
data = {
|
2016-10-12 13:06:39 +01:00
|
|
|
|
'id': current_app.config[template_config_name],
|
|
|
|
|
|
'name': template_config_name,
|
|
|
|
|
|
'template_type': template_type,
|
|
|
|
|
|
'content': content,
|
2016-06-16 17:34:33 +01:00
|
|
|
|
'service': service,
|
|
|
|
|
|
'created_by': user,
|
2016-10-12 13:06:39 +01:00
|
|
|
|
'subject': subject,
|
2016-06-16 17:34:33 +01:00
|
|
|
|
'archived': False
|
|
|
|
|
|
}
|
|
|
|
|
|
template = Template(**data)
|
|
|
|
|
|
db.session.add(template)
|
2017-11-09 14:42:43 +00:00
|
|
|
|
db.session.add(create_history(template, TemplateHistory))
|
2017-02-14 17:59:18 +00:00
|
|
|
|
db.session.commit()
|
2016-06-16 17:34:33 +01:00
|
|
|
|
return template
|
2016-07-07 17:23:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
2016-10-12 13:06:39 +01:00
|
|
|
|
def notify_service(notify_db, notify_db_session):
|
2016-12-28 12:30:21 +00:00
|
|
|
|
user = create_user()
|
2016-07-07 17:23:07 +01:00
|
|
|
|
service = Service.query.get(current_app.config['NOTIFY_SERVICE_ID'])
|
|
|
|
|
|
if not service:
|
2017-11-27 12:30:50 +00:00
|
|
|
|
service = Service(
|
|
|
|
|
|
name='Notify Service',
|
|
|
|
|
|
message_limit=1000,
|
|
|
|
|
|
restricted=False,
|
|
|
|
|
|
email_from='notify.service',
|
|
|
|
|
|
created_by=user,
|
|
|
|
|
|
prefix_sms=False,
|
|
|
|
|
|
)
|
2017-12-01 16:31:21 +00:00
|
|
|
|
dao_create_service(
|
|
|
|
|
|
service=service,
|
|
|
|
|
|
service_id=current_app.config['NOTIFY_SERVICE_ID'],
|
2017-12-06 11:01:18 +00:00
|
|
|
|
user=user
|
2017-12-01 16:31:21 +00:00
|
|
|
|
)
|
2017-11-27 12:30:50 +00:00
|
|
|
|
|
2016-07-07 17:23:07 +01:00
|
|
|
|
data = {
|
2017-11-27 12:30:50 +00:00
|
|
|
|
'service': service,
|
|
|
|
|
|
'email_address': "notify@gov.uk",
|
|
|
|
|
|
'is_default': True,
|
2016-07-07 17:23:07 +01:00
|
|
|
|
}
|
2017-11-27 12:30:50 +00:00
|
|
|
|
reply_to = ServiceEmailReplyTo(**data)
|
|
|
|
|
|
|
|
|
|
|
|
db.session.add(reply_to)
|
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
2016-10-12 13:06:39 +01:00
|
|
|
|
return service, user
|
2016-09-22 17:17:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
2020-07-28 11:22:19 +01:00
|
|
|
|
def sample_service_guest_list(notify_db, notify_db_session):
|
2019-11-01 14:34:15 +00:00
|
|
|
|
service = create_service(check_if_service_exists=True)
|
2020-07-28 11:22:19 +01:00
|
|
|
|
guest_list_user = ServiceGuestList.from_string(service.id, EMAIL_TYPE, 'guest_list_user@digital.gov.uk')
|
2016-09-22 17:17:34 +01:00
|
|
|
|
|
2020-07-28 11:22:19 +01:00
|
|
|
|
notify_db.session.add(guest_list_user)
|
2016-09-22 17:17:34 +01:00
|
|
|
|
notify_db.session.commit()
|
2020-07-28 11:22:19 +01:00
|
|
|
|
return guest_list_user
|
2016-11-18 15:09:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-08-04 12:13:10 +01:00
|
|
|
|
@pytest.fixture
|
2019-11-01 14:34:15 +00:00
|
|
|
|
def sample_inbound_numbers(sample_service):
|
2019-01-02 17:15:27 +00:00
|
|
|
|
service = create_service(service_name='sample service 2', check_if_service_exists=True)
|
2017-12-21 17:18:35 +00:00
|
|
|
|
inbound_numbers = list()
|
2017-08-04 12:13:10 +01:00
|
|
|
|
inbound_numbers.append(create_inbound_number(number='1', provider='mmg'))
|
2017-08-04 19:10:22 +01:00
|
|
|
|
inbound_numbers.append(create_inbound_number(number='2', provider='mmg', active=False, service_id=service.id))
|
2017-08-04 12:13:10 +01:00
|
|
|
|
inbound_numbers.append(create_inbound_number(number='3', provider='firetext', service_id=sample_service.id))
|
|
|
|
|
|
return inbound_numbers
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-02-10 01:34:37 +00:00
|
|
|
|
@pytest.fixture
|
2019-11-01 14:34:15 +00:00
|
|
|
|
def sample_organisation(notify_db_session):
|
2018-02-10 01:34:37 +00:00
|
|
|
|
org = Organisation(name='sample organisation')
|
|
|
|
|
|
dao_create_organisation(org)
|
|
|
|
|
|
return org
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-12-19 16:53:23 +00:00
|
|
|
|
@pytest.fixture
|
|
|
|
|
|
def restore_provider_details(notify_db, notify_db_session):
|
|
|
|
|
|
"""
|
|
|
|
|
|
We view ProviderDetails as a static in notify_db_session, since we don't modify it... except we do, we updated
|
|
|
|
|
|
priority. This fixture is designed to be used in tests that will knowingly touch provider details, to restore them
|
|
|
|
|
|
to previous state.
|
|
|
|
|
|
|
|
|
|
|
|
Note: This doesn't technically require notify_db_session (only notify_db), but kept as a requirement to encourage
|
|
|
|
|
|
good usage - if you're modifying ProviderDetails' state then it's good to clear down the rest of the DB too
|
|
|
|
|
|
"""
|
|
|
|
|
|
existing_provider_details = ProviderDetails.query.all()
|
|
|
|
|
|
existing_provider_details_history = ProviderDetailsHistory.query.all()
|
|
|
|
|
|
# make transient removes the objects from the session - since we'll want to delete them later
|
|
|
|
|
|
for epd in existing_provider_details:
|
|
|
|
|
|
make_transient(epd)
|
|
|
|
|
|
for epdh in existing_provider_details_history:
|
|
|
|
|
|
make_transient(epdh)
|
|
|
|
|
|
|
|
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
|
|
# also delete these as they depend on provider_details
|
|
|
|
|
|
ProviderRates.query.delete()
|
|
|
|
|
|
ProviderDetails.query.delete()
|
|
|
|
|
|
ProviderDetailsHistory.query.delete()
|
|
|
|
|
|
notify_db.session.commit()
|
|
|
|
|
|
notify_db.session.add_all(existing_provider_details)
|
|
|
|
|
|
notify_db.session.add_all(existing_provider_details_history)
|
|
|
|
|
|
notify_db.session.commit()
|
2017-05-22 15:05:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
|
def admin_request(client):
|
2018-12-12 12:57:04 +00:00
|
|
|
|
|
2017-05-22 15:05:05 +01:00
|
|
|
|
class AdminRequest:
|
2018-12-12 12:57:04 +00:00
|
|
|
|
app = client.application
|
2017-05-22 15:05:05 +01:00
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
2017-06-19 13:49:20 +01:00
|
|
|
|
def get(endpoint, _expected_status=200, **endpoint_kwargs):
|
2017-05-22 15:05:05 +01:00
|
|
|
|
resp = client.get(
|
|
|
|
|
|
url_for(endpoint, **(endpoint_kwargs or {})),
|
|
|
|
|
|
headers=[create_authorization_header()]
|
|
|
|
|
|
)
|
2018-07-05 11:09:17 +01:00
|
|
|
|
json_resp = resp.json
|
2017-06-19 13:49:20 +01:00
|
|
|
|
assert resp.status_code == _expected_status
|
2017-05-22 15:05:05 +01:00
|
|
|
|
return json_resp
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
2017-06-19 13:49:20 +01:00
|
|
|
|
def post(endpoint, _data=None, _expected_status=200, **endpoint_kwargs):
|
2017-05-22 15:05:05 +01:00
|
|
|
|
resp = client.post(
|
|
|
|
|
|
url_for(endpoint, **(endpoint_kwargs or {})),
|
2017-06-19 13:49:20 +01:00
|
|
|
|
data=json.dumps(_data),
|
2017-05-22 15:05:05 +01:00
|
|
|
|
headers=[('Content-Type', 'application/json'), create_authorization_header()]
|
|
|
|
|
|
)
|
2017-08-10 12:47:57 +01:00
|
|
|
|
if resp.get_data():
|
2018-07-05 11:09:17 +01:00
|
|
|
|
json_resp = resp.json
|
2017-08-04 21:27:09 +01:00
|
|
|
|
else:
|
|
|
|
|
|
json_resp = None
|
|
|
|
|
|
assert resp.status_code == _expected_status
|
2017-05-22 15:05:05 +01:00
|
|
|
|
return json_resp
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
2017-06-19 13:49:20 +01:00
|
|
|
|
def delete(endpoint, _expected_status=204, **endpoint_kwargs):
|
2017-05-22 15:05:05 +01:00
|
|
|
|
resp = client.delete(
|
|
|
|
|
|
url_for(endpoint, **(endpoint_kwargs or {})),
|
|
|
|
|
|
headers=[create_authorization_header()]
|
|
|
|
|
|
)
|
2018-07-05 11:09:17 +01:00
|
|
|
|
if resp.get_data():
|
|
|
|
|
|
json_resp = resp.json
|
|
|
|
|
|
else:
|
|
|
|
|
|
json_resp = None
|
2017-07-27 11:10:22 +01:00
|
|
|
|
assert resp.status_code == _expected_status, json_resp
|
2017-05-22 15:05:05 +01:00
|
|
|
|
return json_resp
|
|
|
|
|
|
|
|
|
|
|
|
return AdminRequest
|
2017-06-13 15:22:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def datetime_in_past(days=0, seconds=0):
|
|
|
|
|
|
return datetime.now(tz=pytz.utc) - timedelta(days=days, seconds=seconds)
|