|
| 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