Skip to content

Commit

Permalink
adjust tests to use the new EmailMessage API
Browse files Browse the repository at this point in the history
  • Loading branch information
pazz committed Aug 10, 2019
1 parent 58d9e90 commit 89195db
Showing 1 changed file with 19 additions and 45 deletions.
64 changes: 19 additions & 45 deletions tests/db/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import email.mime.application
import email.policy
import email.utils
from email.message import EmailMessage
import io
import os
import os.path
Expand Down Expand Up @@ -606,14 +607,17 @@ def _set_basic_headers(mail):
mail['From'] = 'bar@example.com'

def test_single_text_plain(self):
mail = email.mime.text.MIMEText('This is an email')
mail = EmailMessage()
self._set_basic_headers(mail)
mail.set_content('This is an email')
actual = utils.extract_body(mail)

expected = 'This is an email'
expected = 'This is an email\n'

self.assertEqual(actual, expected)

@unittest.expectedFailure
# This makes no sense
def test_two_text_plain(self):
mail = email.mime.multipart.MIMEMultipart()
self._set_basic_headers(mail)
Expand All @@ -626,30 +630,29 @@ def test_two_text_plain(self):
self.assertEqual(actual, expected)

def test_text_plain_with_attachment_text(self):
mail = email.mime.multipart.MIMEMultipart()
mail = EmailMessage()
self._set_basic_headers(mail)
mail.attach(email.mime.text.MIMEText('This is an email'))
attachment = email.mime.text.MIMEText('this shouldnt be displayed')
attachment['Content-Disposition'] = 'attachment'
mail.attach(attachment)
mail.set_content('This is an email')
mail.add_attachment('this shouldnt be displayed')

actual = utils.extract_body(mail)
expected = 'This is an email'
expected = 'This is an email\n'

self.assertEqual(actual, expected)

def _make_mixed_plain_html(self):
mail = email.mime.multipart.MIMEMultipart()

mail = EmailMessage()
self._set_basic_headers(mail)
mail.attach(email.mime.text.MIMEText('This is an email'))
mail.attach(email.mime.text.MIMEText(
mail.set_content('This is an email')
mail.add_alternative(
'<!DOCTYPE html><html><body>This is an html email</body></html>',
'html'))
subtype='html')
return mail

@mock.patch('alot.db.utils.settings.get', mock.Mock(return_value=True))
def test_prefer_plaintext(self):
expected = 'This is an email'
expected = 'This is an email\n'
mail = self._make_mixed_plain_html()
actual = utils.extract_body(mail)

Expand All @@ -661,45 +664,16 @@ def test_prefer_plaintext(self):
@mock.patch('alot.db.utils.settings.mailcap_find_match',
mock.Mock(return_value=(None, {'view': 'cat'})))
def test_prefer_html(self):
expected = (
'<!DOCTYPE html><html><body>This is an html email</body></html>')
expected = '<!DOCTYPE html><html><body>This is an html email</body></html>\n'
mail = self._make_mixed_plain_html()
actual = utils.extract_body(mail)

self.assertEqual(actual, expected)


@mock.patch('alot.db.utils.settings.mailcap_find_match',
mock.Mock(return_value=(None, {'view': 'cat'})))
def test_require_mailcap_stdin(self):
mail = email.mime.multipart.MIMEMultipart()
self._set_basic_headers(mail)
mail.attach(email.mime.text.MIMEText(
'<!DOCTYPE html><html><body>This is an html email</body></html>',
'html'))
actual = utils.extract_body(mail)
expected = (
'<!DOCTYPE html><html><body>This is an html email</body></html>')

self.assertEqual(actual, expected)

@mock.patch('alot.db.utils.settings.mailcap_find_match',
mock.Mock(return_value=(None, {'view': 'cat %s'})))
def test_require_mailcap_file(self):
mail = email.mime.multipart.MIMEMultipart()
self._set_basic_headers(mail)
mail.attach(email.mime.text.MIMEText(
'<!DOCTYPE html><html><body>This is an html email</body></html>',
'html'))
actual = utils.extract_body(mail)
expected = (
'<!DOCTYPE html><html><body>This is an html email</body></html>')

self.assertEqual(actual, expected)

def test_simple_utf8_file(self):
mail = email.message_from_binary_file(
open('tests/static/mail/utf8.eml', 'rb'))
open('tests/static/mail/utf8.eml', 'rb'),
_class=email.message.EmailMessage)
actual = utils.extract_body(mail)
expected = "Liebe Grüße!\n"
self.assertEqual(actual, expected)
Expand Down

0 comments on commit 89195db

Please sign in to comment.