Skip to content

Commit

Permalink
test: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jennifer-richards committed Oct 10, 2024
1 parent b6546f7 commit ee3bf9d
Showing 1 changed file with 138 additions and 2 deletions.
140 changes: 138 additions & 2 deletions ietf/utils/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
import shutil
import types

from mock import patch
from mock import call, patch
from pyquery import PyQuery
from typing import Dict, List # pyflakes:ignore

from email.message import Message
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
Expand All @@ -32,6 +33,7 @@
from django.template.defaulttags import URLNode
from django.template.loader import get_template, render_to_string
from django.templatetags.static import StaticNode
from django.test import RequestFactory
from django.urls import reverse as urlreverse

import debug # pyflakes:ignore
Expand All @@ -42,7 +44,15 @@
from ietf.utils.draft import PlaintextDraft, getmeta
from ietf.utils.fields import SearchableField
from ietf.utils.log import unreachable, assertion
from ietf.utils.mail import send_mail_preformatted, send_mail_text, send_mail_mime, outbox, get_payload_text
from ietf.utils.mail import (
send_mail_preformatted,
send_mail_text,
send_mail_mime,
outbox,
get_payload_text,
decode_header_value,
show_that_mail_was_sent,
)
from ietf.utils.test_runner import get_template_paths, set_coverage_checking
from ietf.utils.test_utils import TestCase, unicontent
from ietf.utils.text import parse_unicode
Expand Down Expand Up @@ -109,6 +119,132 @@ def test_send_mail_preformatted(self):
recv = outbox[-1]
self.assertEqual(recv['Fuzz'], 'bucket, monger')


class MailUtilsTests(TestCase):
def test_decode_header_value(self):
self.assertEqual(
decode_header_value("cake"),
"cake",
"decodes simple string value",
)
self.assertEqual(
decode_header_value("=?utf-8?b?8J+Ogg==?="),
"\U0001f382",
"decodes single utf-8-encoded part",
)
self.assertEqual(
decode_header_value("=?utf-8?b?8J+Ogg==?= = =?macintosh?b?jYxrjg==?="),
"\U0001f382 = çåké",
"decodes a value with non-utf-8 encodings",
)

@patch("ietf.utils.mail.decode_header_value", side_effect=lambda s: s.upper())
@patch("ietf.utils.mail.messages")
def test_show_that_mail_was_sent(self, mock_messages, mock_decode_header_value):
request = RequestFactory().get("/some/path")
request.user = object() # just needs to exist
msg = Message()
msg["To"] = "to-value"
msg["Subject"] = "subject-value"
msg["Cc"] = "the-cc-value" # the- prefix to distinguish from bcc
with patch("ietf.ietfauth.utils.has_role", return_value=True):
show_that_mail_was_sent(request, "mail was sent", msg, "bcc-value")
self.assertCountEqual(
mock_decode_header_value.call_args_list,
[call("to-value"), call("subject-value"), call("the-cc-value"), call("bcc-value")],
)
self.assertEqual(mock_messages.info.call_args[0][0], request)
self.assertIn("mail was sent", mock_messages.info.call_args[0][1])
self.assertIn("SUBJECT-VALUE", mock_messages.info.call_args[0][1])
self.assertIn("TO-VALUE", mock_messages.info.call_args[0][1])
self.assertIn("THE-CC-VALUE", mock_messages.info.call_args[0][1])
self.assertIn("BCC-VALUE", mock_messages.info.call_args[0][1])
mock_messages.reset_mock()
mock_decode_header_value.reset_mock()

# no bcc
with patch("ietf.ietfauth.utils.has_role", return_value=True):
show_that_mail_was_sent(request, "mail was sent", msg, None)
self.assertCountEqual(
mock_decode_header_value.call_args_list,
[call("to-value"), call("subject-value"), call("the-cc-value")],
)
self.assertEqual(mock_messages.info.call_args[0][0], request)
self.assertIn("mail was sent", mock_messages.info.call_args[0][1])
self.assertIn("SUBJECT-VALUE", mock_messages.info.call_args[0][1])
self.assertIn("TO-VALUE", mock_messages.info.call_args[0][1])
self.assertIn("THE-CC-VALUE", mock_messages.info.call_args[0][1])
self.assertNotIn("BCC-VALUE", mock_messages.info.call_args[0][1])
mock_messages.reset_mock()
mock_decode_header_value.reset_mock()

# no cc
del msg["Cc"]
with patch("ietf.ietfauth.utils.has_role", return_value=True):
show_that_mail_was_sent(request, "mail was sent", msg, None)
self.assertCountEqual(
mock_decode_header_value.call_args_list,
[call("to-value"), call("subject-value")],
)
self.assertEqual(mock_messages.info.call_args[0][0], request)
self.assertIn("mail was sent", mock_messages.info.call_args[0][1])
self.assertIn("SUBJECT-VALUE", mock_messages.info.call_args[0][1])
self.assertIn("TO-VALUE", mock_messages.info.call_args[0][1])
self.assertNotIn("THE-CC-VALUE", mock_messages.info.call_args[0][1])
self.assertNotIn("BCC-VALUE", mock_messages.info.call_args[0][1])
mock_messages.reset_mock()
mock_decode_header_value.reset_mock()

# no to
del msg["To"]
with patch("ietf.ietfauth.utils.has_role", return_value=True):
show_that_mail_was_sent(request, "mail was sent", msg, None)
self.assertCountEqual(
mock_decode_header_value.call_args_list,
[call("[no to]"), call("subject-value")],
)
self.assertEqual(mock_messages.info.call_args[0][0], request)
self.assertIn("mail was sent", mock_messages.info.call_args[0][1])
self.assertIn("SUBJECT-VALUE", mock_messages.info.call_args[0][1])
self.assertIn("[NO TO]", mock_messages.info.call_args[0][1])
self.assertNotIn("TO-VALUE", mock_messages.info.call_args[0][1])
self.assertNotIn("THE-CC-VALUE", mock_messages.info.call_args[0][1])
self.assertNotIn("BCC-VALUE", mock_messages.info.call_args[0][1])
mock_messages.reset_mock()
mock_decode_header_value.reset_mock()

# no subject
del msg["Subject"]
with patch("ietf.ietfauth.utils.has_role", return_value=True):
show_that_mail_was_sent(request, "mail was sent", msg, None)
self.assertCountEqual(
mock_decode_header_value.call_args_list,
[call("[no to]"), call("[no subject]")],
)
self.assertEqual(mock_messages.info.call_args[0][0], request)
self.assertIn("mail was sent", mock_messages.info.call_args[0][1])
self.assertIn("[NO SUBJECT]", mock_messages.info.call_args[0][1])
self.assertNotIn("SUBJECT-VALUE", mock_messages.info.call_args[0][1])
self.assertIn("[NO TO]", mock_messages.info.call_args[0][1])
self.assertNotIn("TO-VALUE", mock_messages.info.call_args[0][1])
self.assertNotIn("THE-CC-VALUE", mock_messages.info.call_args[0][1])
self.assertNotIn("BCC-VALUE", mock_messages.info.call_args[0][1])
mock_messages.reset_mock()
mock_decode_header_value.reset_mock()

# user does not have role
with patch("ietf.ietfauth.utils.has_role", return_value=False):
show_that_mail_was_sent(request, "mail was sent", msg, None)
self.assertFalse(mock_messages.called)

# no user
request.user = None
with patch("ietf.ietfauth.utils.has_role", return_value=True) as mock_has_role:
show_that_mail_was_sent(request, "mail was sent", msg, None)
self.assertFalse(mock_messages.called)
self.assertFalse(mock_has_role.called)


class TestSMTPServer(TestCase):

def test_address_rejected(self):
Expand Down

0 comments on commit ee3bf9d

Please sign in to comment.