diff --git a/app/notifications/receive_notifications.py b/app/notifications/receive_notifications.py index ac116b28a..fa9d11146 100644 --- a/app/notifications/receive_notifications.py +++ b/app/notifications/receive_notifications.py @@ -82,7 +82,11 @@ def receive_firetext_sms(): def format_mmg_message(message): - return unquote(message.replace('+', ' ')) + return unescape_string(unquote(message.replace('+', ' '))) + + +def unescape_string(string): + return string.encode('raw_unicode_escape').decode('unicode_escape') def format_mmg_datetime(date): diff --git a/tests/app/notifications/test_receive_notification.py b/tests/app/notifications/test_receive_notification.py index 26a628bb7..d5d504099 100644 --- a/tests/app/notifications/test_receive_notification.py +++ b/tests/app/notifications/test_receive_notification.py @@ -12,6 +12,7 @@ from app.notifications.receive_notifications import ( create_inbound_sms_object, strip_leading_forty_four, has_inbound_sms_permissions, + unescape_string, ) from app.models import InboundSms, EMAIL_TYPE, SMS_TYPE, INBOUND_SMS_TYPE @@ -166,6 +167,36 @@ def test_format_mmg_message(message, expected_output): assert format_mmg_message(message) == expected_output +@pytest.mark.parametrize('raw, expected', [ + ( + '😬', + '😬', + ), + ( + '1\\n2', + '1\n2', + ), + ( + '\\\'"\\\'', + '\'"\'', + ), + ( + """ + + """, + """ + + """, + ), + ( + '\x79 \\x79 \\\\x79', # we should never see the middle one + 'y y \\x79', + ), +]) +def test_unescape_string(raw, expected): + assert unescape_string(raw) == expected + + @pytest.mark.parametrize('provider_date, expected_output', [ ('2017-01-21+11%3A56%3A11', datetime(2017, 1, 21, 11, 56, 11)), ('2017-05-21+11%3A56%3A11', datetime(2017, 5, 21, 10, 56, 11))