From 0a3be6a6627876290773e38fb8cf775b92c02119 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Mon, 10 May 2021 15:21:28 +0100 Subject: [PATCH] Normalise content for non-templated broadcast events MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- app/broadcast_message/rest.py | 10 ++++++---- tests/app/broadcast_message/test_rest.py | 11 ++++++++--- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/app/broadcast_message/rest.py b/app/broadcast_message/rest.py index d04e7c575..112963907 100644 --- a/app/broadcast_message/rest.py +++ b/app/broadcast_message/rest.py @@ -111,13 +111,12 @@ def create_broadcast_message(service_id): template = dao_get_template_by_id_and_service_id( template_id, data['service_id'] ) - content = template._as_utils_template_with_personalisation( + content = str(template._as_utils_template_with_personalisation( personalisation - ).content_with_placeholders_filled_in + )) reference = None else: - template, content, reference = None, data['content'], data['reference'] - temporary_template = BroadcastMessageTemplate.from_content(content) + temporary_template = BroadcastMessageTemplate.from_content(data['content']) if temporary_template.content_too_long: raise InvalidRequest( ( @@ -130,6 +129,9 @@ def create_broadcast_message(service_id): ), status_code=400, ) + template = None + content = str(temporary_template) + reference = data['reference'] broadcast_message = BroadcastMessage( service_id=service.id, diff --git a/tests/app/broadcast_message/test_rest.py b/tests/app/broadcast_message/test_rest.py index 66382d8b5..8e8802a2a 100644 --- a/tests/app/broadcast_message/test_rest.py +++ b/tests/app/broadcast_message/test_rest.py @@ -130,7 +130,11 @@ def test_get_broadcast_messages_for_service(admin_request, sample_broadcast_serv @pytest.mark.parametrize('training_mode_service', [True, False]) def test_create_broadcast_message(admin_request, sample_broadcast_service, 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( '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['personalisation'] == {} 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) 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( 'broadcast_message.create_broadcast_message', _data={ - 'content': 'Some tailor made broadcast content', + 'content': 'Some content\r\n€ŷŵ~\r\n‘’“”—–-', 'reference': 'abc123', 'service_id': str(sample_broadcast_service.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, _expected_status=201 ) - assert response['content'] == 'Some tailor made broadcast content' + assert response['content'] == 'Some content\n€ŷŵ~\n\'\'""---' assert response['reference'] == 'abc123' assert response['template_id'] is None