Stuff was done.

Signed-off-by: Cliff Hill <Clifford.hill@gsa.gov>
This commit is contained in:
Cliff Hill
2024-02-13 14:48:10 -05:00
parent 63aa12e4ef
commit 3624cd812b
7 changed files with 490 additions and 244 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -7,7 +7,7 @@ from app.dao.notifications_dao import (
insert_notification_history_delete_notifications,
move_notifications_to_notification_history,
)
from app.enums import KeyType
from app.enums import KeyType, NotificationStatus, NotificationType, TemplateType
from app.models import Notification, NotificationHistory
from tests.app.db import (
create_notification,
@@ -23,23 +23,26 @@ def test_move_notifications_does_nothing_if_notification_history_row_already_exi
notification = create_notification(
template=sample_email_template,
created_at=datetime.utcnow() - timedelta(days=8),
status="temporary-failure",
status=NotificationStatus.TEMPORARY_FAILURE,
)
create_notification_history(
id=notification.id,
template=sample_email_template,
created_at=datetime.utcnow() - timedelta(days=8),
status="delivered",
status=NotificationStatus.DELIVERED,
)
move_notifications_to_notification_history(
"email", sample_email_template.service_id, datetime.utcnow(), 1
NotificationType.EMAIL,
sample_email_template.service_id,
datetime.utcnow(),
1,
)
assert Notification.query.count() == 0
history = NotificationHistory.query.all()
assert len(history) == 1
assert history[0].status == "delivered"
assert history[0].status == NotificationStatus.DELIVERED
def test_move_notifications_only_moves_notifications_older_than_provided_timestamp(
@@ -59,7 +62,9 @@ def test_move_notifications_only_moves_notifications_older_than_provided_timesta
old_notification_id = old_notification.id
result = move_notifications_to_notification_history(
"sms", sample_template.service_id, delete_time
NotificationType.SMS,
sample_template.service_id,
delete_time,
)
assert result == 1
@@ -78,12 +83,15 @@ def test_move_notifications_keeps_calling_until_no_more_to_delete_and_then_retur
timestamp = datetime(2021, 1, 1)
result = move_notifications_to_notification_history(
"sms", service_id, timestamp, qry_limit=5
NotificationType.SMS,
service_id,
timestamp,
qry_limit=5,
)
assert result == 11
mock_insert.asset_called_with(
notification_type="sms",
notification_type=NotificationType.SMS,
service_id=service_id,
timestamp_to_delete_backwards_from=timestamp,
qry_limit=5,
@@ -95,17 +103,19 @@ def test_move_notifications_only_moves_for_given_notification_type(sample_servic
delete_time = datetime(2020, 6, 1, 12)
one_second_before = delete_time - timedelta(seconds=1)
sms_template = create_template(sample_service, "sms")
email_template = create_template(sample_service, "email")
sms_template = create_template(sample_service, TemplateType.SMS)
email_template = create_template(sample_service, TemplateType.EMAIL)
create_notification(sms_template, created_at=one_second_before)
create_notification(email_template, created_at=one_second_before)
result = move_notifications_to_notification_history(
"sms", sample_service.id, delete_time
NotificationType.SMS,
sample_service.id,
delete_time,
)
assert result == 1
assert {x.notification_type for x in Notification.query} == {"email"}
assert NotificationHistory.query.one().notification_type == "sms"
assert {x.notification_type for x in Notification.query} == {NotificationType.EMAIL}
assert NotificationHistory.query.one().notification_type == NotificationType.SMS
def test_move_notifications_only_moves_for_given_service(notify_db_session):
@@ -115,13 +125,17 @@ def test_move_notifications_only_moves_for_given_service(notify_db_session):
service = create_service(service_name="service")
other_service = create_service(service_name="other")
template = create_template(service, "sms")
other_template = create_template(other_service, "sms")
template = create_template(service, TemplateType.SMS)
other_template = create_template(other_service, TemplateType.SMS)
create_notification(template, created_at=one_second_before)
create_notification(other_template, created_at=one_second_before)
result = move_notifications_to_notification_history("sms", service.id, delete_time)
result = move_notifications_to_notification_history(
NotificationType.SMS,
service.id,
delete_time,
)
assert result == 1
assert NotificationHistory.query.one().service_id == service.id
@@ -132,17 +146,25 @@ def test_move_notifications_just_deletes_test_key_notifications(sample_template)
delete_time = datetime(2020, 6, 1, 12)
one_second_before = delete_time - timedelta(seconds=1)
create_notification(
template=sample_template, created_at=one_second_before, key_type=KeyType.NORMAL
template=sample_template,
created_at=one_second_before,
key_type=KeyType.NORMAL,
)
create_notification(
template=sample_template, created_at=one_second_before, key_type=KeyType.TEAM
template=sample_template,
created_at=one_second_before,
key_type=KeyType.TEAM,
)
create_notification(
template=sample_template, created_at=one_second_before, key_type=KeyType.TEST
template=sample_template,
created_at=one_second_before,
key_type=KeyType.TEST,
)
result = move_notifications_to_notification_history(
"sms", sample_template.service_id, delete_time
NotificationType.SMS,
sample_template.service_id,
delete_time,
)
assert result == 2
@@ -163,58 +185,58 @@ def test_insert_notification_history_delete_notifications(sample_email_template)
n1 = create_notification(
template=sample_email_template,
created_at=datetime.utcnow() - timedelta(days=1, minutes=4),
status="delivered",
status=NotificationStatus.DELIVERED,
)
n2 = create_notification(
template=sample_email_template,
created_at=datetime.utcnow() - timedelta(days=1, minutes=20),
status="permanent-failure",
status=NotificationStatus.PERMANENT_FAILURE,
)
n3 = create_notification(
template=sample_email_template,
created_at=datetime.utcnow() - timedelta(days=1, minutes=30),
status="temporary-failure",
status=NotificationStatus.TEMPORARY_FAILURE,
)
n4 = create_notification(
template=sample_email_template,
created_at=datetime.utcnow() - timedelta(days=1, minutes=59),
status="temporary-failure",
status=NotificationStatus.TEMPORARY_FAILURE,
)
n5 = create_notification(
template=sample_email_template,
created_at=datetime.utcnow() - timedelta(days=1, hours=1),
status="sending",
status=NotificationStatus.SENDING,
)
n6 = create_notification(
template=sample_email_template,
created_at=datetime.utcnow() - timedelta(days=1, minutes=61),
status="pending",
status=NotificationStatus.PENDING,
)
n7 = create_notification(
template=sample_email_template,
created_at=datetime.utcnow() - timedelta(days=1, hours=1, seconds=1),
status="validation-failed",
status=NotificationStatus.VALIDATION_FAILED,
)
n8 = create_notification(
template=sample_email_template,
created_at=datetime.utcnow() - timedelta(days=1, minutes=20),
status="created",
status=NotificationStatus.CREATED,
)
# should NOT be deleted - wrong status
n9 = create_notification(
template=sample_email_template,
created_at=datetime.utcnow() - timedelta(hours=1),
status="delivered",
status=NotificationStatus.DELIVERED,
)
n10 = create_notification(
template=sample_email_template,
created_at=datetime.utcnow() - timedelta(hours=1),
status="technical-failure",
status=NotificationStatus.TECHNICAL_FAILURE,
)
n11 = create_notification(
template=sample_email_template,
created_at=datetime.utcnow() - timedelta(hours=23, minutes=59),
status="created",
status=NotificationStatus.CREATED,
)
ids_to_move = sorted([n1.id, n2.id, n3.id, n4.id, n5.id, n6.id, n7.id, n8.id])
@@ -239,17 +261,17 @@ def test_insert_notification_history_delete_notifications_more_notifications_tha
create_notification(
template=sample_template,
created_at=datetime.utcnow() + timedelta(minutes=4),
status="delivered",
status=NotificationStatus.DELIVERED,
)
create_notification(
template=sample_template,
created_at=datetime.utcnow() + timedelta(minutes=20),
status="permanent-failure",
status=NotificationStatus.PERMANENT_FAILURE,
)
create_notification(
template=sample_template,
created_at=datetime.utcnow() + timedelta(minutes=30),
status="temporary-failure",
status=NotificationStatus.TEMPORARY_FAILURE,
)
del_count = insert_notification_history_delete_notifications(
@@ -272,14 +294,16 @@ def test_insert_notification_history_delete_notifications_only_insert_delete_for
notification_to_move = create_notification(
template=sample_email_template,
created_at=datetime.utcnow() + timedelta(minutes=4),
status="delivered",
status=NotificationStatus.DELIVERED,
)
another_service = create_service(service_name="Another service")
another_template = create_template(service=another_service, template_type="email")
another_template = create_template(
service=another_service, template_type=TemplateType.EMAIL
)
notification_to_stay = create_notification(
template=another_template,
created_at=datetime.utcnow() + timedelta(minutes=4),
status="delivered",
status=NotificationStatus.DELIVERED,
)
del_count = insert_notification_history_delete_notifications(
@@ -303,20 +327,20 @@ def test_insert_notification_history_delete_notifications_insert_for_key_type(
create_notification(
template=sample_template,
created_at=datetime.utcnow() - timedelta(hours=4),
status="delivered",
key_type="normal",
status=NotificationStatus.DELIVERED,
key_type=KeyType.NORMAL,
)
create_notification(
template=sample_template,
created_at=datetime.utcnow() - timedelta(hours=4),
status="delivered",
key_type="team",
status=NotificationStatus.DELIVERED,
key_type=KeyType.TEAM,
)
with_test_key = create_notification(
template=sample_template,
created_at=datetime.utcnow() - timedelta(hours=4),
status="delivered",
key_type="test",
status=NotificationStatus.DELIVERED,
key_type=KeyType.TEST,
)
del_count = insert_notification_history_delete_notifications(

View File

@@ -12,6 +12,7 @@ from app.dao.templates_dao import (
dao_redact_template,
dao_update_template,
)
from app.enums import TemplateProcessType, TemplateType
from app.models import Template, TemplateHistory, TemplateRedacted
from tests.app.db import create_template
@@ -19,8 +20,8 @@ from tests.app.db import create_template
@pytest.mark.parametrize(
"template_type, subject",
[
("sms", None),
("email", "subject"),
(TemplateType.SMS, None),
(TemplateType.EMAIL, "subject"),
],
)
def test_create_template(sample_service, sample_user, template_type, subject):
@@ -43,7 +44,8 @@ def test_create_template(sample_service, sample_user, template_type, subject):
== "Sample Template"
)
assert (
dao_get_all_templates_for_service(sample_service.id)[0].process_type == "normal"
dao_get_all_templates_for_service(sample_service.id)[0].process_type
== TemplateProcessType.NORMAL
)
@@ -61,7 +63,7 @@ def test_create_template_creates_redact_entry(sample_service):
def test_update_template(sample_service, sample_user):
data = {
"name": "Sample Template",
"template_type": "sms",
"template_type": TemplateType.SMS,
"content": "Template content",
"service": sample_service,
"created_by": sample_user,
@@ -101,19 +103,19 @@ def test_get_all_templates_for_service(service_factory):
create_template(
service=service_1,
template_name="Sample Template 1",
template_type="sms",
template_type=TemplateType.SMS,
content="Template content",
)
create_template(
service=service_1,
template_name="Sample Template 2",
template_type="sms",
template_type=TemplateType.SMS,
content="Template content",
)
create_template(
service=service_2,
template_name="Sample Template 3",
template_type="sms",
template_type=TemplateType.SMS,
content="Template content",
)
@@ -125,19 +127,19 @@ def test_get_all_templates_for_service(service_factory):
def test_get_all_templates_for_service_is_alphabetised(sample_service):
create_template(
template_name="Sample Template 1",
template_type="sms",
template_type=TemplateType.SMS,
content="Template content",
service=sample_service,
)
template_2 = create_template(
template_name="Sample Template 2",
template_type="sms",
template_type=TemplateType.SMS,
content="Template content",
service=sample_service,
)
create_template(
template_name="Sample Template 3",
template_type="sms",
template_type=TemplateType.SMS,
content="Template content",
service=sample_service,
)
@@ -259,7 +261,7 @@ def test_create_template_creates_a_history_record_with_current_data(
assert TemplateHistory.query.count() == 0
data = {
"name": "Sample Template",
"template_type": "email",
"template_type": TemplateType.EMAIL,
"subject": "subject",
"content": "Template content",
"service": sample_service,
@@ -288,7 +290,7 @@ def test_update_template_creates_a_history_record_with_current_data(
assert TemplateHistory.query.count() == 0
data = {
"name": "Sample Template",
"template_type": "email",
"template_type": TemplateType.EMAIL,
"subject": "subject",
"content": "Template content",
"service": sample_service,

View File

@@ -3,7 +3,7 @@ from datetime import datetime, timedelta
from freezegun import freeze_time
from app.dao.uploads_dao import dao_get_uploads_by_service_id
from app.enums import JobStatus, TemplateType
from app.enums import JobStatus, NotificationStatus, NotificationType, TemplateType
from tests.app.db import (
create_job,
create_notification,
@@ -13,7 +13,12 @@ from tests.app.db import (
)
def create_uploaded_letter(letter_template, service, status="created", created_at=None):
def create_uploaded_letter(
letter_template,
service,
status=NotificationStatus.CREATED,
created_at=None,
):
return create_notification(
template=letter_template,
to_field="file-name",
@@ -39,7 +44,9 @@ def create_uploaded_template(service):
@freeze_time("2020-02-02 09:00") # GMT time
def test_get_uploads_for_service(sample_template):
create_service_data_retention(sample_template.service, "sms", days_of_retention=9)
create_service_data_retention(
sample_template.service, NotificationType.SMS, days_of_retention=9
)
job = create_job(sample_template, processing_started=datetime.utcnow())
other_service = create_service(service_name="other service")
@@ -55,7 +62,7 @@ def test_get_uploads_for_service(sample_template):
job.id,
job.original_file_name,
job.notification_count,
"sms",
TemplateType.SMS,
9,
job.created_at,
job.scheduled_for,

View File

@@ -1,5 +1,6 @@
import uuid
from datetime import datetime, timedelta
from types import CodeType
import pytest
from freezegun import freeze_time
@@ -137,7 +138,7 @@ def test_should_not_delete_verification_codes_less_than_one_day_old(sample_user)
def make_verify_code(user, age=None, expiry_age=None, code="12335", code_used=False):
verify_code = VerifyCode(
code_type="sms",
code_type=CodeType.SMS,
_code=code,
created_at=datetime.utcnow() - (age or timedelta(hours=0)),
expiry_datetime=datetime.utcnow() - (expiry_age or timedelta(0)),

View File

@@ -14,7 +14,7 @@ from app.dao import notifications_dao
from app.dao.provider_details_dao import get_provider_details_by_identifier
from app.delivery import send_to_providers
from app.delivery.send_to_providers import get_html_email_options, get_logo_url
from app.enums import BrandType, KeyType
from app.enums import BrandType, KeyType, NotificationStatus
from app.exceptions import NotificationTechnicalFailureException
from app.models import EmailBranding, Notification
from app.serialised_models import SerialisedService
@@ -78,7 +78,7 @@ def test_should_send_personalised_template_to_correct_sms_provider_and_persist(
db_notification = create_notification(
template=sample_sms_template_with_html,
personalisation={"name": "Jo"},
status="created",
status=NotificationStatus.CREATED,
reply_to_text=sample_sms_template_with_html.service.get_default_sms_sender(),
)
@@ -99,7 +99,7 @@ def test_should_send_personalised_template_to_correct_sms_provider_and_persist(
notification = Notification.query.filter_by(id=db_notification.id).one()
assert notification.status == "sending"
assert notification.status == NotificationStatus.SENDING
assert notification.sent_at <= datetime.utcnow()
assert notification.sent_by == "sns"
assert notification.billable_units == 1
@@ -137,7 +137,7 @@ def test_should_send_personalised_template_to_correct_email_provider_and_persist
)
notification = Notification.query.filter_by(id=db_notification.id).one()
assert notification.status == "sending"
assert notification.status == NotificationStatus.SENDING
assert notification.sent_at <= datetime.utcnow()
assert notification.sent_by == "ses"
assert notification.personalisation == {"name": "Jo"}
@@ -175,7 +175,7 @@ def test_send_sms_should_use_template_version_from_notification_not_latest(
db_notification = create_notification(
template=sample_template,
to_field="2028675309",
status="created",
status=NotificationStatus.CREATED,
reply_to_text=sample_template.service.get_default_sms_sender(),
normalised_to="2028675309",
)
@@ -217,7 +217,7 @@ def test_send_sms_should_use_template_version_from_notification_not_latest(
assert persisted_notification.template_id == expected_template_id
assert persisted_notification.template_version == version_on_notification
assert persisted_notification.template_version != t.version
assert persisted_notification.status == "sending"
assert persisted_notification.status == NotificationStatus.SENDING
assert not persisted_notification.personalisation
@@ -225,20 +225,20 @@ def test_should_have_sending_status_if_fake_callback_function_fails(
sample_notification, mocker
):
mocker.patch(
"app.delivery.send_to_providers.send_sms_response", side_effect=HTTPError
"app.delivery.send_to_providers.send_sms_response", side_effect=HTTPError,
)
sample_notification.key_type = KeyType.TEST
with pytest.raises(HTTPError):
send_to_providers.send_sms_to_provider(sample_notification)
assert sample_notification.status == "sending"
assert sample_notification.status == NotificationStatus.SENDING
assert sample_notification.sent_by == "sns"
def test_should_not_send_to_provider_when_status_is_not_created(
sample_template, mocker
):
notification = create_notification(template=sample_template, status="sending")
notification = create_notification(template=sample_template, status=NotificationStatus.SENDING,)
mocker.patch("app.aws_sns_client.send_sms")
response_mock = mocker.patch("app.delivery.send_to_providers.send_sms_response")
@@ -307,7 +307,7 @@ def test_send_email_to_provider_should_not_send_to_provider_when_status_is_not_c
mock_redis = mocker.patch("app.delivery.send_to_providers.redis_store")
mock_redis.get.return_value = "test@example.com".encode("utf-8")
notification = create_notification(template=sample_email_template, status="sending")
notification = create_notification(template=sample_email_template, status=NotificationStatus.SENDING)
mocker.patch("app.aws_ses_client.send_email")
mocker.patch("app.delivery.send_to_providers.send_email_response")
@@ -329,12 +329,10 @@ def test_send_email_should_use_service_reply_to_email(
)
create_reply_to_email(service=sample_service, email_address="foo@bar.com")
send_to_providers.send_email_to_provider(
db_notification,
)
send_to_providers.send_email_to_provider(db_notification)
app.aws_ses_client.send_email.assert_called_once_with(
ANY, ANY, ANY, body=ANY, html_body=ANY, reply_to_address="foo@bar.com"
ANY, ANY, ANY, body=ANY, html_body=ANY, reply_to_address="foo@bar.com",
)
@@ -393,7 +391,7 @@ def test_get_html_email_renderer_with_branding_details_and_render_govuk_banner_o
def test_get_html_email_renderer_prepends_logo_path(notify_api):
Service = namedtuple("Service", ["email_branding"])
EmailBranding = namedtuple(
"EmailBranding", ["brand_type", "colour", "name", "logo", "text"]
"EmailBranding", ["brand_type", "colour", "name", "logo", "text"],
)
email_branding = EmailBranding(
@@ -417,7 +415,7 @@ def test_get_html_email_renderer_prepends_logo_path(notify_api):
def test_get_html_email_renderer_handles_email_branding_without_logo(notify_api):
Service = namedtuple("Service", ["email_branding"])
EmailBranding = namedtuple(
"EmailBranding", ["brand_type", "colour", "name", "logo", "text"]
"EmailBranding", ["brand_type", "colour", "name", "logo", "text"],
)
email_branding = EmailBranding(
@@ -473,9 +471,9 @@ def test_get_logo_url_works_for_different_environments(base_url, expected_url):
@pytest.mark.parametrize(
"starting_status, expected_status",
[
("delivered", "delivered"),
("created", "sending"),
("technical-failure", "technical-failure"),
(NotificationStatus.DELIVERED, NotificationStatus.DELIVERED),
(NotificationStatus.CREATED, NotificationStatus.SENDING),
(NotificationStatus.TECHNICAL_FAILURE, NotificationStatus.TECHNICAL_FAILURE),
],
)
def test_update_notification_to_sending_does_not_update_status_from_a_final_status(
@@ -498,19 +496,19 @@ def __update_notification(notification_to_update, research_mode, expected_status
@pytest.mark.parametrize(
"research_mode,key_type, billable_units, expected_status",
[
(True, KeyType.NORMAL, 0, "delivered"),
(False, KeyType.NORMAL, 1, "sending"),
(False, KeyType.TEST, 0, "sending"),
(True, KeyType.TEST, 0, "sending"),
(True, KeyType.TEAM, 0, "delivered"),
(False, KeyType.TEAM, 1, "sending"),
(True, KeyType.NORMAL, 0, NotificationStatus.DELIVERED),
(False, KeyType.NORMAL, 1, NotificationStatus.SENDING),
(False, KeyType.TEST, 0, NotificationStatus.SENDING),
(True, KeyType.TEST, 0, NotificationStatus.SENDING),
(True, KeyType.TEAM, 0, NotificationStatus.DELIVERED),
(False, KeyType.TEAM, 1, NotificationStatus.SENDING),
],
)
def test_should_update_billable_units_and_status_according_to_research_mode_and_key_type(
sample_template, mocker, research_mode, key_type, billable_units, expected_status
):
notification = create_notification(
template=sample_template, billable_units=0, status="created", key_type=key_type
template=sample_template, billable_units=0, status=NotificationStatus.CREATED, key_type=key_type,
)
mocker.patch("app.aws_sns_client.send_sms")
mocker.patch(
@@ -557,7 +555,7 @@ def test_should_send_sms_to_international_providers(
template=sample_template,
to_field="+6011-17224412",
personalisation={"name": "Jo"},
status="created",
status=NotificationStatus.CREATED,
international=True,
reply_to_text=sample_template.service.get_default_sms_sender(),
normalised_to="601117224412",
@@ -576,7 +574,7 @@ def test_should_send_sms_to_international_providers(
international=True,
)
assert notification_international.status == "sending"
assert notification_international.status == NotificationStatus.SENDING
assert notification_international.sent_by == "sns"
@@ -625,15 +623,13 @@ def test_send_email_to_provider_uses_reply_to_from_notification(
mocker.patch("app.aws_ses_client.send_email", return_value="reference")
db_notification = create_notification(
template=sample_email_template, reply_to_text="test@test.com"
template=sample_email_template, reply_to_text="test@test.com",
)
send_to_providers.send_email_to_provider(
db_notification,
)
send_to_providers.send_email_to_provider(db_notification)
app.aws_ses_client.send_email.assert_called_once_with(
ANY, ANY, ANY, body=ANY, html_body=ANY, reply_to_address="test@test.com"
ANY, ANY, ANY, body=ANY, html_body=ANY, reply_to_address="test@test.com",
)

View File

@@ -3,6 +3,7 @@ from datetime import datetime, timedelta
import pytest
from freezegun import freeze_time
from app.enums import NotificationType
from tests.app.db import (
create_inbound_sms,
create_service,
@@ -131,7 +132,7 @@ def test_post_to_get_most_recent_inbound_sms_for_service_limits_to_a_week(
def test_post_to_get_inbound_sms_for_service_respects_data_retention(
admin_request, sample_service, days_of_retention, too_old_date, returned_date
):
create_service_data_retention(sample_service, "sms", days_of_retention)
create_service_data_retention(sample_service, NotificationType.SMS, days_of_retention,)
create_inbound_sms(sample_service, created_at=too_old_date)
returned_inbound = create_inbound_sms(sample_service, created_at=returned_date)