Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: stephenmcd/django-email-extras
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: bae0064d2dcd58f0714e76fd073091039a9bd7b4
Choose a base ref
..
head repository: stephenmcd/django-email-extras
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 1f4c306a77dd4349eddafe2f1ad9ef04c4e13042
Choose a head ref
Showing with 143 additions and 7 deletions.
  1. +6 −0 .gitignore
  2. +2 −4 email_extras/backends.py
  3. +3 −0 tests/settings.py
  4. +27 −0 tests/test_admin.py
  5. +98 −1 tests/test_backends.py
  6. +2 −2 tests/utils.py
  7. +5 −0 tests/write_mail.sh
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
*.pyc
*.pyo
*.egg-info
.coverage
tests/mail.txt

gpg_keyring/
htmlcov/
tests/fixtures/
6 changes: 2 additions & 4 deletions email_extras/backends.py
Original file line number Diff line number Diff line change
@@ -65,12 +65,10 @@ def copy_message(msg):

def encrypt(text, addr):
encryption_result = gpg.encrypt(text, addr, **encrypt_kwargs)
if not encryption_result.ok:
if not encryption_result.ok or (smart_text(encryption_result) == ""
and text != ""):
raise EncryptionFailedError("Encrypting mail to %s failed: '%s'",
addr, encryption_result.status)
if smart_text(encryption_result) == "" and text != "":
raise EncryptionFailedError("Encrypting mail to %s failed.",
addr)
return smart_text(encryption_result)

def encrypt_attachment(address, attachment, use_asc):
3 changes: 3 additions & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
@@ -132,3 +132,6 @@
EMAIL_EXTRAS_GNUPG_HOME = 'gpg_keyring'
EMAIL_EXTRAS_ALWAYS_TRUST_KEYS = True
EMAIL_EXTRAS_GNUPG_ENCODING = 'utf-8'

os.environ['PATH'] += ':./tests'
os.environ['BROWSER'] = 'write_mail.sh'
27 changes: 27 additions & 0 deletions tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from django.contrib.auth import get_user_model
from django.core.urlresolvers import resolve
from django.test import TestCase
from django.test.client import RequestFactory
from django.urls import reverse


class AdminTestCase(TestCase):
def setUp(self):
self.user_password = 'pw'
self.user = get_user_model().objects.create_user(
'user', email='user@example.com', password=self.user_password,
is_staff=True,
is_superuser=True)

self.factory = RequestFactory()

def tearDown(self):
self.user.delete()

def test_has_add_permission(self):
self.client.login(username=self.user, password=self.user_password)

url = reverse('admin:email_extras_address_changelist')
response = self.client.get(url)

self.assertFalse(response.context['has_add_permission'])
99 changes: 98 additions & 1 deletion tests/test_backends.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,108 @@
import os

from django.conf import settings
from django.core import mail
from django.test import TestCase, override_settings
from django.utils.safestring import mark_safe

from email_extras.utils import EncryptionFailedError

from tests.utils import SendMailMixin
from tests.utils import (SendMailFunctionMixin, SendMailMixin)


@override_settings(
EMAIL_BACKEND='email_extras.backends.BrowsableEmailBackend',
DEBUG=True)
class BrowsableEmailBackendTestCase(SendMailFunctionMixin, TestCase):
mail_file = 'tests/mail.txt'
send_mail_function = 'tests.utils.send_mail_with_backend'

def _remove_mail_file(self):
if os.path.exists(self.mail_file):
os.remove(self.mail_file)

def setUp(self):
self._remove_mail_file()

def tearDown(self):
self._remove_mail_file()

@override_settings(DEBUG=False)
def test_with_debug_false(self):
msg_subject = "Test Subject"
to = ['django-email-extras@example.com']
from_email = settings.DEFAULT_FROM_EMAIL
msg_text = "Test Body Text"
msg_html = "<html><body><b>Hello</b> World <i>Text</i>"

# Make sure the file doesn't exist yet
self.assertFalse(os.path.exists(self.mail_file))

self.send_mail(
msg_subject, msg_text, from_email, to,
html_message=mark_safe(msg_html))

# The backend should bail when DEBUG = False
self.assertFalse(os.path.exists(self.mail_file))

def test_with_txt_mail(self):
msg_subject = "Test Subject"
to = ['django-email-extras@example.com']
from_email = settings.DEFAULT_FROM_EMAIL
msg_text = "Test Body Text"

# Make sure the file doesn't exist yet
self.assertFalse(os.path.exists(self.mail_file))

self.send_mail(
msg_subject, msg_text, from_email, to)

# Since there isn't an HTML alternative, the backend shouldn't fire
self.assertFalse(os.path.exists(self.mail_file))

def test_with_non_html_alternative(self):
msg_subject = "Test Subject"
to = ['django-email-extras@example.com']
from_email = settings.DEFAULT_FROM_EMAIL
msg_text = "Test Body Text"
msg_html = "<html><body><b>Hello</b> World <i>Text</i>"

# Make sure the file doesn't exist yet
self.assertFalse(os.path.exists(self.mail_file))

self.send_mail(
msg_subject, msg_text, from_email, to,
alternatives=[(mark_safe(msg_html), 'application/gpg-encrypted')])

# The backend should skip any non-HTML alternative
self.assertFalse(os.path.exists(self.mail_file))

def test_with_html_mail(self):
msg_subject = "Test Subject"
to = ['django-email-extras@example.com']
from_email = settings.DEFAULT_FROM_EMAIL
msg_text = "Test Body Text"
msg_html = "<html><body><b>Hello</b> World <i>Text</i>"

# Make sure the file doesn't exist yet
self.assertFalse(os.path.exists(self.mail_file))

self.send_mail(
msg_subject, msg_text, from_email, to,
html_message=mark_safe(msg_html))

# Make sure the file exists
self.assertTrue(os.path.exists(self.mail_file))

# Make sure the contents are expected
with open(self.mail_file, 'r') as f:
self.assertEquals(f.read().strip(), msg_html)

# Try to remove it
self._remove_mail_file()

# Make sure the file doesn't exist
self.assertFalse(os.path.exists(self.mail_file))


@override_settings(
4 changes: 2 additions & 2 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -179,7 +179,7 @@ def delete_all_keys(self):
self.gpg.delete_keys([k['fingerprint'] for k in self.gpg.list_keys()])


class SendMailFunctionMixin(KeyMixin):
class SendMailFunctionMixin(GPGMixin):
send_mail_function = None

def send_mail(self, *args, **kwargs):
@@ -203,7 +203,7 @@ def send_mail(self, *args, **kwargs):
return send_mail_actual_function(*args, **kwargs)


class SendMailMixin(SendMailFunctionMixin):
class SendMailMixin(KeyMixin, SendMailFunctionMixin):
def test_send_mail_key_validation_fail_raises_exception(self):
msg_subject = "Test Subject"
to = ['django-email-extras@example.com']
5 changes: 5 additions & 0 deletions tests/write_mail.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

MAIL_FILE="tests/mail.txt"

cat $(echo $1 | sed 's|^file://||') > "$MAIL_FILE"