diff --git a/tests/app/dao/notification_dao/test_notification_dao.py b/tests/app/dao/notification_dao/test_notification_dao.py index 356e648be..18903cc95 100644 --- a/tests/app/dao/notification_dao/test_notification_dao.py +++ b/tests/app/dao/notification_dao/test_notification_dao.py @@ -41,7 +41,6 @@ from app.enums import ( KeyType, NotificationStatus, NotificationType, - TemplateType, ) from app.models import Job, Notification, NotificationHistory from app.utils import utc_now @@ -1217,584 +1216,6 @@ def test_should_exclude_test_key_notifications_by_default( assert len(all_notifications) == 1 -@pytest.mark.skip( - reason="We can't search on recipient if recipient is not kept in the db" -) -def test_dao_get_notifications_by_recipient(sample_template): - recipient_to_search_for = { - "to_field": "+447700900855", - "normalised_to": "447700900855", - } - - notification1 = create_notification( - template=sample_template, - **recipient_to_search_for, - ) - create_notification( - template=sample_template, - key_type=KeyType.TEST, - **recipient_to_search_for, - ) - create_notification( - template=sample_template, - to_field="jack@gmail.com", - normalised_to="jack@gmail.com", - ) - create_notification( - template=sample_template, - to_field="jane@gmail.com", - normalised_to="jane@gmail.com", - ) - - results = dao_get_notifications_by_recipient_or_reference( - notification1.service_id, - recipient_to_search_for["to_field"], - notification_type=NotificationType.SMS, - ) - - assert len(results.items) == 1 - assert notification1.id == results.items[0].id - - -@pytest.mark.skip( - reason="We can't search on recipient if recipient is not kept in the db" -) -def test_dao_get_notifications_by_recipient_is_limited_to_50_results(sample_template): - for _ in range(100): - create_notification( - template=sample_template, - to_field="+447700900855", - normalised_to="447700900855", - ) - - results = dao_get_notifications_by_recipient_or_reference( - sample_template.service_id, - "447700900855", - notification_type=NotificationType.SMS, - page_size=50, - ) - - assert len(results.items) == 50 - - -@pytest.mark.skip( - reason="We can't search on recipient if recipient is not kept in the db" -) -@pytest.mark.parametrize("search_term", ["JACK", "JACK@gmail.com", "jack@gmail.com"]) -def test_dao_get_notifications_by_recipient_is_not_case_sensitive( - sample_email_template, search_term -): - notification = create_notification( - template=sample_email_template, - to_field="jack@gmail.com", - normalised_to="jack@gmail.com", - ) - results = dao_get_notifications_by_recipient_or_reference( - notification.service_id, - search_term, - notification_type=NotificationType.EMAIL, - ) - notification_ids = [notification.id for notification in results.items] - - assert len(results.items) == 1 - assert notification.id in notification_ids - - -@pytest.mark.skip( - reason="We can't search on recipient if recipient is not kept in the db" -) -def test_dao_get_notifications_by_recipient_matches_partial_emails( - sample_email_template, -): - notification_1 = create_notification( - template=sample_email_template, - to_field="jack@gmail.com", - normalised_to="jack@gmail.com", - ) - notification_2 = create_notification( - template=sample_email_template, - to_field="jacque@gmail.com", - normalised_to="jacque@gmail.com", - ) - results = dao_get_notifications_by_recipient_or_reference( - notification_1.service_id, - "ack", - notification_type=NotificationType.EMAIL, - ) - notification_ids = [notification.id for notification in results.items] - - assert len(results.items) == 1 - assert notification_1.id in notification_ids - assert notification_2.id not in notification_ids - - -@pytest.mark.skip( - reason="We can't search on recipient if recipient is not kept in the db" -) -@pytest.mark.parametrize( - "search_term, expected_result_count", - [ - ("foobar", 1), - ("foo", 2), - ("bar", 2), - ("foo%", 1), - ("%%bar", 1), - ("%_", 1), - ("%", 2), - ("_", 1), - ("/", 1), - ("\\", 1), - ("baz\\baz", 1), - ("%foo", 0), - ("%_%", 0), - ("example.com", 5), - ], -) -def test_dao_get_notifications_by_recipient_escapes( - sample_email_template, - search_term, - expected_result_count, -): - for email_address in { - "foo%_@example.com", - "%%bar@example.com", - "foobar@example.com", - "/@example.com", - "baz\\baz@example.com", - }: - create_notification( - template=sample_email_template, - to_field=email_address, - normalised_to=email_address, - ) - - assert ( - len( - dao_get_notifications_by_recipient_or_reference( - sample_email_template.service_id, - search_term, - notification_type=NotificationType.EMAIL, - ).items - ) - == expected_result_count - ) - - -@pytest.mark.skip( - reason="We can't search on recipient if recipient is not kept in the db" -) -@pytest.mark.parametrize( - "search_term, expected_result_count", - [ - ("foobar", 1), - ("foo", 2), - ("bar", 2), - ("foo%", 1), - ("%%bar", 1), - ("%_", 1), - ("%", 2), - ("_", 1), - ("/", 1), - ("\\", 1), - ("baz\\baz", 1), - ("%foo", 0), - ("%_%", 0), - ("test@example.com", 5), - ], -) -def test_dao_get_notifications_by_reference_escapes_special_character( - sample_email_template, - search_term, - expected_result_count, -): - for reference in { - "foo%_", - "%%bar", - "foobar", - "/", - "baz\\baz", - }: - create_notification( - template=sample_email_template, - to_field="test@example.com", - normalised_to="test@example.com", - client_reference=reference, - ) - - assert ( - len( - dao_get_notifications_by_recipient_or_reference( - sample_email_template.service_id, - search_term, - notification_type=NotificationType.EMAIL, - ).items - ) - == expected_result_count - ) - - -@pytest.mark.skip( - reason="We can't search on recipient if recipient is not kept in the db" -) -@pytest.mark.parametrize( - "search_term", - [ - "309", - "530", - "8675309", - "202867", - "202 867", - "202-867-5309", - "2028675309", - "+12028675309", - ], -) -def test_dao_get_notifications_by_recipient_matches_partial_phone_numbers( - sample_template, - search_term, -): - notification_1 = create_notification( - template=sample_template, - to_field="202-867-5309", - normalised_to="+12028675309", - ) - notification_2 = create_notification( - template=sample_template, - to_field="202-678-5000", - normalised_to="+12026785000", - ) - results = dao_get_notifications_by_recipient_or_reference( - notification_1.service_id, - search_term, - notification_type=NotificationType.SMS, - ) - notification_ids = [notification.id for notification in results.items] - - assert len(results.items) == 1 - assert notification_1.id in notification_ids - assert notification_2.id not in notification_ids - - -@pytest.mark.skip( - reason="We can't search on recipient if recipient is not kept in the db" -) -@pytest.mark.parametrize("to", ["not@email", "123"]) -def test_dao_get_notifications_by_recipient_accepts_invalid_phone_numbers_and_email_addresses( - sample_template, - to, -): - notification = create_notification( - template=sample_template, - to_field="test@example.com", - normalised_to="test@example.com", - ) - results = dao_get_notifications_by_recipient_or_reference( - notification.service_id, - to, - notification_type=NotificationType.EMAIL, - ) - assert len(results.items) == 0 - - -@pytest.mark.skip( - reason="We can't search on recipient if recipient is not kept in the db" -) -def test_dao_get_notifications_by_recipient_ignores_spaces(sample_template): - notification1 = create_notification( - template=sample_template, to_field="+447700900855", normalised_to="447700900855" - ) - notification2 = create_notification( - template=sample_template, - to_field="+44 77 00900 855", - normalised_to="447700900855", - ) - notification3 = create_notification( - template=sample_template, - to_field=" +4477009 00 855 ", - normalised_to="447700900855", - ) - create_notification( - template=sample_template, - to_field="jaCK@gmail.com", - normalised_to="jack@gmail.com", - ) - - results = dao_get_notifications_by_recipient_or_reference( - notification1.service_id, - "+447700900855", - notification_type=NotificationType.SMS, - ) - notification_ids = [notification.id for notification in results.items] - - assert len(results.items) == 3 - assert notification1.id in notification_ids - assert notification2.id in notification_ids - assert notification3.id in notification_ids - - -@pytest.mark.skip( - reason="We can't search on recipient if recipient is not kept in the db" -) -@pytest.mark.parametrize("phone_search", ("202", "7-5", "+1 (202) 867-5309")) -@pytest.mark.parametrize( - "email_search", - ( - "example", - "eXaMpLe", - ), -) -def test_dao_get_notifications_by_recipient_searches_across_notification_types( - notify_db_session, - phone_search, - email_search, -): - service = create_service() - sms_template = create_template(service=service) - email_template = create_template(service=service, template_type=TemplateType.EMAIL) - sms = create_notification( - template=sms_template, - to_field="202-867-5309", - normalised_to="+12028675309", - ) - email = create_notification( - template=email_template, - to_field="202@example.com", - normalised_to="202@example.com", - ) - - results = dao_get_notifications_by_recipient_or_reference( - 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=NotificationType.EMAIL, - ) - assert len(results.items) == 1 - assert results.items[0].id == email.id - - results = dao_get_notifications_by_recipient_or_reference(service.id, "202") - assert len(results.items) == 2 - assert results.items[0].id == email.id - assert results.items[1].id == sms.id - - -@pytest.mark.skip( - reason="We can't search on recipient if recipient is not kept in the db" -) -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=TemplateType.EMAIL, - ) - sms = create_notification( - template=sms_template, - to_field="07711111111", - normalised_to="447711111111", - client_reference="77aA", - ) - email = create_notification( - template=email_template, - to_field="077@example.com", - normalised_to="077@example.com", - client_reference="77bB", - ) - - results = dao_get_notifications_by_recipient_or_reference(service.id, "77") - assert len(results.items) == 2 - assert results.items[0].id == email.id - assert results.items[1].id == sms.id - - # If notification_type isn’t specified then we can’t normalise the - # phone number to 4477… so this query will only find the email sent - # to 077@example.com - results = dao_get_notifications_by_recipient_or_reference(service.id, "077") - assert len(results.items) == 1 - assert results.items[0].id == email.id - - results = dao_get_notifications_by_recipient_or_reference(service.id, "077@") - assert len(results.items) == 1 - assert results.items[0].id == email.id - - results = dao_get_notifications_by_recipient_or_reference( - 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=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=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=NotificationType.SMS, - ) - assert len(results.items) == 0 - - results = dao_get_notifications_by_recipient_or_reference( - 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=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=NotificationType.EMAIL, - ) - assert len(results.items) == 0 - - -@pytest.mark.skip( - reason="We can't search on recipient if recipient is not kept in the db" -) -def test_dao_get_notifications_by_to_field_filters_status(sample_template): - notification = create_notification( - template=sample_template, - to_field="+447700900855", - normalised_to="447700900855", - status=NotificationStatus.DELIVERED, - ) - create_notification( - template=sample_template, - to_field="+447700900855", - normalised_to="447700900855", - status=NotificationStatus.TEMPORARY_FAILURE, - ) - - notifications = dao_get_notifications_by_recipient_or_reference( - notification.service_id, - "+447700900855", - statuses=[NotificationStatus.DELIVERED], - notification_type=NotificationStatus.SMS, - ) - - assert len(notifications.items) == 1 - assert notification.id == notifications.items[0].id - - -@pytest.mark.skip( - reason="We can't search on recipient if recipient is not kept in the db" -) -def test_dao_get_notifications_by_to_field_filters_multiple_statuses(sample_template): - notification1 = create_notification( - template=sample_template, - to_field="+447700900855", - normalised_to="447700900855", - status=NotificationStatus.DELIVERED, - ) - notification2 = create_notification( - template=sample_template, - to_field="+447700900855", - normalised_to="447700900855", - status=NotificationStatus.SENDING, - ) - - notifications = dao_get_notifications_by_recipient_or_reference( - notification1.service_id, - "+447700900855", - statuses=[NotificationStatus.DELIVERED, NotificationStatus.SENDING], - notification_type=NotificationType.SMS, - ) - notification_ids = [notification.id for notification in notifications.items] - - assert len(notifications.items) == 2 - assert notification1.id in notification_ids - assert notification2.id in notification_ids - - -@pytest.mark.skip( - reason="We can't search on recipient if recipient is not kept in the db" -) -def test_dao_get_notifications_by_to_field_returns_all_if_no_status_filter( - sample_template, -): - notification1 = create_notification( - template=sample_template, - to_field="+447700900855", - normalised_to="447700900855", - status=NotificationStatus.DELIVERED, - ) - notification2 = create_notification( - template=sample_template, - to_field="+447700900855", - normalised_to="447700900855", - status=NotificationStatus.TEMPORARY_FAILURE, - ) - - notifications = dao_get_notifications_by_recipient_or_reference( - notification1.service_id, - "+447700900855", - notification_type=NotificationType.SMS, - ) - notification_ids = [notification.id for notification in notifications.items] - - assert len(notifications.items) == 2 - assert notification1.id in notification_ids - assert notification2.id in notification_ids - - -@pytest.mark.skip( - reason="We can't search on recipient if recipient is not kept in the db" -) -@freeze_time("2016-01-01 11:10:00") -def test_dao_get_notifications_by_to_field_orders_by_created_at_desc(sample_template): - notification = partial( - create_notification, - template=sample_template, - to_field="+447700900855", - normalised_to="447700900855", - ) - - notification_a_minute_ago = notification( - created_at=utc_now() - timedelta(minutes=1) - ) - notification = notification(created_at=utc_now()) - - notifications = dao_get_notifications_by_recipient_or_reference( - sample_template.service_id, - "+447700900855", - notification_type=NotificationType.SMS, - ) - - assert len(notifications.items) == 2 - assert notifications.items[0].id == notification.id - assert notifications.items[1].id == notification_a_minute_ago.id - - def test_dao_get_last_notification_added_for_job_id_valid_job_id(sample_template): job = create_job( template=sample_template,