forked from ajford/flask-sendmail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
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 = "[email protected]"
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=['[email protected]'])
self.assertEqual(msg.sender, "[email protected]")
self.assertEqual(msg.recipients, ['[email protected]'])
def test_recipients_properly_initialized(self):
msg = Message(subject="subject")
self.assertEqual(msg.recipients, [])
msg2 = Message(subject="subject")
msg2.add_recipient("[email protected]")
self.assertEqual(len(msg2.recipients), 1)
#def test_sendto_properly_set(self):
#msg = Message(subject="subject", recipients=["[email protected]"],
#cc=["[email protected]"], bcc=["[email protected]"])
#self.assertEqual(len(msg.send_to), 3)
#msg.add_recipient("[email protected]")
#self.assertEqual(len(msg.send_to), 3)
def test_add_recipient(self):
msg = Message("testing")
msg.add_recipient("[email protected]")
self.assertEqual(msg.recipients, ["[email protected]"])
def test_sender_as_tuple(self):
msg = Message(subject="testing",
sender=("tester", "[email protected]"),
body="test")
msg_str = msg.dump()
self.assertTrue("From: tester <[email protected]>" in str(msg_str))
def test_reply_to(self):
msg = Message(subject="testing",
recipients=["[email protected]"],
sender="spammer <[email protected]>",
reply_to="somebody <[email protected]>",
body="testing")
msg_str = msg.dump()
self.assertTrue("Reply-To: somebody <[email protected]>" in str(msg_str))
def test_send_without_sender(self):
del self.app.config['DEFAULT_MAIL_SENDER']
msg = Message(subject="testing",
recipients=["[email protected]"],
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=["[email protected]"])
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=["[email protected]"],
#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=["[email protected]"],
body="testing",
bcc=["[email protected]"])
msg_str = msg.dump()
self.assertTrue("Bcc: [email protected]" in str(msg_str))
def test_cc(self):
msg = Message(subject="testing",
recipients=["[email protected]"],
body="testing",
cc=["[email protected]"])
msg_str = msg.dump()
self.assertTrue("Cc: [email protected]" in str(msg_str))