Use the more modern set_content instead of set_payload to handle non-ascii chars in email

Signed-off-by: Aurélien Bompard <aurelien@bompard.org>
This commit is contained in:
Aurélien Bompard 2024-11-26 18:11:50 +01:00
parent a2f3ad9bec
commit 5b009dc35b
No known key found for this signature in database
GPG key ID: 31584CFEB9BF64AD
2 changed files with 67 additions and 15 deletions

View file

@ -1,3 +1,4 @@
from base64 import b64encode
from unittest.mock import Mock, patch
import toddlers.utils.notify
@ -8,7 +9,7 @@ def assert_message_sent(smtp_mock, msg_string, from_addr, to_addrs):
assert smtp_mock.send_message.call_args.args[1] == from_addr
assert smtp_mock.send_message.call_args.args[2] == to_addrs
sent_msg = smtp_mock.send_message.call_args.args[0]
assert msg_string == sent_msg.as_string()
assert sent_msg.as_string() == msg_string
class TestNotify:
@ -17,18 +18,24 @@ class TestNotify:
smtp_server = Mock()
mock_smtp.SMTP.return_value = smtp_server
msg = """To: pingou@email
expected_msg = """To: pingou@email
From: admin@server
Subject: Fedora Account System and Bugzilla Mismatch
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0
Hello pingou,
We have identified you[1] as either a Fedora packager or someone who has asked to
We have identified you[1] as either a Fedora packager or someone who has aske=
d to
be included in the CC list of tickets created for one or more component on
bugzilla. Fedora packagers are granted special permissions on the Fedora bugs in
bugzilla. Fedora packagers are granted special permissions on the Fedora bugs=
in
bugzilla.
However, to enable these functionalities (granting you these permissions or
including you to the CC list of your packages of interest), we need to have your
including you to the CC list of your packages of interest), we need to have y=
our
bugzilla email address stored in the Fedora Account System[2].
At the moment you have:
@ -62,7 +69,7 @@ admin@server
email="pingou@email",
)
mock_smtp.SMTP.assert_called_with("server.mail")
assert_message_sent(smtp_server, msg, "admin@server", ["pingou@email"])
assert_message_sent(smtp_server, expected_msg, "admin@server", ["pingou@email"])
@patch("toddlers.utils.notify.smtplib")
def test_notify_admins(self, mock_smtp):
@ -85,9 +92,12 @@ admin@server
smtp_server = Mock()
mock_smtp.SMTP.return_value = smtp_server
msg = """To: info_admin@server
expected_msg = """To: info_admin@server
From: admin@server
Subject: Fedora Account System and Bugzilla Mismatch
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit
MIME-Version: 1.0
The following people are in the packager group but do not have email addresses
@ -104,22 +114,29 @@ that are valid in bugzilla:
users=users,
)
mock_smtp.SMTP.assert_called_with("server.mail")
assert_message_sent(smtp_server, msg, "admin@server", ["info_admin@server"])
assert_message_sent(
smtp_server, expected_msg, "admin@server", ["info_admin@server"]
)
@patch("toddlers.utils.notify.smtplib")
def test_notify_admins_distgit_sync_error(self, mock_smtp):
smtp_server = Mock()
mock_smtp.SMTP.return_value = smtp_server
msg = """To: info_admin@server
expected_msg = """To: info_admin@server
From: admin@server
Subject: Errors while syncing bugzilla with the PackageDB
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0
Greetings, our mighty admins,
While updating bugzilla with information from the Package Database some errors were encountered.
While updating bugzilla with information from the Package Database some error=
s were encountered.
Don't panic!
Please, be so kind and see if you can do something with them. Here is the list of the errors:
Please, be so kind and see if you can do something with them. Here is the lis=
t of the errors:
Oops, something happened
Oh no, this is bad
@ -134,7 +151,9 @@ Oh no, this is bad
errors=errors,
)
mock_smtp.SMTP.assert_called_with("server.mail")
assert_message_sent(smtp_server, msg, "admin@server", ["info_admin@server"])
assert_message_sent(
smtp_server, expected_msg, "admin@server", ["info_admin@server"]
)
@patch("toddlers.utils.notify.smtplib")
def test_notify_send_email_with_cc_addresses(self, mock_smtp):
@ -148,10 +167,13 @@ This is a test email!
Ahah!
"""
msg = f"""To: info_admin@server
expected_msg = f"""To: info_admin@server
From: admin@server
Cc: admin@server2
Subject: Test email
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit
MIME-Version: 1.0
{content}"""
toddlers.utils.notify.send_email(
@ -164,5 +186,35 @@ Subject: Test email
)
mock_smtp.SMTP.assert_called_with("server.mail")
assert_message_sent(
smtp_server, msg, "admin@server", ["info_admin@server", "admin@server2"]
smtp_server,
expected_msg,
"admin@server",
["info_admin@server", "admin@server2"],
)
@patch("toddlers.utils.notify.smtplib")
def test_notify_send_email_utf8(self, mock_smtp):
smtp_server = Mock()
mock_smtp.SMTP.return_value = smtp_server
content = "Some non-ascii: Miro Hron\u010dok.\n"
expected_msg = f"""To: info_admin@server
From: admin@server
Subject: Test email
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: base64
MIME-Version: 1.0
{b64encode(content.encode('utf8')).decode()}
"""
toddlers.utils.notify.send_email(
to_addresses=["info_admin@server"],
from_address="admin@server",
subject="Test email",
content=content,
mail_server="server.mail",
)
mock_smtp.SMTP.assert_called_with("server.mail")
assert_message_sent(
smtp_server, expected_msg, "admin@server", ["info_admin@server"]
)

View file

@ -206,7 +206,7 @@ def send_email(
to_addresses += cc_address
msg.add_header("Subject", subject)
msg.set_payload(content)
msg.set_content(content)
smtp = smtplib.SMTP(mail_server)
smtp.send_message(msg, from_address, to_addresses)
smtp.quit()