From 63aa12e4efb036ed6a856e8f469de6522aef8e89 Mon Sep 17 00:00:00 2001 From: Cliff Hill Date: Tue, 13 Feb 2024 11:04:02 -0500 Subject: [PATCH] More cleanup. Signed-off-by: Cliff Hill --- tests/app/billing/test_rest.py | 4 +- tests/app/celery/test_nightly_tasks.py | 8 +- tests/app/celery/test_reporting_tasks.py | 63 +++-- tests/app/celery/test_scheduled_tasks.py | 28 ++- .../app/celery/test_service_callback_tasks.py | 19 +- tests/app/celery/test_tasks.py | 56 ++++- tests/app/conftest.py | 2 +- tests/app/dao/test_fact_billing_dao.py | 161 +++++++++--- .../dao/test_fact_notification_status_dao.py | 232 ++++++++++++++---- tests/app/dao/test_inbound_sms_dao.py | 18 +- tests/app/dao/test_provider_details_dao.py | 12 +- .../dao/test_service_data_retention_dao.py | 53 +++- tests/app/dao/test_services_dao.py | 201 ++++++++++----- tests/app/test_model.py | 6 +- tests/app/v2/templates/test_get_templates.py | 2 - 15 files changed, 662 insertions(+), 203 deletions(-) diff --git a/tests/app/billing/test_rest.py b/tests/app/billing/test_rest.py index 5d503b019..e1dfc33d3 100644 --- a/tests/app/billing/test_rest.py +++ b/tests/app/billing/test_rest.py @@ -174,7 +174,9 @@ def test_get_yearly_usage_by_monthly_from_ft_billing(admin_request, notify_db_se email_rows = [row for row in json_response if row["notification_type"] == "email"] assert len(email_rows) == 0 - sms_row = next(x for x in json_response if x["notification_type"] == NotificationType.SMS) + sms_row = next( + x for x in json_response if x["notification_type"] == NotificationType.SMS + ) assert sms_row["month"] == "January" assert sms_row["notification_type"] == NotificationType.SMS diff --git a/tests/app/celery/test_nightly_tasks.py b/tests/app/celery/test_nightly_tasks.py index f4eddc58b..0e98d0f48 100644 --- a/tests/app/celery/test_nightly_tasks.py +++ b/tests/app/celery/test_nightly_tasks.py @@ -278,8 +278,12 @@ def test_delete_notifications_task_calls_task_for_services_with_data_retention_o letter_service = create_service(service_name="c") create_service_data_retention(sms_service, notification_type=NotificationType.SMS) - create_service_data_retention(email_service, notification_type=NotificationType.EMAIL) - create_service_data_retention(letter_service, notification_type=NotificationType.LETTER) + create_service_data_retention( + email_service, notification_type=NotificationType.EMAIL + ) + create_service_data_retention( + letter_service, notification_type=NotificationType.LETTER + ) mock_subtask = mocker.patch( "app.celery.nightly_tasks.delete_notifications_for_service_and_type" diff --git a/tests/app/celery/test_reporting_tasks.py b/tests/app/celery/test_reporting_tasks.py index cec90c473..296d1a1c0 100644 --- a/tests/app/celery/test_reporting_tasks.py +++ b/tests/app/celery/test_reporting_tasks.py @@ -415,25 +415,38 @@ def test_create_nightly_notification_status_for_service_and_day(notify_db_sessio first_service = create_service(service_name="First Service") first_template = create_template(service=first_service) second_service = create_service(service_name="second Service") - second_template = create_template(service=second_service, template_type=TemplateType.EMAIL,) + second_template = create_template( + service=second_service, + template_type=TemplateType.EMAIL, + ) process_day = datetime.utcnow().date() - timedelta(days=5) with freeze_time(datetime.combine(process_day, time.max)): - create_notification(template=first_template, status=NotificationStatus.DELIVERED,) + create_notification( + template=first_template, + status=NotificationStatus.DELIVERED, + ) create_notification(template=second_template, status=NotificationStatus.FAILED) # team API key notifications are included create_notification( - template=second_template, status=NotificationStatus.SENDING, key_type=KeyType.TEAM, + template=second_template, + status=NotificationStatus.SENDING, + key_type=KeyType.TEAM, ) # test notifications are ignored create_notification( - template=second_template, status=NotificationStatus.SENDING, key_type=KeyType.TEST, + template=second_template, + status=NotificationStatus.SENDING, + key_type=KeyType.TEST, ) # historical notifications are included - create_notification_history(template=second_template, status=NotificationStatus.DELIVERED,) + create_notification_history( + template=second_template, + status=NotificationStatus.DELIVERED, + ) # these created notifications from a different day get ignored with freeze_time( @@ -445,10 +458,14 @@ def test_create_nightly_notification_status_for_service_and_day(notify_db_sessio assert len(FactNotificationStatus.query.all()) == 0 create_nightly_notification_status_for_service_and_day( - str(process_day), first_service.id, NotificationType.SMS, + str(process_day), + first_service.id, + NotificationType.SMS, ) create_nightly_notification_status_for_service_and_day( - str(process_day), second_service.id, NotificationType.EMAIL, + str(process_day), + second_service.id, + NotificationType.EMAIL, ) new_fact_data = FactNotificationStatus.query.order_by( @@ -501,9 +518,13 @@ def test_create_nightly_notification_status_for_service_and_day_overwrites_old_d process_day = datetime.utcnow().date() # first run: one notification, expect one row (just one status) - notification = create_notification(template=first_template, status=NotificationStatus.SENDING) + notification = create_notification( + template=first_template, status=NotificationStatus.SENDING + ) create_nightly_notification_status_for_service_and_day( - str(process_day), first_service.id, NotificationType.SMS, + str(process_day), + first_service.id, + NotificationType.SMS, ) new_fact_data = FactNotificationStatus.query.all() @@ -516,7 +537,9 @@ def test_create_nightly_notification_status_for_service_and_day_overwrites_old_d notification.status = NotificationStatus.DELIVERED create_notification(template=first_template, status=NotificationStatus.CREATED) create_nightly_notification_status_for_service_and_day( - str(process_day), first_service.id, NotificationType.SMS, + str(process_day), + first_service.id, + NotificationType.SMS, ) updated_fact_data = FactNotificationStatus.query.order_by( @@ -536,22 +559,32 @@ def test_create_nightly_notification_status_for_service_and_day_respects_bst( sample_template, ): create_notification( - sample_template, status=NotificationStatus.DELIVERED, created_at=datetime(2019, 4, 2, 5, 0), + sample_template, + status=NotificationStatus.DELIVERED, + created_at=datetime(2019, 4, 2, 5, 0), ) # too new create_notification( - sample_template, status=NotificationStatus.CREATED, created_at=datetime(2019, 4, 2, 5, 59), + sample_template, + status=NotificationStatus.CREATED, + created_at=datetime(2019, 4, 2, 5, 59), ) create_notification( - sample_template, status=NotificationStatus.CREATED, created_at=datetime(2019, 4, 1, 4, 0), + sample_template, + status=NotificationStatus.CREATED, + created_at=datetime(2019, 4, 1, 4, 0), ) create_notification( - sample_template, status=NotificationStatus.DELIVERED, created_at=datetime(2019, 3, 21, 17, 59), + sample_template, + status=NotificationStatus.DELIVERED, + created_at=datetime(2019, 3, 21, 17, 59), ) # too old create_nightly_notification_status_for_service_and_day( - "2019-04-01", sample_template.service_id, NotificationType.SMS, + "2019-04-01", + sample_template.service_id, + NotificationType.SMS, ) noti_status = FactNotificationStatus.query.order_by( diff --git a/tests/app/celery/test_scheduled_tasks.py b/tests/app/celery/test_scheduled_tasks.py index 0472e9fdb..94b586a3a 100644 --- a/tests/app/celery/test_scheduled_tasks.py +++ b/tests/app/celery/test_scheduled_tasks.py @@ -101,7 +101,9 @@ def test_should_update_scheduled_jobs_and_put_on_queue(mocker, sample_template): one_minute_in_the_past = datetime.utcnow() - timedelta(minutes=1) job = create_job( - sample_template, job_status=JobStatus.SCHEDULED, scheduled_for=one_minute_in_the_past + sample_template, + job_status=JobStatus.SCHEDULED, + scheduled_for=one_minute_in_the_past, ) run_scheduled_jobs() @@ -118,10 +120,14 @@ def test_should_update_all_scheduled_jobs_and_put_on_queue(sample_template, mock ten_minutes_in_the_past = datetime.utcnow() - timedelta(minutes=10) twenty_minutes_in_the_past = datetime.utcnow() - timedelta(minutes=20) job_1 = create_job( - sample_template, job_status=JobStatus.SCHEDULED, scheduled_for=one_minute_in_the_past, + sample_template, + job_status=JobStatus.SCHEDULED, + scheduled_for=one_minute_in_the_past, ) job_2 = create_job( - sample_template, job_status=JobStatus.SCHEDULED, scheduled_for=ten_minutes_in_the_past, + sample_template, + job_status=JobStatus.SCHEDULED, + scheduled_for=ten_minutes_in_the_past, ) job_3 = create_job( sample_template, @@ -299,8 +305,12 @@ def test_replay_created_notifications(notify_db_session, sample_service, mocker) "app.celery.provider_tasks.deliver_sms.apply_async" ) - sms_template = create_template(service=sample_service, template_type=TemplateType.SMS) - email_template = create_template(service=sample_service, template_type=TemplateType.EMAIL) + sms_template = create_template( + service=sample_service, template_type=TemplateType.SMS + ) + email_template = create_template( + service=sample_service, template_type=TemplateType.EMAIL + ) older_than = (60 * 60) + (60 * 15) # 1 hour 15 minutes # notifications expected to be resent old_sms = create_notification( @@ -325,10 +335,14 @@ def test_replay_created_notifications(notify_db_session, sample_service, mocker) status=NotificationStatus.DELIVERED, ) create_notification( - template=sms_template, created_at=datetime.utcnow(), status=NotificationStatus.CREATED, + template=sms_template, + created_at=datetime.utcnow(), + status=NotificationStatus.CREATED, ) create_notification( - template=email_template, created_at=datetime.utcnow(), status=NotificationStatus.CREATED, + template=email_template, + created_at=datetime.utcnow(), + status=NotificationStatus.CREATED, ) replay_created_notifications() diff --git a/tests/app/celery/test_service_callback_tasks.py b/tests/app/celery/test_service_callback_tasks.py index 5dd7f18ca..af1e8abd4 100644 --- a/tests/app/celery/test_service_callback_tasks.py +++ b/tests/app/celery/test_service_callback_tasks.py @@ -21,7 +21,9 @@ from tests.app.db import ( ) -@pytest.mark.parametrize("notification_type", [NotificationType.EMAIL, NotificationType.SMS]) +@pytest.mark.parametrize( + "notification_type", [NotificationType.EMAIL, NotificationType.SMS] +) def test_send_delivery_status_to_service_post_https_request_to_service_with_encrypted_data( notify_db_session, notification_type ): @@ -103,7 +105,10 @@ def test_send_complaint_to_service_posts_https_request_to_service_with_encrypted ] == "Bearer {}".format(callback_api.bearer_token) -@pytest.mark.parametrize("notification_type", [NotificationType.EMAIL, NotificationType.SMS],) +@pytest.mark.parametrize( + "notification_type", + [NotificationType.EMAIL, NotificationType.SMS], +) @pytest.mark.parametrize("status_code", [429, 500, 503]) def test__send_data_to_service_callback_api_retries_if_request_returns_error_code_with_encrypted_data( notify_db_session, mocker, notification_type, status_code @@ -131,7 +136,10 @@ def test__send_data_to_service_callback_api_retries_if_request_returns_error_cod assert mocked.call_args[1]["queue"] == "service-callbacks-retry" -@pytest.mark.parametrize("notification_type", [NotificationType.EMAIL, NotificationType.SMS],) +@pytest.mark.parametrize( + "notification_type", + [NotificationType.EMAIL, NotificationType.SMS], +) def test__send_data_to_service_callback_api_does_not_retry_if_request_returns_404_with_encrypted_data( notify_db_session, mocker, notification_type ): @@ -160,7 +168,10 @@ def test__send_data_to_service_callback_api_does_not_retry_if_request_returns_40 def test_send_delivery_status_to_service_succeeds_if_sent_at_is_none( notify_db_session, mocker ): - callback_api, template = _set_up_test_data(NotificationType.EMAIL, CallbackType.DELIVERY_STATUS,) + callback_api, template = _set_up_test_data( + NotificationType.EMAIL, + CallbackType.DELIVERY_STATUS, + ) datestr = datetime(2017, 6, 20) notification = create_notification( template=template, diff --git a/tests/app/celery/test_tasks.py b/tests/app/celery/test_tasks.py index bda1837a3..7d61e28ad 100644 --- a/tests/app/celery/test_tasks.py +++ b/tests/app/celery/test_tasks.py @@ -475,7 +475,11 @@ def test_save_email_should_save_default_email_reply_to_text_on_notification( create_reply_to_email( service=service, email_address="reply_to@digital.fake.gov", is_default=True ) - template = create_template(service=service, template_type=TemplateType.EMAIL, subject="Hello",) + template = create_template( + service=service, + template_type=TemplateType.EMAIL, + subject="Hello", + ) notification = _notification_json(template, to="test@example.com") mocker.patch("app.celery.provider_tasks.deliver_email.apply_async") @@ -536,7 +540,11 @@ def test_should_not_save_email_if_restricted_service_and_invalid_email_address( ): user = create_user() service = create_service(user=user, restricted=True) - template = create_template(service=service, template_type=TemplateType.EMAIL, subject="Hello",) + template = create_template( + service=service, + template_type=TemplateType.EMAIL, + subject="Hello", + ) notification = _notification_json(template, to="test@example.com") notification_id = uuid.uuid4() @@ -551,7 +559,10 @@ def test_should_not_save_email_if_restricted_service_and_invalid_email_address( def test_should_save_sms_template_to_and_persist_with_job_id(sample_job, mocker): notification = _notification_json( - sample_job.template, to="+447234123123", job_id=sample_job.id, row_number=2, + sample_job.template, + to="+447234123123", + job_id=sample_job.id, + row_number=2, ) mocker.patch("app.celery.provider_tasks.deliver_sms.apply_async") @@ -726,11 +737,15 @@ def test_save_email_uses_the_reply_to_text_when_provided(sample_email_template, service = sample_email_template.service notification_id = uuid.uuid4() service_email_reply_to_dao.add_reply_to_email_address_for_service( - service.id, "default@example.com", True, + service.id, + "default@example.com", + True, ) other_email_reply_to = ( service_email_reply_to_dao.add_reply_to_email_address_for_service( - service.id, "other@example.com", False, + service.id, + "other@example.com", + False, ) ) @@ -754,7 +769,9 @@ def test_save_email_uses_the_default_reply_to_text_if_sender_id_is_none( service = sample_email_template.service notification_id = uuid.uuid4() service_email_reply_to_dao.add_reply_to_email_address_for_service( - service.id, "default@example.com", True, + service.id, + "default@example.com", + True, ) save_email( @@ -925,7 +942,9 @@ def test_save_sms_uses_non_default_sms_sender_reply_to_text_if_provided( service = create_service_with_defined_sms_sender(sms_sender_value="2028675309") template = create_template(service=service) new_sender = service_sms_sender_dao.dao_add_sms_sender_for_service( - service.id, "new-sender", False, + service.id, + "new-sender", + False, ) notification = _notification_json(template, to="202-867-5301") @@ -1377,7 +1396,10 @@ def test_process_incomplete_jobs_sets_status_to_in_progress_and_resets_processin @freeze_time("2020-03-25 14:30") -@pytest.mark.parametrize("notification_type", [NotificationType.SMS, NotificationType.EMAIL],) +@pytest.mark.parametrize( + "notification_type", + [NotificationType.SMS, NotificationType.EMAIL], +) def test_save_api_email_or_sms(mocker, sample_service, notification_type): template = ( create_template(sample_service) @@ -1427,7 +1449,9 @@ def test_save_api_email_or_sms(mocker, sample_service, notification_type): @freeze_time("2020-03-25 14:30") -@pytest.mark.parametrize("notification_type", [NotificationType.SMS, NotificationType.EMAIL]) +@pytest.mark.parametrize( + "notification_type", [NotificationType.SMS, NotificationType.EMAIL] +) def test_save_api_email_dont_retry_if_notification_already_exists( sample_service, mocker, notification_type ): @@ -1549,8 +1573,18 @@ def test_save_tasks_use_cached_service_and_template( @pytest.mark.parametrize( "notification_type, task_function, expected_queue, recipient", ( - (NotificationType.SMS, save_api_sms, QueueNames.SEND_SMS, "+447700900855",), - (NotificationType.EMAIL, save_api_email, QueueNames.SEND_EMAIL, "jane.citizen@example.com",), + ( + NotificationType.SMS, + save_api_sms, + QueueNames.SEND_SMS, + "+447700900855", + ), + ( + NotificationType.EMAIL, + save_api_email, + QueueNames.SEND_EMAIL, + "jane.citizen@example.com", + ), ), ) def test_save_api_tasks_use_cache( diff --git a/tests/app/conftest.py b/tests/app/conftest.py index 2f550c1d4..44e93b98b 100644 --- a/tests/app/conftest.py +++ b/tests/app/conftest.py @@ -1,7 +1,7 @@ import json -from types import CodeType import uuid from datetime import datetime, timedelta +from types import CodeType import pytest import pytz diff --git a/tests/app/dao/test_fact_billing_dao.py b/tests/app/dao/test_fact_billing_dao.py index 41a3dd485..85d40c3e6 100644 --- a/tests/app/dao/test_fact_billing_dao.py +++ b/tests/app/dao/test_fact_billing_dao.py @@ -22,7 +22,7 @@ from app.dao.fact_billing_dao import ( ) from app.dao.organization_dao import dao_add_service_to_organization from app.enums import NotificationStatus, NotificationType, TemplateType -from app.models import FactBilling, Notification +from app.models import FactBilling from tests.app.db import ( create_annual_billing, create_ft_billing, @@ -95,7 +95,11 @@ def test_fetch_billing_data_for_today_includes_data_with_the_right_key_type( service = create_service() template = create_template(service=service, template_type=TemplateType.EMAIL) for key_type in ["normal", "test", "team"]: - create_notification(template=template, status=NotificationStatus.DELIVERED, key_type=key_type,) + create_notification( + template=template, + status=NotificationStatus.DELIVERED, + key_type=key_type, + ) today = datetime.utcnow() results = fetch_billing_data_for_day(today.date()) @@ -103,7 +107,9 @@ def test_fetch_billing_data_for_today_includes_data_with_the_right_key_type( assert results[0].notifications_sent == 2 -@pytest.mark.parametrize("notification_type", [NotificationType.EMAIL, NotificationType.SMS]) +@pytest.mark.parametrize( + "notification_type", [NotificationType.EMAIL, NotificationType.SMS] +) def test_fetch_billing_data_for_day_only_calls_query_for_permission_type( notify_db_session, notification_type ): @@ -119,7 +125,10 @@ def test_fetch_billing_data_for_day_only_calls_query_for_permission_type( assert len(results) == 1 -@pytest.mark.parametrize("notification_type", [NotificationType.EMAIL, NotificationType.SMS],) +@pytest.mark.parametrize( + "notification_type", + [NotificationType.EMAIL, NotificationType.SMS], +) def test_fetch_billing_data_for_day_only_calls_query_for_all_channels( notify_db_session, notification_type ): @@ -130,7 +139,8 @@ def test_fetch_billing_data_for_day_only_calls_query_for_all_channels( create_notification(template=sms_template, status=NotificationStatus.DELIVERED) today = datetime.utcnow() results = fetch_billing_data_for_day( - process_day=today.date(), check_permissions=False, + process_day=today.date(), + check_permissions=False, ) assert len(results) == 2 @@ -142,7 +152,11 @@ def test_fetch_billing_data_for_today_includes_data_with_the_right_date( process_day = datetime(2018, 4, 1, 13, 30, 0) service = create_service() template = create_template(service=service, template_type=TemplateType.EMAIL) - create_notification(template=template, status=NotificationStatus.DELIVERED, created_at=process_day,) + create_notification( + template=template, + status=NotificationStatus.DELIVERED, + created_at=process_day, + ) create_notification( template=template, status=NotificationStatus.DELIVERED, @@ -155,7 +169,12 @@ def test_fetch_billing_data_for_today_includes_data_with_the_right_date( created_at=datetime(2018, 4, 1, 0, 23, 23), ) create_notification( - template=template, status=NotificationStatus.SENDING, created_at=process_day + timedelta(days=1,) + template=template, + status=NotificationStatus.SENDING, + created_at=process_day + + timedelta( + days=1, + ), ) day_under_test = process_day @@ -198,8 +217,16 @@ def test_fetch_billing_data_for_day_is_grouped_by_service(notify_db_session): def test_fetch_billing_data_for_day_is_grouped_by_provider(notify_db_session): service = create_service() template = create_template(service=service) - create_notification(template=template, status=NotificationStatus.DELIVERED, sent_by="sns",) - create_notification(template=template, status=NotificationStatus.DELIVERED, sent_by="sns",) + create_notification( + template=template, + status=NotificationStatus.DELIVERED, + sent_by="sns", + ) + create_notification( + template=template, + status=NotificationStatus.DELIVERED, + sent_by="sns", + ) today = datetime.utcnow() results = fetch_billing_data_for_day(today.date()) @@ -211,8 +238,16 @@ def test_fetch_billing_data_for_day_is_grouped_by_provider(notify_db_session): def test_fetch_billing_data_for_day_is_grouped_by_rate_mulitplier(notify_db_session): service = create_service() template = create_template(service=service) - create_notification(template=template, status=NotificationStatus.DELIVERED, rate_multiplier=1,) - create_notification(template=template, status=NotificationStatus.DELIVERED, rate_multiplier=2,) + create_notification( + template=template, + status=NotificationStatus.DELIVERED, + rate_multiplier=1, + ) + create_notification( + template=template, + status=NotificationStatus.DELIVERED, + rate_multiplier=2, + ) today = datetime.utcnow() results = fetch_billing_data_for_day(today.date()) @@ -224,8 +259,16 @@ def test_fetch_billing_data_for_day_is_grouped_by_rate_mulitplier(notify_db_sess def test_fetch_billing_data_for_day_is_grouped_by_international(notify_db_session): service = create_service() sms_template = create_template(service=service) - create_notification(template=sms_template, status=NotificationStatus.DELIVERED, international=True,) - create_notification(template=sms_template, status=NotificationStatus.DELIVERED, international=False,) + create_notification( + template=sms_template, + status=NotificationStatus.DELIVERED, + international=True, + ) + create_notification( + template=sms_template, + status=NotificationStatus.DELIVERED, + international=False, + ) today = datetime.utcnow() results = fetch_billing_data_for_day(today.date()) @@ -266,10 +309,14 @@ def test_fetch_billing_data_for_day_uses_correct_table(notify_db_session): five_days_ago = datetime.utcnow() - timedelta(days=5) create_notification( - template=sms_template, status=NotificationStatus.DELIVERED, created_at=five_days_ago, + template=sms_template, + status=NotificationStatus.DELIVERED, + created_at=five_days_ago, ) create_notification_history( - template=email_template, status=NotificationStatus.DELIVERED, created_at=five_days_ago, + template=email_template, + status=NotificationStatus.DELIVERED, + created_at=five_days_ago, ) results = fetch_billing_data_for_day( @@ -311,16 +358,24 @@ def test_fetch_billing_data_for_day_bills_correctly_for_status(notify_db_session ) sms_results = [x for x in results if x.notification_type == NotificationType.SMS] - email_results = [x for x in results if x.notification_type == NotificationType.EMAIL] + email_results = [ + x for x in results if x.notification_type == NotificationType.EMAIL + ] # we expect as many rows as we check for notification types assert 6 == sms_results[0].notifications_sent assert 4 == email_results[0].notifications_sent def test_get_rates_for_billing(notify_db_session): - create_rate(start_date=datetime.utcnow(), value=12, notification_type=NotificationType.EMAIL) - create_rate(start_date=datetime.utcnow(), value=22, notification_type=NotificationType.SMS) - create_rate(start_date=datetime.utcnow(), value=33, notification_type=NotificationType.EMAIL) + create_rate( + start_date=datetime.utcnow(), value=12, notification_type=NotificationType.EMAIL + ) + create_rate( + start_date=datetime.utcnow(), value=22, notification_type=NotificationType.SMS + ) + create_rate( + start_date=datetime.utcnow(), value=33, notification_type=NotificationType.EMAIL + ) rates = get_rates_for_billing() assert len(rates) == 3 @@ -329,17 +384,25 @@ def test_get_rates_for_billing(notify_db_session): @freeze_time("2017-06-01 12:00") def test_get_rate(notify_db_session): create_rate( - start_date=datetime(2017, 5, 30, 23, 0), value=1.2, notification_type=NotificationType.EMAIL, + start_date=datetime(2017, 5, 30, 23, 0), + value=1.2, + notification_type=NotificationType.EMAIL, ) create_rate( - start_date=datetime(2017, 5, 30, 23, 0), value=2.2, notification_type=NotificationType.SMS, + start_date=datetime(2017, 5, 30, 23, 0), + value=2.2, + notification_type=NotificationType.SMS, ) create_rate( - start_date=datetime(2017, 5, 30, 23, 0), value=3.3, notification_type=NotificationType.EMAIL, + start_date=datetime(2017, 5, 30, 23, 0), + value=3.3, + notification_type=NotificationType.EMAIL, ) rates = get_rates_for_billing() - rate = get_rate(rates, notification_type=NotificationType.SMS, date=date(2017, 6, 1)) + rate = get_rate( + rates, notification_type=NotificationType.SMS, date=date(2017, 6, 1) + ) assert rate == 2.2 @@ -351,10 +414,14 @@ def test_get_rate_chooses_right_rate_depending_on_date( notify_db_session, date, expected_rate ): create_rate( - start_date=datetime(2016, 1, 1, 0, 0), value=1.2, notification_type=NotificationType.SMS, + start_date=datetime(2016, 1, 1, 0, 0), + value=1.2, + notification_type=NotificationType.SMS, ) create_rate( - start_date=datetime(2018, 9, 30, 23, 0), value=2.2, notification_type=NotificationType.SMS, + start_date=datetime(2018, 9, 30, 23, 0), + value=2.2, + notification_type=NotificationType.SMS, ) rates = get_rates_for_billing() @@ -397,7 +464,9 @@ def test_fetch_monthly_billing_for_year(notify_db_session): def test_fetch_monthly_billing_for_year_variable_rates(notify_db_session): service = set_up_yearly_data_variable_rates() create_annual_billing( - service_id=service.id, free_sms_fragment_limit=6, financial_year_start=2018, + service_id=service.id, + free_sms_fragment_limit=6, + financial_year_start=2018, ) results = fetch_monthly_billing_for_year(service.id, 2018) @@ -501,7 +570,9 @@ def test_fetch_billing_totals_for_year_uses_current_annual_billing(notify_db_ses def test_fetch_billing_totals_for_year_variable_rates(notify_db_session): service = set_up_yearly_data_variable_rates() create_annual_billing( - service_id=service.id, free_sms_fragment_limit=6, financial_year_start=2018, + service_id=service.id, + free_sms_fragment_limit=6, + financial_year_start=2018, ) results = fetch_billing_totals_for_year(service_id=service.id, year=2018) @@ -558,13 +629,21 @@ def test_fetch_sms_free_allowance_remainder_until_date_with_two_services( org = create_organization(name="Org for {}".format(service.name)) dao_add_service_to_organization(service=service, organization_id=org.id) create_annual_billing( - service_id=service.id, free_sms_fragment_limit=10, financial_year_start=2016, + service_id=service.id, + free_sms_fragment_limit=10, + financial_year_start=2016, ) create_ft_billing( - template=template, local_date=datetime(2016, 4, 20), billable_unit=2, rate=0.11, + template=template, + local_date=datetime(2016, 4, 20), + billable_unit=2, + rate=0.11, ) create_ft_billing( - template=template, local_date=datetime(2016, 5, 20), billable_unit=3, rate=0.11, + template=template, + local_date=datetime(2016, 5, 20), + billable_unit=3, + rate=0.11, ) service_2 = create_service(service_name="used free allowance") @@ -693,7 +772,9 @@ def test_fetch_sms_billing_for_all_services_with_remainder(notify_db_session): ) service_4 = create_service(service_name="d - email only") - email_template = create_template(service=service_4, template_type=TemplateType.EMAIL) + email_template = create_template( + service=service_4, template_type=TemplateType.EMAIL + ) org_4 = create_organization(name="Org for {}".format(service_4.name)) dao_add_service_to_organization(service=service_4, organization_id=org_4.id) create_annual_billing( @@ -827,13 +908,17 @@ def test_fetch_usage_year_for_organization(notify_db_session): financial_year_start=2019, ) dao_add_service_to_organization( - service=service_with_emails_for_org, organization_id=fixtures["org_1"].id, + service=service_with_emails_for_org, + organization_id=fixtures["org_1"].id, ) template = create_template( - service=service_with_emails_for_org, template_type=TemplateType.EMAIL, + service=service_with_emails_for_org, + template_type=TemplateType.EMAIL, ) create_ft_billing( - local_date=datetime(2019, 5, 1), template=template, notifications_sent=1100, + local_date=datetime(2019, 5, 1), + template=template, + notifications_sent=1100, ) results = fetch_usage_year_for_organization(fixtures["org_1"].id, 2019) @@ -1039,8 +1124,12 @@ def test_fetch_usage_year_for_organization_only_returns_data_for_live_services( live_service = create_service(restricted=False) sms_template = create_template(service=live_service) trial_service = create_service(restricted=True, service_name="trial_service") - email_template = create_template(service=trial_service, template_type=TemplateType.EMAIL) - trial_sms_template = create_template(service=trial_service, template_type=TemplateType.SMS) + email_template = create_template( + service=trial_service, template_type=TemplateType.EMAIL + ) + trial_sms_template = create_template( + service=trial_service, template_type=TemplateType.SMS + ) dao_add_service_to_organization(service=live_service, organization_id=org.id) dao_add_service_to_organization(service=trial_service, organization_id=org.id) create_ft_billing( diff --git a/tests/app/dao/test_fact_notification_status_dao.py b/tests/app/dao/test_fact_notification_status_dao.py index be750cb83..99181e811 100644 --- a/tests/app/dao/test_fact_notification_status_dao.py +++ b/tests/app/dao/test_fact_notification_status_dao.py @@ -255,23 +255,52 @@ def test_fetch_notification_status_by_template_for_service_for_today_and_7_previ # create unused email template create_template(service=service_1, template_type=TemplateType.EMAIL) - create_ft_notification_status(date(2018, 10, 29), NotificationType.SMS, service_1, count=10,) - create_ft_notification_status(date(2018, 10, 29), NotificationType.SMS, service_1, count=11,) - create_ft_notification_status(date(2018, 10, 25), NotificationType.SMS, service_1, count=8,) create_ft_notification_status( - date(2018, 10, 29), NotificationType.SMS, service_1, notification_status=NotificationStatus.CREATED, + date(2018, 10, 29), + NotificationType.SMS, + service_1, + count=10, + ) + create_ft_notification_status( + date(2018, 10, 29), + NotificationType.SMS, + service_1, + count=11, + ) + create_ft_notification_status( + date(2018, 10, 25), + NotificationType.SMS, + service_1, + count=8, + ) + create_ft_notification_status( + date(2018, 10, 29), + NotificationType.SMS, + service_1, + notification_status=NotificationStatus.CREATED, + ) + create_ft_notification_status( + date(2018, 10, 29), + NotificationType.EMAIL, + service_1, + count=3, ) - create_ft_notification_status(date(2018, 10, 29), NotificationType.EMAIL, service_1, count=3,) create_notification(sms_template, created_at=datetime(2018, 10, 31, 11, 0, 0)) create_notification( - sms_template, created_at=datetime(2018, 10, 31, 12, 0, 0), status=NotificationStatus.DELIVERED, + sms_template, + created_at=datetime(2018, 10, 31, 12, 0, 0), + status=NotificationStatus.DELIVERED, ) create_notification( - sms_template_2, created_at=datetime(2018, 10, 31, 12, 0, 0), status=NotificationStatus.DELIVERED, + sms_template_2, + created_at=datetime(2018, 10, 31, 12, 0, 0), + status=NotificationStatus.DELIVERED, ) create_notification( - email_template, created_at=datetime(2018, 10, 31, 13, 0, 0), status=NotificationStatus.DELIVERED, + email_template, + created_at=datetime(2018, 10, 31, 13, 0, 0), + status=NotificationStatus.DELIVERED, ) # too early, shouldn't be included @@ -282,19 +311,83 @@ def test_fetch_notification_status_by_template_for_service_for_today_and_7_previ ) results = fetch_notification_status_for_service_for_today_and_7_previous_days( - service_1.id, by_template=True, + service_1.id, + by_template=True, ) assert [ - ("email Template Name", False, mock.ANY, NotificationType.EMAIL, NotificationStatus.DELIVERED, 1,), - ("email Template Name", False, mock.ANY, NotificationType.EMAIL, NotificationStatus.DELIVERED, 3,), - ("sms Template 1", False, mock.ANY, NotificationType.SMS, NotificationStatus.CREATED, 1,), - ("sms Template Name", False, mock.ANY, NotificationType.SMS, NotificationStatus.CREATED, 1,), - ("sms Template 1", False, mock.ANY, NotificationType.SMS, NotificationStatus.DELIVERED, 1,), - ("sms Template 2", False, mock.ANY, NotificationType.SMS, NotificationStatus.DELIVERED, 1,), - ("sms Template Name", False, mock.ANY, NotificationType.SMS, NotificationStatus.DELIVERED, 8,), - ("sms Template Name", False, mock.ANY, NotificationType.SMS, NotificationStatus.DELIVERED, 10,), - ("sms Template Name", False, mock.ANY, NotificationType.SMS, NotificationStatus.DELIVERED, 11,), + ( + "email Template Name", + False, + mock.ANY, + NotificationType.EMAIL, + NotificationStatus.DELIVERED, + 1, + ), + ( + "email Template Name", + False, + mock.ANY, + NotificationType.EMAIL, + NotificationStatus.DELIVERED, + 3, + ), + ( + "sms Template 1", + False, + mock.ANY, + NotificationType.SMS, + NotificationStatus.CREATED, + 1, + ), + ( + "sms Template Name", + False, + mock.ANY, + NotificationType.SMS, + NotificationStatus.CREATED, + 1, + ), + ( + "sms Template 1", + False, + mock.ANY, + NotificationType.SMS, + NotificationStatus.DELIVERED, + 1, + ), + ( + "sms Template 2", + False, + mock.ANY, + NotificationType.SMS, + NotificationStatus.DELIVERED, + 1, + ), + ( + "sms Template Name", + False, + mock.ANY, + NotificationType.SMS, + NotificationStatus.DELIVERED, + 8, + ), + ( + "sms Template Name", + False, + mock.ANY, + NotificationType.SMS, + NotificationStatus.DELIVERED, + 10, + ), + ( + "sms Template Name", + False, + mock.ANY, + NotificationType.SMS, + NotificationStatus.DELIVERED, + 11, + ), ] == sorted( results, key=lambda x: (x.notification_type, x.status, x.template_name, x.count) ) @@ -352,19 +445,29 @@ def test_fetch_notification_status_totals_for_all_services_works_in_est( ) create_notification( - sms_template, created_at=datetime(2018, 4, 20, 12, 0, 0), status=NotificationStatus.DELIVERED, + sms_template, + created_at=datetime(2018, 4, 20, 12, 0, 0), + status=NotificationStatus.DELIVERED, ) create_notification( - sms_template, created_at=datetime(2018, 4, 21, 11, 0, 0), status=NotificationStatus.CREATED, + sms_template, + created_at=datetime(2018, 4, 21, 11, 0, 0), + status=NotificationStatus.CREATED, ) create_notification( - sms_template, created_at=datetime(2018, 4, 21, 12, 0, 0), status=NotificationStatus.DELIVERED, + sms_template, + created_at=datetime(2018, 4, 21, 12, 0, 0), + status=NotificationStatus.DELIVERED, ) create_notification( - email_template, created_at=datetime(2018, 4, 21, 13, 0, 0), status=NotificationStatus.DELIVERED, + email_template, + created_at=datetime(2018, 4, 21, 13, 0, 0), + status=NotificationStatus.DELIVERED, ) create_notification( - email_template, created_at=datetime(2018, 4, 21, 14, 0, 0), status=NotificationStatus.DELIVERED, + email_template, + created_at=datetime(2018, 4, 21, 14, 0, 0), + status=NotificationStatus.DELIVERED, ) results = sorted( @@ -396,12 +499,27 @@ def set_up_data(): email_template = create_template( service=service_1, template_type=TemplateType.EMAIL ) - create_ft_notification_status(date(2018, 10, 24), NotificationType.SMS, service_1, count=8,) - create_ft_notification_status(date(2018, 10, 29), NotificationType.SMS, service_1, count=10,) create_ft_notification_status( - date(2018, 10, 29), NotificationType.SMS, service_1, notification_status=NotificationStatus.CREATED, + date(2018, 10, 24), + NotificationType.SMS, + service_1, + count=8, + ) + create_ft_notification_status( + date(2018, 10, 29), + NotificationType.SMS, + service_1, + count=10, + ) + create_ft_notification_status( + date(2018, 10, 29), + NotificationType.SMS, + service_1, + notification_status=NotificationStatus.CREATED, + ) + create_ft_notification_status( + date(2018, 10, 29), NotificationType.EMAIL, service_1, count=3 ) - create_ft_notification_status(date(2018, 10, 29), NotificationType.EMAIL, service_1, count=3) create_notification( service_1.templates[0], @@ -410,10 +528,14 @@ def set_up_data(): ) create_notification(sms_template, created_at=datetime(2018, 10, 31, 11, 0, 0)) create_notification( - sms_template, created_at=datetime(2018, 10, 31, 12, 0, 0), status=NotificationStatus.DELIVERED, + sms_template, + created_at=datetime(2018, 10, 31, 12, 0, 0), + status=NotificationStatus.DELIVERED, ) create_notification( - email_template, created_at=datetime(2018, 10, 31, 13, 0, 0), status=NotificationStatus.DELIVERED, + email_template, + created_at=datetime(2018, 10, 31, 13, 0, 0), + status=NotificationStatus.DELIVERED, ) return service_1, service_2 @@ -423,16 +545,28 @@ def test_fetch_notification_statuses_for_job(sample_template): j2 = create_job(sample_template) create_ft_notification_status( - date(2018, 10, 1), job=j1, notification_status=NotificationStatus.CREATED, count=1, + date(2018, 10, 1), + job=j1, + notification_status=NotificationStatus.CREATED, + count=1, ) create_ft_notification_status( - date(2018, 10, 1), job=j1, notification_status=NotificationStatus.DELIVERED, count=2, + date(2018, 10, 1), + job=j1, + notification_status=NotificationStatus.DELIVERED, + count=2, ) create_ft_notification_status( - date(2018, 10, 2), job=j1, notification_status=NotificationStatus.CREATED, count=4, + date(2018, 10, 2), + job=j1, + notification_status=NotificationStatus.CREATED, + count=4, ) create_ft_notification_status( - date(2018, 10, 1), job=j2, notification_status=NotificationStatus.CREATED, count=8, + date(2018, 10, 1), + job=j2, + notification_status=NotificationStatus.CREATED, + count=8, ) assert {x.status: x.count for x in fetch_notification_statuses_for_job(j1.id)} == { @@ -473,10 +607,14 @@ def test_fetch_stats_for_all_services_by_date_range(notify_db_session): @freeze_time("2018-03-30 14:00") def test_fetch_monthly_template_usage_for_service(sample_service): template_one = create_template( - service=sample_service, template_type=TemplateType.SMS, template_name="a", + service=sample_service, + template_type=TemplateType.SMS, + template_name="a", ) template_two = create_template( - service=sample_service, template_type=TemplateType.EMAIL, template_name="b", + service=sample_service, + template_type=TemplateType.EMAIL, + template_name="b", ) create_ft_notification_status( @@ -548,10 +686,14 @@ def test_fetch_monthly_template_usage_for_service_does_join_to_notifications_if_ sample_service, ): template_one = create_template( - service=sample_service, template_type=TemplateType.SMS, template_name="a", + service=sample_service, + template_type=TemplateType.SMS, + template_name="a", ) template_two = create_template( - service=sample_service, template_type=TmplateType.EMAIL, template_name="b", + service=sample_service, + template_type=TemplateType.EMAIL, + template_name="b", ) create_ft_notification_status( local_date=date(2018, 2, 1), @@ -604,7 +746,9 @@ def test_fetch_monthly_template_usage_for_service_does_not_include_cancelled_sta count=15, ) create_notification( - template=sample_template, created_at=datetime.utcnow(), status=NotificationStatus.CANCELLED, + template=sample_template, + created_at=datetime.utcnow(), + status=NotificationStatus.CANCELLED, ) results = fetch_monthly_template_usage_for_service( datetime(2018, 1, 1), datetime(2018, 3, 31), sample_template.service_id @@ -632,7 +776,9 @@ def test_fetch_monthly_template_usage_for_service_does_not_include_test_notifica key_type=KeyType.TEST, ) results = fetch_monthly_template_usage_for_service( - datetime(2018, 1, 1), datetime(2018, 3, 31), sample_template.service_id, + datetime(2018, 1, 1), + datetime(2018, 3, 31), + sample_template.service_id, ) assert len(results) == 0 @@ -821,10 +967,14 @@ def test_fetch_monthly_notification_statuses_per_service_for_rows_that_should_be def test_get_total_notifications_for_date_range(sample_service): template_sms = create_template( - service=sample_service, template_type=TemplateType.SMS, template_name="a", + service=sample_service, + template_type=TemplateType.SMS, + template_name="a", ) template_email = create_template( - service=sample_service, template_type=TemplateType.EMAIL, template_name="b", + service=sample_service, + template_type=TemplateType.EMAIL, + template_name="b", ) create_ft_notification_status( local_date=date(2021, 2, 28), diff --git a/tests/app/dao/test_inbound_sms_dao.py b/tests/app/dao/test_inbound_sms_dao.py index a53180965..9f3d6738d 100644 --- a/tests/app/dao/test_inbound_sms_dao.py +++ b/tests/app/dao/test_inbound_sms_dao.py @@ -66,10 +66,12 @@ def test_get_all_inbound_sms_filters_on_service(notify_db_session): def test_get_all_inbound_sms_filters_on_time(sample_service, notify_db_session): create_inbound_sms( - sample_service, created_at=datetime(2017, 8, 6, 23, 59), + sample_service, + created_at=datetime(2017, 8, 6, 23, 59), ) # sunday evening sms_two = create_inbound_sms( - sample_service, created_at=datetime(2017, 8, 7, 0, 0), + sample_service, + created_at=datetime(2017, 8, 7, 0, 0), ) # monday (7th) morning with freeze_time("2017-08-14 12:00"): @@ -111,13 +113,19 @@ def test_should_delete_inbound_sms_according_to_data_retention(notify_db_session services = [short_retention_service, no_retention_service, long_retention_service] create_service_data_retention( - long_retention_service, notification_type=NotificationType.SMS, days_of_retention=30, + long_retention_service, + notification_type=NotificationType.SMS, + days_of_retention=30, ) create_service_data_retention( - short_retention_service, notification_type=NotificationType.SMS, days_of_retention=3, + short_retention_service, + notification_type=NotificationType.SMS, + days_of_retention=3, ) create_service_data_retention( - short_retention_service, notification_type=NotificationType.EMAIL, days_of_retention=4, + short_retention_service, + notification_type=NotificationType.EMAIL, + days_of_retention=4, ) dates = [ diff --git a/tests/app/dao/test_provider_details_dao.py b/tests/app/dao/test_provider_details_dao.py index aa81ed0c0..8af524fa6 100644 --- a/tests/app/dao/test_provider_details_dao.py +++ b/tests/app/dao/test_provider_details_dao.py @@ -46,7 +46,9 @@ def test_can_get_sms_non_international_providers(notify_db_session): def test_can_get_sms_international_providers(notify_db_session): - sms_providers = get_provider_details_by_notification_type(NotificationType.SMS, True) + sms_providers = get_provider_details_by_notification_type( + NotificationType.SMS, True + ) assert len(sms_providers) == 1 assert all(NotificationType.SMS == prov.notification_type for prov in sms_providers) assert all(prov.supports_international for prov in sms_providers) @@ -68,9 +70,13 @@ def test_can_get_email_providers(notify_db_session): assert len(get_provider_details_by_notification_type(NotificationType.EMAIL)) == 1 types = [ provider.notification_type - for provider in get_provider_details_by_notification_type(NotificationType.EMAIL) + for provider in get_provider_details_by_notification_type( + NotificationType.EMAIL + ) ] - assert all(NotificationType.EMAIL == notification_type for notification_type in types) + assert all( + NotificationType.EMAIL == notification_type for notification_type in types + ) def test_should_not_error_if_any_provider_in_code_not_in_database( diff --git a/tests/app/dao/test_service_data_retention_dao.py b/tests/app/dao/test_service_data_retention_dao.py index 9ddc6263e..f800bd464 100644 --- a/tests/app/dao/test_service_data_retention_dao.py +++ b/tests/app/dao/test_service_data_retention_dao.py @@ -17,8 +17,16 @@ from tests.app.db import create_service, create_service_data_retention def test_fetch_service_data_retention(sample_service): - email_data_retention = insert_service_data_retention(sample_service.id, NotificationType.EMAIL, 3,) - sms_data_retention = insert_service_data_retention(sample_service.id, NotificationType.SMS, 5,) + email_data_retention = insert_service_data_retention( + sample_service.id, + NotificationType.EMAIL, + 3, + ) + sms_data_retention = insert_service_data_retention( + sample_service.id, + NotificationType.SMS, + 5, + ) list_of_data_retention = fetch_service_data_retention(sample_service.id) @@ -29,7 +37,11 @@ def test_fetch_service_data_retention(sample_service): def test_fetch_service_data_retention_only_returns_row_for_service(sample_service): another_service = create_service(service_name="Another service") - email_data_retention = insert_service_data_retention(sample_service.id, NotificationType.EMAIL, 3,) + email_data_retention = insert_service_data_retention( + sample_service.id, + NotificationType.EMAIL, + 3, + ) insert_service_data_retention(another_service.id, NotificationType.SMS, 5) list_of_data_retention = fetch_service_data_retention(sample_service.id) @@ -62,7 +74,11 @@ def test_fetch_service_data_retention_by_id_returns_none_if_id_not_for_service( sample_service, ): another_service = create_service(service_name="Another service") - email_data_retention = insert_service_data_retention(sample_service.id, NotificationType.EMAIL, 3,) + email_data_retention = insert_service_data_retention( + sample_service.id, + NotificationType.EMAIL, + 3, + ) result = fetch_service_data_retention_by_id( another_service.id, email_data_retention.id ) @@ -71,7 +87,9 @@ def test_fetch_service_data_retention_by_id_returns_none_if_id_not_for_service( def test_insert_service_data_retention(sample_service): insert_service_data_retention( - service_id=sample_service.id, notification_type=NotificationType.EMAIL, days_of_retention=3, + service_id=sample_service.id, + notification_type=NotificationType.EMAIL, + days_of_retention=3, ) results = ServiceDataRetention.query.all() @@ -84,17 +102,23 @@ def test_insert_service_data_retention(sample_service): def test_insert_service_data_retention_throws_unique_constraint(sample_service): insert_service_data_retention( - service_id=sample_service.id, notification_type=NotificationType.EMAIL, days_of_retention=3, + service_id=sample_service.id, + notification_type=NotificationType.EMAIL, + days_of_retention=3, ) with pytest.raises(expected_exception=IntegrityError): insert_service_data_retention( - service_id=sample_service.id, notification_type=NotificationType.EMAIL, days_of_retention=5, + service_id=sample_service.id, + notification_type=NotificationType.EMAIL, + days_of_retention=5, ) def test_update_service_data_retention(sample_service): data_retention = insert_service_data_retention( - service_id=sample_service.id, notification_type=NotificationType.SMS, days_of_retention=3 + service_id=sample_service.id, + notification_type=NotificationType.SMS, + days_of_retention=3, ) updated_count = update_service_data_retention( service_data_retention_id=data_retention.id, @@ -128,7 +152,9 @@ def test_update_service_data_retention_does_not_update_row_if_data_retention_is_ sample_service, ): data_retention = insert_service_data_retention( - service_id=sample_service.id, notification_type=NotificationType.EMAIL, days_of_retention=3, + service_id=sample_service.id, + notification_type=NotificationType.EMAIL, + days_of_retention=3, ) updated_count = update_service_data_retention( service_data_retention_id=data_retention.id, @@ -139,7 +165,11 @@ def test_update_service_data_retention_does_not_update_row_if_data_retention_is_ @pytest.mark.parametrize( - "notification_type, alternate", [(NotificationType.SMS, NotificationType.EMAIL), (NotificationType.EMAIL, NotificationType.SMS),], + "notification_type, alternate", + [ + (NotificationType.SMS, NotificationType.EMAIL), + (NotificationType.EMAIL, NotificationType.SMS), + ], ) def test_fetch_service_data_retention_by_notification_type( sample_service, notification_type, alternate @@ -158,5 +188,6 @@ def test_fetch_service_data_retention_by_notification_type_returns_none_when_no_ sample_service, ): assert not fetch_service_data_retention_by_notification_type( - sample_service.id, NotificationType.EMAIL, + sample_service.id, + NotificationType.EMAIL, ) diff --git a/tests/app/dao/test_services_dao.py b/tests/app/dao/test_services_dao.py index 44be3c50e..990315260 100644 --- a/tests/app/dao/test_services_dao.py +++ b/tests/app/dao/test_services_dao.py @@ -40,7 +40,15 @@ from app.dao.services_dao import ( get_services_by_partial_name, ) from app.dao.users_dao import create_user_code, save_model_user -from app.enums import KeyType, OrganizationType +from app.enums import ( + CodeType, + KeyType, + NotificationStatus, + NotificationType, + OrganizationType, + ServicePermissionType, + TemplateType, +) from app.models import ( ApiKey, InvitedUser, @@ -51,7 +59,6 @@ from app.models import ( Permission, Service, ServicePermission, - ServicePermissionType, ServiceUser, Template, TemplateHistory, @@ -766,7 +773,9 @@ def test_update_service_permission_creates_a_history_record_with_current_data( ), ) - permission = [p for p in service.permissions if p.permission == "sms"][0] + permission = [ + p for p in service.permissions if p.permission == ServicePermissionType.SMS + ][0] service.permissions.remove(permission) dao_update_service(service) @@ -824,8 +833,8 @@ def test_delete_service_and_associated_objects(notify_db_session): service = create_service( user=user, service_permissions=None, organization=organization ) - create_user_code(user=user, code="somecode", code_type="email") - create_user_code(user=user, code="somecode", code_type="sms") + create_user_code(user=user, code="somecode", code_type=CodeType.EMAIL) + create_user_code(user=user, code="somecode", code_type=CodeType.SMS) template = create_template(service=service) api_key = create_api_key(service=service) create_notification(template=template, api_key=api_key) @@ -953,27 +962,29 @@ def test_fetch_stats_ignores_historical_notification_data(sample_template): def test_dao_fetch_todays_stats_for_service(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) # two created email, one failed email, and one created sms - create_notification(template=email_template, status="created") - create_notification(template=email_template, status="created") - create_notification(template=email_template, status="technical-failure") - create_notification(template=sms_template, status="created") + create_notification(template=email_template, status=NotificationStatus.CREATED) + create_notification(template=email_template, status=NotificationStatus.CREATED) + create_notification( + template=email_template, status=NotificationStatus.TECHNICAL_FAILURE + ) + create_notification(template=sms_template, status=NotificationStatus.CREATED) stats = dao_fetch_todays_stats_for_service(service.id) stats = sorted(stats, key=lambda x: (x.notification_type, x.status)) assert len(stats) == 3 - assert stats[0].notification_type == "email" - assert stats[0].status == "created" + assert stats[0].notification_type == NotificationType.EMAIL + assert stats[0].status == NotificationStatus.CREATED assert stats[0].count == 2 - assert stats[1].notification_type == "email" - assert stats[1].status == "technical-failure" + assert stats[1].notification_type == NotificationType.EMAIL + assert stats[1].status == NotificationStatus.TECHNICAL_FAILURE assert stats[1].count == 1 - assert stats[2].notification_type == "sms" - assert stats[2].status == "created" + assert stats[2].notification_type == NotificationType.SMS + assert stats[2].status == NotificationStatus.CREATED assert stats[2].count == 1 @@ -998,8 +1009,8 @@ def test_dao_fetch_todays_stats_for_service_should_ignore_test_key(notify_db_ses stats = dao_fetch_todays_stats_for_service(service.id) assert len(stats) == 1 - assert stats[0].notification_type == "sms" - assert stats[0].status == "created" + assert stats[0].notification_type == NotificationType.SMS + assert stats[0].status == NotificationStatus.CREATED assert stats[0].count == 3 @@ -1008,15 +1019,27 @@ def test_dao_fetch_todays_stats_for_service_only_includes_today(notify_db_sessio # two created email, one failed email, and one created sms with freeze_time("2001-01-02T04:59:00"): # just_before_midnight_yesterday - create_notification(template=template, to_field="1", status="delivered") + create_notification( + template=template, + to_field="1", + status=NotificationStatus.DELIVERED, + ) with freeze_time("2001-01-02T05:01:00"): # just_after_midnight_today - create_notification(template=template, to_field="2", status="failed") + create_notification( + template=template, + to_field="2", + status=NotificationStatus.FAILED, + ) with freeze_time("2001-01-02T12:00:00"): # right_now - create_notification(template=template, to_field="3", status="created") + create_notification( + template=template, + to_field="3", + status=NotificationStatus.CREATED, + ) stats = dao_fetch_todays_stats_for_service(template.service_id) @@ -1033,14 +1056,26 @@ def test_dao_fetch_todays_stats_for_service_only_includes_today_when_clocks_spri template = create_template(service=create_service()) with freeze_time("2021-03-27T23:59:59"): # just before midnight yesterday in UTC -- not included - create_notification(template=template, to_field="1", status="permanent-failure") + create_notification( + template=template, + to_field="1", + status=NotificationStatus.PERMANENT_FAILURE, + ) with freeze_time("2021-03-28T00:01:00"): # just after midnight yesterday in UTC -- included - create_notification(template=template, to_field="2", status="failed") + create_notification( + template=template, + to_field="2", + status=NotificationStatus.FAILED, + ) with freeze_time("2021-03-28T12:00:00"): # we have entered BST at this point but had not for the previous two notifications --included # collect stats for this timestamp - create_notification(template=template, to_field="3", status="created") + create_notification( + template=template, + to_field="3", + status=NotificationStatus.CREATED, + ) stats = dao_fetch_todays_stats_for_service(template.service_id) stats = {row.status: row.count for row in stats} @@ -1057,14 +1092,20 @@ def test_dao_fetch_todays_stats_for_service_only_includes_today_during_bst( template = create_template(service=create_service()) with freeze_time("2021-03-28T23:59:59"): # just before midnight BST -- not included - create_notification(template=template, to_field="1", status="permanent-failure") + create_notification( + template=template, to_field="1", status=NotificationStatus.PERMANENT_FAILURE + ) with freeze_time("2021-03-29T04:00:01"): # just after midnight BST -- included - create_notification(template=template, to_field="2", status="failed") + create_notification( + template=template, to_field="2", status=NotificationStatus.FAILED + ) with freeze_time("2021-03-29T12:00:00"): # well after midnight BST -- included # collect stats for this timestamp - create_notification(template=template, to_field="3", status="created") + create_notification( + template=template, to_field="3", status=NotificationStatus.CREATED + ) stats = dao_fetch_todays_stats_for_service(template.service_id) stats = {row.status: row.count for row in stats} @@ -1080,15 +1121,21 @@ def test_dao_fetch_todays_stats_for_service_only_includes_today_when_clocks_fall template = create_template(service=create_service()) with freeze_time("2021-10-30T22:59:59"): # just before midnight BST -- not included - create_notification(template=template, to_field="1", status="permanent-failure") + create_notification( + template=template, to_field="1", status=NotificationStatus.PERMANENT_FAILURE + ) with freeze_time("2021-10-31T23:00:01"): # just after midnight BST -- included - create_notification(template=template, to_field="2", status="failed") + create_notification( + template=template, to_field="2", status=NotificationStatus.FAILED + ) # clocks go back to UTC on 31 October at 2am with freeze_time("2021-10-31T12:00:00"): # well after midnight -- included # collect stats for this timestamp - create_notification(template=template, to_field="3", status="created") + create_notification( + template=template, to_field="3", status=NotificationStatus.CREATED + ) stats = dao_fetch_todays_stats_for_service(template.service_id) stats = {row.status: row.count for row in stats} @@ -1102,15 +1149,21 @@ def test_dao_fetch_todays_stats_for_service_only_includes_during_utc(notify_db_s template = create_template(service=create_service()) with freeze_time("2021-10-30T12:59:59"): # just before midnight UTC -- not included - create_notification(template=template, to_field="1", status="permanent-failure") + create_notification( + template=template, to_field="1", status=NotificationStatus.PERMANENT_FAILURE + ) with freeze_time("2021-10-31T05:00:01"): # just after midnight UTC -- included - create_notification(template=template, to_field="2", status="failed") + create_notification( + template=template, to_field="2", status=NotificationStatus.FAILED + ) # clocks go back to UTC on 31 October at 2am with freeze_time("2021-10-31T12:00:00"): # well after midnight -- included # collect stats for this timestamp - create_notification(template=template, to_field="3", status="created") + create_notification( + template=template, to_field="3", status=NotificationStatus.CREATED + ) stats = dao_fetch_todays_stats_for_service(template.service_id) stats = {row.status: row.count for row in stats} @@ -1126,10 +1179,14 @@ def test_dao_fetch_todays_stats_for_all_services_includes_all_services( # two services, each with an email and sms notification service1 = create_service(service_name="service 1", email_from="service.1") service2 = create_service(service_name="service 2", email_from="service.2") - template_email_one = create_template(service=service1, template_type="email") - template_sms_one = create_template(service=service1, template_type="sms") - template_email_two = create_template(service=service2, template_type="email") - template_sms_two = create_template(service=service2, template_type="sms") + template_email_one = create_template( + service=service1, template_type=TemplateType.EMAIL + ) + template_sms_one = create_template(service=service1, template_type=TemplateType.SMS) + template_email_two = create_template( + service=service2, template_type=TemplateType.EMAIL + ) + template_sms_two = create_template(service=service2, template_type=TemplateType.SMS) create_notification(template=template_email_one) create_notification(template=template_sms_one) create_notification(template=template_email_two) @@ -1146,11 +1203,15 @@ def test_dao_fetch_todays_stats_for_all_services_only_includes_today(notify_db_s template = create_template(service=create_service()) with freeze_time("2001-01-01T23:59:00"): # just_before_midnight_yesterday - create_notification(template=template, to_field="1", status="delivered") + create_notification( + template=template, to_field="1", status=NotificationStatus.DELIVERED + ) with freeze_time("2001-01-02T05:01:00"): # just_after_midnight_today - create_notification(template=template, to_field="2", status="failed") + create_notification( + template=template, to_field="2", status=NotificationStatus.FAILED + ) with freeze_time("2001-01-02T12:00:00"): stats = dao_fetch_todays_stats_for_all_services() @@ -1164,12 +1225,12 @@ def test_dao_fetch_todays_stats_for_all_services_groups_correctly(notify_db_sess service1 = create_service(service_name="service 1", email_from="service.1") service2 = create_service(service_name="service 2", email_from="service.2") template_sms = create_template(service=service1) - template_email = create_template(service=service1, template_type="email") + template_email = create_template(service=service1, template_type=TemplateType.EMAIL) template_two = create_template(service=service2) # service1: 2 sms with status "created" and one "failed", and one email create_notification(template=template_sms) create_notification(template=template_sms) - create_notification(template=template_sms, status="failed") + create_notification(template=template_sms, status=NotificationStatus.FAILED) create_notification(template=template_email) # service2: 1 sms "created" create_notification(template=template_two) @@ -1182,8 +1243,8 @@ def test_dao_fetch_todays_stats_for_all_services_groups_correctly(notify_db_sess service1.restricted, service1.active, service1.created_at, - "sms", - "created", + NotificationType.SMS, + NotificationStatus.CREATED, 2, ) in stats assert ( @@ -1192,8 +1253,8 @@ def test_dao_fetch_todays_stats_for_all_services_groups_correctly(notify_db_sess service1.restricted, service1.active, service1.created_at, - "sms", - "failed", + NotificationType.SMS, + NotificationStatus.FAILED, 1, ) in stats assert ( @@ -1202,8 +1263,8 @@ def test_dao_fetch_todays_stats_for_all_services_groups_correctly(notify_db_sess service1.restricted, service1.active, service1.created_at, - "email", - "created", + NotificationType.EMAIL, + NotificationStatus.CREATED, 1, ) in stats assert ( @@ -1212,8 +1273,8 @@ def test_dao_fetch_todays_stats_for_all_services_groups_correctly(notify_db_sess service2.restricted, service2.active, service2.created_at, - "sms", - "created", + NotificationType.SMS, + NotificationType.CREATED, 1, ) in stats @@ -1388,7 +1449,9 @@ def test_dao_find_services_sending_to_tv_numbers(notify_db_session, fake_uuid): template = create_template(service) for _ in range(0, 5): create_notification( - template, normalised_to=tv_number, status="permanent-failure" + template, + normalised_to=tv_number, + status=NotificationStatus.PERMANENT_FAILURE, ) service_6 = create_service( @@ -1398,27 +1461,35 @@ def test_dao_find_services_sending_to_tv_numbers(notify_db_session, fake_uuid): template_6 = create_template(service_6) for _ in range(0, 5): create_notification( - template_6, normalised_to=tv_number, status="permanent-failure" + template_6, + normalised_to=tv_number, + status=NotificationStatus.PERMANENT_FAILURE, ) service_2 = create_service(service_name="Service 2") # below threshold is excluded template_2 = create_template(service_2) - create_notification(template_2, normalised_to=tv_number, status="permanent-failure") + create_notification( + template_2, + normalised_to=tv_number, + status=NotificationStatus.PERMANENT_FAILURE, + ) for _ in range(0, 5): # test key type is excluded create_notification( template_2, normalised_to=tv_number, - status="permanent-failure", - key_type="test", + status=NotificationStatus.PERMANENT_FAILURE, + key_type=KeyType.TEST, ) for _ in range(0, 5): # normal numbers are not counted by the query - create_notification(template_2, normalised_to=normal_number, status="delivered") + create_notification( + template_2, normalised_to=normal_number, status=NotificationStatus.DELIVERED + ) create_notification( template_2, normalised_to=normal_number_resembling_tv_number, - status="delivered", + status=NotificationStatus.DELIVERED, ) start_date = datetime.utcnow() - timedelta(days=1) @@ -1442,27 +1513,31 @@ def test_dao_find_services_with_high_failure_rates(notify_db_session, fake_uuid) for service in services: template = create_template(service) for _ in range(0, 3): - create_notification(template, status="permanent-failure") - create_notification(template, status="delivered") - create_notification(template, status="sending") - create_notification(template, status="temporary-failure") + create_notification(template, status=NotificationStatus.PERMANENT_FAILURE) + create_notification(template, status=NotificationStatus.DELIVERED) + create_notification(template, status=NotificationStatus.SENDING) + create_notification(template, status=NotificationStatus.TEMPORART_FAILURE) service_6 = create_service(service_name="Service 6") with freeze_time("2019-11-30 15:00:00.000000"): template_6 = create_template(service_6) for _ in range(0, 4): create_notification( - template_6, status="permanent-failure" + template_6, + status=NotificationStatus.PERMANENT_FAILURE, ) # notifications too old are excluded service_2 = create_service(service_name="Service 2") template_2 = create_template(service_2) for _ in range(0, 4): create_notification( - template_2, status="permanent-failure", key_type="test" + template_2, + status=NotificationStatus.PERMANENT_FAILURE, + key_type=KeyType.TEST, ) # test key type is excluded create_notification( - template_2, status="permanent-failure" + template_2, + status=NotificationStatus.PERMANET_FAILURE, ) # below threshold is excluded start_date = datetime.utcnow() - timedelta(days=1) diff --git a/tests/app/test_model.py b/tests/app/test_model.py index 461056a26..67a83e661 100644 --- a/tests/app/test_model.py +++ b/tests/app/test_model.py @@ -150,7 +150,11 @@ def test_notification_for_csv_returns_correct_job_row_number(sample_job): [ (TemplateType.EMAIL, "failed", "Failed"), (TemplateType.EMAIL, "technical-failure", "Technical failure"), - (TemplateType.EMAIL, "temporary-failure", "Inbox not accepting messages right now",), + ( + TemplateType.EMAIL, + "temporary-failure", + "Inbox not accepting messages right now", + ), (TemplateType.EMAIL, "permanent-failure", "Email address doesn’t exist"), ( TemplateType.SMS, diff --git a/tests/app/v2/templates/test_get_templates.py b/tests/app/v2/templates/test_get_templates.py index 396d663fd..ebed4fbcf 100644 --- a/tests/app/v2/templates/test_get_templates.py +++ b/tests/app/v2/templates/test_get_templates.py @@ -1,5 +1,3 @@ -from itertools import product - import pytest from flask import json