mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 10:21:14 -05:00
Normalise content for non-templated broadcast events
We found that non-templated broadcast messages weren’t having their content normalised before saving into an event. This means that stuff like `\r\n` and curly quotes were being passed through to the CBC proxy. This commit firstly changes templated events to use `str(BroadcastMessageTemplate)` to normalise the content, because it’s non-obvious that calling `BroadcastMessageTemplate.content_with_placeholders_filled_in` also normalises content. Then it changes the non-templated route to also call `str(BroadcastMessageTemplate)`, where previously it was passing the content straight through.
This commit is contained in:
@@ -111,13 +111,12 @@ def create_broadcast_message(service_id):
|
|||||||
template = dao_get_template_by_id_and_service_id(
|
template = dao_get_template_by_id_and_service_id(
|
||||||
template_id, data['service_id']
|
template_id, data['service_id']
|
||||||
)
|
)
|
||||||
content = template._as_utils_template_with_personalisation(
|
content = str(template._as_utils_template_with_personalisation(
|
||||||
personalisation
|
personalisation
|
||||||
).content_with_placeholders_filled_in
|
))
|
||||||
reference = None
|
reference = None
|
||||||
else:
|
else:
|
||||||
template, content, reference = None, data['content'], data['reference']
|
temporary_template = BroadcastMessageTemplate.from_content(data['content'])
|
||||||
temporary_template = BroadcastMessageTemplate.from_content(content)
|
|
||||||
if temporary_template.content_too_long:
|
if temporary_template.content_too_long:
|
||||||
raise InvalidRequest(
|
raise InvalidRequest(
|
||||||
(
|
(
|
||||||
@@ -130,6 +129,9 @@ def create_broadcast_message(service_id):
|
|||||||
),
|
),
|
||||||
status_code=400,
|
status_code=400,
|
||||||
)
|
)
|
||||||
|
template = None
|
||||||
|
content = str(temporary_template)
|
||||||
|
reference = data['reference']
|
||||||
|
|
||||||
broadcast_message = BroadcastMessage(
|
broadcast_message = BroadcastMessage(
|
||||||
service_id=service.id,
|
service_id=service.id,
|
||||||
|
|||||||
@@ -130,7 +130,11 @@ def test_get_broadcast_messages_for_service(admin_request, sample_broadcast_serv
|
|||||||
@pytest.mark.parametrize('training_mode_service', [True, False])
|
@pytest.mark.parametrize('training_mode_service', [True, False])
|
||||||
def test_create_broadcast_message(admin_request, sample_broadcast_service, training_mode_service):
|
def test_create_broadcast_message(admin_request, sample_broadcast_service, training_mode_service):
|
||||||
sample_broadcast_service.restricted = training_mode_service
|
sample_broadcast_service.restricted = training_mode_service
|
||||||
t = create_template(sample_broadcast_service, BROADCAST_TYPE)
|
t = create_template(
|
||||||
|
sample_broadcast_service,
|
||||||
|
BROADCAST_TYPE,
|
||||||
|
content='Some content\r\n€ŷŵ~\r\n‘’“”—–-',
|
||||||
|
)
|
||||||
|
|
||||||
response = admin_request.post(
|
response = admin_request.post(
|
||||||
'broadcast_message.create_broadcast_message',
|
'broadcast_message.create_broadcast_message',
|
||||||
@@ -149,6 +153,7 @@ def test_create_broadcast_message(admin_request, sample_broadcast_service, train
|
|||||||
assert response['created_by_id'] == str(t.created_by_id)
|
assert response['created_by_id'] == str(t.created_by_id)
|
||||||
assert response['personalisation'] == {}
|
assert response['personalisation'] == {}
|
||||||
assert response['areas'] == []
|
assert response['areas'] == []
|
||||||
|
assert response['content'] == 'Some content\n€ŷŵ~\n\'\'""---'
|
||||||
|
|
||||||
broadcast_message = dao_get_broadcast_message_by_id_and_service_id(response["id"], sample_broadcast_service.id)
|
broadcast_message = dao_get_broadcast_message_by_id_and_service_id(response["id"], sample_broadcast_service.id)
|
||||||
assert broadcast_message.stubbed == training_mode_service
|
assert broadcast_message.stubbed == training_mode_service
|
||||||
@@ -231,7 +236,7 @@ def test_create_broadcast_message_can_be_created_from_content(admin_request, sam
|
|||||||
response = admin_request.post(
|
response = admin_request.post(
|
||||||
'broadcast_message.create_broadcast_message',
|
'broadcast_message.create_broadcast_message',
|
||||||
_data={
|
_data={
|
||||||
'content': 'Some tailor made broadcast content',
|
'content': 'Some content\r\n€ŷŵ~\r\n‘’“”—–-',
|
||||||
'reference': 'abc123',
|
'reference': 'abc123',
|
||||||
'service_id': str(sample_broadcast_service.id),
|
'service_id': str(sample_broadcast_service.id),
|
||||||
'created_by': str(sample_broadcast_service.created_by_id),
|
'created_by': str(sample_broadcast_service.created_by_id),
|
||||||
@@ -239,7 +244,7 @@ def test_create_broadcast_message_can_be_created_from_content(admin_request, sam
|
|||||||
service_id=sample_broadcast_service.id,
|
service_id=sample_broadcast_service.id,
|
||||||
_expected_status=201
|
_expected_status=201
|
||||||
)
|
)
|
||||||
assert response['content'] == 'Some tailor made broadcast content'
|
assert response['content'] == 'Some content\n€ŷŵ~\n\'\'""---'
|
||||||
assert response['reference'] == 'abc123'
|
assert response['reference'] == 'abc123'
|
||||||
assert response['template_id'] is None
|
assert response['template_id'] is None
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user