forked from ajford/flask-sendmail
/
tests.py
150 lines (103 loc) · 4.21 KB
/
tests.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import unittest
import mailbox
from email import encoders
from flask import Flask, g
from flask_sendmail import Mail, Message, BadHeaderError
class TestCase(unittest.TestCase):
TESTING = True
DEFAULT_MAIL_SENDER = "support@example.com"
def setUp(self):
self.app = Flask(__name__)
self.app.config.from_object(self)
self.assertTrue(self.app.testing)
self.mail = Mail(self.app)
self.ctx = self.app.test_request_context()
self.ctx.push()
def tearDown(self):
self.ctx.pop()
class TestMessage(TestCase):
def test_initialize(self):
msg = Message(subject="subject",
recipients=['to@example.com'])
self.assertEqual(msg.sender, "support@example.com")
self.assertEqual(msg.recipients, ['to@example.com'])
def test_recipients_properly_initialized(self):
msg = Message(subject="subject")
self.assertEqual(msg.recipients, [])
msg2 = Message(subject="subject")
msg2.add_recipient("somebody@example.com")
self.assertEqual(len(msg2.recipients), 1)
#def test_sendto_properly_set(self):
#msg = Message(subject="subject", recipients=["somebody@example.com"],
#cc=["cc@example.com"], bcc=["bcc@example.com"])
#self.assertEqual(len(msg.send_to), 3)
#msg.add_recipient("cc@example.com")
#self.assertEqual(len(msg.send_to), 3)
def test_add_recipient(self):
msg = Message("testing")
msg.add_recipient("to@example.com")
self.assertEqual(msg.recipients, ["to@example.com"])
def test_sender_as_tuple(self):
msg = Message(subject="testing",
sender=("tester", "tester@example.com"),
body="test")
msg_str = msg.dump()
self.assertTrue("From: tester <tester@example.com>" in str(msg_str))
def test_reply_to(self):
msg = Message(subject="testing",
recipients=["to@example.com"],
sender="spammer <spammer@example.com>",
reply_to="somebody <somebody@example.com>",
body="testing")
msg_str = msg.dump()
self.assertTrue("Reply-To: somebody <somebody@example.com>" in str(msg_str))
def test_send_without_sender(self):
del self.app.config['DEFAULT_MAIL_SENDER']
msg = Message(subject="testing",
recipients=["to@example.com"],
body="testing")
self.assertRaises(AssertionError, self.mail.send, msg)
def test_send_without_recipients(self):
msg = Message(subject="testing",
recipients=[],
body="testing")
self.assertRaises(AssertionError, self.mail.send, msg)
def test_send_without_body(self):
msg = Message(subject="testing",
recipients=["to@example.com"])
self.assertRaises(AssertionError, self.mail.send, msg)
#def test_normal_send(self):
#"""
#This will not actually send a message unless the mail server
#is set up. The error will be logged but test should still
#pass.
#"""
#
#self.app.config['TESTING'] = False
#self.mail.init_app(self.app)
#
#with self.mail.record_messages() as outbox:
#
#msg = Message(subject="testing",
#recipients=["to@example.com"],
#body="testing")
#
#self.mail.send(msg)
#
#self.assertEqual(len(outbox), 1)
#
#self.app.config['TESTING'] = True
def test_bcc(self):
msg = Message(subject="testing",
recipients=["to@example.com"],
body="testing",
bcc=["tosomeoneelse@example.com"])
msg_str = msg.dump()
self.assertTrue("Bcc: tosomeoneelse@example.com" in str(msg_str))
def test_cc(self):
msg = Message(subject="testing",
recipients=["to@example.com"],
body="testing",
cc=["tosomeoneelse@example.com"])
msg_str = msg.dump()
self.assertTrue("Cc: tosomeoneelse@example.com" in str(msg_str))