-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_send_mail.py
106 lines (92 loc) · 3.32 KB
/
test_send_mail.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# coding: utf-8
# vim: fenc=utf-8 ft=python ts=4 sts=4 sw=4 ai et
from __future__ import unicode_literals, print_function
import unittest
import os
import codecs
from send_mail import send_mail
PLAINTEXT_EMAIL = """
Yo...
"""
EMAIL_TEMPLATE = """
<html>
<head></head>
<body>
<h1>Hello, Welcome to the mailing group</h1>
<p>See you in the inbox</p>
<br/>
<br/>
</p>Regards</p>
</body>
</html>
"""
def cheap_dot_env(dot_env_path):
"""Help fn to load constants from file into env vars"""
if os.path.exists(dot_env_path):
with codecs.open(dot_env_path, encoding='utf-8') as f:
for line in f:
line = line.strip()
if len(line) == 0 or line.startswith(';') or line.startswith('#'):
continue
var = line.replace('"', '')\
.strip()\
.split('=', 1)
if len(var) == 2:
key = var[0]
value = var[1].replace('"', '').strip()
os.environ[key] = value
else:
raise Exception('no dot env file')
class MailTestCase(unittest.TestCase):
def setUp(self):
if not os.getenv('TEST_ENV') == 'travis':
cheap_dot_env(os.path.join(os.path.abspath(os.path.dirname(__file__)), '.env'))
def test_full_email_is_sent(self):
send_mail(
'[Mail Test] I should be delivered to the inbox',
message=PLAINTEXT_EMAIL,
html_message=EMAIL_TEMPLATE,
to=[('To Example', '[email protected]'), '[email protected]'],
bcc=[
('You Know Who', '[email protected]')
],
sender=('App', '[email protected]'),
reply_to='[email protected], [email protected]',
attachments=[
os.path.abspath(os.path.dirname(__file__)) + '/LICENSE',
os.path.abspath(os.path.dirname(__file__)) + '/README.rst'
],
custom_headers={'X-Mailer': 'SendMail'}
)
def test_full_email_is_sent_with_details_as_keywords(self):
host = os.getenv('SMTP_HOST')
port = os.getenv('SMTP_PORT')
username = os.getenv('SMTP_USERNAME')
password = os.getenv('SMTP_PASSWORD')
use_tls = os.getenv('SMTP_USE_TLS')
send_mail(
'[Mail Test] I should be delivered to the inbox',
message=PLAINTEXT_EMAIL,
html_message=EMAIL_TEMPLATE,
to=[('To Example', '[email protected]'), '[email protected]'],
bcc=[
('You Know Who', '[email protected]')
],
sender=('App', '[email protected]'),
reply_to='[email protected], [email protected]',
attachments=[
os.path.abspath(os.path.dirname(__file__)) + '/LICENSE',
os.path.abspath(os.path.dirname(__file__)) + '/README.rst'
],
host=host,
port=port,
username=username,
password=password,
use_tls=use_tls
)
if __name__ == '__main__':
unittest.main()