Skip to content

Commit 519a02c

Browse files
committedApr 20, 2012
Adding tests
1 parent 8c2ef44 commit 519a02c

File tree

2 files changed

+155
-6
lines changed

2 files changed

+155
-6
lines changed
 

‎setup.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
Links
1111
`````
1212
13-
* `documentation <http://packages.python.org/Flask-Mail>`_
13+
* `documentation <http://packages.python.org/Flask-Sendmail>`_
1414
* `development version
1515
<https://github.com/ajford/flask-sendmail/tarball/master#egg=Flask-Sendmail>`_
1616
"""
@@ -27,16 +27,15 @@
2727
description='Flask extension for sendmail',
2828
long_description=__doc__,
2929
packages=['flask_sendmail'],
30-
#test_suite='nose.collector',
30+
test_suite='nose.collector',
3131
zip_safe=False,
3232
platforms='any',
3333
install_requires=[
3434
'Flask',
3535
],
36-
#tests_require=[
37-
#'nose',
38-
#'blinker',
39-
#],
36+
tests_require=[
37+
'nose',
38+
],
4039
classifiers=[
4140
'Development Status :: 4 - Beta',
4241
'Environment :: Web Environment',

‎tests.py

+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
import unittest
2+
import mailbox
3+
4+
from email import encoders
5+
6+
from flask import Flask, g
7+
from flask_sendmail import Mail, Message, BadHeaderError
8+
9+
class TestCase(unittest.TestCase):
10+
11+
TESTING = True
12+
DEFAULT_MAIL_SENDER = "support@example.com"
13+
14+
def setUp(self):
15+
16+
self.app = Flask(__name__)
17+
self.app.config.from_object(self)
18+
19+
self.assertTrue(self.app.testing)
20+
21+
self.mail = Mail(self.app)
22+
23+
self.ctx = self.app.test_request_context()
24+
self.ctx.push()
25+
26+
def tearDown(self):
27+
self.ctx.pop()
28+
29+
class TestMessage(TestCase):
30+
31+
def test_initialize(self):
32+
33+
msg = Message(subject="subject",
34+
recipients=['to@example.com'])
35+
36+
self.assertEqual(msg.sender, "support@example.com")
37+
self.assertEqual(msg.recipients, ['to@example.com'])
38+
39+
def test_recipients_properly_initialized(self):
40+
41+
msg = Message(subject="subject")
42+
self.assertEqual(msg.recipients, [])
43+
44+
msg2 = Message(subject="subject")
45+
msg2.add_recipient("somebody@example.com")
46+
self.assertEqual(len(msg2.recipients), 1)
47+
48+
#def test_sendto_properly_set(self):
49+
#msg = Message(subject="subject", recipients=["somebody@example.com"],
50+
#cc=["cc@example.com"], bcc=["bcc@example.com"])
51+
#self.assertEqual(len(msg.send_to), 3)
52+
#msg.add_recipient("cc@example.com")
53+
#self.assertEqual(len(msg.send_to), 3)
54+
55+
def test_add_recipient(self):
56+
57+
msg = Message("testing")
58+
msg.add_recipient("to@example.com")
59+
60+
self.assertEqual(msg.recipients, ["to@example.com"])
61+
62+
63+
def test_sender_as_tuple(self):
64+
65+
msg = Message(subject="testing",
66+
sender=("tester", "tester@example.com"),
67+
body="test")
68+
69+
msg_str = msg.dump()
70+
self.assertTrue("From: tester <tester@example.com>" in str(msg_str))
71+
72+
73+
def test_reply_to(self):
74+
75+
msg = Message(subject="testing",
76+
recipients=["to@example.com"],
77+
sender="spammer <spammer@example.com>",
78+
reply_to="somebody <somebody@example.com>",
79+
body="testing")
80+
81+
msg_str = msg.dump()
82+
self.assertTrue("Reply-To: somebody <somebody@example.com>" in str(msg_str))
83+
84+
def test_send_without_sender(self):
85+
86+
del self.app.config['DEFAULT_MAIL_SENDER']
87+
88+
msg = Message(subject="testing",
89+
recipients=["to@example.com"],
90+
body="testing")
91+
92+
self.assertRaises(AssertionError, self.mail.send, msg)
93+
94+
def test_send_without_recipients(self):
95+
96+
msg = Message(subject="testing",
97+
recipients=[],
98+
body="testing")
99+
100+
self.assertRaises(AssertionError, self.mail.send, msg)
101+
102+
def test_send_without_body(self):
103+
104+
msg = Message(subject="testing",
105+
recipients=["to@example.com"])
106+
107+
self.assertRaises(AssertionError, self.mail.send, msg)
108+
109+
#def test_normal_send(self):
110+
#"""
111+
#This will not actually send a message unless the mail server
112+
#is set up. The error will be logged but test should still
113+
#pass.
114+
#"""
115+
#
116+
#self.app.config['TESTING'] = False
117+
#self.mail.init_app(self.app)
118+
#
119+
#with self.mail.record_messages() as outbox:
120+
#
121+
#msg = Message(subject="testing",
122+
#recipients=["to@example.com"],
123+
#body="testing")
124+
#
125+
#self.mail.send(msg)
126+
#
127+
#self.assertEqual(len(outbox), 1)
128+
#
129+
#self.app.config['TESTING'] = True
130+
131+
def test_bcc(self):
132+
133+
msg = Message(subject="testing",
134+
recipients=["to@example.com"],
135+
body="testing",
136+
bcc=["tosomeoneelse@example.com"])
137+
138+
msg_str = msg.dump()
139+
self.assertTrue("Bcc: tosomeoneelse@example.com" in str(msg_str))
140+
141+
def test_cc(self):
142+
143+
msg = Message(subject="testing",
144+
recipients=["to@example.com"],
145+
body="testing",
146+
cc=["tosomeoneelse@example.com"])
147+
148+
msg_str = msg.dump()
149+
self.assertTrue("Cc: tosomeoneelse@example.com" in str(msg_str))
150+

0 commit comments

Comments
 (0)
Please sign in to comment.