diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index 3eb999d3b..3f46577b3 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -1,5 +1,4 @@ import functools -import string from itertools import groupby from operator import attrgetter from datetime import ( @@ -570,8 +569,6 @@ def dao_update_notifications_by_reference(references, update_dict): @statsd(namespace="dao") def dao_get_notifications_by_recipient_or_reference(service_id, search_term, notification_type=None, statuses=None): - if notification_type is None: - notification_type = guess_notification_type(search_term) if notification_type == SMS_TYPE: normalised = try_validate_and_format_phone_number(search_term) @@ -587,9 +584,12 @@ def dao_get_notifications_by_recipient_or_reference(service_id, search_term, not except InvalidEmailError: normalised = search_term.lower() - else: + elif notification_type == LETTER_TYPE: raise InvalidRequest("Only email and SMS can use search by recipient", 400) + else: + normalised = search_term.lower() + normalised = escape_special_characters(normalised) search_term = escape_special_characters(search_term) @@ -765,13 +765,6 @@ def dao_precompiled_letters_still_pending_virus_check(): return notifications -def guess_notification_type(search_term): - if set(search_term) & set(string.ascii_letters + '@'): - return EMAIL_TYPE - else: - return SMS_TYPE - - def _duplicate_update_warning(notification, status): current_app.logger.info( ( diff --git a/app/service/rest.py b/app/service/rest.py index 60141e87e..7cdd259d5 100644 --- a/app/service/rest.py +++ b/app/service/rest.py @@ -455,7 +455,7 @@ def cancel_notification_for_service(service_id, notification_id): def search_for_notification_by_to_field(service_id, search_term, statuses, notification_type): - results = notifications_dao.dao_get_notifications_by_to_field( + results = notifications_dao.dao_get_notifications_by_recipient_or_reference( service_id=service_id, search_term=search_term, statuses=statuses, diff --git a/tests/app/dao/notification_dao/test_notification_dao.py b/tests/app/dao/notification_dao/test_notification_dao.py index 546288c2c..7cfa9e76e 100644 --- a/tests/app/dao/notification_dao/test_notification_dao.py +++ b/tests/app/dao/notification_dao/test_notification_dao.py @@ -1272,18 +1272,23 @@ def test_dao_get_notifications_by_recipient_searches_across_notification_types( email = create_notification( template=email_template, to_field='077@example.com', normalised_to='077@example.com' ) - results = dao_get_notifications_by_recipient_or_reference(service.id, phone_search, notification_type='sms') + + results = dao_get_notifications_by_recipient_or_reference( + service.id, phone_search, notification_type='sms' + ) assert len(results) == 1 assert results[0].id == sms.id - results = dao_get_notifications_by_recipient_or_reference(service.id, phone_search) # should assume SMS - assert len(results) == 1 - assert results[0].id == sms.id - results = dao_get_notifications_by_recipient_or_reference(service.id, '077', notification_type='email') + + results = dao_get_notifications_by_recipient_or_reference( + service.id, email_search, notification_type='email' + ) assert len(results) == 1 assert results[0].id == email.id - results = dao_get_notifications_by_recipient_or_reference(service.id, email_search) # should assume email - assert len(results) == 1 + + results = dao_get_notifications_by_recipient_or_reference(service.id, '77') + assert len(results) == 2 assert results[0].id == email.id + assert results[1].id == sms.id def test_dao_get_notifications_by_reference( @@ -1307,10 +1312,11 @@ def test_dao_get_notifications_by_reference( results = dao_get_notifications_by_recipient_or_reference(service.id, '77') assert len(results) == 2 - assert results[0].id == sms.id assert results[0].id == email.id + assert results[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 + # 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) == 1 assert results[0].id == email.id @@ -1318,21 +1324,26 @@ def test_dao_get_notifications_by_reference( results = dao_get_notifications_by_recipient_or_reference(service.id, '077', notification_type='sms') assert len(results) == 1 assert results[0].id == sms.id + results = dao_get_notifications_by_recipient_or_reference(service.id, '77', notification_type='sms') assert len(results) == 1 assert results[0].id == sms.id + results = dao_get_notifications_by_recipient_or_reference(service.id, 'Aa', notification_type='sms') assert len(results) == 1 assert results[0].id == sms.id + results = dao_get_notifications_by_recipient_or_reference(service.id, 'bB', notification_type='sms') assert len(results) == 0 results = dao_get_notifications_by_recipient_or_reference(service.id, '77', notification_type='email') assert len(results) == 1 assert results[0].id == email.id + results = dao_get_notifications_by_recipient_or_reference(service.id, 'Bb', notification_type='email') assert len(results) == 1 assert results[0].id == email.id + results = dao_get_notifications_by_recipient_or_reference(service.id, 'aA', notification_type='email') assert len(results) == 0