More cleanup.

Signed-off-by: Cliff Hill <Clifford.hill@gsa.gov>
This commit is contained in:
Cliff Hill
2024-02-13 11:04:02 -05:00
parent acecf9792d
commit 63aa12e4ef
15 changed files with 662 additions and 203 deletions

View File

@@ -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"

View File

@@ -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(

View File

@@ -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()

View File

@@ -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,

View File

@@ -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(