From 3624cd812bc613508495b610338bdd60841bdc91 Mon Sep 17 00:00:00 2001 From: Cliff Hill Date: Tue, 13 Feb 2024 14:48:10 -0500 Subject: [PATCH] Stuff was done. Signed-off-by: Cliff Hill --- .../notification_dao/test_notification_dao.py | 515 +++++++++++++----- ...t_notification_dao_delete_notifications.py | 110 ++-- tests/app/dao/test_templates_dao.py | 26 +- tests/app/dao/test_uploads_dao.py | 15 +- tests/app/dao/test_users_dao.py | 3 +- tests/app/delivery/test_send_to_providers.py | 62 +-- tests/app/inbound_sms/test_rest.py | 3 +- 7 files changed, 490 insertions(+), 244 deletions(-) diff --git a/tests/app/dao/notification_dao/test_notification_dao.py b/tests/app/dao/notification_dao/test_notification_dao.py index b45940ac6..849adfb45 100644 --- a/tests/app/dao/notification_dao/test_notification_dao.py +++ b/tests/app/dao/notification_dao/test_notification_dao.py @@ -28,7 +28,13 @@ from app.dao.notifications_dao import ( update_notification_status_by_id, update_notification_status_by_reference, ) -from app.enums import JobStatus, KeyType, NotificationStatus, NotificationType +from app.enums import ( + JobStatus, + KeyType, + NotificationStatus, + NotificationType, + TemplateType, +) from app.models import Job, Notification, NotificationHistory from tests.app.db import ( create_ft_notification_status, @@ -43,18 +49,22 @@ from tests.app.db import ( def test_should_by_able_to_update_status_by_reference( sample_email_template, ses_provider ): - data = _notification_json(sample_email_template, status="sending") + data = _notification_json(sample_email_template, status=NotificationStatus.SENDING) notification = Notification(**data) dao_create_notification(notification) - assert Notification.query.get(notification.id).status == "sending" + assert Notification.query.get(notification.id).status == NotificationStatus.SENDING notification.reference = "reference" dao_update_notification(notification) - updated = update_notification_status_by_reference("reference", "delivered") - assert updated.status == "delivered" - assert Notification.query.get(notification.id).status == "delivered" + updated = update_notification_status_by_reference( + "reference", NotificationStatus.DELIVERED + ) + assert updated.status == NotificationStatus.DELIVERED + assert ( + Notification.query.get(notification.id).status == NotificationStatus.DELIVERED + ) def test_should_by_able_to_update_status_by_id( @@ -62,33 +72,48 @@ def test_should_by_able_to_update_status_by_id( ): with freeze_time("2000-01-01 12:00:00"): data = _notification_json( - sample_template, job_id=sample_job.id, status="sending" + sample_template, + job_id=sample_job.id, + status=NotificationStatus.SENDING, ) notification = Notification(**data) dao_create_notification(notification) - assert notification.status == "sending" + assert notification.status == NotificationStatus.SENDING - assert Notification.query.get(notification.id).status == "sending" + assert Notification.query.get(notification.id).status == NotificationStatus.SENDING with freeze_time("2000-01-02 12:00:00"): - updated = update_notification_status_by_id(notification.id, "delivered") + updated = update_notification_status_by_id( + notification.id, + NotificationStatus.DELIVERED, + ) - assert updated.status == "delivered" + assert updated.status == NotificationStatus.DELIVERED assert updated.updated_at == datetime(2000, 1, 2, 12, 0, 0) - assert Notification.query.get(notification.id).status == "delivered" + assert ( + Notification.query.get(notification.id).status == NotificationStatus.DELIVERED + ) assert notification.updated_at == datetime(2000, 1, 2, 12, 0, 0) - assert notification.status == "delivered" + assert notification.status == NotificationStatus.DELIVERED def test_should_not_update_status_by_id_if_not_sending_and_does_not_update_job( sample_job, ): notification = create_notification( - template=sample_job.template, status="delivered", job=sample_job + template=sample_job.template, + status=NotificationStatus.DELIVERED, + job=sample_job, + ) + assert ( + Notification.query.get(notification.id).status == NotificationStatus.DELIVERED + ) + assert not update_notification_status_by_id( + notification.id, NotificationStatus.FAILED + ) + assert ( + Notification.query.get(notification.id).status == NotificationStatus.DELIVERED ) - assert Notification.query.get(notification.id).status == "delivered" - assert not update_notification_status_by_id(notification.id, "failed") - assert Notification.query.get(notification.id).status == "delivered" assert sample_job == Job.query.get(notification.job_id) @@ -97,30 +122,49 @@ def test_should_not_update_status_by_reference_if_not_sending_and_does_not_updat ): notification = create_notification( template=sample_job.template, - status="delivered", + status=NotificationStatus.DELIVERED, reference="reference", job=sample_job, ) - assert Notification.query.get(notification.id).status == "delivered" - assert not update_notification_status_by_reference("reference", "failed") - assert Notification.query.get(notification.id).status == "delivered" + assert ( + Notification.query.get(notification.id).status == NotificationStatus.DELIVERED + ) + assert not update_notification_status_by_reference( + "reference", NotificationStatus.FAILED + ) + assert ( + Notification.query.get(notification.id).status == NotificationStatus.DELIVERED + ) assert sample_job == Job.query.get(notification.job_id) def test_should_update_status_by_id_if_created(sample_template, sample_notification): - assert Notification.query.get(sample_notification.id).status == "created" - updated = update_notification_status_by_id(sample_notification.id, "failed") - assert Notification.query.get(sample_notification.id).status == "failed" - assert updated.status == "failed" + assert ( + Notification.query.get(sample_notification.id).status + == NotificationStatus.CREATED + ) + updated = update_notification_status_by_id( + sample_notification.id, + NotificationStatus.FAILED, + ) + assert ( + Notification.query.get(sample_notification.id).status + == NotificationStatus.FAILED + ) + assert updated.status == NotificationStatus.FAILED def test_should_update_status_by_id_and_set_sent_by(sample_template): - notification = create_notification(template=sample_template, status="sending") + notification = create_notification( + template=sample_template, status=NotificationStatus.SENDING + ) updated = update_notification_status_by_id( - notification.id, "delivered", sent_by="sns" + notification.id, + NotificationStatus.DELIVERED, + sent_by="sns", ) - assert updated.status == "delivered" + assert updated.status == NotificationStatus.DELIVERED assert updated.sent_by == "sns" @@ -131,7 +175,7 @@ def test_should_not_update_status_by_reference_if_from_country_with_no_delivery_ sample_template, status=NotificationStatus.SENT, reference="foo" ) - res = update_notification_status_by_reference("foo", "failed") + res = update_notification_status_by_reference("foo", NotificationStatus.FAILED) assert res is None assert notification.status == NotificationStatus.SENT @@ -147,7 +191,9 @@ def test_should_not_update_status_by_id_if_sent_to_country_with_unknown_delivery phone_prefix="249", # sudan has no delivery receipts (or at least, that we know about) ) - res = update_notification_status_by_id(notification.id, "delivered") + res = update_notification_status_by_id( + notification.id, NotificationStatus.DELIVERED + ) assert res is None assert notification.status == NotificationStatus.SENT @@ -163,7 +209,10 @@ def test_should_not_update_status_by_id_if_sent_to_country_with_carrier_delivery phone_prefix="1", # americans only have carrier delivery receipts ) - res = update_notification_status_by_id(notification.id, "delivered") + res = update_notification_status_by_id( + notification.id, + NotificationStatus.DELIVERED, + ) assert res is None assert notification.status == NotificationStatus.SENT @@ -179,7 +228,10 @@ def test_should_not_update_status_by_id_if_sent_to_country_with_delivery_receipt phone_prefix="7", # russians have full delivery receipts ) - res = update_notification_status_by_id(notification.id, "delivered") + res = update_notification_status_by_id( + notification.id, + NotificationStatus.DELIVERED, + ) assert res == notification assert notification.status == NotificationStatus.DELIVERED @@ -187,11 +239,15 @@ def test_should_not_update_status_by_id_if_sent_to_country_with_delivery_receipt def test_should_not_update_status_by_reference_if_not_sending(sample_template): notification = create_notification( - template=sample_template, status="created", reference="reference" + template=sample_template, + status=NotificationStatus.CREATED, + reference="reference", ) - assert Notification.query.get(notification.id).status == "created" - updated = update_notification_status_by_reference("reference", "failed") - assert Notification.query.get(notification.id).status == "created" + assert Notification.query.get(notification.id).status == NotificationStatus.CREATED + updated = update_notification_status_by_reference( + "reference", NotificationStatus.FAILED + ) + assert Notification.query.get(notification.id).status == NotificationStatus.CREATED assert not updated @@ -199,72 +255,119 @@ def test_should_by_able_to_update_status_by_id_from_pending_to_delivered( sample_template, sample_job ): notification = create_notification( - template=sample_template, job=sample_job, status="sending" + template=sample_template, + job=sample_job, + status=NotificationStatus.SENDING, ) assert update_notification_status_by_id( - notification_id=notification.id, status="pending" + notification_id=notification.id, status=NotificationStatus.PENDING ) - assert Notification.query.get(notification.id).status == "pending" + assert Notification.query.get(notification.id).status == NotificationStatus.PENDING - assert update_notification_status_by_id(notification.id, "delivered") - assert Notification.query.get(notification.id).status == "delivered" + assert update_notification_status_by_id( + notification.id, + NotificationStatus.DELIVERED, + ) + assert ( + Notification.query.get(notification.id).status == NotificationStatus.DELIVERED + ) def test_should_by_able_to_update_status_by_id_from_pending_to_temporary_failure( sample_template, sample_job ): notification = create_notification( - template=sample_template, job=sample_job, status="sending", sent_by="sns" + template=sample_template, + job=sample_job, + status=NotificationStatus.SENDING, + sent_by="sns", ) assert update_notification_status_by_id( - notification_id=notification.id, status="pending" + notification_id=notification.id, + status=NotificationStatus.PENDING, ) - assert Notification.query.get(notification.id).status == "pending" + assert Notification.query.get(notification.id).status == NotificationStatus.PENDING - assert update_notification_status_by_id(notification.id, status="permanent-failure") + assert update_notification_status_by_id( + notification.id, + status=NotificationStatus.PERMANENT_FAILURE, + ) - assert Notification.query.get(notification.id).status == "temporary-failure" + assert ( + Notification.query.get(notification.id).status + == NotificationStatus.TEMPORARY_FAILURE + ) def test_should_by_able_to_update_status_by_id_from_sending_to_permanent_failure( sample_template, sample_job ): - data = _notification_json(sample_template, job_id=sample_job.id, status="sending") + data = _notification_json( + sample_template, + job_id=sample_job.id, + status=NotificationStatus.SENDING, + ) notification = Notification(**data) dao_create_notification(notification) - assert Notification.query.get(notification.id).status == "sending" + assert Notification.query.get(notification.id).status == NotificationStatus.SENDING - assert update_notification_status_by_id(notification.id, status="permanent-failure") - assert Notification.query.get(notification.id).status == "permanent-failure" + assert update_notification_status_by_id( + notification.id, + status=NotificationStatus.PERMANENT_FAILURE, + ) + assert ( + Notification.query.get(notification.id).status + == NotificationStatus.PERMANENT_FAILURE + ) def test_should_not_update_status_once_notification_status_is_delivered( sample_email_template, ): - notification = create_notification(template=sample_email_template, status="sending") - assert Notification.query.get(notification.id).status == "sending" + notification = create_notification( + template=sample_email_template, + status=NotificationStatus.SENDING, + ) + assert Notification.query.get(notification.id).status == NotificationStatus.SENDING notification.reference = "reference" dao_update_notification(notification) - update_notification_status_by_reference("reference", "delivered") - assert Notification.query.get(notification.id).status == "delivered" + update_notification_status_by_reference( + "reference", + NotificationStatus.DELIVERED, + ) + assert ( + Notification.query.get(notification.id).status == NotificationStatus.DELIVERED + ) - update_notification_status_by_reference("reference", "failed") - assert Notification.query.get(notification.id).status == "delivered" + update_notification_status_by_reference( + "reference", + NotificationStatus.FAILED, + ) + assert ( + Notification.query.get(notification.id).status == NotificationStatus.DELIVERED + ) def test_should_return_zero_count_if_no_notification_with_id(): - assert not update_notification_status_by_id(str(uuid.uuid4()), "delivered") + assert not update_notification_status_by_id( + str(uuid.uuid4()), + NotificationStatus.DELIVERED, + ) def test_should_return_zero_count_if_no_notification_with_reference(): - assert not update_notification_status_by_reference("something", "delivered") + assert not update_notification_status_by_reference( + "something", + NotificationStatus.DELIVERED, + ) def test_create_notification_creates_notification_with_personalisation( - sample_template_with_placeholders, sample_job + sample_template_with_placeholders, + sample_job, ): assert Notification.query.count() == 0 @@ -272,7 +375,7 @@ def test_create_notification_creates_notification_with_personalisation( template=sample_template_with_placeholders, job=sample_job, personalisation={"name": "Jo"}, - status="created", + status=NotificationStatus.CREATED, ) assert Notification.query.count() == 1 @@ -284,7 +387,7 @@ def test_create_notification_creates_notification_with_personalisation( assert data.template == notification_from_db.template assert data.template_version == notification_from_db.template_version assert data.created_at == notification_from_db.created_at - assert notification_from_db.status == "created" + assert notification_from_db.status == NotificationStatus.CREATED assert {"name": "Jo"} == notification_from_db.personalisation @@ -305,7 +408,7 @@ def test_save_notification_creates_sms(sample_template, sample_job): assert data["template_id"] == notification_from_db.template_id assert data["template_version"] == notification_from_db.template_version assert data["created_at"] == notification_from_db.created_at - assert notification_from_db.status == "created" + assert notification_from_db.status == NotificationStatus.CREATED def test_save_notification_and_create_email(sample_email_template, sample_job): @@ -325,7 +428,7 @@ def test_save_notification_and_create_email(sample_email_template, sample_job): assert data["template_id"] == notification_from_db.template_id assert data["template_version"] == notification_from_db.template_version assert data["created_at"] == notification_from_db.created_at - assert notification_from_db.status == "created" + assert notification_from_db.status == NotificationStatus.CREATED def test_save_notification(sample_email_template, sample_job): @@ -363,10 +466,10 @@ def test_update_notification_with_research_mode_service_does_not_create_or_updat assert Notification.query.count() == 1 assert NotificationHistory.query.count() == 0 - notification.status = "delivered" + notification.status = NotificationStatus.DELIVERED dao_update_notification(notification) - assert Notification.query.one().status == "delivered" + assert Notification.query.one().status == NotificationStatus.DELIVERED assert NotificationHistory.query.count() == 0 @@ -402,7 +505,7 @@ def test_save_notification_and_increment_job(sample_template, sample_job, sns_pr assert data["template_id"] == notification_from_db.template_id assert data["template_version"] == notification_from_db.template_version assert data["created_at"] == notification_from_db.created_at - assert notification_from_db.status == "created" + assert notification_from_db.status == NotificationStatus.CREATED notification_2 = Notification(**data) dao_create_notification(notification_2) @@ -428,7 +531,7 @@ def test_save_notification_and_increment_correct_job(sample_template, sns_provid assert data["template_id"] == notification_from_db.template_id assert data["template_version"] == notification_from_db.template_version assert data["created_at"] == notification_from_db.created_at - assert notification_from_db.status == "created" + assert notification_from_db.status == NotificationStatus.CREATED assert job_1.id != job_2.id @@ -447,13 +550,18 @@ def test_save_notification_with_no_job(sample_template, sns_provider): assert data["template_id"] == notification_from_db.template_id assert data["template_version"] == notification_from_db.template_version assert data["created_at"] == notification_from_db.created_at - assert notification_from_db.status == "created" + assert notification_from_db.status == NotificationStatus.CREATED def test_get_notification_with_personalisation_by_id(sample_template): - notification = create_notification(template=sample_template, status="created") + notification = create_notification( + template=sample_template, + status=NotificationStatus.CREATED, + ) notification_from_db = get_notification_with_personalisation( - sample_template.service.id, notification.id, key_type=None + sample_template.service.id, + notification.id, + key_type=None, ) assert notification == notification_from_db @@ -507,7 +615,7 @@ def test_save_notification_no_job_id(sample_template): assert data["service"] == notification_from_db.service assert data["template_id"] == notification_from_db.template_id assert data["template_version"] == notification_from_db.template_version - assert notification_from_db.status == "created" + assert notification_from_db.status == NotificationStatus.CREATED assert data.get("job_id") is None @@ -572,11 +680,11 @@ def test_dao_get_notification_count_for_job_id_returns_zero_for_no_notifications def test_update_notification_sets_status(sample_notification): - assert sample_notification.status == "created" - sample_notification.status = "failed" + assert sample_notification.status == NotificationStatus.CREATED + sample_notification.status = NotificationStatus.FAILED dao_update_notification(sample_notification) notification_from_db = Notification.query.get(sample_notification.id) - assert notification_from_db.status == "failed" + assert notification_from_db.status == NotificationStatus.FAILED @freeze_time("2016-01-10") @@ -589,7 +697,9 @@ def test_should_limit_notifications_return_by_day_limit_plus_one(sample_template past_date = "2016-01-{0:02d} 12:00:00".format(i) with freeze_time(past_date): create_notification( - sample_template, created_at=datetime.utcnow(), status="failed" + sample_template, + created_at=datetime.utcnow(), + status=NotificationStatus.FAILED, ) all_notifications = Notification.query.all() @@ -680,32 +790,56 @@ def _notification_json(sample_template, job_id=None, id=None, status=None): def test_dao_timeout_notifications(sample_template): with freeze_time(datetime.utcnow() - timedelta(minutes=2)): - created = create_notification(sample_template, status="created") - sending = create_notification(sample_template, status="sending") - pending = create_notification(sample_template, status="pending") - delivered = create_notification(sample_template, status="delivered") + created = create_notification( + sample_template, + status=NotificationStatus.CREATED, + ) + sending = create_notification( + sample_template, + status=NotificationStatus.SENDING, + ) + pending = create_notification( + sample_template, + status=NotificationStatus.PENDING, + ) + delivered = create_notification( + sample_template, + status=NotificationStatus.DELIVERED, + ) temporary_failure_notifications = dao_timeout_notifications(datetime.utcnow()) assert len(temporary_failure_notifications) == 2 - assert Notification.query.get(created.id).status == "created" - assert Notification.query.get(sending.id).status == "temporary-failure" - assert Notification.query.get(pending.id).status == "temporary-failure" - assert Notification.query.get(delivered.id).status == "delivered" + assert Notification.query.get(created.id).status == NotificationStatus.CREATED + assert ( + Notification.query.get(sending.id).status + == NotificationStatus.TEMPORARY_FAILURE + ) + assert ( + Notification.query.get(pending.id).status + == NotificationStatus.TEMPORARY_FAILURE + ) + assert Notification.query.get(delivered.id).status == NotificationStatus.DELIVERED def test_dao_timeout_notifications_only_updates_for_older_notifications( sample_template, ): with freeze_time(datetime.utcnow() + timedelta(minutes=10)): - sending = create_notification(sample_template, status="sending") - pending = create_notification(sample_template, status="pending") + sending = create_notification( + sample_template, + status=NotificationStatus.SENDING, + ) + pending = create_notification( + sample_template, + status=NotificationStatus.PENDING, + ) temporary_failure_notifications = dao_timeout_notifications(datetime.utcnow()) assert len(temporary_failure_notifications) == 0 - assert Notification.query.get(sending.id).status == "sending" - assert Notification.query.get(pending.id).status == "pending" + assert Notification.query.get(sending.id).status == NotificationStatus.SENDING + assert Notification.query.get(pending.id).status == NotificationStatus.PENDING def test_should_return_notifications_excluding_jobs_by_default( @@ -821,7 +955,9 @@ def test_get_notifications_with_a_live_api_key_type( sample_job, sample_api_key, sample_team_api_key, sample_test_api_key ): create_notification( - template=sample_job.template, created_at=datetime.utcnow(), job=sample_job + template=sample_job.template, + created_at=datetime.utcnow(), + job=sample_job, ) create_notification( template=sample_job.template, @@ -885,13 +1021,18 @@ def test_get_notifications_with_a_test_api_key_type( # only those created with test API key, no jobs all_notifications = get_notifications_for_service( - sample_job.service_id, limit_days=1, key_type=KeyType.TEST + sample_job.service_id, + limit_days=1, + key_type=KeyType.TEST, ).items assert len(all_notifications) == 1 # only those created with test API key, no jobs, even when requested all_notifications = get_notifications_for_service( - sample_job.service_id, limit_days=1, include_jobs=True, key_type=KeyType.TEST + sample_job.service_id, + limit_days=1, + include_jobs=True, + key_type=KeyType.TEST, ).items assert len(all_notifications) == 1 @@ -900,7 +1041,9 @@ def test_get_notifications_with_a_team_api_key_type( sample_job, sample_api_key, sample_team_api_key, sample_test_api_key ): create_notification( - template=sample_job.template, created_at=datetime.utcnow(), job=sample_job + template=sample_job.template, + created_at=datetime.utcnow(), + job=sample_job, ) create_notification( template=sample_job.template, @@ -923,13 +1066,18 @@ def test_get_notifications_with_a_team_api_key_type( # only those created with team API key, no jobs all_notifications = get_notifications_for_service( - sample_job.service_id, limit_days=1, key_type=KeyType.TEAM + sample_job.service_id, + limit_days=1, + key_type=KeyType.TEAM, ).items assert len(all_notifications) == 1 # only those created with team API key, no jobs, even when requested all_notifications = get_notifications_for_service( - sample_job.service_id, limit_days=1, include_jobs=True, key_type=KeyType.TEAM + sample_job.service_id, + limit_days=1, + include_jobs=True, + key_type=KeyType.TEAM, ).items assert len(all_notifications) == 1 @@ -938,7 +1086,9 @@ def test_should_exclude_test_key_notifications_by_default( sample_job, sample_api_key, sample_team_api_key, sample_test_api_key ): create_notification( - template=sample_job.template, created_at=datetime.utcnow(), job=sample_job + template=sample_job.template, + created_at=datetime.utcnow(), + job=sample_job, ) create_notification( @@ -969,12 +1119,16 @@ def test_should_exclude_test_key_notifications_by_default( assert len(all_notifications) == 2 all_notifications = get_notifications_for_service( - sample_job.service_id, limit_days=1, include_jobs=True + sample_job.service_id, + limit_days=1, + include_jobs=True, ).items assert len(all_notifications) == 3 all_notifications = get_notifications_for_service( - sample_job.service_id, limit_days=1, key_type=KeyType.TEST + sample_job.service_id, + limit_days=1, + key_type=KeyType.TEST, ).items assert len(all_notifications) == 1 @@ -989,10 +1143,13 @@ def test_dao_get_notifications_by_recipient(sample_template): } notification1 = create_notification( - template=sample_template, **recipient_to_search_for + template=sample_template, + **recipient_to_search_for, ) create_notification( - template=sample_template, key_type=KeyType.TEST, **recipient_to_search_for + template=sample_template, + key_type=KeyType.TEST, + **recipient_to_search_for, ) create_notification( template=sample_template, @@ -1008,7 +1165,7 @@ def test_dao_get_notifications_by_recipient(sample_template): results = dao_get_notifications_by_recipient_or_reference( notification1.service_id, recipient_to_search_for["to_field"], - notification_type="sms", + notification_type=NotificationType.SMS, ) assert len(results.items) == 1 @@ -1029,7 +1186,7 @@ def test_dao_get_notifications_by_recipient_is_limited_to_50_results(sample_temp results = dao_get_notifications_by_recipient_or_reference( sample_template.service_id, "447700900855", - notification_type="sms", + notification_type=NotificationType.SMS, page_size=50, ) @@ -1049,7 +1206,9 @@ def test_dao_get_notifications_by_recipient_is_not_case_sensitive( normalised_to="jack@gmail.com", ) results = dao_get_notifications_by_recipient_or_reference( - notification.service_id, search_term, notification_type="email" + notification.service_id, + search_term, + notification_type=NotificationType.EMAIL, ) notification_ids = [notification.id for notification in results.items] @@ -1074,7 +1233,9 @@ def test_dao_get_notifications_by_recipient_matches_partial_emails( normalised_to="jacque@gmail.com", ) results = dao_get_notifications_by_recipient_or_reference( - notification_1.service_id, "ack", notification_type="email" + notification_1.service_id, + "ack", + notification_type=NotificationType.EMAIL, ) notification_ids = [notification.id for notification in results.items] @@ -1128,7 +1289,7 @@ def test_dao_get_notifications_by_recipient_escapes( dao_get_notifications_by_recipient_or_reference( sample_email_template.service_id, search_term, - notification_type="email", + notification_type=NotificationType.EMAIL, ).items ) == expected_result_count @@ -1181,7 +1342,7 @@ def test_dao_get_notifications_by_reference_escapes_special_character( dao_get_notifications_by_recipient_or_reference( sample_email_template.service_id, search_term, - notification_type="email", + notification_type=NotificationType.EMAIL, ).items ) == expected_result_count @@ -1219,7 +1380,9 @@ def test_dao_get_notifications_by_recipient_matches_partial_phone_numbers( normalised_to="+12026785000", ) results = dao_get_notifications_by_recipient_or_reference( - notification_1.service_id, search_term, notification_type="sms" + notification_1.service_id, + search_term, + notification_type=NotificationType.SMS, ) notification_ids = [notification.id for notification in results.items] @@ -1242,7 +1405,9 @@ def test_dao_get_notifications_by_recipient_accepts_invalid_phone_numbers_and_em normalised_to="test@example.com", ) results = dao_get_notifications_by_recipient_or_reference( - notification.service_id, to, notification_type="email" + notification.service_id, + to, + notification_type=NotificationType.EMAIL, ) assert len(results.items) == 0 @@ -1271,7 +1436,9 @@ def test_dao_get_notifications_by_recipient_ignores_spaces(sample_template): ) results = dao_get_notifications_by_recipient_or_reference( - notification1.service_id, "+447700900855", notification_type="sms" + notification1.service_id, + "+447700900855", + notification_type=NotificationType.SMS, ) notification_ids = [notification.id for notification in results.items] @@ -1299,9 +1466,11 @@ def test_dao_get_notifications_by_recipient_searches_across_notification_types( ): service = create_service() sms_template = create_template(service=service) - email_template = create_template(service=service, template_type="email") + email_template = create_template(service=service, template_type=TemplateType.EMAIL) sms = create_notification( - template=sms_template, to_field="202-867-5309", normalised_to="+12028675309" + template=sms_template, + to_field="202-867-5309", + normalised_to="+12028675309", ) email = create_notification( template=email_template, @@ -1310,13 +1479,17 @@ def test_dao_get_notifications_by_recipient_searches_across_notification_types( ) results = dao_get_notifications_by_recipient_or_reference( - service.id, phone_search, notification_type="sms" + service.id, + phone_search, + notification_type=NotificationType.SMS, ) assert len(results.items) == 1 assert results.items[0].id == sms.id results = dao_get_notifications_by_recipient_or_reference( - service.id, email_search, notification_type="email" + service.id, + email_search, + notification_type=NotificationType.EMAIL, ) assert len(results.items) == 1 assert results.items[0].id == email.id @@ -1333,7 +1506,10 @@ def test_dao_get_notifications_by_recipient_searches_across_notification_types( def test_dao_get_notifications_by_reference(notify_db_session): service = create_service() sms_template = create_template(service=service) - email_template = create_template(service=service, template_type="email") + email_template = create_template( + service=service, + template_type=TemplateType.EMAIL, + ) sms = create_notification( template=sms_template, to_field="07711111111", @@ -1364,42 +1540,56 @@ def test_dao_get_notifications_by_reference(notify_db_session): assert results.items[0].id == email.id results = dao_get_notifications_by_recipient_or_reference( - service.id, "077", notification_type="sms" + service.id, + "077", + notification_type=NotificationType.SMS, ) assert len(results.items) == 1 assert results.items[0].id == sms.id results = dao_get_notifications_by_recipient_or_reference( - service.id, "77", notification_type="sms" + service.id, + "77", + notification_type=NotificationType.SMS, ) assert len(results.items) == 1 assert results.items[0].id == sms.id results = dao_get_notifications_by_recipient_or_reference( - service.id, "Aa", notification_type="sms" + service.id, + "Aa", + notification_type=NotificationType.SMS, ) assert len(results.items) == 1 assert results.items[0].id == sms.id results = dao_get_notifications_by_recipient_or_reference( - service.id, "bB", notification_type="sms" + service.id, + "bB", + notification_type=NotificationType.SMS, ) assert len(results.items) == 0 results = dao_get_notifications_by_recipient_or_reference( - service.id, "77", notification_type="email" + service.id, + "77", + notification_type=NotificationType.EMAIL, ) assert len(results.items) == 1 assert results.items[0].id == email.id results = dao_get_notifications_by_recipient_or_reference( - service.id, "Bb", notification_type="email" + service.id, + "Bb", + notification_type=NotificationType.EMAIL, ) assert len(results.items) == 1 assert results.items[0].id == email.id results = dao_get_notifications_by_recipient_or_reference( - service.id, "aA", notification_type="email" + service.id, + "aA", + notification_type=NotificationType.EMAIL, ) assert len(results.items) == 0 @@ -1412,20 +1602,20 @@ def test_dao_get_notifications_by_to_field_filters_status(sample_template): template=sample_template, to_field="+447700900855", normalised_to="447700900855", - status="delivered", + status=NotificationStatus.DELIVERED, ) create_notification( template=sample_template, to_field="+447700900855", normalised_to="447700900855", - status="temporary-failure", + status=NotificationStatus.TEMPORARY_FAILURE, ) notifications = dao_get_notifications_by_recipient_or_reference( notification.service_id, "+447700900855", - statuses=["delivered"], - notification_type="sms", + statuses=[NotificationStatus.DELIVERED], + notification_type=NotificationStatus.SMS, ) assert len(notifications.items) == 1 @@ -1440,20 +1630,20 @@ def test_dao_get_notifications_by_to_field_filters_multiple_statuses(sample_temp template=sample_template, to_field="+447700900855", normalised_to="447700900855", - status="delivered", + status=NotificationStatus.DELIVERED, ) notification2 = create_notification( template=sample_template, to_field="+447700900855", normalised_to="447700900855", - status="sending", + status=NotificationStatus.SENDING, ) notifications = dao_get_notifications_by_recipient_or_reference( notification1.service_id, "+447700900855", - statuses=["delivered", "sending"], - notification_type="sms", + statuses=[NotificationStatus.DELIVERED, NotificationStatus.SENDING], + notification_type=NotificationType.SMS, ) notification_ids = [notification.id for notification in notifications.items] @@ -1472,17 +1662,19 @@ def test_dao_get_notifications_by_to_field_returns_all_if_no_status_filter( template=sample_template, to_field="+447700900855", normalised_to="447700900855", - status="delivered", + status=NotificationStatus.DELIVERED, ) notification2 = create_notification( template=sample_template, to_field="+447700900855", normalised_to="447700900855", - status="temporary-failure", + status=NotificationStatus.TEMPORARY_FAILURE, ) notifications = dao_get_notifications_by_recipient_or_reference( - notification1.service_id, "+447700900855", notification_type="sms" + notification1.service_id, + "+447700900855", + notification_type=NotificationType.SMS, ) notification_ids = [notification.id for notification in notifications.items] @@ -1509,7 +1701,9 @@ def test_dao_get_notifications_by_to_field_orders_by_created_at_desc(sample_temp notification = notification(created_at=datetime.utcnow()) notifications = dao_get_notifications_by_recipient_or_reference( - sample_template.service_id, "+447700900855", notification_type="sms" + sample_template.service_id, + "+447700900855", + notification_type=NotificationType.SMS, ) assert len(notifications.items) == 2 @@ -1556,15 +1750,15 @@ def test_dao_update_notifications_by_reference_updated_notifications(sample_temp updated_count, updated_history_count = dao_update_notifications_by_reference( references=["ref1", "ref2"], - update_dict={"status": "delivered", "billable_units": 2}, + update_dict={"status": NotificationStatus.DELIVERED, "billable_units": 2}, ) assert updated_count == 2 updated_1 = Notification.query.get(notification_1.id) assert updated_1.billable_units == 2 - assert updated_1.status == "delivered" + assert updated_1.status == NotificationStatus.DELIVERED updated_2 = Notification.query.get(notification_2.id) assert updated_2.billable_units == 2 - assert updated_2.status == "delivered" + assert updated_2.status == NotificationStatus.DELIVERED assert updated_history_count == 0 @@ -1577,7 +1771,7 @@ def test_dao_update_notifications_by_reference_updates_history_some_notification updated_count, updated_history_count = dao_update_notifications_by_reference( references=["ref1", "ref2"], - update_dict={"status": "delivered", "billable_units": 2}, + update_dict={"status": NotificationStatus.DELIVERED, "billable_units": 2}, ) assert updated_count == 1 assert updated_history_count == 1 @@ -1591,7 +1785,7 @@ def test_dao_update_notifications_by_reference_updates_history_no_notifications_ updated_count, updated_history_count = dao_update_notifications_by_reference( references=["ref1", "ref2"], - update_dict={"status": "delivered", "billable_units": 2}, + update_dict={"status": NotificationStatus.DELIVERED, "billable_units": 2}, ) assert updated_count == 0 assert updated_history_count == 2 @@ -1601,7 +1795,8 @@ def test_dao_update_notifications_by_reference_returns_zero_when_no_notification notify_db_session, ): updated_count, updated_history_count = dao_update_notifications_by_reference( - references=["ref"], update_dict={"status": "delivered", "billable_units": 2} + references=["ref"], + update_dict={"status": NotificationStatus.DELIVERED, "billable_units": 2}, ) assert updated_count == 0 @@ -1617,13 +1812,19 @@ def test_dao_update_notifications_by_reference_updates_history_when_one_of_two_n notification2 = create_notification(template=sample_template, reference="ref2") updated_count, updated_history_count = dao_update_notifications_by_reference( - references=["ref1", "ref2"], update_dict={"status": "delivered"} + references=["ref1", "ref2"], + update_dict={"status": NotificationStatus.DELIVERED}, ) assert updated_count == 1 assert updated_history_count == 1 - assert Notification.query.get(notification2.id).status == "delivered" - assert NotificationHistory.query.get(notification1.id).status == "delivered" + assert ( + Notification.query.get(notification2.id).status == NotificationStatus.DELIVERED + ) + assert ( + NotificationHistory.query.get(notification1.id).status + == NotificationStatus.DELIVERED + ) def test_dao_get_notification_by_reference_with_one_match_returns_notification( @@ -1678,22 +1879,26 @@ def test_dao_get_notification_history_by_reference_with_no_matches_raises_error( dao_get_notification_history_by_reference("REF1") -@pytest.mark.parametrize("notification_type", ["email", "sms"]) +@pytest.mark.parametrize( + "notification_type", [NotificationType.EMAIL, NotificationType.SMS] +) def test_notifications_not_yet_sent(sample_service, notification_type): older_than = 4 # number of seconds the notification can not be older than template = create_template(service=sample_service, template_type=notification_type) old_notification = create_notification( template=template, created_at=datetime.utcnow() - timedelta(seconds=older_than), - status="created", + status=NotificationStatus.CREATED, ) create_notification( template=template, created_at=datetime.utcnow() - timedelta(seconds=older_than), - status="sending", + status=NotificationStatus.SENDING, ) create_notification( - template=template, created_at=datetime.utcnow(), status="created" + template=template, + created_at=datetime.utcnow(), + status=NotificationStatus.CREATED, ) results = notifications_not_yet_sent(older_than, notification_type) @@ -1701,18 +1906,26 @@ def test_notifications_not_yet_sent(sample_service, notification_type): assert results[0] == old_notification -@pytest.mark.parametrize("notification_type", ["email", "sms"]) +@pytest.mark.parametrize( + "notification_type", [NotificationType.EMAIL, NotificationType.SMS] +) def test_notifications_not_yet_sent_return_no_rows(sample_service, notification_type): older_than = 5 # number of seconds the notification can not be older than template = create_template(service=sample_service, template_type=notification_type) create_notification( - template=template, created_at=datetime.utcnow(), status="created" + template=template, + created_at=datetime.utcnow(), + status=NotificationStatus.CREATED, ) create_notification( - template=template, created_at=datetime.utcnow(), status="sending" + template=template, + created_at=datetime.utcnow(), + status=NotificationStatus.SENDING, ) create_notification( - template=template, created_at=datetime.utcnow(), status="delivered" + template=template, + created_at=datetime.utcnow(), + status=NotificationStatus.DELIVERED, ) results = notifications_not_yet_sent(older_than, notification_type) @@ -1750,7 +1963,8 @@ def test_get_service_ids_with_notifications_on_date_checks_ft_status( assert ( len( get_service_ids_with_notifications_on_date( - NotificationType.SMS, date(2022, 1, 1) + NotificationType.SMS, + date(2022, 1, 1), ) ) == 1 @@ -1758,7 +1972,8 @@ def test_get_service_ids_with_notifications_on_date_checks_ft_status( assert ( len( get_service_ids_with_notifications_on_date( - NotificationType.SMS, date(2022, 1, 2) + NotificationType.SMS, + date(2022, 1, 2), ) ) == 1 diff --git a/tests/app/dao/notification_dao/test_notification_dao_delete_notifications.py b/tests/app/dao/notification_dao/test_notification_dao_delete_notifications.py index 807996748..086f3c9e9 100644 --- a/tests/app/dao/notification_dao/test_notification_dao_delete_notifications.py +++ b/tests/app/dao/notification_dao/test_notification_dao_delete_notifications.py @@ -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( diff --git a/tests/app/dao/test_templates_dao.py b/tests/app/dao/test_templates_dao.py index c371baa1c..bfe0e59d1 100644 --- a/tests/app/dao/test_templates_dao.py +++ b/tests/app/dao/test_templates_dao.py @@ -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, diff --git a/tests/app/dao/test_uploads_dao.py b/tests/app/dao/test_uploads_dao.py index c95b2f000..5a4fb33b8 100644 --- a/tests/app/dao/test_uploads_dao.py +++ b/tests/app/dao/test_uploads_dao.py @@ -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, diff --git a/tests/app/dao/test_users_dao.py b/tests/app/dao/test_users_dao.py index 7b10cf4d5..6e4529310 100644 --- a/tests/app/dao/test_users_dao.py +++ b/tests/app/dao/test_users_dao.py @@ -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)), diff --git a/tests/app/delivery/test_send_to_providers.py b/tests/app/delivery/test_send_to_providers.py index 09dc0f39a..59d5395df 100644 --- a/tests/app/delivery/test_send_to_providers.py +++ b/tests/app/delivery/test_send_to_providers.py @@ -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", ) diff --git a/tests/app/inbound_sms/test_rest.py b/tests/app/inbound_sms/test_rest.py index 74e78b13d..3601f4f01 100644 --- a/tests/app/inbound_sms/test_rest.py +++ b/tests/app/inbound_sms/test_rest.py @@ -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)