|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
import unittest |
|
3
|
|
|
|
|
4
|
|
|
from oe_utils.email import get_oe_email_footer_html, get_oe_email_footer_plain |
|
5
|
|
|
from oe_utils.email.smtp import SMTPClient |
|
6
|
|
|
from tests import load_fixture |
|
7
|
|
|
|
|
8
|
|
|
try: |
|
9
|
|
|
from unittest.mock import Mock, patch, MagicMock, call, ANY |
|
10
|
|
|
except: |
|
11
|
|
|
from mock import Mock, patch, MagicMock, call, ANY |
|
12
|
|
|
|
|
13
|
|
|
exception = Exception('test') |
|
14
|
|
|
smtp_obj_mock2 = MagicMock() |
|
15
|
|
|
smtp_obj_mock2.sendmail = Mock(side_effect=exception) |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
class EmailUtilTests(unittest.TestCase): |
|
19
|
|
|
def setUp(self): |
|
20
|
|
|
self.smtp_obj_mock = MagicMock() |
|
21
|
|
|
self.smtp_obj_mock.sendmail = Mock(return_value=True) |
|
22
|
|
|
self.email_content = u"""Dear, |
|
23
|
|
|
|
|
24
|
|
|
Thank you for your e-mail. Your message has been forwarded to Darth Vader for action as I am in a galaxy far far away. |
|
25
|
|
|
|
|
26
|
|
|
Kind regards, |
|
27
|
|
|
|
|
28
|
|
|
The Emperor |
|
29
|
|
|
""" |
|
30
|
|
|
|
|
31
|
|
|
def test_sendmail(self): |
|
32
|
|
|
with patch('oe_utils.email.smtp.SMTPClient.smtp_obj') as smtp_mock: |
|
33
|
|
|
smtp_mock.__get__ = Mock(return_value=self.smtp_obj_mock) |
|
34
|
|
|
email_engine = SMTPClient('smtp', '[email protected]') |
|
35
|
|
|
email_engine.send_mail(['[email protected]'], 'test subject', self.email_content) |
|
36
|
|
|
self.assertEqual(self.smtp_obj_mock.sendmail.call_count, 1) |
|
37
|
|
|
self.smtp_obj_mock.sendmail.assert_called_once_with('[email protected]', ['[email protected]'], ANY) |
|
38
|
|
|
|
|
39
|
|
|
def test_sendmail_html(self): |
|
40
|
|
|
with patch('oe_utils.email.smtp.SMTPClient.smtp_obj') as smtp_mock: |
|
41
|
|
|
smtp_mock.__get__ = Mock(return_value=self.smtp_obj_mock) |
|
42
|
|
|
email_engine = SMTPClient('smtp', '[email protected]') |
|
43
|
|
|
email_engine.send_mail(['[email protected]'], 'test subject', self.email_content, self.email_content) |
|
44
|
|
|
self.assertEqual(self.smtp_obj_mock.sendmail.call_count, 1) |
|
45
|
|
|
self.smtp_obj_mock.sendmail.assert_called_once_with('[email protected]', ['[email protected]'], ANY) |
|
46
|
|
|
|
|
47
|
|
|
def test_sendmail_exception(self): |
|
48
|
|
|
with patch('oe_utils.email.smtp.SMTPClient.smtp_obj') as smtp_mock: |
|
49
|
|
|
smtp_mock.__get__ = Mock(return_value=smtp_obj_mock2) |
|
50
|
|
|
email_engine = SMTPClient('smtp', '[email protected]') |
|
51
|
|
|
self.assertRaises(Exception, email_engine.send_mail, ['[email protected]'], 'test subject', self.email_content) |
|
52
|
|
|
self.assertEqual(smtp_obj_mock2.sendmail.call_count, 1) |
|
53
|
|
|
|
|
54
|
|
View Code Duplication |
def test_sendmail_cc(self): |
|
|
|
|
|
|
55
|
|
|
with patch('oe_utils.email.smtp.SMTPClient.smtp_obj') as smtp_mock: |
|
56
|
|
|
smtp_mock.__get__ = Mock(return_value=self.smtp_obj_mock) |
|
57
|
|
|
email_engine = SMTPClient('smtp', '[email protected]') |
|
58
|
|
|
email_engine.send_mail(['[email protected]'], 'test subject', self.email_content, |
|
59
|
|
|
cc=['[email protected]']) |
|
60
|
|
|
self.assertEqual(self.smtp_obj_mock.sendmail.call_count, 1) |
|
61
|
|
|
content = self.smtp_obj_mock.sendmail.mock_calls[0][1][2] |
|
62
|
|
|
self.smtp_obj_mock.sendmail.assert_called_once_with('[email protected]', |
|
63
|
|
|
['[email protected]', '[email protected]'], ANY) |
|
64
|
|
|
self.assertIn('CC', content) |
|
65
|
|
|
self.assertIn('galatic-empire', content) |
|
66
|
|
|
|
|
67
|
|
View Code Duplication |
def test_sendmail_bcc(self): |
|
|
|
|
|
|
68
|
|
|
with patch('oe_utils.email.smtp.SMTPClient.smtp_obj') as smtp_mock: |
|
69
|
|
|
smtp_mock.__get__ = Mock(return_value=self.smtp_obj_mock) |
|
70
|
|
|
email_engine = SMTPClient('smtp', '[email protected]') |
|
71
|
|
|
email_engine.send_mail(['[email protected]'], 'test subject', self.email_content, |
|
72
|
|
|
bcc=['[email protected]']) |
|
73
|
|
|
self.assertEqual(self.smtp_obj_mock.sendmail.call_count, 1) |
|
74
|
|
|
content = self.smtp_obj_mock.sendmail.mock_calls[0][1][2] |
|
75
|
|
|
self.smtp_obj_mock.sendmail.assert_called_once_with('[email protected]', |
|
76
|
|
|
['[email protected]', '[email protected]'], ANY) |
|
77
|
|
|
self.assertIn('BCC', content) |
|
78
|
|
|
self.assertIn('galatic-empire', content) |
|
79
|
|
|
|
|
80
|
|
|
def test_sendmail_attachment(self): |
|
81
|
|
|
with patch('oe_utils.email.smtp.SMTPClient.smtp_obj') as smtp_mock: |
|
82
|
|
|
attachments = [ |
|
83
|
|
|
{'name': 'test.pdf', 'mime': 'application/pdf', 'data': load_fixture('fixtures/Test_document.pdf')}] |
|
84
|
|
|
smtp_mock.__get__ = Mock(return_value=self.smtp_obj_mock) |
|
85
|
|
|
email_engine = SMTPClient('smtp', '[email protected]') |
|
86
|
|
|
email_engine.send_mail(['[email protected]'], 'test subject', self.email_content, files=attachments) |
|
87
|
|
|
self.assertEqual(self.smtp_obj_mock.sendmail.call_count, 1) |
|
88
|
|
|
self.smtp_obj_mock.sendmail.assert_called_once_with('[email protected]', ['[email protected]'], ANY) |
|
89
|
|
|
|
|
90
|
|
|
def test_get_oe_email_footer_html(self): |
|
91
|
|
|
template = get_oe_email_footer_html() |
|
92
|
|
|
expected = """ |
|
93
|
|
|
<div style="float:left; width:100%;"> |
|
94
|
|
|
Onroerend Erfgoed<br/> |
|
95
|
|
|
Herman Teirlinckgebouw | Havenlaan 88 bus 5 | 1000 Brussel <br/> |
|
96
|
|
|
<div style="width:100%"> |
|
97
|
|
|
<div style="float:left; width:100%"> |
|
98
|
|
|
<a href='https://www.onroerenderfgoed.be'>www.onroerenderfgoed.be</a> |
|
99
|
|
|
</div> |
|
100
|
|
|
<div style="float:left; width:100%"> |
|
101
|
|
|
<img src="https://www.onroerenderfgoed.be/assets/img/logo-vlaanderen.png" height="100"> |
|
102
|
|
|
</div> |
|
103
|
|
|
</div> |
|
104
|
|
|
</div> |
|
105
|
|
|
""" |
|
106
|
|
|
self.assertEqual(template, expected) |
|
107
|
|
|
|
|
108
|
|
|
def test_get_oe_email_footer_plain(self): |
|
109
|
|
|
template = get_oe_email_footer_plain() |
|
110
|
|
|
expected = """ |
|
111
|
|
|
\n |
|
112
|
|
|
Onroerend Erfgoed\n |
|
113
|
|
|
Herman Teirlinckgebouw | Havenlaan 88 bus 5 | 1000 Brussel\n |
|
114
|
|
|
www.onroerenderfgoed.be\n |
|
115
|
|
|
\n |
|
116
|
|
|
""" |
|
117
|
|
|
self.assertEqual(template, expected) |
|
118
|
|
|
|
|
119
|
|
|
|