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.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
|
2023-07-10 11:06:29 -07:00
|
|
|
|
from app.dao.organization_dao import dao_create_organization
|
2021-03-10 13:55:06 +00:00
|
|
|
|
from app.dao.services_dao import dao_add_user_to_service, dao_create_service
|
2018-09-18 15:22:08 +01:00
|
|
|
|
from app.dao.templates_dao import dao_create_template
|
|
|
|
|
|
from app.dao.users_dao import create_secret_code, create_user_code
|
2024-01-16 15:12:57 -05:00
|
|
|
|
from app.enums import (
|
2024-02-26 20:07:23 -05:00
|
|
|
|
CodeType,
|
|
|
|
|
|
InvitedUserStatus,
|
2024-02-21 10:24:18 -05:00
|
|
|
|
JobStatus,
|
2024-01-16 15:12:57 -05:00
|
|
|
|
KeyType,
|
|
|
|
|
|
NotificationStatus,
|
2024-02-21 13:47:04 -05:00
|
|
|
|
PermissionType,
|
2024-01-16 15:12:57 -05:00
|
|
|
|
RecipientType,
|
|
|
|
|
|
ServicePermissionType,
|
2024-02-21 14:14:45 -05:00
|
|
|
|
TemplateProcessType,
|
2024-01-16 15:12:57 -05:00
|
|
|
|
TemplateType,
|
|
|
|
|
|
)
|
2018-09-18 15:22:08 +01:00
|
|
|
|
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
|
|
|
|
ApiKey,
|
2021-03-10 13:55:06 +00:00
|
|
|
|
InvitedUser,
|
2016-04-28 12:01:27 +01:00
|
|
|
|
Job,
|
|
|
|
|
|
Notification,
|
2016-07-26 11:00:03 +01:00
|
|
|
|
NotificationHistory,
|
2023-07-10 11:06:29 -07:00
|
|
|
|
Organization,
|
2016-04-28 12:01:27 +01:00
|
|
|
|
Permission,
|
2016-05-10 09:13:02 +01:00
|
|
|
|
ProviderDetails,
|
2016-12-19 16:53:23 +00:00
|
|
|
|
ProviderDetailsHistory,
|
2021-03-10 13:55:06 +00:00
|
|
|
|
Service,
|
2020-09-15 14:54:09 +01:00
|
|
|
|
ServiceEmailReplyTo,
|
2021-03-10 13:55:06 +00:00
|
|
|
|
ServiceGuestList,
|
|
|
|
|
|
Template,
|
|
|
|
|
|
TemplateHistory,
|
2017-11-27 12:30:50 +00:00
|
|
|
|
)
|
2024-05-23 13:59:51 -07:00
|
|
|
|
from app.utils import utc_now
|
2021-08-04 15:12:09 +01:00
|
|
|
|
from tests import create_admin_authorization_header
|
2017-08-04 19:10:22 +01:00
|
|
|
|
from tests.app.db import (
|
|
|
|
|
|
create_api_key,
|
2022-04-19 12:25:11 +01:00
|
|
|
|
create_email_branding,
|
2017-10-03 13:35:09 +01:00
|
|
|
|
create_inbound_number,
|
2018-02-19 17:14:01 +00:00
|
|
|
|
create_invited_org_user,
|
2021-03-10 13:55:06 +00:00
|
|
|
|
create_job,
|
|
|
|
|
|
create_notification,
|
|
|
|
|
|
create_service,
|
|
|
|
|
|
create_template,
|
|
|
|
|
|
create_user,
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-09-15 14:59:13 -07:00
|
|
|
|
def create_sample_notification(
|
|
|
|
|
|
notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
service=None,
|
|
|
|
|
|
template=None,
|
|
|
|
|
|
job=None,
|
|
|
|
|
|
job_row_number=None,
|
|
|
|
|
|
to_field=None,
|
2024-02-21 11:27:38 -05:00
|
|
|
|
status=NotificationStatus.CREATED,
|
2022-09-15 14:59:13 -07:00
|
|
|
|
provider_response=None,
|
|
|
|
|
|
reference=None,
|
|
|
|
|
|
created_at=None,
|
|
|
|
|
|
sent_at=None,
|
|
|
|
|
|
billable_units=1,
|
|
|
|
|
|
personalisation=None,
|
|
|
|
|
|
api_key=None,
|
2024-01-16 14:46:17 -05:00
|
|
|
|
key_type=KeyType.NORMAL,
|
2022-09-15 14:59:13 -07:00
|
|
|
|
sent_by=None,
|
|
|
|
|
|
international=False,
|
|
|
|
|
|
client_reference=None,
|
|
|
|
|
|
rate_multiplier=1.0,
|
|
|
|
|
|
scheduled_for=None,
|
|
|
|
|
|
normalised_to=None,
|
|
|
|
|
|
):
|
|
|
|
|
|
if created_at is None:
|
2024-05-23 13:59:51 -07:00
|
|
|
|
created_at = utc_now()
|
2022-09-15 14:59:13 -07:00
|
|
|
|
if service is None:
|
|
|
|
|
|
service = create_service(check_if_service_exists=True)
|
|
|
|
|
|
if template is None:
|
|
|
|
|
|
template = create_template(service=service)
|
|
|
|
|
|
|
|
|
|
|
|
if job is None and api_key is None:
|
|
|
|
|
|
# we didn't specify in test - lets create it
|
2023-08-29 14:54:30 -07:00
|
|
|
|
api_key = ApiKey.query.filter(
|
|
|
|
|
|
ApiKey.service == template.service, ApiKey.key_type == key_type
|
|
|
|
|
|
).first()
|
2022-09-15 14:59:13 -07:00
|
|
|
|
if not api_key:
|
|
|
|
|
|
api_key = create_api_key(template.service, key_type=key_type)
|
|
|
|
|
|
|
|
|
|
|
|
notification_id = uuid.uuid4()
|
|
|
|
|
|
|
|
|
|
|
|
if to_field:
|
|
|
|
|
|
to = to_field
|
|
|
|
|
|
else:
|
|
|
|
|
|
to = "+16502532222"
|
|
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
|
"id": notification_id,
|
|
|
|
|
|
"to": to,
|
|
|
|
|
|
"job_id": job.id if job else None,
|
|
|
|
|
|
"job": job,
|
|
|
|
|
|
"service_id": service.id,
|
|
|
|
|
|
"service": service,
|
|
|
|
|
|
"template_id": template.id,
|
|
|
|
|
|
"template_version": template.version,
|
|
|
|
|
|
"status": status,
|
|
|
|
|
|
"provider_response": provider_response,
|
|
|
|
|
|
"reference": reference,
|
|
|
|
|
|
"created_at": created_at,
|
|
|
|
|
|
"sent_at": sent_at,
|
|
|
|
|
|
"billable_units": billable_units,
|
|
|
|
|
|
"personalisation": personalisation,
|
|
|
|
|
|
"notification_type": template.template_type,
|
|
|
|
|
|
"api_key": api_key,
|
|
|
|
|
|
"api_key_id": api_key and api_key.id,
|
|
|
|
|
|
"key_type": api_key.key_type if api_key else key_type,
|
|
|
|
|
|
"sent_by": sent_by,
|
2024-02-26 20:07:23 -05:00
|
|
|
|
"updated_at": (
|
|
|
|
|
|
created_at if status in NotificationStatus.completed_types() else None
|
|
|
|
|
|
),
|
2022-09-15 14:59:13 -07:00
|
|
|
|
"client_reference": client_reference,
|
|
|
|
|
|
"rate_multiplier": rate_multiplier,
|
|
|
|
|
|
"normalised_to": normalised_to,
|
|
|
|
|
|
}
|
|
|
|
|
|
if job_row_number is not None:
|
|
|
|
|
|
data["job_row_number"] = job_row_number
|
|
|
|
|
|
notification = Notification(**data)
|
|
|
|
|
|
dao_create_notification(notification)
|
|
|
|
|
|
|
|
|
|
|
|
return notification
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.fixture(scope="function")
|
2021-09-14 09:37:26 +01:00
|
|
|
|
def service_factory(sample_user):
|
2016-02-16 14:06:56 +00:00
|
|
|
|
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:
|
2021-09-08 11:16:46 +01:00
|
|
|
|
user = sample_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
|
|
|
|
)
|
2024-02-21 10:24:18 -05:00
|
|
|
|
if template_type == TemplateType.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",
|
2024-02-09 12:24:09 -05:00
|
|
|
|
template_type=TemplateType.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
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.fixture(scope="function")
|
2016-12-28 12:30:21 +00:00
|
|
|
|
def sample_user(notify_db_session):
|
2023-08-29 14:54:30 -07:00
|
|
|
|
return create_user(email="notify@digital.fake.gov")
|
2016-01-21 17:29:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-11-06 13:31:03 -08:00
|
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
|
|
def sample_platform_admin(notify_db_session):
|
|
|
|
|
|
return create_user(email="notify_pa@digital.fake.gov", platform_admin=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.fixture(scope="function")
|
2018-02-23 13:30:35 +00:00
|
|
|
|
def notify_user(notify_db_session):
|
|
|
|
|
|
return create_user(
|
2023-08-16 07:19:18 -07:00
|
|
|
|
email="notify-service-user@digital.fake.gov",
|
2023-08-29 14:54:30 -07:00
|
|
|
|
id_=current_app.config["NOTIFY_USER_ID"],
|
2018-02-23 13:30:35 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.fixture(scope="function")
|
2019-11-01 14:34:15 +00:00
|
|
|
|
def sample_sms_code(notify_db_session):
|
2024-02-09 12:24:09 -05:00
|
|
|
|
code, txt_code = create_code(notify_db_session, code_type=CodeType.SMS)
|
2016-01-21 17:29:24 +00:00
|
|
|
|
code.txt_code = txt_code
|
|
|
|
|
|
return code
|
2016-01-08 12:18:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.fixture(scope="function")
|
2021-09-14 09:37:26 +01:00
|
|
|
|
def sample_service(sample_user):
|
2023-08-29 14:54:30 -07:00
|
|
|
|
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 = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"name": service_name,
|
|
|
|
|
|
"message_limit": 1000,
|
2023-08-31 10:28:44 -04:00
|
|
|
|
"total_message_limit": 250000,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"restricted": False,
|
|
|
|
|
|
"email_from": email_from,
|
|
|
|
|
|
"created_by": sample_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)
|
2023-11-06 11:49:17 -07:00
|
|
|
|
dao_create_service(service, sample_user, service_permissions=None)
|
2016-03-22 13:14:23 +00:00
|
|
|
|
else:
|
2021-09-08 11:16:46 +01:00
|
|
|
|
if sample_user not in service.users:
|
|
|
|
|
|
dao_add_user_to_service(service, sample_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
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.fixture(scope="function", name="sample_service_full_permissions")
|
2018-10-17 16:12:57 +01:00
|
|
|
|
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",
|
2024-01-10 09:48:32 -05:00
|
|
|
|
service_permissions=set(ServicePermissionType),
|
2020-10-20 18:53:59 +01:00
|
|
|
|
check_if_service_exists=True,
|
2017-09-21 11:50:49 +01:00
|
|
|
|
)
|
2023-08-29 14:54:30 -07:00
|
|
|
|
create_inbound_number("12345", service_id=service.id)
|
2017-10-03 13:35:09 +01:00
|
|
|
|
return service
|
2017-09-20 10:27:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.fixture(scope="function")
|
2021-09-14 09:37:26 +01:00
|
|
|
|
def sample_template(sample_user):
|
2023-08-29 14:54:30 -07:00
|
|
|
|
service = create_service(
|
2024-01-18 10:27:31 -05:00
|
|
|
|
service_permissions=[ServicePermissionType.EMAIL, ServicePermissionType.SMS],
|
|
|
|
|
|
check_if_service_exists=True,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
)
|
2017-02-14 17:59:18 +00:00
|
|
|
|
|
2017-02-24 13:39:58 +00:00
|
|
|
|
data = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"name": "Template Name",
|
2024-02-09 12:24:09 -05:00
|
|
|
|
"template_type": TemplateType.SMS,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"content": "This is a template:\nwith a newline",
|
|
|
|
|
|
"service": service,
|
|
|
|
|
|
"created_by": sample_user,
|
|
|
|
|
|
"archived": False,
|
|
|
|
|
|
"hidden": False,
|
2024-02-21 14:14:45 -05:00
|
|
|
|
"process_type": TemplateProcessType.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
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.fixture(scope="function")
|
2019-10-28 16:31:47 +00:00
|
|
|
|
def sample_template_without_sms_permission(notify_db_session):
|
2023-08-29 14:54:30 -07:00
|
|
|
|
service = create_service(
|
2024-02-28 12:41:57 -05:00
|
|
|
|
service_permissions=[ServicePermissionType.EMAIL], check_if_service_exists=True
|
2023-08-29 14:54:30 -07:00
|
|
|
|
)
|
2024-02-28 12:41:57 -05:00
|
|
|
|
return create_template(service, template_type=TemplateType.SMS)
|
2017-06-26 14:18:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07: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
|
2023-08-29 14:54:30 -07:00
|
|
|
|
return create_template(
|
|
|
|
|
|
sample_service, content="Hello (( Name))\nYour thing is due soon"
|
|
|
|
|
|
)
|
2016-02-29 11:23:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07: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
|
2023-08-29 14:54:30 -07:00
|
|
|
|
return create_template(
|
|
|
|
|
|
sample_service, content="Hello (( Name))\nHere is <em>some HTML</em> & entities"
|
|
|
|
|
|
)
|
2017-01-19 12:05:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.fixture(scope="function")
|
2021-09-14 09:37:26 +01:00
|
|
|
|
def sample_email_template(sample_user):
|
2023-08-29 14:54:30 -07:00
|
|
|
|
service = create_service(
|
|
|
|
|
|
user=sample_user,
|
2024-02-28 12:41:57 -05:00
|
|
|
|
service_permissions=[ServicePermissionType.EMAIL, ServicePermissionType.SMS],
|
2023-08-29 14:54:30 -07:00
|
|
|
|
check_if_service_exists=True,
|
|
|
|
|
|
)
|
2016-02-22 17:17:29 +00:00
|
|
|
|
data = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"name": "Email Template Name",
|
2024-02-28 12:41:57 -05:00
|
|
|
|
"template_type": TemplateType.EMAIL,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"content": "This is a template",
|
|
|
|
|
|
"service": service,
|
|
|
|
|
|
"created_by": sample_user,
|
|
|
|
|
|
"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
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.fixture(scope="function")
|
2019-10-28 16:31:47 +00:00
|
|
|
|
def sample_template_without_email_permission(notify_db_session):
|
2023-08-29 14:54:30 -07:00
|
|
|
|
service = create_service(
|
2024-02-28 12:41:57 -05:00
|
|
|
|
service_permissions=[ServicePermissionType.SMS], check_if_service_exists=True
|
2023-08-29 14:54:30 -07:00
|
|
|
|
)
|
2024-02-28 12:41:57 -05:00
|
|
|
|
return create_template(service, template_type=TemplateType.EMAIL)
|
2017-06-26 14:18:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07: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,
|
2024-02-28 12:41:57 -05:00
|
|
|
|
template_type=TemplateType.EMAIL,
|
2019-10-31 09:42:07 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07: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,
|
2024-02-28 12:41:57 -05:00
|
|
|
|
template_type=TemplateType.EMAIL,
|
2019-10-31 09:42:07 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07: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)
|
2023-08-29 14:54:30 -07:00
|
|
|
|
data = {
|
|
|
|
|
|
"service": service,
|
|
|
|
|
|
"name": uuid.uuid4(),
|
|
|
|
|
|
"created_by": service.created_by,
|
2024-01-16 14:46:17 -05:00
|
|
|
|
"key_type": KeyType.NORMAL,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07: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)
|
|
|
|
|
|
|
2024-01-16 14:46:17 -05:00
|
|
|
|
return create_api_key(service, key_type=KeyType.TEST)
|
2016-09-15 16:00:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07: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)
|
|
|
|
|
|
|
2024-01-16 14:46:17 -05:00
|
|
|
|
return create_api_key(service, key_type=KeyType.TEAM)
|
2016-09-15 16:00:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07: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 = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"id": uuid.uuid4(),
|
|
|
|
|
|
"service_id": service.id,
|
|
|
|
|
|
"service": service,
|
|
|
|
|
|
"template_id": template.id,
|
|
|
|
|
|
"template_version": template.version,
|
|
|
|
|
|
"original_file_name": "some.csv",
|
|
|
|
|
|
"notification_count": 1,
|
2024-05-23 13:59:51 -07:00
|
|
|
|
"created_at": utc_now(),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"created_by": service.created_by,
|
2024-02-21 10:24:18 -05:00
|
|
|
|
"job_status": JobStatus.PENDING,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"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
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.fixture(scope="function")
|
2016-03-09 07:27:26 +00:00
|
|
|
|
def sample_job_with_placeholdered_template(
|
2023-08-29 14:54:30 -07: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
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07: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,
|
2024-02-21 10:24:18 -05:00
|
|
|
|
job_status=JobStatus.SCHEDULED,
|
2024-05-23 13:59:51 -07:00
|
|
|
|
scheduled_for=(utc_now() + timedelta(minutes=60)).isoformat(),
|
2016-09-01 14:31:01 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07: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,
|
2024-02-21 10:24:18 -05:00
|
|
|
|
status=NotificationStatus.CREATED,
|
2016-09-15 16:00:04 +01:00
|
|
|
|
reference=None,
|
|
|
|
|
|
created_at=None,
|
|
|
|
|
|
sent_at=None,
|
|
|
|
|
|
billable_units=1,
|
|
|
|
|
|
personalisation=None,
|
2017-08-02 15:35:56 +01:00
|
|
|
|
api_key=None,
|
2024-01-16 14:46:17 -05:00
|
|
|
|
key_type=KeyType.NORMAL,
|
2016-09-15 16:00:04 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.fixture(scope="function")
|
2019-11-01 14:34:15 +00:00
|
|
|
|
def sample_notification(notify_db_session):
|
2024-05-23 13:59:51 -07:00
|
|
|
|
created_at = utc_now()
|
2019-11-01 14:34:15 +00:00
|
|
|
|
service = create_service(check_if_service_exists=True)
|
|
|
|
|
|
template = create_template(service=service)
|
2016-02-09 12:01:17 +00:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
api_key = ApiKey.query.filter(
|
2024-01-16 14:46:17 -05:00
|
|
|
|
ApiKey.service == template.service, ApiKey.key_type == KeyType.NORMAL
|
2023-08-29 14:54:30 -07:00
|
|
|
|
).first()
|
2019-11-01 14:34:15 +00:00
|
|
|
|
if not api_key:
|
2024-01-16 14:46:17 -05:00
|
|
|
|
api_key = create_api_key(template.service, key_type=KeyType.NORMAL)
|
2017-08-02 15:35:56 +01:00
|
|
|
|
|
2016-03-01 13:30:10 +00:00
|
|
|
|
notification_id = uuid.uuid4()
|
2023-08-29 14:54:30 -07:00
|
|
|
|
to = "+447700900855"
|
2016-02-09 12:01:17 +00:00
|
|
|
|
|
|
|
|
|
|
data = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"id": notification_id,
|
|
|
|
|
|
"to": to,
|
|
|
|
|
|
"job_id": None,
|
|
|
|
|
|
"job": None,
|
|
|
|
|
|
"service_id": service.id,
|
|
|
|
|
|
"service": service,
|
|
|
|
|
|
"template_id": template.id,
|
|
|
|
|
|
"template_version": template.version,
|
2024-02-21 11:27:38 -05:00
|
|
|
|
"status": NotificationStatus.CREATED,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"reference": None,
|
|
|
|
|
|
"created_at": created_at,
|
|
|
|
|
|
"sent_at": None,
|
|
|
|
|
|
"billable_units": 1,
|
|
|
|
|
|
"personalisation": None,
|
|
|
|
|
|
"notification_type": template.template_type,
|
|
|
|
|
|
"api_key": api_key,
|
|
|
|
|
|
"api_key_id": api_key and api_key.id,
|
|
|
|
|
|
"key_type": api_key.key_type,
|
|
|
|
|
|
"sent_by": None,
|
|
|
|
|
|
"updated_at": None,
|
|
|
|
|
|
"client_reference": None,
|
|
|
|
|
|
"rate_multiplier": 1.0,
|
|
|
|
|
|
"normalised_to": 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)
|
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
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.fixture(scope="function")
|
2019-10-29 14:33:01 +00:00
|
|
|
|
def sample_email_notification(notify_db_session):
|
2024-05-23 13:59:51 -07:00
|
|
|
|
created_at = utc_now()
|
2019-01-02 17:15:27 +00:00
|
|
|
|
service = create_service(check_if_service_exists=True)
|
2024-02-28 12:41:57 -05:00
|
|
|
|
template = create_template(service, template_type=TemplateType.EMAIL)
|
2019-10-29 14:33:01 +00:00
|
|
|
|
job = create_job(template)
|
2016-08-30 15:51:56 +01:00
|
|
|
|
|
|
|
|
|
|
notification_id = uuid.uuid4()
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
to = "foo@bar.com"
|
2016-08-30 15:51:56 +01:00
|
|
|
|
|
|
|
|
|
|
data = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"id": notification_id,
|
|
|
|
|
|
"to": to,
|
|
|
|
|
|
"job_id": job.id,
|
|
|
|
|
|
"job": job,
|
|
|
|
|
|
"service_id": service.id,
|
|
|
|
|
|
"service": service,
|
|
|
|
|
|
"template_id": template.id,
|
|
|
|
|
|
"template_version": template.version,
|
2024-02-21 11:27:38 -05:00
|
|
|
|
"status": NotificationStatus.CREATED,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"reference": None,
|
|
|
|
|
|
"created_at": created_at,
|
|
|
|
|
|
"billable_units": 0,
|
|
|
|
|
|
"personalisation": None,
|
|
|
|
|
|
"notification_type": template.template_type,
|
|
|
|
|
|
"api_key_id": None,
|
2024-01-16 14:46:17 -05:00
|
|
|
|
"key_type": KeyType.NORMAL,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"job_row_number": 1,
|
2016-08-30 15:51:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
notification = Notification(**data)
|
|
|
|
|
|
dao_create_notification(notification)
|
|
|
|
|
|
return notification
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.fixture(scope="function")
|
2022-05-03 17:00:51 +01:00
|
|
|
|
def sample_notification_history(notify_db_session, sample_template):
|
2024-05-23 13:59:51 -07:00
|
|
|
|
created_at = utc_now()
|
|
|
|
|
|
sent_at = utc_now()
|
2019-11-01 14:34:15 +00:00
|
|
|
|
notification_type = sample_template.template_type
|
2024-01-16 14:46:17 -05:00
|
|
|
|
api_key = create_api_key(sample_template.service, key_type=KeyType.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,
|
2024-02-21 10:24:18 -05:00
|
|
|
|
status=NotificationStatus.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,
|
2024-01-16 14:46:17 -05:00
|
|
|
|
key_type=KeyType.NORMAL,
|
2017-08-31 12:44:06 +01:00
|
|
|
|
api_key=api_key,
|
|
|
|
|
|
api_key_id=api_key and api_key.id,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
sent_at=sent_at,
|
2016-07-26 11:00:03 +01:00
|
|
|
|
)
|
2022-05-03 17:00:51 +01:00
|
|
|
|
notify_db_session.add(notification_history)
|
|
|
|
|
|
notify_db_session.commit()
|
2016-07-26 11:00:03 +01:00
|
|
|
|
|
|
|
|
|
|
return notification_history
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07: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)
|
2023-08-29 14:54:30 -07:00
|
|
|
|
to_email_address = "invited_user@digital.fake.gov"
|
2016-02-25 11:22:36 +00:00
|
|
|
|
|
|
|
|
|
|
from_user = service.users[0]
|
|
|
|
|
|
|
|
|
|
|
|
data = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"service": service,
|
|
|
|
|
|
"email_address": to_email_address,
|
|
|
|
|
|
"from_user": from_user,
|
|
|
|
|
|
"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
|
|
|
|
|
|
|
|
|
|
|
2023-12-07 16:26:25 -05:00
|
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
|
|
def sample_expired_user(notify_db_session):
|
|
|
|
|
|
service = create_service(check_if_service_exists=True)
|
|
|
|
|
|
to_email_address = "expired_user@digital.fake.gov"
|
|
|
|
|
|
|
|
|
|
|
|
from_user = service.users[0]
|
|
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
|
"service": service,
|
|
|
|
|
|
"email_address": to_email_address,
|
|
|
|
|
|
"from_user": from_user,
|
|
|
|
|
|
"permissions": "send_messages,manage_service,manage_api_keys",
|
|
|
|
|
|
"folder_permissions": ["folder_1_id", "folder_2_id"],
|
2024-05-23 13:59:51 -07:00
|
|
|
|
"created_at": utc_now() - timedelta(days=3),
|
2024-02-26 20:07:23 -05:00
|
|
|
|
"status": InvitedUserStatus.EXPIRED,
|
2023-12-07 16:26:25 -05:00
|
|
|
|
}
|
|
|
|
|
|
expired_user = InvitedUser(**data)
|
|
|
|
|
|
save_invited_user(expired_user)
|
|
|
|
|
|
return expired_user
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.fixture(scope="function")
|
2023-07-10 11:06:29 -07:00
|
|
|
|
def sample_invited_org_user(sample_user, sample_organization):
|
|
|
|
|
|
return create_invited_org_user(sample_organization, sample_user)
|
2018-02-19 17:14:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.fixture(scope="function")
|
2021-09-14 09:37:26 +01:00
|
|
|
|
def sample_user_service_permission(sample_user):
|
2021-09-08 11:16:46 +01:00
|
|
|
|
service = create_service(user=sample_user, check_if_service_exists=True)
|
2024-02-21 13:47:04 -05:00
|
|
|
|
permission = PermissionType.MANAGE_SETTINGS
|
2019-11-01 14:34:15 +00:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
data = {"user": sample_user, "service": service, "permission": permission}
|
2016-03-02 11:10:52 +00:00
|
|
|
|
p_model = Permission.query.filter_by(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
user=sample_user, service=service, permission=permission
|
|
|
|
|
|
).first()
|
2016-03-02 11:10:52 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.fixture(scope="function")
|
2016-04-08 13:34:46 +01:00
|
|
|
|
def fake_uuid():
|
|
|
|
|
|
return "6ce466d0-fd6a-11e5-82f5-e0accb9d11a6"
|
2016-04-21 11:37:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.fixture(scope="function")
|
2016-05-06 09:09:47 +01:00
|
|
|
|
def ses_provider():
|
2023-08-29 14:54:30 -07:00
|
|
|
|
return ProviderDetails.query.filter_by(identifier="ses").one()
|
2016-04-21 11:37:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.fixture(scope="function")
|
2022-12-19 10:48:53 -05:00
|
|
|
|
def sns_provider():
|
2023-08-29 14:54:30 -07:00
|
|
|
|
return ProviderDetails.query.filter_by(identifier="sns").one()
|
2016-04-28 12:01:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.fixture(scope="function")
|
2021-06-25 17:27:26 +01:00
|
|
|
|
def sms_code_template(notify_service):
|
2017-03-08 13:03:44 +00:00
|
|
|
|
return create_custom_template(
|
2021-06-25 17:27:26 +01:00
|
|
|
|
service=notify_service,
|
|
|
|
|
|
user=notify_service.users[0],
|
2023-08-29 14:54:30 -07:00
|
|
|
|
template_config_name="SMS_CODE_TEMPLATE_ID",
|
|
|
|
|
|
content="((verify_code))",
|
2024-02-09 12:24:09 -05:00
|
|
|
|
template_type=TemplateType.SMS,
|
2017-03-08 13:03:44 +00:00
|
|
|
|
)
|
2016-06-09 16:41:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.fixture(scope="function")
|
2021-06-25 17:27:26 +01:00
|
|
|
|
def email_2fa_code_template(notify_service):
|
2017-11-03 16:00:22 +00:00
|
|
|
|
return create_custom_template(
|
2021-06-25 17:27:26 +01:00
|
|
|
|
service=notify_service,
|
|
|
|
|
|
user=notify_service.users[0],
|
2023-08-29 14:54:30 -07:00
|
|
|
|
template_config_name="EMAIL_2FA_TEMPLATE_ID",
|
2017-11-03 16:00:22 +00:00
|
|
|
|
content=(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"Hi ((name)),"
|
|
|
|
|
|
""
|
|
|
|
|
|
"To sign in to GOV.UK Notify please open this link:"
|
|
|
|
|
|
"((url))"
|
2017-11-03 16:00:22 +00:00
|
|
|
|
),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
subject="Sign in to GOV.UK Notify",
|
2024-02-21 10:24:18 -05:00
|
|
|
|
template_type=TemplateType.EMAIL,
|
2017-11-03 16:00:22 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.fixture(scope="function")
|
2021-06-25 17:27:26 +01:00
|
|
|
|
def email_verification_template(notify_service):
|
2017-03-08 13:03:44 +00:00
|
|
|
|
return create_custom_template(
|
2021-06-25 17:27:26 +01:00
|
|
|
|
service=notify_service,
|
|
|
|
|
|
user=notify_service.users[0],
|
2023-08-29 14:54:30 -07:00
|
|
|
|
template_config_name="NEW_USER_EMAIL_VERIFICATION_TEMPLATE_ID",
|
|
|
|
|
|
content="((user_name)) use ((url)) to complete registration",
|
2024-02-21 10:24:18 -05:00
|
|
|
|
template_type=TemplateType.EMAIL,
|
2017-03-08 13:03:44 +00:00
|
|
|
|
)
|
2016-06-16 17:34:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.fixture(scope="function")
|
2021-06-25 17:27:26 +01:00
|
|
|
|
def invitation_email_template(notify_service):
|
2023-08-29 14:54:30 -07:00
|
|
|
|
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(
|
2021-06-25 17:27:26 +01:00
|
|
|
|
service=notify_service,
|
|
|
|
|
|
user=notify_service.users[0],
|
2023-08-29 14:54:30 -07:00
|
|
|
|
template_config_name="INVITATION_EMAIL_TEMPLATE_ID",
|
2017-03-08 13:03:44 +00:00
|
|
|
|
content=content,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
subject="Invitation to ((service_name))",
|
2024-02-21 10:24:18 -05:00
|
|
|
|
template_type=TemplateType.EMAIL,
|
2017-03-08 13:03:44 +00:00
|
|
|
|
)
|
2016-06-16 17:34:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.fixture(scope="function")
|
2021-06-25 17:27:26 +01:00
|
|
|
|
def org_invite_email_template(notify_service):
|
2018-02-19 17:14:01 +00:00
|
|
|
|
return create_custom_template(
|
2021-06-25 17:27:26 +01:00
|
|
|
|
service=notify_service,
|
|
|
|
|
|
user=notify_service.users[0],
|
2023-08-29 14:54:30 -07:00
|
|
|
|
template_config_name="ORGANIZATION_INVITATION_EMAIL_TEMPLATE_ID",
|
|
|
|
|
|
content="((user_name)) ((organization_name)) ((url))",
|
|
|
|
|
|
subject="Invitation to ((organization_name))",
|
2024-02-21 10:24:18 -05:00
|
|
|
|
template_type=TemplateType.EMAIL,
|
2018-02-19 17:14:01 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.fixture(scope="function")
|
2021-06-25 17:27:26 +01:00
|
|
|
|
def password_reset_email_template(notify_service):
|
2017-03-08 13:03:44 +00:00
|
|
|
|
return create_custom_template(
|
2021-06-25 17:27:26 +01:00
|
|
|
|
service=notify_service,
|
|
|
|
|
|
user=notify_service.users[0],
|
2023-08-29 14:54:30 -07:00
|
|
|
|
template_config_name="PASSWORD_RESET_TEMPLATE_ID",
|
|
|
|
|
|
content="((user_name)) you can reset password by clicking ((url))",
|
|
|
|
|
|
subject="Reset your password",
|
2024-02-21 10:24:18 -05:00
|
|
|
|
template_type=TemplateType.EMAIL,
|
2017-03-08 13:03:44 +00:00
|
|
|
|
)
|
2016-07-07 17:23:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.fixture(scope="function")
|
2021-06-25 17:27:26 +01:00
|
|
|
|
def verify_reply_to_address_email_template(notify_service):
|
2019-05-10 15:32:24 +01:00
|
|
|
|
return create_custom_template(
|
2021-06-25 17:27:26 +01:00
|
|
|
|
service=notify_service,
|
|
|
|
|
|
user=notify_service.users[0],
|
2023-08-29 14:54:30 -07:00
|
|
|
|
template_config_name="REPLY_TO_EMAIL_ADDRESS_VERIFICATION_TEMPLATE_ID",
|
2019-05-10 15:32:24 +01:00
|
|
|
|
content="Hi,This address has been provided as the reply-to email address so we are verifying if it's working",
|
2023-08-29 14:54:30 -07:00
|
|
|
|
subject="Your GOV.UK Notify reply-to email address",
|
2024-02-21 10:24:18 -05:00
|
|
|
|
template_type=TemplateType.EMAIL,
|
2019-05-10 15:32:24 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.fixture(scope="function")
|
2021-06-25 17:27:26 +01:00
|
|
|
|
def team_member_email_edit_template(notify_service):
|
2019-02-26 16:25:04 +00:00
|
|
|
|
return create_custom_template(
|
2021-06-25 17:27:26 +01:00
|
|
|
|
service=notify_service,
|
|
|
|
|
|
user=notify_service.users[0],
|
2023-08-29 14:54:30 -07:00
|
|
|
|
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",
|
2024-02-21 10:24:18 -05:00
|
|
|
|
template_type=TemplateType.EMAIL,
|
2019-02-26 16:25:04 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.fixture(scope="function")
|
2021-06-25 17:27:26 +01:00
|
|
|
|
def team_member_mobile_edit_template(notify_service):
|
2019-02-26 16:25:04 +00:00
|
|
|
|
return create_custom_template(
|
2021-06-25 17:27:26 +01:00
|
|
|
|
service=notify_service,
|
|
|
|
|
|
user=notify_service.users[0],
|
2023-08-29 14:54:30 -07:00
|
|
|
|
template_config_name="TEAM_MEMBER_EDIT_MOBILE_TEMPLATE_ID",
|
|
|
|
|
|
content="Your mobile number was changed by ((servicemanagername)).",
|
2024-02-09 12:24:09 -05:00
|
|
|
|
template_type=TemplateType.SMS,
|
2019-02-26 16:25:04 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.fixture(scope="function")
|
2021-06-25 17:27:26 +01:00
|
|
|
|
def already_registered_template(notify_service):
|
2016-10-12 13:06:39 +01:00
|
|
|
|
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(
|
2021-06-25 17:27:26 +01:00
|
|
|
|
service=notify_service,
|
|
|
|
|
|
user=notify_service.users[0],
|
2023-08-29 14:54:30 -07:00
|
|
|
|
template_config_name="ALREADY_REGISTERED_EMAIL_TEMPLATE_ID",
|
2017-03-08 13:03:44 +00:00
|
|
|
|
content=content,
|
2024-02-21 10:24:18 -05:00
|
|
|
|
template_type=TemplateType.EMAIL,
|
2017-03-08 13:03:44 +00:00
|
|
|
|
)
|
2016-10-12 13:06:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.fixture(scope="function")
|
2021-06-25 17:27:26 +01:00
|
|
|
|
def change_email_confirmation_template(notify_service):
|
2016-10-12 13:06:39 +01:00
|
|
|
|
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(
|
2021-06-25 17:27:26 +01:00
|
|
|
|
service=notify_service,
|
|
|
|
|
|
user=notify_service.users[0],
|
2023-08-29 14:54:30 -07:00
|
|
|
|
template_config_name="CHANGE_EMAIL_CONFIRMATION_TEMPLATE_ID",
|
2017-03-08 13:03:44 +00:00
|
|
|
|
content=content,
|
2024-02-21 10:24:18 -05:00
|
|
|
|
template_type=TemplateType.EMAIL,
|
2017-03-08 13:03:44 +00:00
|
|
|
|
)
|
2016-10-12 13:06:39 +01:00
|
|
|
|
return template
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.fixture(scope="function")
|
2021-06-25 17:27:26 +01:00
|
|
|
|
def mou_signed_templates(notify_service):
|
2019-07-08 12:33:53 +01:00
|
|
|
|
import importlib
|
2023-08-29 14:54:30 -07:00
|
|
|
|
|
|
|
|
|
|
alembic_script = importlib.import_module(
|
|
|
|
|
|
"migrations.versions.0298_add_mou_signed_receipt"
|
|
|
|
|
|
)
|
2019-07-08 12:33:53 +01:00
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
config_name: create_custom_template(
|
2021-06-25 17:27:26 +01:00
|
|
|
|
notify_service,
|
|
|
|
|
|
notify_service.users[0],
|
2019-07-08 12:33:53 +01:00
|
|
|
|
config_name,
|
2024-02-21 10:24:18 -05:00
|
|
|
|
TemplateType.EMAIL,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
content="\n".join(
|
2019-07-08 12:33:53 +01:00
|
|
|
|
next(
|
|
|
|
|
|
x
|
|
|
|
|
|
for x in alembic_script.templates
|
2023-08-29 14:54:30 -07:00
|
|
|
|
if x["id"] == current_app.config[config_name]
|
|
|
|
|
|
)["content_lines"]
|
2019-07-08 12:33:53 +01:00
|
|
|
|
),
|
|
|
|
|
|
)
|
|
|
|
|
|
for config_name in [
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"MOU_SIGNER_RECEIPT_TEMPLATE_ID",
|
|
|
|
|
|
"MOU_SIGNED_ON_BEHALF_SIGNER_RECEIPT_TEMPLATE_ID",
|
|
|
|
|
|
"MOU_SIGNED_ON_BEHALF_ON_BEHALF_RECEIPT_TEMPLATE_ID",
|
2019-07-08 12:33:53 +01:00
|
|
|
|
]
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07: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])
|
|
|
|
|
|
if not template:
|
|
|
|
|
|
data = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"id": current_app.config[template_config_name],
|
|
|
|
|
|
"name": template_config_name,
|
|
|
|
|
|
"template_type": template_type,
|
|
|
|
|
|
"content": content,
|
|
|
|
|
|
"service": service,
|
|
|
|
|
|
"created_by": user,
|
|
|
|
|
|
"subject": subject,
|
|
|
|
|
|
"archived": False,
|
2016-10-12 13:06:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
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-10-12 13:06:39 +01:00
|
|
|
|
return template
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-06-25 17:27:26 +01:00
|
|
|
|
@pytest.fixture
|
2022-05-03 17:00:51 +01:00
|
|
|
|
def notify_service(notify_db_session, sample_user):
|
2023-08-29 14:54:30 -07:00
|
|
|
|
service = Service.query.get(current_app.config["NOTIFY_SERVICE_ID"])
|
2016-07-07 17:23:07 +01:00
|
|
|
|
if not service:
|
2017-11-27 12:30:50 +00:00
|
|
|
|
service = Service(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
name="Notify Service",
|
2017-11-27 12:30:50 +00:00
|
|
|
|
message_limit=1000,
|
|
|
|
|
|
restricted=False,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
email_from="notify.service",
|
2021-09-08 11:16:46 +01:00
|
|
|
|
created_by=sample_user,
|
2017-11-27 12:30:50 +00:00
|
|
|
|
prefix_sms=False,
|
|
|
|
|
|
)
|
2017-12-01 16:31:21 +00:00
|
|
|
|
dao_create_service(
|
|
|
|
|
|
service=service,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
service_id=current_app.config["NOTIFY_SERVICE_ID"],
|
|
|
|
|
|
user=sample_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 = {
|
2023-08-29 14:54:30 -07: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)
|
|
|
|
|
|
|
2022-05-03 17:00:51 +01:00
|
|
|
|
notify_db_session.add(reply_to)
|
|
|
|
|
|
notify_db_session.commit()
|
2017-11-27 12:30:50 +00:00
|
|
|
|
|
2021-06-25 17:27:26 +01:00
|
|
|
|
return service
|
2016-09-22 17:17:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.fixture(scope="function")
|
2022-05-03 17:00:51 +01:00
|
|
|
|
def sample_service_guest_list(notify_db_session):
|
2019-11-01 14:34:15 +00:00
|
|
|
|
service = create_service(check_if_service_exists=True)
|
2023-08-29 14:54:30 -07:00
|
|
|
|
guest_list_user = ServiceGuestList.from_string(
|
2024-01-16 14:46:17 -05:00
|
|
|
|
service.id, RecipientType.EMAIL, "guest_list_user@digital.fake.gov"
|
2023-08-29 14:54:30 -07:00
|
|
|
|
)
|
2016-09-22 17:17:34 +01:00
|
|
|
|
|
2022-05-03 17:00:51 +01:00
|
|
|
|
notify_db_session.add(guest_list_user)
|
|
|
|
|
|
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):
|
2023-08-29 14:54:30 -07: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()
|
2023-08-29 14:54:30 -07:00
|
|
|
|
inbound_numbers.append(create_inbound_number(number="1", provider="sns"))
|
|
|
|
|
|
inbound_numbers.append(
|
|
|
|
|
|
create_inbound_number(
|
2024-02-21 10:24:45 -05:00
|
|
|
|
number="2",
|
|
|
|
|
|
provider="sns",
|
|
|
|
|
|
active=False,
|
|
|
|
|
|
service_id=service.id,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
)
|
|
|
|
|
|
)
|
2017-08-04 12:13:10 +01:00
|
|
|
|
return inbound_numbers
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-02-10 01:34:37 +00:00
|
|
|
|
@pytest.fixture
|
2023-07-10 11:06:29 -07:00
|
|
|
|
def sample_organization(notify_db_session):
|
2023-08-29 14:54:30 -07:00
|
|
|
|
org = Organization(name="sample organization")
|
2023-07-10 11:06:29 -07:00
|
|
|
|
dao_create_organization(org)
|
2018-02-10 01:34:37 +00:00
|
|
|
|
return org
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-04-19 12:25:11 +01:00
|
|
|
|
@pytest.fixture
|
|
|
|
|
|
def nhs_email_branding(notify_db_session):
|
|
|
|
|
|
# we wipe email_branding table in test db between the tests, so we have to recreate this branding
|
|
|
|
|
|
# that is normally present on all environments and applied through migration
|
2023-08-29 14:54:30 -07:00
|
|
|
|
nhs_email_branding_id = current_app.config["NHS_EMAIL_BRANDING_ID"]
|
2022-04-19 12:25:11 +01:00
|
|
|
|
|
|
|
|
|
|
return create_email_branding(
|
|
|
|
|
|
id=nhs_email_branding_id,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
logo="1ac6f483-3105-4c9e-9017-dd7fb2752c44-nhs-blue_x2.png",
|
|
|
|
|
|
name="NHS",
|
2022-04-19 12:25:11 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-12-19 16:53:23 +00:00
|
|
|
|
@pytest.fixture
|
2022-05-03 17:00:51 +01:00
|
|
|
|
def restore_provider_details(notify_db_session):
|
2016-12-19 16:53:23 +00:00
|
|
|
|
"""
|
|
|
|
|
|
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
|
|
|
|
|
|
ProviderDetails.query.delete()
|
|
|
|
|
|
ProviderDetailsHistory.query.delete()
|
2022-05-03 17:00:51 +01:00
|
|
|
|
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):
|
|
|
|
|
|
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 {})),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[create_admin_authorization_header()],
|
2017-05-22 15:05:05 +01:00
|
|
|
|
)
|
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),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[
|
|
|
|
|
|
("Content-Type", "application/json"),
|
|
|
|
|
|
create_admin_authorization_header(),
|
|
|
|
|
|
],
|
2017-05-22 15:05:05 +01:00
|
|
|
|
)
|
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 {})),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[create_admin_authorization_header()],
|
2017-05-22 15:05:05 +01:00
|
|
|
|
)
|
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)
|