mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-23 15:56:45 -04:00
Merge pull request #1905 from alphagov/smart-phone-search
Search for emails or texts, depending on term
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import functools
|
import functools
|
||||||
|
import string
|
||||||
from datetime import (
|
from datetime import (
|
||||||
datetime,
|
datetime,
|
||||||
timedelta,
|
timedelta,
|
||||||
@@ -425,7 +426,10 @@ def dao_update_notifications_by_reference(references, update_dict):
|
|||||||
|
|
||||||
|
|
||||||
@statsd(namespace="dao")
|
@statsd(namespace="dao")
|
||||||
def dao_get_notifications_by_to_field(service_id, search_term, notification_type, statuses=None):
|
def dao_get_notifications_by_to_field(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:
|
if notification_type == SMS_TYPE:
|
||||||
normalised = try_validate_and_format_phone_number(search_term)
|
normalised = try_validate_and_format_phone_number(search_term)
|
||||||
@@ -440,6 +444,7 @@ def dao_get_notifications_by_to_field(service_id, search_term, notification_type
|
|||||||
normalised = validate_and_format_email_address(search_term)
|
normalised = validate_and_format_email_address(search_term)
|
||||||
except InvalidEmailError:
|
except InvalidEmailError:
|
||||||
normalised = search_term.lower()
|
normalised = search_term.lower()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise InvalidRequest("Only email and SMS can use search by recipient", 400)
|
raise InvalidRequest("Only email and SMS can use search by recipient", 400)
|
||||||
|
|
||||||
@@ -597,3 +602,10 @@ def notifications_not_yet_sent(should_be_sending_after_seconds, notification_typ
|
|||||||
Notification.status == NOTIFICATION_CREATED
|
Notification.status == NOTIFICATION_CREATED
|
||||||
).all()
|
).all()
|
||||||
return notifications
|
return notifications
|
||||||
|
|
||||||
|
|
||||||
|
def guess_notification_type(search_term):
|
||||||
|
if set(search_term) & set(string.ascii_letters + '@'):
|
||||||
|
return EMAIL_TYPE
|
||||||
|
else:
|
||||||
|
return SMS_TYPE
|
||||||
|
|||||||
@@ -1553,20 +1553,34 @@ def test_dao_get_notifications_by_to_field_search_ignores_spaces(sample_template
|
|||||||
assert notification3.id in notification_ids
|
assert notification3.id in notification_ids
|
||||||
|
|
||||||
|
|
||||||
def test_dao_get_notifications_by_to_field_only_searches_for_notification_type(
|
@pytest.mark.parametrize('phone_search', (
|
||||||
notify_db_session
|
'077', '7-7', '+44(0)7711 111111'
|
||||||
|
))
|
||||||
|
@pytest.mark.parametrize('email_search', (
|
||||||
|
'example', 'eXaMpLe',
|
||||||
|
))
|
||||||
|
def test_dao_get_notifications_by_to_field_only_searches_one_notification_type(
|
||||||
|
notify_db_session,
|
||||||
|
phone_search,
|
||||||
|
email_search,
|
||||||
):
|
):
|
||||||
service = create_service()
|
service = create_service()
|
||||||
sms_template = create_template(service=service)
|
sms_template = create_template(service=service)
|
||||||
email_template = create_template(service=service, template_type='email')
|
email_template = create_template(service=service, template_type='email')
|
||||||
sms = create_notification(template=sms_template, to_field='0771111111', normalised_to='0771111111')
|
sms = create_notification(template=sms_template, to_field='07711111111', normalised_to='447711111111')
|
||||||
email = create_notification(
|
email = create_notification(
|
||||||
template=email_template, to_field='077@example.com', normalised_to='077@example.com'
|
template=email_template, to_field='077@example.com', normalised_to='077@example.com'
|
||||||
)
|
)
|
||||||
results = dao_get_notifications_by_to_field(service.id, "077", notification_type='sms')
|
results = dao_get_notifications_by_to_field(service.id, phone_search, notification_type='sms')
|
||||||
assert len(results) == 1
|
assert len(results) == 1
|
||||||
assert results[0].id == sms.id
|
assert results[0].id == sms.id
|
||||||
results = dao_get_notifications_by_to_field(service.id, "077", notification_type='email')
|
results = dao_get_notifications_by_to_field(service.id, phone_search) # should assume SMS
|
||||||
|
assert len(results) == 1
|
||||||
|
assert results[0].id == sms.id
|
||||||
|
results = dao_get_notifications_by_to_field(service.id, '077', notification_type='email')
|
||||||
|
assert len(results) == 1
|
||||||
|
assert results[0].id == email.id
|
||||||
|
results = dao_get_notifications_by_to_field(service.id, email_search) # should assume email
|
||||||
assert len(results) == 1
|
assert len(results) == 1
|
||||||
assert results[0].id == email.id
|
assert results[0].id == email.id
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user