Permalink
Browse files

moved exceptions into own module

  • Loading branch information...
1 parent 3876d30 commit da3bb5a7ff09f692ba496000ee97d66313936694 @danjac danjac committed Feb 2, 2011
Showing with 88 additions and 39 deletions.
  1. +0 −1 pyramid_mailer/__init__.py
  2. +11 −0 pyramid_mailer/exceptions.py
  3. +12 −7 pyramid_mailer/message.py
  4. +65 −31 pyramid_mailer/tests.py
@@ -1,2 +1 @@
from pyramid_mailer.mailer import Mailer
-from pyramid_mailer.message import Message
@@ -0,0 +1,11 @@
+
+class InvalidMessage(RuntimeError):
+ """
+ Raised if message is missing vital headers, such
+ as recipients or sender address.
+ """
+
+class BadHeaders(RuntimeError):
+ """
+ Raised if message contains newlines in headers.
+ """
View
@@ -1,8 +1,8 @@
from lamson.mail import MailResponse
-class BadHeaderError(Exception): pass
-
+from pyramid_mailer.exceptions import BadHeaders
+from pyramid_mailer.exceptions import InvalidMessage
class Attachment(object):
@@ -118,13 +118,18 @@ def send(self, mailer):
"""
Verifies and sends the message.
"""
-
- assert self.recipients, "No recipients have been added"
- assert self.body or self.html, "No body or HTML has been set"
- assert self.sender, "No sender address has been set"
+
+ if not self.recipients:
+ raise InvalidMessage, "No recipients have been added"
+
+ if not self.body or not self.html:
+ raise InvalidMessage, "No body has been set"
+
+ if not self.sender:
+ raise InvalidMessage, "No sender address has been set"
if self.is_bad_headers():
- raise BadHeaderError
+ raise BadHeaders
mailer.send(self)
View
@@ -12,7 +12,7 @@ class TestMessage(unittest.TestCase):
def test_initialize(self):
- from pyramid_mailer import Message
+ from pyramid_mailer.message import Message
msg = Message(subject="subject",
sender="support@mysite.com",
@@ -25,7 +25,7 @@ def test_initialize(self):
def test_recipients_properly_initialized(self):
- from pyramid_mailer import Message
+ from pyramid_mailer.message import Message
msg = Message(subject="subject")
@@ -43,7 +43,7 @@ def test_recipients_properly_initialized(self):
def test_add_recipient(self):
- from pyramid_mailer import Message
+ from pyramid_mailer.message import Message
msg = Message("testing")
msg.add_recipient("to@example.com")
@@ -53,15 +53,15 @@ def test_add_recipient(self):
def test_sender_as_tuple(self):
- from pyramid_mailer import Message
+ from pyramid_mailer.message import Message
msg = Message(subject="testing",
sender=("tester", "tester@example.com"))
def test_send_without_sender(self):
- from pyramid_mailer import Message
+ from pyramid_mailer.message import Message
from pyramid_mailer import Mailer
msg = Message(subject="testing",
@@ -76,9 +76,11 @@ def test_send_without_sender(self):
def test_send_without_recipients(self):
- from pyramid_mailer import Message
+ from pyramid_mailer.message import Message
from pyramid_mailer import Mailer
+ mailer = Mailer()
+
msg = Message(subject="testing",
recipients=[],
body="testing")
@@ -87,7 +89,7 @@ def test_send_without_recipients(self):
def test_send_without_body(self):
- from pyramid_mailer import Message
+ from pyramid_mailer.message import Message
from pyramid_mailer import Mailer
msg = Message(subject="testing",
@@ -103,7 +105,7 @@ def test_send_without_body(self):
def test_bcc(self):
- from pyramid_mailer import Message
+ from pyramid_mailer.message import Message
msg = Message(subject="testing",
recipients=["to@example.com"],
@@ -115,7 +117,7 @@ def test_bcc(self):
def test_cc(self):
- from pyramid_mailer import Message
+ from pyramid_mailer.message import Message
msg = Message(subject="testing",
recipients=["to@example.com"],
@@ -127,7 +129,7 @@ def test_cc(self):
def test_attach(self):
- from pyramid_mailer import Message
+ from pyramid_mailer.message import Message
msg = Message(subject="testing",
recipients=["to@example.com"],
@@ -147,37 +149,43 @@ def test_attach(self):
def test_bad_header_subject(self):
- from pyramid_mailer import Message
+ from pyramid_mailer.message import Message
from pyramid_mailer import Mailer
+ from pyramid_mailer.exceptions import BadHeaders
+
msg = Message(subject="testing\n\r",
sender="from@example.com",
body="testing",
recipients=["to@example.com"])
mailer = Mailer()
- self.assertRaises(BadHeaderError, mailer.send, msg)
+ self.assertRaises(BadHeaders, mailer.send, msg)
def test_bad_header_sender(self):
- from pyramid_mailer import Message
+ from pyramid_mailer.message import Message
from pyramid_mailer import Mailer
+ from pyramid_mailer.exceptions import BadHeaders
+
mailer = Mailer()
msg = Message(subject="testing",
sender="from@example.com\n\r",
recipients=["to@example.com"],
body="testing")
- self.assertRaises(BadHeaderError, mailer.send, msg)
+ self.assertRaises(BadHeaders, mailer.send, msg)
def test_bad_header_recipient(self):
- from pyramid_mailer import Message
+ from pyramid_mailer.message import Message
from pyramid_mailer import Mailer
+ from pyramid_mailer.exceptions import BadHeaders
+
mailer = Mailer()
msg = Message(subject="testing",
@@ -187,15 +195,49 @@ def test_bad_header_recipient(self):
"to\r\n@example.com"],
body="testing")
- self.assertRaises(BadHeaderError, mailer.send, msg)
+ self.assertRaises(BadHeaders, mailer.send, msg)
+ def test_send_to(self):
+
+ from pyramid_mailer.message import Message
+
+ msg = Message(subject="testing",
+ sender="from@example.com",
+ recipients=[
+ "to@example.com"],
+ cc=['somebodyelse@example.com',
+ 'to@example.com'],
+ bcc=['anotherperson@example.com'],
+ body="testing")
+
+
+ self.assert_(msg.send_to == set(["to@example.com",
+ "somebodyelse@example.com",
+ "anotherperson@example.com"]))
+
+ def test_is_bad_headers_if_no_bad_headers(self):
+ msg = Message(subject="testing",
+ sender="from@example.com",
+ body="testing",
+ recipients=["to@example.com"])
+
+ self.assert_(not(msg.is_bad_headers()))
+
+ def test_is_bad_headers_if_bad_headers(self):
+
+ msg = Message(subject="testing\n\r",
+ sender="from@\nexample.com",
+ body="testing",
+ recipients=["to@example.com"])
+
+ self.assert_(msg.is_bad_headers())
class TestMail(unittest.TestCase):
def test_send(self):
from pyramid_mailer import Mailer
- from pyramid_mailer import Message
+ from pyramid_mailer.message import Message
mailer = Mailer()
@@ -205,25 +247,17 @@ def test_send(self):
mailer.send(msg)
- assert len(outbox) == 1
def test_send_to_queue(self):
from pyramid_mailer import Mailer
+ from pyramid_mailer.message import Message
- mailer = Mailer()
-
- mailer.send_message(subject="testing",
- recipients=["tester@example.com"],
- body="test")
-
- assert len(outbox) == 1
-
- msg = outbox[0]
-
- assert msg.subject == "testing"
- assert msg.recipients == ["tester@example.com"]
- assert msg.body == "test"
+ mailer = Mailer({'mail:queue_path':'/tmp'})
+ msg = Message(subject="testing",
+ recipients=["tester@example.com"],
+ body="test")
+ mailer.send_to_queue(msg)

0 comments on commit da3bb5a

Please sign in to comment.