mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-09 03:13:12 -04:00
Merge pull request #1769 from alphagov/escape-special-characters-in-search
Escape special characters in search by recipient
This commit is contained in:
@@ -455,6 +455,12 @@ def dao_get_notifications_by_to_field(service_id, search_term, notification_type
|
|||||||
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)
|
||||||
|
|
||||||
|
for special_character in ('\\', '_', '%', '/'):
|
||||||
|
normalised = normalised.replace(
|
||||||
|
special_character,
|
||||||
|
'\{}'.format(special_character)
|
||||||
|
)
|
||||||
|
|
||||||
filters = [
|
filters = [
|
||||||
Notification.service_id == service_id,
|
Notification.service_id == service_id,
|
||||||
Notification.normalised_to.like("%{}%".format(normalised)),
|
Notification.normalised_to.like("%{}%".format(normalised)),
|
||||||
|
|||||||
@@ -1769,6 +1769,48 @@ def test_dao_get_notifications_by_to_field_matches_partial_emails(sample_email_t
|
|||||||
assert notification_2.id not in notification_ids
|
assert notification_2.id not in notification_ids
|
||||||
|
|
||||||
|
|
||||||
|
@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_to_field_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_to_field(
|
||||||
|
sample_email_template.service_id,
|
||||||
|
search_term,
|
||||||
|
notification_type='email',
|
||||||
|
)) == expected_result_count
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('search_term', [
|
@pytest.mark.parametrize('search_term', [
|
||||||
'001',
|
'001',
|
||||||
'100',
|
'100',
|
||||||
|
|||||||
Reference in New Issue
Block a user