Merge pull request #1745 from alphagov/match-on-partial-email-addresses

Match on partial email addresses and phone numbers in search
This commit is contained in:
Chris Hill-Scott
2018-03-12 09:48:50 +00:00
committed by GitHub
4 changed files with 182 additions and 37 deletions

View File

@@ -1733,24 +1733,77 @@ def test_dao_get_notifications_by_to_field(sample_template):
results = dao_get_notifications_by_to_field(
notification1.service_id,
recipient_to_search_for["to_field"]
recipient_to_search_for["to_field"],
notification_type='sms'
)
assert len(results) == 1
assert notification1.id == results[0].id
def test_dao_get_notifications_by_to_field_search_is_not_case_sensitive(sample_template):
@pytest.mark.parametrize("search_term",
["JACK", "JACK@gmail.com", "jack@gmail.com"])
def test_dao_get_notifications_by_to_field_search_is_not_case_sensitive(sample_email_template, search_term):
notification = create_notification(
template=sample_template, to_field='jack@gmail.com', normalised_to='jack@gmail.com'
template=sample_email_template, to_field='jack@gmail.com', normalised_to='jack@gmail.com'
)
results = dao_get_notifications_by_to_field(notification.service_id, 'JACK@gmail.com')
results = dao_get_notifications_by_to_field(notification.service_id, search_term, notification_type='email')
notification_ids = [notification.id for notification in results]
assert len(results) == 1
assert notification.id in notification_ids
def test_dao_get_notifications_by_to_field_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_to_field(notification_1.service_id, 'ack', notification_type='email')
notification_ids = [notification.id for notification in results]
assert len(results) == 1
assert notification_1.id in notification_ids
assert notification_2.id not in notification_ids
@pytest.mark.parametrize('search_term', [
'001',
'100',
'09001',
'077009001',
'07700 9001',
'(0)7700 9001',
'4477009001',
'+4477009001',
pytest.mark.skip('+44077009001', reason='No easy way to normalise this'),
pytest.mark.skip('+44(0)77009001', reason='No easy way to normalise this'),
])
def test_dao_get_notifications_by_to_field_matches_partial_phone_numbers(
sample_template,
search_term,
):
notification_1 = create_notification(
template=sample_template,
to_field='+447700900100',
normalised_to='447700900100',
)
notification_2 = create_notification(
template=sample_template,
to_field='+447700900200',
normalised_to='447700900200',
)
results = dao_get_notifications_by_to_field(notification_1.service_id, search_term, notification_type='sms')
notification_ids = [notification.id for notification in results]
assert len(results) == 1
assert notification_1.id in notification_ids
assert notification_2.id not in notification_ids
@pytest.mark.parametrize('to', [
'not@email', '123'
])
@@ -1761,7 +1814,7 @@ def test_dao_get_notifications_by_to_field_accepts_invalid_phone_numbers_and_ema
notification = create_notification(
template=sample_template, to_field='test@example.com', normalised_to='test@example.com'
)
results = dao_get_notifications_by_to_field(notification.service_id, to)
results = dao_get_notifications_by_to_field(notification.service_id, to, notification_type='email')
assert len(results) == 0
@@ -1779,7 +1832,7 @@ def test_dao_get_notifications_by_to_field_search_ignores_spaces(sample_template
template=sample_template, to_field='jaCK@gmail.com', normalised_to='jack@gmail.com'
)
results = dao_get_notifications_by_to_field(notification1.service_id, '+447700900855')
results = dao_get_notifications_by_to_field(notification1.service_id, '+447700900855', notification_type='sms')
notification_ids = [notification.id for notification in results]
assert len(results) == 3
@@ -1788,6 +1841,24 @@ def test_dao_get_notifications_by_to_field_search_ignores_spaces(sample_template
assert notification3.id in notification_ids
def test_dao_get_notifications_by_to_field_only_searches_for_notification_type(
notify_db_session
):
service = create_service()
sms_template = create_template(service=service)
email_template = create_template(service=service, template_type='email')
sms = create_notification(template=sms_template, to_field='0771111111', normalised_to='0771111111')
email = create_notification(
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')
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
def test_dao_created_scheduled_notification(sample_notification):
scheduled_notification = ScheduledNotification(notification_id=sample_notification.id,
@@ -1838,7 +1909,9 @@ def test_dao_get_notifications_by_to_field_filters_status(sample_template):
normalised_to='447700900855', status='temporary-failure'
)
notifications = dao_get_notifications_by_to_field(notification.service_id, "+447700900855", statuses=['delivered'])
notifications = dao_get_notifications_by_to_field(notification.service_id, "+447700900855",
statuses=['delivered'],
notification_type='sms')
assert len(notifications) == 1
assert notification.id == notifications[0].id
@@ -1855,7 +1928,7 @@ def test_dao_get_notifications_by_to_field_filters_multiple_statuses(sample_temp
)
notifications = dao_get_notifications_by_to_field(
notification1.service_id, "+447700900855", statuses=['delivered', 'sending']
notification1.service_id, "+447700900855", statuses=['delivered', 'sending'], notification_type='sms'
)
notification_ids = [notification.id for notification in notifications]
@@ -1875,7 +1948,7 @@ def test_dao_get_notifications_by_to_field_returns_all_if_no_status_filter(sampl
)
notifications = dao_get_notifications_by_to_field(
notification1.service_id, "+447700900855"
notification1.service_id, "+447700900855", notification_type='sms'
)
notification_ids = [notification.id for notification in notifications]
@@ -1897,7 +1970,7 @@ def test_dao_get_notifications_by_to_field_orders_by_created_at_desc(sample_temp
notification = notification(created_at=datetime.utcnow())
notifications = dao_get_notifications_by_to_field(
sample_template.service_id, '+447700900855'
sample_template.service_id, '+447700900855', notification_type='sms'
)
assert len(notifications) == 2

View File

@@ -1914,13 +1914,15 @@ def test_get_template_usage_by_month_returns_two_templates(
assert resp_json[2]["is_precompiled_letter"] is False
def test_search_for_notification_by_to_field(client, notify_db, notify_db_session):
create_notification = partial(create_sample_notification, notify_db, notify_db_session)
notification1 = create_notification(to_field='+447700900855', normalised_to='447700900855')
notification2 = create_notification(to_field='jack@gmail.com', normalised_to='jack@gmail.com')
def test_search_for_notification_by_to_field(client, sample_template, sample_email_template):
notification1 = create_notification(template=sample_template, to_field='+447700900855',
normalised_to='447700900855')
notification2 = create_notification(template=sample_email_template, to_field='jack@gmail.com',
normalised_to='jack@gmail.com')
response = client.get(
'/service/{}/notifications?to={}'.format(notification1.service_id, 'jack@gmail.com'),
'/service/{}/notifications?to={}&template_type={}'.format(notification1.service_id, 'jack@gmail.com', 'email'),
headers=[create_authorization_header()]
)
notifications = json.loads(response.get_data(as_text=True))['notifications']
@@ -1938,7 +1940,7 @@ def test_search_for_notification_by_to_field_return_empty_list_if_there_is_no_ma
create_notification(to_field='jack@gmail.com')
response = client.get(
'/service/{}/notifications?to={}'.format(notification1.service_id, '+447700900800'),
'/service/{}/notifications?to={}&template_type={}'.format(notification1.service_id, '+447700900800', 'sms'),
headers=[create_authorization_header()]
)
notifications = json.loads(response.get_data(as_text=True))['notifications']
@@ -1955,7 +1957,7 @@ def test_search_for_notification_by_to_field_return_multiple_matches(client, not
notification4 = create_notification(to_field='jack@gmail.com', normalised_to='jack@gmail.com')
response = client.get(
'/service/{}/notifications?to={}'.format(notification1.service_id, '+447700900855'),
'/service/{}/notifications?to={}&template_type={}'.format(notification1.service_id, '+447700900855', 'sms'),
headers=[create_authorization_header()]
)
notifications = json.loads(response.get_data(as_text=True))['notifications']
@@ -1970,6 +1972,18 @@ def test_search_for_notification_by_to_field_return_multiple_matches(client, not
assert str(notification4.id) not in notification_ids
def test_search_for_notification_by_to_field_return_400_for_letter_type(
client, notify_db, notify_db_session, sample_service
):
response = client.get(
'/service/{}/notifications?to={}&template_type={}'.format(sample_service.id, 'A. Name', 'letter'),
headers=[create_authorization_header()]
)
response.status_code = 400
error_message = json.loads(response.get_data(as_text=True))
assert error_message['message'] == 'Only email and SMS can use search by recipient'
def test_update_service_calls_send_notification_as_service_becomes_live(notify_db, notify_db_session, client, mocker):
send_notification_mock = mocker.patch('app.service.rest.send_notification_to_service_users')
@@ -2049,8 +2063,8 @@ def test_search_for_notification_by_to_field_filters_by_status(client, notify_db
create_notification(status='sending')
response = client.get(
'/service/{}/notifications?to={}&status={}'.format(
notification1.service_id, '+447700900855', 'delivered'
'/service/{}/notifications?to={}&status={}&template_type={}'.format(
notification1.service_id, '+447700900855', 'delivered', 'sms'
),
headers=[create_authorization_header()]
)
@@ -2074,8 +2088,8 @@ def test_search_for_notification_by_to_field_filters_by_statuses(client, notify_
notification2 = create_notification(status='sending')
response = client.get(
'/service/{}/notifications?to={}&status={}&status={}'.format(
notification1.service_id, '+447700900855', 'delivered', 'sending'
'/service/{}/notifications?to={}&status={}&status={}&template_type={}'.format(
notification1.service_id, '+447700900855', 'delivered', 'sending', 'sms'
),
headers=[create_authorization_header()]
)
@@ -2104,8 +2118,8 @@ def test_search_for_notification_by_to_field_returns_content(
)
response = client.get(
'/service/{}/notifications?to={}'.format(
sample_template_with_placeholders.service_id, '+447700900855'
'/service/{}/notifications?to={}&template_type={}'.format(
sample_template_with_placeholders.service_id, '+447700900855', 'sms'
),
headers=[create_authorization_header()]
)
@@ -2225,8 +2239,8 @@ def test_search_for_notification_by_to_field_returns_personlisation(
)
response = client.get(
'/service/{}/notifications?to={}'.format(
sample_template_with_placeholders.service_id, '+447700900855'
'/service/{}/notifications?to={}&template_type={}'.format(
sample_template_with_placeholders.service_id, '+447700900855', 'sms'
),
headers=[create_authorization_header()]
)
@@ -2238,6 +2252,42 @@ def test_search_for_notification_by_to_field_returns_personlisation(
assert notifications[0]['personalisation']['name'] == 'Foo'
def test_search_for_notification_by_to_field_returns_notifications_by_type(
client,
notify_db,
notify_db_session,
sample_template,
sample_email_template
):
sms_notification = create_sample_notification(
notify_db,
notify_db_session,
to_field='+447700900855',
normalised_to='447700900855',
template=sample_template
)
create_sample_notification(
notify_db,
notify_db_session,
to_field='44770@gamil.com',
normalised_to='44770@gamil.com',
template=sample_email_template
)
response = client.get(
'/service/{}/notifications?to={}&template_type={}'.format(
sms_notification.service_id, '0770', 'sms'
),
headers=[create_authorization_header()]
)
notifications = json.loads(response.get_data(as_text=True))['notifications']
assert response.status_code == 200
assert len(notifications) == 1
assert notifications[0]['id'] == str(sms_notification.id)
def test_is_service_name_unique_returns_200_if_unique(admin_request, notify_db, notify_db_session):
service = create_service(service_name='unique', email_from='unique')