From 86b0082e0b878632932d328c77b5c4517adb45f5 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 11 Mar 2024 17:00:39 +0000 Subject: [PATCH 1/2] Bump mypy from 1.8.0 to 1.9.0 Bumps [mypy](https://github.com/python/mypy) from 1.8.0 to 1.9.0. - [Changelog](https://github.com/python/mypy/blob/master/CHANGELOG.md) - [Commits](https://github.com/python/mypy/compare/v1.8.0...1.9.0) --- updated-dependencies: - dependency-name: mypy dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index b942bf7f78..343ea5d8eb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -53,6 +53,6 @@ botocore-stubs==1.34.59 django-stubs==4.2.6 djangorestframework-stubs==3.14.4 mypy-boto3-ses==1.34.0 -mypy==1.8.0 +mypy==1.9.0 types-pyOpenSSL==24.0.0.20240228 types-requests==2.31.0.20240310 From a951006183c454e612b0133b470e551b63b51aa1 Mon Sep 17 00:00:00 2001 From: John Whitlock Date: Mon, 11 Mar 2024 14:06:25 -0500 Subject: [PATCH 2/2] Replace legacy Message.get_payload with .walk --- emails/tests/views_tests.py | 38 +++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/emails/tests/views_tests.py b/emails/tests/views_tests.py index 548896d2a5..c061ea719e 100644 --- a/emails/tests/views_tests.py +++ b/emails/tests/views_tests.py @@ -1990,6 +1990,36 @@ def test_reply_requires_premium_test(rf, forwarded, content_type, caplog): pytest.fail(message) +def get_text_and_html_content(msg: EmailMessage) -> tuple[str, str]: + """ + Return the plain text and HTML content of an email message. + + This replaces the legacy function msg.get_payload(). Another + option is get_body(), which will return the first match. + """ + text_content: str | None = None + html_content: str | None = None + for part in msg.walk(): + content_type = part.get_content_type() + if content_type == "text/plain": + if text_content is None: + text_content = part.get_content() + else: + raise Exception("Second plain text section found.") + elif content_type == "text/html": + if html_content is None: + html_content = part.get_content() + else: + raise Exception("Second HTML section found.") + elif content_type.startswith("multipart/"): + pass + else: + raise ValueError(f"Unexpected content type {content_type}") + assert text_content is not None, "Plain text not found" + assert html_content is not None, "HTML not found" + return text_content, html_content + + @pytest.mark.django_db def test_build_reply_requires_premium_email_first_time_includes_forward_text(): # First create a valid reply record from an external sender to a free Relay user @@ -2032,9 +2062,7 @@ def test_build_reply_requires_premium_email_first_time_includes_forward_text(): assert msg["From"] == expected_From assert msg["To"] == relay_address.full_address - text_part, html_part = msg.get_payload() - text_content = text_part.get_payload() - html_content = html_part.get_payload() + text_content, html_content = get_text_and_html_content(msg) assert "sent this reply" in text_content assert "sent this reply" in html_content @@ -2086,9 +2114,7 @@ def test_build_reply_requires_premium_email_after_forward(): assert msg["From"] == expected_From assert msg["To"] == relay_address.full_address - text_part, html_part = msg.get_payload() - text_content = text_part.get_payload() - html_content = html_part.get_payload() + text_content, html_content = get_text_and_html_content(msg) assert "Your reply was not sent" in text_content assert "Your reply was not sent" in html_content