Exemplo n.º 1
0
class EmailModuleFuncTest(unittest.TestCase):

    def setUp(self):
        self.controller = QueueController("localhost", 9025)
        self.controller.start()

    def tearDown(self):
        self.controller.stop()

    def test_warnings(self):

        email._mailer = None

        with warnings.catch_warnings(record=True):
            warnings.simplefilter("error")
            self.assertRaises(UserWarning, email.send_email, "*****@*****.**", "*****@*****.**",
                              "test subject", "test body")
            self.assertRaises(UserWarning, email.send_html_email, "*****@*****.**",
                              "*****@*****.**", "test subject", "plain body", "<p>html body</p>")

    def test_send_email(self):
        email.configure({"host": "localhost",
                         "port": 9025})

        body = "This is the bloody test body"
        email.send_email("*****@*****.**", "*****@*****.**", "test subject", body)

        message = list(self.controller)[0]
        (from_str, from_cs) = decode_header(message["From"])[0]
        (to_str, to_cs) = decode_header(message["To"])[0]
        (subject_str, subject_cs) = decode_header(message["Subject"])[0]

        self.assertEqual("*****@*****.**", from_str)
        self.assertEqual("*****@*****.**", to_str)
        self.assertEqual("test subject", subject_str)
        self.assertEqual(body, unicode(message.get_payload(decode=True),
                                       message.get_content_charset()))

    def test_send_html_email(self):
        email.configure({"host": "localhost",
                         "port": 9025})

        text = u"This is the bloody test body"
        html = u"<p>This is the bloody test body</p>"
        email.send_html_email("*****@*****.**", "*****@*****.**", "test subject", text, html)

        message = list(self.controller)[0]
        (from_str, from_cs) = decode_header(message["From"])[0]
        (to_str, to_cs) = decode_header(message["To"])[0]
        (subject_str, subject_cs) = decode_header(message["Subject"])[0]

        self.assertEqual("*****@*****.**", from_str)
        self.assertEqual("*****@*****.**", to_str)
        self.assertEqual("test subject", subject_str)
        self.assertEqual(text, unicode(message.get_payload(0).get_payload(decode=True),
                                       message.get_payload(0).get_content_charset()))
        self.assertEqual("text/plain", message.get_payload(0).get_content_type())
        self.assertEqual(html, unicode(message.get_payload(1).get_payload(decode=True),
                                       message.get_payload(1).get_content_charset()))
        self.assertEqual("text/html", message.get_payload(1).get_content_type())
Exemplo n.º 2
0
class MailerTest(unittest.TestCase):
    def setUp(self):
        self.controller = QueueController("localhost", 9025)
        self.controller.start()

    def tearDown(self):
        self.controller.stop()

    def test_send_email(self):
        mailer = Mailer("localhost", 9025)
        body = "This is the bloody test body"
        mailer.send_email("*****@*****.**", "*****@*****.**",
                          "test subject", body)

        message = list(self.controller)[0]
        (from_str, from_cs) = decode_header(message["From"])[0]
        (to_str, to_cs) = decode_header(message["To"])[0]
        (subject_str, subject_cs) = decode_header(message["Subject"])[0]

        self.assertEqual("*****@*****.**", from_str)
        self.assertEqual("*****@*****.**", to_str)
        self.assertEqual("test subject", subject_str)
        self.assertEqual(
            body,
            unicode(message.get_payload(decode=True),
                    message.get_content_charset()))

    def test_send_html_email(self):
        mailer = Mailer("localhost", 9025)
        text = u"This is the bloody test body"
        html = u"<p>This is the bloody test body</p>"
        mailer.send_html_email("*****@*****.**", "*****@*****.**",
                               "test subject", text, html)

        message = list(self.controller)[0]
        (from_str, from_cs) = decode_header(message["From"])[0]
        (to_str, to_cs) = decode_header(message["To"])[0]
        (subject_str, subject_cs) = decode_header(message["Subject"])[0]

        self.assertEqual("*****@*****.**", from_str)
        self.assertEqual("*****@*****.**", to_str)
        self.assertEqual("test subject", subject_str)
        self.assertEqual(
            text,
            unicode(
                message.get_payload(0).get_payload(decode=True),
                message.get_payload(0).get_content_charset()))
        self.assertEqual("text/plain",
                         message.get_payload(0).get_content_type())
        self.assertEqual(
            html,
            unicode(
                message.get_payload(1).get_payload(decode=True),
                message.get_payload(1).get_content_charset()))
        self.assertEqual("text/html",
                         message.get_payload(1).get_content_type())
Exemplo n.º 3
0
class BaseEmailTestCase(unittest.TestCase):
    def setUp(self):
        self.controller = QueueController('localhost', 9025)
        self.controller.start()

        self._smtp_host = self.controller.server.host
        self._smtp_port = self.controller.server.port

    def tearDown(self):
        self.controller.stop()
        del self._smtp_host
        del self._smtp_port
Exemplo n.º 4
0
class MailerTest(unittest.TestCase):

    def setUp(self):
        self.controller = QueueController("localhost", 9025)
        self.controller.start()

    def tearDown(self):
        self.controller.stop()

    def test_send_email(self):
        mailer = Mailer("localhost", 9025)
        body = "This is the bloody test body"
        mailer.send_email("*****@*****.**", "*****@*****.**", "test subject", body)

        message = list(self.controller)[0]
        (from_str, from_cs) = decode_header(message["From"])[0]
        (to_str, to_cs) = decode_header(message["To"])[0]
        (subject_str, subject_cs) = decode_header(message["Subject"])[0]

        self.assertEqual("*****@*****.**", from_str)
        self.assertEqual("*****@*****.**", to_str)
        self.assertEqual("test subject", subject_str)
        self.assertEqual(body, unicode(message.get_payload(decode=True),
                                       message.get_content_charset()))

    def test_send_html_email(self):
        mailer = Mailer("localhost", 9025)
        text = u"This is the bloody test body"
        html = u"<p>This is the bloody test body</p>"
        mailer.send_html_email("*****@*****.**", "*****@*****.**", "test subject", text, html)

        message = list(self.controller)[0]
        (from_str, from_cs) = decode_header(message["From"])[0]
        (to_str, to_cs) = decode_header(message["To"])[0]
        (subject_str, subject_cs) = decode_header(message["Subject"])[0]

        self.assertEqual("*****@*****.**", from_str)
        self.assertEqual("*****@*****.**", to_str)
        self.assertEqual("test subject", subject_str)
        self.assertEqual(text, unicode(message.get_payload(0).get_payload(decode=True),
                                       message.get_payload(0).get_content_charset()))
        self.assertEqual("text/plain", message.get_payload(0).get_content_type())
        self.assertEqual(html, unicode(message.get_payload(1).get_payload(decode=True),
                                       message.get_payload(1).get_content_charset()))
        self.assertEqual("text/html", message.get_payload(1).get_content_type())
Exemplo n.º 5
0
 def start(self):
     """See `lazr.smtptest.controller.QueueController`."""
     QueueController.start(self)
     # Reset the connection statistics, since the base class's start()
     # method causes a connection to occur.
     self.reset()
Exemplo n.º 6
0
 def start(self):
     """See `lazr.smtptest.controller.QueueController`."""
     QueueController.start(self)
     # Reset the connection statistics, since the base class's start()
     # method causes a connection to occur.
     self.reset()
Exemplo n.º 7
0
class EmailModuleFuncTest(unittest.TestCase):
    def setUp(self):
        self.controller = QueueController("localhost", 9025)
        self.controller.start()

    def tearDown(self):
        self.controller.stop()

    def test_warnings(self):

        email._mailer = None

        with warnings.catch_warnings(record=True):
            warnings.simplefilter("error")
            self.assertRaises(UserWarning, email.send_email,
                              "*****@*****.**", "*****@*****.**",
                              "test subject", "test body")
            self.assertRaises(UserWarning, email.send_html_email,
                              "*****@*****.**", "*****@*****.**",
                              "test subject", "plain body", "<p>html body</p>")

    def test_send_email(self):
        email.configure({"host": "localhost", "port": 9025})

        body = "This is the bloody test body"
        email.send_email("*****@*****.**", "*****@*****.**",
                         "test subject", body)

        message = list(self.controller)[0]
        (from_str, from_cs) = decode_header(message["From"])[0]
        (to_str, to_cs) = decode_header(message["To"])[0]
        (subject_str, subject_cs) = decode_header(message["Subject"])[0]

        self.assertEqual("*****@*****.**", from_str)
        self.assertEqual("*****@*****.**", to_str)
        self.assertEqual("test subject", subject_str)
        self.assertEqual(
            body,
            unicode(message.get_payload(decode=True),
                    message.get_content_charset()))

    def test_send_html_email(self):
        email.configure({"host": "localhost", "port": 9025})

        text = u"This is the bloody test body"
        html = u"<p>This is the bloody test body</p>"
        email.send_html_email("*****@*****.**", "*****@*****.**",
                              "test subject", text, html)

        message = list(self.controller)[0]
        (from_str, from_cs) = decode_header(message["From"])[0]
        (to_str, to_cs) = decode_header(message["To"])[0]
        (subject_str, subject_cs) = decode_header(message["Subject"])[0]

        self.assertEqual("*****@*****.**", from_str)
        self.assertEqual("*****@*****.**", to_str)
        self.assertEqual("test subject", subject_str)
        self.assertEqual(
            text,
            unicode(
                message.get_payload(0).get_payload(decode=True),
                message.get_payload(0).get_content_charset()))
        self.assertEqual("text/plain",
                         message.get_payload(0).get_content_type())
        self.assertEqual(
            html,
            unicode(
                message.get_payload(1).get_payload(decode=True),
                message.get_payload(1).get_content_charset()))
        self.assertEqual("text/html",
                         message.get_payload(1).get_content_type())