-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Encoding unicode in "friendly-from" field (#1990)
This PR changes the way we are encoding the "friendly from" field in the email headers, so that we can include non-ascii characters that show up in service names. The AWS documentation provided examples of how to do this. There are 2 steps: - encode the utf-8 characters as base64 - replace the ascii-encoded "friendly from" field with "MIME encoded-word syntax" that includes the base64 encoded string Here is a link to the AWS docs that I used: https://docs.aws.amazon.com/ses/latest/dg/send-email-raw.html#send-email-mime-encoding-headers
- Loading branch information
Showing
3 changed files
with
85 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import uuid | ||
from collections import namedtuple | ||
from datetime import datetime | ||
from unittest import TestCase | ||
from unittest.mock import ANY, MagicMock, call | ||
|
||
import pytest | ||
|
@@ -153,7 +154,7 @@ def test_should_send_personalised_template_to_correct_email_provider_and_persist | |
send_to_providers.send_email_to_provider(db_notification) | ||
|
||
app.aws_ses_client.send_email.assert_called_once_with( | ||
'"Sample service" <[email protected]>', | ||
'"=?utf-8?B?U2FtcGxlIHNlcnZpY2U=?=" <[email protected]>', | ||
"[email protected]", | ||
"Jo <em>some HTML</em>", | ||
body="Hello Jo\nThis is an email from GOV.\u200bUK with <em>some HTML</em>\n", | ||
|
@@ -244,7 +245,7 @@ def test_should_respect_custom_sending_domains(sample_service, mocker, sample_em | |
send_to_providers.send_email_to_provider(db_notification) | ||
|
||
app.aws_ses_client.send_email.assert_called_once_with( | ||
'"Sample service" <[email protected]>', | ||
'"=?utf-8?B?U2FtcGxlIHNlcnZpY2U=?=" <[email protected]>', | ||
"[email protected]", | ||
"Jo <em>some HTML</em>", | ||
body="Hello Jo\nThis is an email from GOV.\u200bUK with <em>some HTML</em>\n", | ||
|
@@ -1221,3 +1222,57 @@ def test_check_service_over_bounce_rate_normal(self, mocker: MockFixture, notify | |
mock_logger = mocker.patch("app.notifications.validators.current_app.logger.warning") | ||
assert send_to_providers.check_service_over_bounce_rate(fake_uuid) is None | ||
mock_logger.assert_not_called() | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"encoded_text, charset, encoding, expected", | ||
[ | ||
("hello_world", "utf-8", "B", "=?utf-8?B?hello_world?="), | ||
("hello_world", "utf-8", "Q", "=?utf-8?Q?hello_world?="), | ||
("hello_world2", "utf-8", "B", "=?utf-8?B?hello_world2?="), | ||
], | ||
) | ||
def test_mime_encoded_word_syntax_encoding(encoded_text, charset, encoding, expected): | ||
result = send_to_providers.mime_encoded_word_syntax(encoded_text=encoded_text, charset=charset, encoding=encoding) | ||
assert result == expected | ||
|
||
|
||
class TestGetFromAddress(TestCase): | ||
def test_get_from_address_ascii(self): | ||
# Arrange | ||
friendly_from = "John Doe" | ||
email_from = "johndoe" | ||
sending_domain = "example.com" | ||
|
||
# Act | ||
result = send_to_providers.get_from_address(friendly_from, email_from, sending_domain) | ||
|
||
# Assert | ||
expected_result = '"=?utf-8?B?Sm9obiBEb2U=?=" <[email protected]>' | ||
self.assertEqual(result, expected_result) | ||
|
||
def test_get_from_address_non_ascii(self): | ||
# Arrange | ||
friendly_from = "Jöhn Döe" | ||
email_from = "johndoe" | ||
sending_domain = "example.com" | ||
|
||
# Act | ||
result = send_to_providers.get_from_address(friendly_from, email_from, sending_domain) | ||
|
||
# Assert | ||
expected_result = '"=?utf-8?B?SsO2aG4gRMO2ZQ==?=" <[email protected]>' | ||
self.assertEqual(result, expected_result) | ||
|
||
def test_get_from_address_empty_friendly_from(self): | ||
# Arrange | ||
friendly_from = "" | ||
email_from = "johndoe" | ||
sending_domain = "example.com" | ||
|
||
# Act | ||
result = send_to_providers.get_from_address(friendly_from, email_from, sending_domain) | ||
|
||
# Assert | ||
expected_result = '"=?utf-8?B??=" <[email protected]>' | ||
self.assertEqual(result, expected_result) |