2017-01-17 15:38:54 +00:00
|
|
|
|
import requests_mock
|
|
|
|
|
|
import pytest
|
2016-06-01 15:59:44 +01:00
|
|
|
|
import uuid
|
2016-09-01 14:31:01 +01:00
|
|
|
|
from datetime import (datetime, date, timedelta)
|
2016-06-01 15:59:44 +01:00
|
|
|
|
|
2017-01-17 15:38:54 +00:00
|
|
|
|
from sqlalchemy import asc
|
2016-12-19 16:53:23 +00:00
|
|
|
|
from sqlalchemy.orm.session import make_transient
|
2016-06-06 11:51:12 +01:00
|
|
|
|
from flask import current_app
|
2016-06-01 15:59:44 +01:00
|
|
|
|
|
2016-03-31 17:46:18 +01:00
|
|
|
|
from app import db
|
2016-02-26 12:00:16 +00:00
|
|
|
|
from app.models import (
|
2016-04-28 12:01:27 +01:00
|
|
|
|
User,
|
|
|
|
|
|
Service,
|
|
|
|
|
|
Template,
|
|
|
|
|
|
ApiKey,
|
|
|
|
|
|
Job,
|
|
|
|
|
|
Notification,
|
2016-07-26 11:00:03 +01:00
|
|
|
|
NotificationHistory,
|
2016-04-28 12:01:27 +01:00
|
|
|
|
InvitedUser,
|
|
|
|
|
|
Permission,
|
2016-05-05 10:45:47 +01:00
|
|
|
|
ProviderStatistics,
|
2016-05-10 09:13:02 +01:00
|
|
|
|
ProviderDetails,
|
2016-12-19 16:53:23 +00:00
|
|
|
|
ProviderDetailsHistory,
|
|
|
|
|
|
ProviderRates,
|
2016-06-23 16:45:20 +01:00
|
|
|
|
NotificationStatistics,
|
2016-09-22 17:17:34 +01:00
|
|
|
|
ServiceWhitelist,
|
2016-09-27 13:50:23 +01:00
|
|
|
|
KEY_TYPE_NORMAL, KEY_TYPE_TEST, KEY_TYPE_TEAM,
|
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
|
|
|
|
MOBILE_TYPE, EMAIL_TYPE, LETTER_TYPE, NOTIFICATION_STATUS_TYPES_COMPLETED)
|
2016-12-28 12:30:21 +00:00
|
|
|
|
from app.dao.users_dao import (create_user_code, create_secret_code)
|
2016-03-22 13:14:23 +00:00
|
|
|
|
from app.dao.services_dao import (dao_create_service, dao_add_user_to_service)
|
2016-02-24 11:51:02 +00:00
|
|
|
|
from app.dao.templates_dao import dao_create_template
|
2016-01-19 12:07:00 +00:00
|
|
|
|
from app.dao.api_key_dao import save_model_api_key
|
2016-02-24 17:12:30 +00:00
|
|
|
|
from app.dao.jobs_dao import dao_create_job
|
2016-02-25 09:59:50 +00:00
|
|
|
|
from app.dao.notifications_dao import dao_create_notification
|
2016-02-25 11:22:36 +00:00
|
|
|
|
from app.dao.invited_user_dao import save_invited_user
|
2016-11-18 15:09:15 +00:00
|
|
|
|
from app.dao.provider_rates_dao import create_provider_rates
|
2016-06-01 15:59:44 +01:00
|
|
|
|
from app.clients.sms.firetext import FiretextClient
|
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
|
|
|
|
|
|
|
|
|
|
from tests.app.db import create_user, create_template, create_notification
|
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
|
2016-03-31 17:46:18 +01:00
|
|
|
|
service = sample_service(notify_db, notify_db_session, service_name, user, email_from=email_from)
|
2016-02-22 17:17:29 +00:00
|
|
|
|
if template_type == 'email':
|
|
|
|
|
|
sample_template(
|
|
|
|
|
|
notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
template_type=template_type,
|
2016-03-31 17:46:18 +01:00
|
|
|
|
subject_line=service.email_from,
|
2016-02-22 17:17:29 +00:00
|
|
|
|
service=service
|
|
|
|
|
|
)
|
|
|
|
|
|
else:
|
|
|
|
|
|
sample_template(
|
|
|
|
|
|
notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
service=service
|
|
|
|
|
|
)
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_code(notify_db, notify_db_session, code_type, usr=None, code=None):
|
|
|
|
|
|
if code is None:
|
|
|
|
|
|
code = create_secret_code()
|
|
|
|
|
|
if usr is None:
|
2016-12-28 12:30:21 +00:00
|
|
|
|
usr = create_user()
|
2016-01-21 17:29:24 +00:00
|
|
|
|
return create_user_code(usr, code, code_type), code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def sample_email_code(notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
code=None,
|
|
|
|
|
|
code_type="email",
|
|
|
|
|
|
usr=None):
|
|
|
|
|
|
code, txt_code = create_code(notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
code_type,
|
|
|
|
|
|
usr=usr,
|
|
|
|
|
|
code=code)
|
|
|
|
|
|
code.txt_code = txt_code
|
|
|
|
|
|
return code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def sample_sms_code(notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
code=None,
|
|
|
|
|
|
code_type="sms",
|
|
|
|
|
|
usr=None):
|
|
|
|
|
|
code, txt_code = create_code(notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
code_type,
|
|
|
|
|
|
usr=usr,
|
|
|
|
|
|
code=code)
|
|
|
|
|
|
code.txt_code = txt_code
|
|
|
|
|
|
return code
|
2016-01-08 12:18:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
2017-02-13 15:46:06 +00:00
|
|
|
|
def sample_service(
|
|
|
|
|
|
notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
service_name="Sample service",
|
|
|
|
|
|
user=None,
|
|
|
|
|
|
restricted=False,
|
|
|
|
|
|
limit=1000,
|
2017-02-24 13:39:58 +00:00
|
|
|
|
email_from=None
|
2017-02-13 15:46:06 +00:00
|
|
|
|
):
|
2016-01-08 12:18:12 +00:00
|
|
|
|
if user is None:
|
2016-12-28 12:30:21 +00:00
|
|
|
|
user = create_user()
|
2016-09-23 10:45:12 +01:00
|
|
|
|
if email_from is None:
|
|
|
|
|
|
email_from = service_name.lower().replace(' ', '.')
|
2016-01-11 15:07:13 +00:00
|
|
|
|
data = {
|
|
|
|
|
|
'name': service_name,
|
2016-04-08 16:13:10 +01:00
|
|
|
|
'message_limit': limit,
|
2016-03-03 12:05:18 +00:00
|
|
|
|
'restricted': restricted,
|
2016-04-14 15:09:59 +01:00
|
|
|
|
'email_from': email_from,
|
|
|
|
|
|
'created_by': user
|
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)
|
2017-02-24 13:39:58 +00:00
|
|
|
|
dao_create_service(service, user)
|
2016-03-22 13:14:23 +00:00
|
|
|
|
else:
|
|
|
|
|
|
if user not in service.users:
|
|
|
|
|
|
dao_add_user_to_service(service, user)
|
2016-01-11 15:07:13 +00:00
|
|
|
|
return service
|
2016-01-13 11:04:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
2017-02-13 15:46:06 +00:00
|
|
|
|
def sample_template(
|
|
|
|
|
|
notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
template_name="Template Name",
|
|
|
|
|
|
template_type="sms",
|
|
|
|
|
|
content="This is a template:\nwith a newline",
|
|
|
|
|
|
archived=False,
|
|
|
|
|
|
subject_line='Subject',
|
|
|
|
|
|
user=None,
|
|
|
|
|
|
service=None,
|
|
|
|
|
|
created_by=None,
|
2017-02-24 13:39:58 +00:00
|
|
|
|
process_type='normal'
|
2017-02-13 15:46:06 +00:00
|
|
|
|
):
|
2016-04-25 10:38:37 +01:00
|
|
|
|
if user is None:
|
2016-12-28 12:30:21 +00:00
|
|
|
|
user = create_user()
|
2016-01-13 11:04:13 +00:00
|
|
|
|
if service is None:
|
|
|
|
|
|
service = sample_service(notify_db, notify_db_session)
|
2016-04-25 16:28:08 +01:00
|
|
|
|
if created_by is None:
|
2016-12-28 12:30:21 +00:00
|
|
|
|
created_by = create_user()
|
2017-02-14 17:59:18 +00:00
|
|
|
|
|
2017-02-24 13:39:58 +00:00
|
|
|
|
data = {
|
|
|
|
|
|
'name': template_name,
|
|
|
|
|
|
'template_type': template_type,
|
|
|
|
|
|
'content': content,
|
|
|
|
|
|
'service': service,
|
|
|
|
|
|
'created_by': created_by,
|
|
|
|
|
|
'archived': archived,
|
|
|
|
|
|
'process_type': process_type
|
|
|
|
|
|
}
|
|
|
|
|
|
if template_type in ['email', 'letter']:
|
|
|
|
|
|
data.update({
|
|
|
|
|
|
'subject': subject_line
|
|
|
|
|
|
})
|
|
|
|
|
|
template = Template(**data)
|
|
|
|
|
|
dao_create_template(template)
|
2016-02-22 17:17:29 +00:00
|
|
|
|
return template
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-29 11:23:34 +00:00
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def sample_template_with_placeholders(notify_db, notify_db_session):
|
2016-11-01 15:19:56 +00:00
|
|
|
|
# deliberate space and title case in placeholder
|
|
|
|
|
|
return sample_template(notify_db, notify_db_session, 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')
|
|
|
|
|
|
def sample_sms_template_with_html(notify_db, notify_db_session):
|
|
|
|
|
|
# deliberate space and title case in placeholder
|
|
|
|
|
|
return sample_template(notify_db, notify_db_session, content=(
|
|
|
|
|
|
"Hello (( Name))\nHere is <em>some HTML</em> & entities"
|
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-22 17:17:29 +00:00
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def sample_email_template(
|
|
|
|
|
|
notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
template_name="Email Template Name",
|
|
|
|
|
|
template_type="email",
|
2016-04-25 10:38:37 +01:00
|
|
|
|
user=None,
|
2016-02-22 17:17:29 +00:00
|
|
|
|
content="This is a template",
|
|
|
|
|
|
subject_line='Email Subject',
|
|
|
|
|
|
service=None):
|
2016-04-25 10:38:37 +01:00
|
|
|
|
if user is None:
|
2016-12-28 12:30:21 +00:00
|
|
|
|
user = create_user()
|
2016-02-22 17:17:29 +00:00
|
|
|
|
if service is None:
|
|
|
|
|
|
service = sample_service(notify_db, notify_db_session)
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'name': template_name,
|
|
|
|
|
|
'template_type': template_type,
|
|
|
|
|
|
'content': content,
|
2016-04-25 10:38:37 +01:00
|
|
|
|
'service': service,
|
2016-12-22 14:40:33 +00:00
|
|
|
|
'created_by': user,
|
|
|
|
|
|
'subject': subject_line
|
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
|
|
|
|
|
|
|
|
|
|
|
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_template(sample_service):
|
|
|
|
|
|
return create_template(sample_service, template_type=LETTER_TYPE)
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-29 14:43:44 +00:00
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def sample_email_template_with_placeholders(notify_db, notify_db_session):
|
2016-04-13 15:31:08 +01:00
|
|
|
|
return sample_email_template(
|
|
|
|
|
|
notify_db,
|
|
|
|
|
|
notify_db_session,
|
2016-07-08 11:02:19 +01:00
|
|
|
|
content="Hello ((name))\nThis is an email from GOV.UK",
|
2016-04-13 15:31:08 +01:00
|
|
|
|
subject_line="((name))")
|
2016-02-29 14:43:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-01-19 12:05:28 +00:00
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def sample_email_template_with_html(notify_db, notify_db_session):
|
|
|
|
|
|
return sample_email_template(
|
|
|
|
|
|
notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
content="Hello ((name))\nThis is an email from GOV.UK with <em>some HTML</em>",
|
|
|
|
|
|
subject_line="((name)) <em>some HTML</em>")
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-01-14 11:30:45 +00:00
|
|
|
|
@pytest.fixture(scope='function')
|
2016-01-19 12:07:00 +00:00
|
|
|
|
def sample_api_key(notify_db,
|
|
|
|
|
|
notify_db_session,
|
2016-06-23 16:45:20 +01:00
|
|
|
|
service=None,
|
2016-09-23 14:44:15 +01:00
|
|
|
|
key_type=KEY_TYPE_NORMAL,
|
|
|
|
|
|
name=None):
|
2016-01-14 11:30:45 +00:00
|
|
|
|
if service is None:
|
|
|
|
|
|
service = sample_service(notify_db, notify_db_session)
|
2016-09-23 14:44:15 +01:00
|
|
|
|
data = {'service': service, 'name': name or uuid.uuid4(), 'created_by': service.created_by, 'key_type': key_type}
|
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')
|
|
|
|
|
|
def sample_test_api_key(notify_db, notify_db_session, service=None):
|
|
|
|
|
|
return sample_api_key(notify_db, notify_db_session, service, KEY_TYPE_TEST)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def sample_team_api_key(notify_db, notify_db_session, service=None):
|
|
|
|
|
|
return sample_api_key(notify_db, notify_db_session, service, KEY_TYPE_TEAM)
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-01-15 12:16:07 +00:00
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def sample_job(notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
service=None,
|
2016-03-09 14:52:07 +00:00
|
|
|
|
template=None,
|
2016-03-14 16:31:57 +00:00
|
|
|
|
notification_count=1,
|
2016-09-21 14:34:38 +01:00
|
|
|
|
created_at=None,
|
2016-08-24 16:24:55 +01:00
|
|
|
|
job_status='pending',
|
2016-10-08 11:44:55 +01:00
|
|
|
|
scheduled_for=None,
|
2016-10-11 14:30:40 +01:00
|
|
|
|
processing_started=None,
|
|
|
|
|
|
original_file_name='some.csv'):
|
2016-01-15 12:16:07 +00:00
|
|
|
|
if service is None:
|
|
|
|
|
|
service = sample_service(notify_db, notify_db_session)
|
|
|
|
|
|
if template is None:
|
|
|
|
|
|
template = sample_template(notify_db, notify_db_session,
|
|
|
|
|
|
service=service)
|
|
|
|
|
|
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,
|
2016-10-11 14:30:40 +01:00
|
|
|
|
'original_file_name': original_file_name,
|
2016-03-14 16:31:57 +00:00
|
|
|
|
'notification_count': notification_count,
|
2016-09-21 14:34:38 +01:00
|
|
|
|
'created_at': created_at or datetime.utcnow(),
|
2016-08-24 16:24:55 +01:00
|
|
|
|
'created_by': service.created_by,
|
|
|
|
|
|
'job_status': job_status,
|
2016-10-08 11:44:55 +01:00
|
|
|
|
'scheduled_for': scheduled_for,
|
|
|
|
|
|
'processing_started': processing_started
|
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(
|
2016-08-30 15:51:56 +01:00
|
|
|
|
notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
service=None
|
2016-03-09 07:27:26 +00:00
|
|
|
|
):
|
|
|
|
|
|
return sample_job(
|
|
|
|
|
|
notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
service=service,
|
|
|
|
|
|
template=sample_template_with_placeholders(notify_db, notify_db_session)
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-09-01 14:31:01 +01:00
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def sample_scheduled_job(
|
|
|
|
|
|
notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
service=None
|
|
|
|
|
|
):
|
|
|
|
|
|
return sample_job(
|
|
|
|
|
|
notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
service=service,
|
|
|
|
|
|
template=sample_template_with_placeholders(notify_db, notify_db_session),
|
|
|
|
|
|
scheduled_for=(datetime.utcnow() + timedelta(minutes=60)).isoformat(),
|
|
|
|
|
|
job_status='scheduled'
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-25 09:59:50 +00:00
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def sample_email_job(notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
service=None,
|
|
|
|
|
|
template=None):
|
|
|
|
|
|
if service is None:
|
|
|
|
|
|
service = sample_service(notify_db, notify_db_session)
|
|
|
|
|
|
if template is None:
|
|
|
|
|
|
template = sample_email_template(
|
|
|
|
|
|
notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
service=service)
|
|
|
|
|
|
job_id = uuid.uuid4()
|
|
|
|
|
|
data = {
|
2016-05-24 11:31:44 +01:00
|
|
|
|
'id': job_id,
|
2016-02-25 09:59:50 +00:00
|
|
|
|
'service_id': service.id,
|
2016-04-07 13:44:04 +01:00
|
|
|
|
'service': service,
|
2016-02-25 09:59:50 +00:00
|
|
|
|
'template_id': template.id,
|
2016-05-13 16:25:05 +01:00
|
|
|
|
'template_version': template.version,
|
2016-02-25 09:59:50 +00:00
|
|
|
|
'original_file_name': 'some.csv',
|
2016-04-26 16:15:34 +01:00
|
|
|
|
'notification_count': 1,
|
|
|
|
|
|
'created_by': service.created_by
|
2016-02-25 09:59:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
job = Job(**data)
|
|
|
|
|
|
dao_create_job(job)
|
|
|
|
|
|
return job
|
|
|
|
|
|
|
|
|
|
|
|
|
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_job(sample_service, sample_letter_template):
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'id': uuid.uuid4(),
|
|
|
|
|
|
'service_id': sample_service.id,
|
|
|
|
|
|
'service': sample_service,
|
|
|
|
|
|
'template_id': sample_letter_template.id,
|
|
|
|
|
|
'template_version': sample_letter_template.version,
|
|
|
|
|
|
'original_file_name': 'some.csv',
|
|
|
|
|
|
'notification_count': 1,
|
|
|
|
|
|
'created_at': datetime.utcnow(),
|
|
|
|
|
|
'created_by': sample_service.created_by,
|
|
|
|
|
|
}
|
|
|
|
|
|
job = Job(**data)
|
|
|
|
|
|
dao_create_job(job)
|
|
|
|
|
|
return job
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-09-15 16:00:04 +01:00
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def sample_notification_with_job(
|
|
|
|
|
|
notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
service=None,
|
|
|
|
|
|
template=None,
|
|
|
|
|
|
job=None,
|
|
|
|
|
|
job_row_number=None,
|
|
|
|
|
|
to_field=None,
|
|
|
|
|
|
status='created',
|
|
|
|
|
|
reference=None,
|
|
|
|
|
|
created_at=None,
|
|
|
|
|
|
sent_at=None,
|
|
|
|
|
|
billable_units=1,
|
|
|
|
|
|
personalisation=None,
|
|
|
|
|
|
api_key_id=None,
|
|
|
|
|
|
key_type=KEY_TYPE_NORMAL
|
|
|
|
|
|
):
|
|
|
|
|
|
if job is None:
|
|
|
|
|
|
job = sample_job(notify_db, notify_db_session, service=service, template=template)
|
|
|
|
|
|
|
|
|
|
|
|
return sample_notification(
|
|
|
|
|
|
notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
service,
|
|
|
|
|
|
template,
|
|
|
|
|
|
job=job,
|
|
|
|
|
|
job_row_number=job_row_number if job_row_number else None,
|
|
|
|
|
|
to_field=to_field,
|
|
|
|
|
|
status=status,
|
|
|
|
|
|
reference=reference,
|
|
|
|
|
|
created_at=created_at,
|
|
|
|
|
|
sent_at=sent_at,
|
|
|
|
|
|
billable_units=billable_units,
|
|
|
|
|
|
personalisation=personalisation,
|
|
|
|
|
|
api_key_id=api_key_id,
|
|
|
|
|
|
key_type=key_type
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-09 12:01:17 +00:00
|
|
|
|
@pytest.fixture(scope='function')
|
2017-02-13 14:27:32 +00:00
|
|
|
|
def sample_notification(
|
|
|
|
|
|
notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
service=None,
|
|
|
|
|
|
template=None,
|
|
|
|
|
|
job=None,
|
|
|
|
|
|
job_row_number=None,
|
|
|
|
|
|
to_field=None,
|
|
|
|
|
|
status='created',
|
|
|
|
|
|
reference=None,
|
|
|
|
|
|
created_at=None,
|
|
|
|
|
|
sent_at=None,
|
|
|
|
|
|
billable_units=1,
|
|
|
|
|
|
personalisation=None,
|
|
|
|
|
|
api_key_id=None,
|
|
|
|
|
|
key_type=KEY_TYPE_NORMAL,
|
|
|
|
|
|
sent_by=None,
|
2017-02-24 13:39:58 +00:00
|
|
|
|
client_reference=None
|
2017-02-13 14:27:32 +00:00
|
|
|
|
):
|
2016-07-22 15:16:24 +01:00
|
|
|
|
if created_at is None:
|
|
|
|
|
|
created_at = datetime.utcnow()
|
2016-02-09 12:01:17 +00:00
|
|
|
|
if service is None:
|
|
|
|
|
|
service = sample_service(notify_db, notify_db_session)
|
|
|
|
|
|
if template is None:
|
|
|
|
|
|
template = sample_template(notify_db, notify_db_session, service=service)
|
|
|
|
|
|
|
2016-03-01 13:30:10 +00:00
|
|
|
|
notification_id = uuid.uuid4()
|
2016-03-01 14:58:27 +00:00
|
|
|
|
|
|
|
|
|
|
if to_field:
|
|
|
|
|
|
to = to_field
|
|
|
|
|
|
else:
|
2016-03-06 12:58:28 +00:00
|
|
|
|
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,
|
2016-09-15 16:00:04 +01:00
|
|
|
|
'job_id': job.id if job else None,
|
2016-02-09 12:48:27 +00:00
|
|
|
|
'job': job,
|
2016-03-08 15:23:19 +00:00
|
|
|
|
'service_id': service.id,
|
2016-02-09 12:48:27 +00:00
|
|
|
|
'service': service,
|
2016-12-15 16:59:03 +00:00
|
|
|
|
'template_id': template.id if template else None,
|
2016-02-25 09:59:50 +00:00
|
|
|
|
'template': template,
|
2016-05-11 17:04:51 +01:00
|
|
|
|
'template_version': template.version,
|
2016-03-09 17:46:01 +00:00
|
|
|
|
'status': status,
|
2016-03-11 10:19:40 +00:00
|
|
|
|
'reference': reference,
|
2016-04-21 11:37:38 +01:00
|
|
|
|
'created_at': created_at,
|
2016-09-13 13:04:44 +01:00
|
|
|
|
'sent_at': sent_at,
|
2016-08-03 16:26:11 +01:00
|
|
|
|
'billable_units': billable_units,
|
2016-06-29 11:23:02 +01:00
|
|
|
|
'personalisation': personalisation,
|
2016-06-30 18:43:15 +01:00
|
|
|
|
'notification_type': template.template_type,
|
|
|
|
|
|
'api_key_id': api_key_id,
|
2016-11-18 15:09:15 +00:00
|
|
|
|
'key_type': key_type,
|
2016-11-25 14:53:21 +00:00
|
|
|
|
'sent_by': sent_by,
|
2017-02-24 13:39:58 +00:00
|
|
|
|
'updated_at': created_at if status in NOTIFICATION_STATUS_TYPES_COMPLETED else None,
|
2016-12-07 14:08:22 +00:00
|
|
|
|
'client_reference': client_reference
|
2016-02-09 12:01:17 +00:00
|
|
|
|
}
|
2016-05-19 10:46:03 +01:00
|
|
|
|
if job_row_number:
|
|
|
|
|
|
data['job_row_number'] = job_row_number
|
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)
|
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 = {
|
|
|
|
|
|
'addressline1': 'A1',
|
|
|
|
|
|
'addressline2': 'A2',
|
|
|
|
|
|
'addressline3': 'A3',
|
|
|
|
|
|
'addressline4': 'A4',
|
|
|
|
|
|
'postcode': 'A_POST'
|
|
|
|
|
|
}
|
|
|
|
|
|
return create_notification(sample_letter_template, personalisation=address)
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-09-23 14:44:15 +01:00
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def sample_notification_with_api_key(notify_db, notify_db_session):
|
|
|
|
|
|
notification = sample_notification(notify_db, notify_db_session)
|
|
|
|
|
|
notification.api_key_id = sample_api_key(
|
|
|
|
|
|
notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
name='Test key'
|
|
|
|
|
|
).id
|
|
|
|
|
|
return notification
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-08-30 15:51:56 +01:00
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def sample_email_notification(notify_db, notify_db_session):
|
|
|
|
|
|
created_at = datetime.utcnow()
|
|
|
|
|
|
service = sample_service(notify_db, notify_db_session)
|
|
|
|
|
|
template = sample_email_template(notify_db, notify_db_session, service=service)
|
|
|
|
|
|
job = sample_job(notify_db, notify_db_session, service=service, template=template)
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
|
'template': template,
|
|
|
|
|
|
'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-08-02 14:23:47 +01:00
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def mock_statsd_inc(mocker):
|
|
|
|
|
|
return mocker.patch('app.statsd_client.incr')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def mock_statsd_timing(mocker):
|
|
|
|
|
|
return mocker.patch('app.statsd_client.timing')
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-26 11:00:03 +01:00
|
|
|
|
@pytest.fixture(scope='function')
|
2017-01-27 12:22:36 +00:00
|
|
|
|
def sample_notification_history(
|
|
|
|
|
|
notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
sample_template,
|
|
|
|
|
|
status='created',
|
|
|
|
|
|
created_at=None,
|
|
|
|
|
|
notification_type=None,
|
|
|
|
|
|
key_type=KEY_TYPE_NORMAL
|
|
|
|
|
|
):
|
2016-07-26 11:00:03 +01:00
|
|
|
|
if created_at is None:
|
|
|
|
|
|
created_at = datetime.utcnow()
|
|
|
|
|
|
|
2017-01-27 12:22:36 +00:00
|
|
|
|
if notification_type is None:
|
|
|
|
|
|
notification_type = sample_template.template_type
|
|
|
|
|
|
|
2016-07-26 11:00:03 +01:00
|
|
|
|
notification_history = NotificationHistory(
|
|
|
|
|
|
id=uuid.uuid4(),
|
|
|
|
|
|
service=sample_template.service,
|
|
|
|
|
|
template=sample_template,
|
|
|
|
|
|
template_version=sample_template.version,
|
|
|
|
|
|
status=status,
|
|
|
|
|
|
created_at=created_at,
|
2017-01-27 12:22:36 +00:00
|
|
|
|
notification_type=notification_type,
|
|
|
|
|
|
key_type=key_type
|
2016-07-26 11:00:03 +01:00
|
|
|
|
)
|
|
|
|
|
|
notify_db.session.add(notification_history)
|
|
|
|
|
|
notify_db.session.commit()
|
|
|
|
|
|
|
|
|
|
|
|
return notification_history
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-17 15:41:33 +00:00
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def mock_celery_send_sms_code(mocker):
|
|
|
|
|
|
return mocker.patch('app.celery.tasks.send_sms_code.apply_async')
|
2016-02-17 17:48:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
2016-03-17 13:06:17 +00:00
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def mock_celery_email_registration_verification(mocker):
|
|
|
|
|
|
return mocker.patch('app.celery.tasks.email_registration_verification.apply_async')
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-04-01 11:12:44 +01:00
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def mock_celery_send_email(mocker):
|
|
|
|
|
|
return mocker.patch('app.celery.tasks.send_email.apply_async')
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-18 09:52:27 +00:00
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def mock_encryption(mocker):
|
|
|
|
|
|
return mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
2016-02-25 11:22:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def sample_invited_user(notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
service=None,
|
|
|
|
|
|
to_email_address=None):
|
|
|
|
|
|
|
|
|
|
|
|
if service is None:
|
|
|
|
|
|
service = sample_service(notify_db, notify_db_session)
|
|
|
|
|
|
if to_email_address is None:
|
|
|
|
|
|
to_email_address = 'invited_user@digital.gov.uk'
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
|
'permissions': 'send_messages,manage_service,manage_api_keys'
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def sample_permission(notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
service=None,
|
|
|
|
|
|
user=None,
|
2016-03-02 15:34:26 +00:00
|
|
|
|
permission="manage_settings"):
|
2016-02-26 12:00:16 +00:00
|
|
|
|
if user is None:
|
2016-12-28 12:30:21 +00:00
|
|
|
|
user = create_user()
|
2016-02-26 12:00:16 +00:00
|
|
|
|
data = {
|
|
|
|
|
|
'user': user,
|
|
|
|
|
|
'permission': permission
|
|
|
|
|
|
}
|
2016-05-06 11:07:11 +01:00
|
|
|
|
if service is None:
|
|
|
|
|
|
service = sample_service(notify_db, notify_db_session)
|
2016-02-26 12:00:16 +00:00
|
|
|
|
if service:
|
|
|
|
|
|
data['service'] = service
|
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 12:00:16 +00:00
|
|
|
|
return p_model
|
2016-02-26 15:57:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def sample_service_permission(notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
service=None,
|
|
|
|
|
|
user=None,
|
2016-03-02 15:34:26 +00:00
|
|
|
|
permission="manage_settings"):
|
2016-02-26 15:57:24 +00:00
|
|
|
|
if user is None:
|
2016-12-28 12:30:21 +00:00
|
|
|
|
user = create_user()
|
2016-02-26 15:57:24 +00:00
|
|
|
|
if service is None:
|
2016-03-22 13:14:23 +00:00
|
|
|
|
service = sample_service(notify_db, notify_db_session, user=user)
|
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
|
|
|
|
|
|
|
|
|
|
|
2017-01-17 15:38:54 +00:00
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def current_sms_provider():
|
|
|
|
|
|
return ProviderDetails.query.filter_by(
|
|
|
|
|
|
notification_type='sms'
|
|
|
|
|
|
).order_by(
|
|
|
|
|
|
asc(ProviderDetails.priority)
|
|
|
|
|
|
).first()
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
2016-05-06 09:09:47 +01:00
|
|
|
|
def firetext_provider():
|
2017-01-17 15:38:54 +00:00
|
|
|
|
return ProviderDetails.query.filter_by(identifier='firetext').one()
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def sample_provider_statistics(notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
sample_service,
|
|
|
|
|
|
provider=None,
|
|
|
|
|
|
day=None,
|
|
|
|
|
|
unit_count=1):
|
2016-05-06 09:09:47 +01:00
|
|
|
|
|
2016-04-28 12:01:27 +01:00
|
|
|
|
if provider is None:
|
2016-05-06 09:09:47 +01:00
|
|
|
|
provider = ProviderDetails.query.filter_by(identifier='mmg').first()
|
2016-04-28 12:01:27 +01:00
|
|
|
|
if day is None:
|
|
|
|
|
|
day = date.today()
|
|
|
|
|
|
stats = ProviderStatistics(
|
|
|
|
|
|
service=sample_service,
|
2016-05-06 09:09:47 +01:00
|
|
|
|
provider_id=provider.id,
|
2016-04-28 12:01:27 +01:00
|
|
|
|
day=day,
|
|
|
|
|
|
unit_count=unit_count)
|
|
|
|
|
|
notify_db.session.add(stats)
|
|
|
|
|
|
notify_db.session.commit()
|
|
|
|
|
|
return stats
|
2016-05-05 10:45:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def sample_notification_statistics(notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
service=None,
|
|
|
|
|
|
day=None,
|
|
|
|
|
|
emails_requested=2,
|
|
|
|
|
|
emails_delivered=1,
|
|
|
|
|
|
emails_failed=1,
|
|
|
|
|
|
sms_requested=2,
|
|
|
|
|
|
sms_delivered=1,
|
|
|
|
|
|
sms_failed=1):
|
|
|
|
|
|
if service is None:
|
|
|
|
|
|
service = sample_service(notify_db, notify_db_session)
|
|
|
|
|
|
if day is None:
|
|
|
|
|
|
day = date.today()
|
|
|
|
|
|
stats = NotificationStatistics(
|
|
|
|
|
|
service=service,
|
|
|
|
|
|
day=day,
|
|
|
|
|
|
emails_requested=emails_requested,
|
|
|
|
|
|
emails_delivered=emails_delivered,
|
|
|
|
|
|
emails_failed=emails_failed,
|
|
|
|
|
|
sms_requested=sms_requested,
|
|
|
|
|
|
sms_delivered=sms_delivered,
|
|
|
|
|
|
sms_failed=sms_failed)
|
|
|
|
|
|
notify_db.session.add(stats)
|
|
|
|
|
|
notify_db.session.commit()
|
|
|
|
|
|
return stats
|
2016-06-01 15:59:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def mock_firetext_client(mocker, statsd_client=None):
|
|
|
|
|
|
client = FiretextClient()
|
|
|
|
|
|
statsd_client = statsd_client or mocker.Mock()
|
|
|
|
|
|
current_app = mocker.Mock(config={
|
|
|
|
|
|
'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)
|
|
|
|
|
|
return create_notify_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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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)
|
|
|
|
|
|
return create_notify_template(service=service,
|
|
|
|
|
|
user=user,
|
|
|
|
|
|
template_config_name='EMAIL_VERIFY_CODE_TEMPLATE_ID',
|
|
|
|
|
|
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',
|
|
|
|
|
|
return create_notify_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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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)
|
|
|
|
|
|
|
|
|
|
|
|
return create_notify_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')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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))"""
|
|
|
|
|
|
return create_notify_template(service=service, user=user,
|
|
|
|
|
|
template_config_name='ALREADY_REGISTERED_EMAIL_TEMPLATE_ID',
|
|
|
|
|
|
content=content,
|
|
|
|
|
|
template_type='email')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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))"""
|
|
|
|
|
|
template = create_notify_template(service=service,
|
|
|
|
|
|
user=user,
|
|
|
|
|
|
template_config_name='CHANGE_EMAIL_CONFIRMATION_TEMPLATE_ID',
|
|
|
|
|
|
content=content,
|
|
|
|
|
|
template_type='email')
|
|
|
|
|
|
return template
|
|
|
|
|
|
|
2016-06-16 17:34:33 +01:00
|
|
|
|
|
2016-10-12 13:06:39 +01:00
|
|
|
|
def create_notify_template(service, user, template_config_name, content, template_type, subject=None):
|
|
|
|
|
|
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-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:
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'id': current_app.config['NOTIFY_SERVICE_ID'],
|
|
|
|
|
|
'name': 'Notify Service',
|
|
|
|
|
|
'message_limit': 1000,
|
|
|
|
|
|
'active': True,
|
|
|
|
|
|
'restricted': False,
|
|
|
|
|
|
'email_from': 'notify.service',
|
|
|
|
|
|
'created_by': user
|
|
|
|
|
|
}
|
|
|
|
|
|
service = Service(**data)
|
|
|
|
|
|
db.session.add(service)
|
2016-10-12 13:06:39 +01:00
|
|
|
|
return service, user
|
2016-09-22 17:17:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def sample_service_whitelist(notify_db, notify_db_session, service=None, email_address=None, mobile_number=None):
|
|
|
|
|
|
if service is None:
|
|
|
|
|
|
service = sample_service(notify_db, notify_db_session)
|
|
|
|
|
|
|
2016-09-27 13:50:23 +01:00
|
|
|
|
if email_address:
|
|
|
|
|
|
whitelisted_user = ServiceWhitelist.from_string(service.id, EMAIL_TYPE, email_address)
|
|
|
|
|
|
elif mobile_number:
|
|
|
|
|
|
whitelisted_user = ServiceWhitelist.from_string(service.id, MOBILE_TYPE, mobile_number)
|
|
|
|
|
|
else:
|
|
|
|
|
|
whitelisted_user = ServiceWhitelist.from_string(service.id, EMAIL_TYPE, 'whitelisted_user@digital.gov.uk')
|
2016-09-22 17:17:34 +01:00
|
|
|
|
|
|
|
|
|
|
notify_db.session.add(whitelisted_user)
|
|
|
|
|
|
notify_db.session.commit()
|
|
|
|
|
|
return whitelisted_user
|
2016-11-18 15:09:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
|
|
def sample_provider_rate(notify_db, notify_db_session, valid_from=None, rate=None, provider_identifier=None):
|
|
|
|
|
|
create_provider_rates(
|
|
|
|
|
|
provider_identifier=provider_identifier if provider_identifier is not None else 'mmg',
|
|
|
|
|
|
valid_from=valid_from if valid_from is not None else datetime.utcnow(),
|
|
|
|
|
|
rate=rate if rate is not None else 1,
|
|
|
|
|
|
)
|
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()
|