-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
650cbe7
commit 3106c41
Showing
4 changed files
with
45 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,14 @@ | |
|
||
from django.test import TestCase, override_settings | ||
from django.core.mail import EmailMessage, EmailMultiAlternatives | ||
from django.core.files.storage import default_storage | ||
try: | ||
from django.core.files.storage import get_storage_class | ||
except ImportError: | ||
from django.core.files.storage import storages | ||
|
||
def get_storage_class(s): | ||
storages[s] | ||
|
||
from django.template import TemplateDoesNotExist | ||
from django.core import mail | ||
|
||
|
@@ -69,17 +76,11 @@ class TemplateBackendTestCase(MockedNetworkTestCaseMixin, | |
template_backend_klass = TemplateBackend | ||
|
||
def setUp(self): | ||
self.storage_mock = Mock(wraps=default_storage)() | ||
self.storage_mock.save = Mock() | ||
self.backend = self.template_backend_klass() | ||
self.context = {'username': 'vintasoftware', | ||
'joindate': date(2016, 8, 22), | ||
'full_name': 'Foo Bar'} | ||
|
||
def patch_storage(self): | ||
return patch('django.core.files.storage.default_storage._wrapped', | ||
self.storage_mock) | ||
|
||
def test_inexistent_base_email(self): | ||
try: | ||
self.backend._render_email('inexistent_base.email', {}) | ||
|
@@ -168,22 +169,23 @@ def test_get_email_message_with_create_link(self, mocked): | |
uuid=uuid) | ||
self.assertEqual(saved_email.content, HTML_RESULT) | ||
|
||
@patch('django.core.files.storage.FileSystemStorage.save') | ||
@patch('django.core.files.storage.FileSystemStorage.url') | ||
@patch.object( | ||
template_backend_klass, '_render_email', | ||
return_value={'html': HTML_RESULT, 'plain': PLAIN_RESULT, | ||
'subject': SUBJECT_RESULT} | ||
) | ||
def test_get_email_message_with_inline_image(self, mocked): | ||
self.storage_mock.save = Mock(return_value='saved_url') | ||
with self.patch_storage(): | ||
self.backend.get_email_message( | ||
'foo.email', {'an_image': InlineImage('file.png', b'foo', | ||
subtype='png')}, | ||
from_email='[email protected]', cc=['[email protected]'], | ||
bcc=['[email protected]'], to=['[email protected]'], | ||
create_link=True) | ||
second_call_context = mocked.call_args_list[1][0][1] | ||
self.assertEqual(second_call_context['an_image'], '/media/saved_url') | ||
def test_get_email_message_with_inline_image(self, mock_render_email, mock_url, mock_save): | ||
mock_url.return_value = 'media/saved_url' | ||
self.backend.get_email_message( | ||
'foo.email', {'an_image': InlineImage('file.png', b'foo', | ||
subtype='png')}, | ||
from_email='[email protected]', cc=['[email protected]'], | ||
bcc=['[email protected]'], to=['[email protected]'], | ||
create_link=True) | ||
second_call_context = mock_render_email.call_args_list[1][0][1] | ||
self.assertEqual(second_call_context['an_image'], 'media/saved_url') | ||
|
||
@override_settings(TEMPLATED_EMAIL_EMAIL_MESSAGE_CLASS='anymail.message.AnymailMessage') | ||
@patch.object( | ||
|
@@ -496,30 +498,32 @@ def test_removal_of_legacy(self): | |
except TemplateDoesNotExist as e: | ||
self.assertEqual(e.args[0], 'templated_email/legacy.email') | ||
|
||
def test_host_inline_image_if_not_exist(self): | ||
@patch('django.core.files.storage.FileSystemStorage.url') | ||
@patch('django.core.files.storage.FileSystemStorage.save') | ||
def test_host_inline_image_if_not_exist(self, mock_save, mock_url): | ||
mock_url.return_value = 'media/saved_url' | ||
inline_image = InlineImage('foo.jpg', b'bar') | ||
self.storage_mock.save = Mock(return_value='saved_url') | ||
|
||
with self.patch_storage(): | ||
filename = self.backend.host_inline_image(inline_image) | ||
self.assertEqual(filename, '/media/saved_url') | ||
self.storage_mock.save.assert_called_once() | ||
name, content = self.storage_mock.save.call_args[0] | ||
filename = self.backend.host_inline_image(inline_image) | ||
self.assertEqual(filename, 'media/saved_url') | ||
mock_save.assert_called_once() | ||
name, content = mock_save.call_args[0] | ||
self.assertEqual( | ||
name, | ||
'templated_email/37b51d194a7513e45b56f6524f2d51f2foo.jpg') | ||
self.assertTrue(isinstance(content, BytesIO)) | ||
|
||
def test_host_inline_image_if_exist(self): | ||
@patch('django.core.files.storage.FileSystemStorage.exists') | ||
@patch('django.core.files.storage.FileSystemStorage.save') | ||
def test_host_inline_image_if_exist(self, mock_save, mock_exists): | ||
inline_image = InlineImage('foo.jpg', b'bar') | ||
self.storage_mock.exists = Mock(return_value=True) | ||
mock_exists.return_value = True | ||
|
||
with self.patch_storage(): | ||
filename = self.backend.host_inline_image(inline_image) | ||
filename = self.backend.host_inline_image(inline_image) | ||
self.assertEqual( | ||
filename, | ||
'/media/templated_email/37b51d194a7513e45b56f6524f2d51f2foo.jpg') | ||
|
||
self.storage_mock.save.assert_not_called() | ||
self.storage_mock.exists.assert_called_once_with( | ||
mock_save.assert_not_called() | ||
mock_exists.assert_called_once_with( | ||
'templated_email/37b51d194a7513e45b56f6524f2d51f2foo.jpg') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
from django.core.exceptions import ImproperlyConfigured | ||
from django.core import mail | ||
|
||
import unittest.mock | ||
from unittest.mock import patch | ||
|
||
from templated_email.generic_views import TemplatedEmailFormViewMixin | ||
from tests.generic_views.views import AuthorCreateView | ||
|
@@ -44,10 +44,10 @@ def test_templated_email_get_recipients(self): | |
self.mixin_object.templated_email_get_recipients, | ||
form=None) | ||
|
||
@mock.patch.object(TemplatedEmailFormViewMixin, | ||
@patch.object(TemplatedEmailFormViewMixin, | ||
'templated_email_get_template_names', | ||
return_value=['template']) | ||
@mock.patch.object(TemplatedEmailFormViewMixin, | ||
@patch.object(TemplatedEmailFormViewMixin, | ||
'templated_email_get_recipients', | ||
return_value=['[email protected]']) | ||
def test_templated_email_get_send_email_kwargs_valid( | ||
|
@@ -65,10 +65,10 @@ class FakeForm(object): | |
self.assertEqual(kwargs['recipient_list'], ['[email protected]']) | ||
self.assertEqual(kwargs['context'], {'form_data': 'foo'}) | ||
|
||
@mock.patch.object(TemplatedEmailFormViewMixin, | ||
@patch.object(TemplatedEmailFormViewMixin, | ||
'templated_email_get_template_names', | ||
return_value=['template']) | ||
@mock.patch.object(TemplatedEmailFormViewMixin, | ||
@patch.object(TemplatedEmailFormViewMixin, | ||
'templated_email_get_recipients', | ||
return_value=['[email protected]']) | ||
def test_templated_email_get_send_email_kwargs_not_valid( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters