Exemplo n.º 1
0
    def test_should_send(self):
        template = MagicMock()
        email = Email(sender='ben',
                      recipients=['bob', 'betty'],
                      subject='email',
                      template=template,
                      data={})

        negative_render_return_cases = ['', ' ', ' \n ', ' \t ', ' \n\t ']
        for test_case in negative_render_return_cases:
            email.template.render.return_value = test_case
            self.assertIsInstance(
                email.should_send(), NoContent,
                "Should not send if rendered output is '%s'" % test_case)

        positive_render_return_cases = ['foo', '\nfoo']
        for test_case in positive_render_return_cases:
            email.template.render.return_value = test_case
            self.assertIsInstance(
                email.should_send(), OK,
                "Should send if rendered output is '%s'" % test_case)

        email.template.render.side_effect = Exception(
            "Fake template rendering exception")
        self.assertIsInstance(
            email.should_send(), Failure,
            "Should not send if rendering raises an Exception")
Exemplo n.º 2
0
    def test_get_text_body(self):
        template = MagicMock()
        template.render.side_effect = str
        email = Email(sender='ben',
                      recipients=['bob', 'betty'],
                      subject='email',
                      template=template,
                      data={'foo': 'bar'})

        self.assertEqual(email.get_text_body(), "{'foo': 'bar'}")
Exemplo n.º 3
0
    def test_parse_recipients(self):
        template = MagicMock()
        email = Email(sender='ben',
                      recipients=['bob', 'betty'],
                      subject='email',
                      template=template,
                      data={})

        self.assertEqual(email.parse_recipients('ben'), ['ben'])
        self.assertEqual(email.parse_recipients(['ben']), ['ben'])
        self.assertEqual(email.parse_recipients(['ben', 'bob']),
                         ['ben', 'bob'])
Exemplo n.º 4
0
    def test_as_string(self):
        email = Email(sender='ben',
                      recipients=['bob', 'betty'],
                      subject='email',
                      template=None,
                      data=None)
        email.get_text_body = MagicMock()
        email.get_text_body.return_value = 'Some text'

        expected_string = """\
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: email
From: ben
To: bob, betty

Some text"""

        self.assertEqual(email.as_string(), expected_string)