Skip to content

Commit

Permalink
test: migrates more test cases from Django to increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
tkzt committed Nov 29, 2023
1 parent e975ed5 commit 899b930
Show file tree
Hide file tree
Showing 14 changed files with 1,530 additions and 11 deletions.
50 changes: 49 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ mkdocs-autorefs = {version = "*", optional = true}
pre-commit = {version = "*", optional = true}
toml = {version = "*", optional = true}
bump2version = {version = "*", optional = true}
aiosmtpd = {version = "^1.4.4.post2", optional = true}

[tool.poetry.extras]
test = [
"pytest",
"black",
"isort",
"flake8",
"pytest-cov"
"pytest-cov",
"aiosmtpd"
]

dev = ["tox", "pre-commit", "virtualenv", "pip", "twine", "toml", "bump2version"]
Expand Down
46 changes: 45 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from email import message_from_bytes
import unittest
from contextlib import contextmanager

Expand All @@ -12,7 +13,7 @@ class TestCase(unittest.TestCase):
MAIL_DEFAULT_SENDER = "support@mysite.com"

def setUp(self):
self.app = Flask(__name__)
self.app = Flask(import_name=__name__)
self.app.config.from_object(self)
self.assertTrue(self.app.testing)
self.mail = Mail(self.app)
Expand Down Expand Up @@ -63,3 +64,46 @@ def assertIsNotNone(self, obj, msg=None):
@pytest.fixture(autouse=True)
def capsys(self, capsys):
self.capsys = capsys


class MailmanCustomizedTestCase(TestCase):
def assertMessageHasHeaders(self, message, headers):
"""
Asserts that the `message` has all `headers`.
message: can be an instance of an email.Message subclass or a string
with the contents of an email message.
headers: should be a set of (header-name, header-value) tuples.
"""
if isinstance(message, bytes):
message = message_from_bytes(message)
msg_headers = set(message.items())
self.assertTrue(
headers.issubset(msg_headers),
msg="Message is missing " "the following headers: %s" % (headers - msg_headers),
)

def get_decoded_attachments(self, message):
"""
Encode the specified EmailMessage, then decode
it using Python's email.parser module and, for each attachment of the
message, return a list of tuples with (filename, content, mimetype).
"""
msg_bytes = message.message().as_bytes()
email_message = message_from_bytes(msg_bytes)

def iter_attachments():
for i in email_message.walk():
if i.get_content_disposition() == "attachment":
filename = i.get_filename()
content = i.get_payload(decode=True)
mimetype = i.get_content_type()
yield filename, content, mimetype

return list(iter_attachments())

def get_message(self):
return self.mail.outbox[0].message()

def flush_mailbox(self):
self.mail.outbox.clear()
44 changes: 44 additions & 0 deletions tests/attachments/file.eml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
MIME-Version: 1.0
Received: by 10.220.191.194 with HTTP; Wed, 11 May 2011 12:27:12 -0700 (PDT)
Date: Wed, 11 May 2011 13:27:12 -0600
Delivered-To: jncjkq@gmail.com
Message-ID: <BANLkTi=JCQO1h3ET-pT_PLEHejhSSYxTZw@mail.jncjkq.com>
Subject: Test
From: Bill Jncjkq <jncjkq@gmail.com>
To: bookmarks@jncjkq.net
Content-Type: multipart/mixed; boundary=bcaec54eecc63acce904a3050f79

--bcaec54eecc63acce904a3050f79
Content-Type: multipart/alternative; boundary=bcaec54eecc63acce604a3050f77
--bcaec54eecc63acce604a3050f77
Content-Type: text/plain; charset=ISO-8859-1
--
Bill Jncjkq

--bcaec54eecc63acce604a3050f77
Content-Type: text/html; charset=ISO-8859-1

<br clear="all">--<br>Bill Jncjkq<br>

--bcaec54eecc63acce604a3050f77--
--bcaec54eecc63acce904a3050f79
Content-Type: text/html; charset=US-ASCII; name="bookmarks-really-short.html"
Content-Disposition: attachment; filename="bookmarks-really-short.html"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_gnknv6u70

PCFET0NUWVBFIE5FVFNDQVBFLUJvb2ttYXJrLWZpbGUtMT4KCTxIVE1MPgoJPE1FVEEgSFRUUC1F
UVVJVj0iQ29udGVudC1UeXBlIiBDT05URU5UPSJ0ZXh0L2h0bWw7IGNoYXJzZXQ9VVRGLTgiPgoJ
PFRpdGxlPkJvb2ttYXJrczwvVGl0bGU+Cgk8SDE+Qm9va21hcmtzPC9IMT4KCQk8RFQ+PEgzIEZP
TERFRD5UZWNoIE5ld3M8L0gzPgoJCTxETD48cD4KCQkJPERUPjxBIEhSRUY9Imh0dHA6Ly93d3cu
Y25ldC5jb20vIj5DTmV0PC9BPgoJCQk8RFQ+PEEgSFJFRj0iaHR0cDovL3d3dy53aXJlZC5jb20v
Ij5XaXJlZCBOZXdzPC9BPgoJCTwvREw+PHA+CgkJPERUPjxIMyBGT0xERUQ+VG9vbHMgYW5kIFJl
ZmVyZW5jZTwvSDM+CgkJPERMPjxwPgoJCQk8RFQ+PEEgSFJFRj0iaHR0cDovL3d3dy5tb25zdGVy
LmNvbS8iPk1vbnN0ZXIuY29tPC9BPgoJCQk8RFQ+PEEgSFJFRj0iaHR0cDovL3d3dy53ZWJtZC5j
b20vIj5XZWJNRDwvQT4KCQk8L0RMPjxwPgoJCTxEVD48SDMgRk9MREVEPlRyYXZlbDwvSDM+CgkJ
PERMPjxwPgoJCQk8RFQ+PEEgSFJFRj0iaHR0cDovL2ZvZG9ycy5jb20vIj5Gb2RvcnM8L0E+CgkJ
CTxEVD48QSBIUkVGPSJodHRwOi8vd3d3LnRyYXZlbG9jaXR5LmNvbS8iPlRyYXZlbG9jaXR5PC9B
PgoJCTwvREw+PHA+Cgk8L0RMPjxwPgo8L0hUTUw+
--bcaec54eecc63acce904a3050f79--
Binary file added tests/attachments/file.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/attachments/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
django/django
Binary file added tests/attachments/file_png
Binary file not shown.
Binary file added tests/attachments/file_png.txt
Binary file not shown.
1 change: 1 addition & 0 deletions tests/attachments/file_txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
django/django
1 change: 1 addition & 0 deletions tests/attachments/file_txt.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 899b930

Please sign in to comment.