Example #1
0
    def test_0015_email_inheritance(self):
        """
        Email inheritance is working!
        """
        Mail = POOL.get('mail.mail')

        with Transaction().start(DB_NAME, USER, CONTEXT):
            # Stubbing ``Mail.jinja_loader_func`` as it needs an actual
            # template.
            stub(Mail.jinja_loader_func, template_loader)

            email_message = Mail.render_email(
                from_email="*****@*****.**",
                to='*****@*****.**',
                subject='Dummy subject of email',
                text_template='mail/invite.html',
                cc=u'*****@*****.**',
                message="testing"
            )

            self.assertFalse(email_message.is_multipart())
            self.assertEqual(
                email_message.get_payload(decode=True),
                "||testing\n|block 2 from base",
            )
Example #2
0
    def test_0020_render_email_unicode(self):
        """
        Render email unicode
        """
        Mail = POOL.get('mail.mail')

        with Transaction().start(DB_NAME, USER, CONTEXT):
            # Stubbing ``Mail.jinja_loader_func`` as it needs an actual
            # template from filesystem.
            stub(Mail.jinja_loader_func, template_loader)

            email_message = Mail.render_email(
                from_email=u"Soméøne <*****@*****.**>",
                to=u'Søméone Else <*****@*****.**> ',
                subject=u'Añ üñîçø∂é émåîl',
                text_template='mail/unicode.html',
                cc=u'*****@*****.**'
            )

            self.assertEqual(
                decode_header(email_message['From'])[0], (
                    u"Soméøne <*****@*****.**>".encode('ISO-8859-1'),
                    'iso-8859-1'
                )
            )
Example #3
0
    def test_0020_render_email_unicode(self):
        """
        Render email unicode
        """
        Mail = POOL.get('mail.mail')

        with Transaction().start(DB_NAME, USER, CONTEXT):
            # Stubbing ``Mail.jinja_loader_func`` as it needs an actual
            # template from filesystem.
            stub(Mail.jinja_loader_func, template_loader)

            email_message = Mail.render_email(
                from_email=u"Soméøne <*****@*****.**>",
                to=u'Søméone Else <*****@*****.**> ',
                subject=u'Añ üñîçø∂é émåîl',
                text_template='mail/unicode.html',
                cc=u'*****@*****.**'
            )

            self.assertEqual(
                decode_header(email_message['From'])[0], (
                    u"Soméøne <*****@*****.**>".encode('ISO-8859-1'),
                    'iso-8859-1'
                )
            )
Example #4
0
    def test_0015_email_inheritance(self):
        """
        Email inheritance is working!
        """
        Mail = POOL.get('mail.mail')

        with Transaction().start(DB_NAME, USER, CONTEXT):
            # Stubbing ``Mail.jinja_loader_func`` as it needs an actual
            # template.
            stub(Mail.jinja_loader_func, template_loader)

            email_message = Mail.render_email(
                from_email="*****@*****.**",
                to='*****@*****.**',
                subject='Dummy subject of email',
                text_template='mail/invite.html',
                cc=u'*****@*****.**',
                message="testing"
            )

            self.assertFalse(email_message.is_multipart())
            self.assertEqual(
                email_message.get_payload(decode=True),
                "||testing\n|block 2 from base",
            )
Example #5
0
    def test_0005_render_email(self):
        """
        Render email
        """
        Mail = POOL.get('mail.mail')

        with Transaction().start(DB_NAME, USER, CONTEXT):
            # Stubbing ``Mail.jinja_loader_func`` as it needs an actual
            # template from filesystem.
            stub(Mail.jinja_loader_func, template_loader)

            with self.assertRaises(Exception):
                # Try rendering mail without passing text
                # or html template
                Mail.render_email(
                    from_email="*****@*****.**",
                    to='*****@*****.**',
                    subject='Dummy subject of email',
                    cc=u'*****@*****.**'
                )

            email_message = Mail.render_email(
                from_email="*****@*****.**",
                to='*****@*****.**',
                subject='Dummy subject of email',
                text_template='mail/base.html',
                cc=u'*****@*****.**'
            )

            self.assertEqual(
                decode_header(email_message['From'])[0],
                ("*****@*****.**", None)
            )

            self.assertEqual(
                decode_header(email_message['Subject'])[0],
                ('Dummy subject of email', None)
            )

            self.assertFalse(email_message.is_multipart())
            self.assertEqual(
                email_message.get_payload(decode=True),
                "|block 1 from base\n|block 2 from base",
            )

            # Email to can take comma separated email addresses.
            email_message = Mail.render_email(
                from_email="*****@*****.**",
                to='[email protected], [email protected]',
                subject='Dummy subject of email',
                text_template='mail/base.html',
                cc=u'*****@*****.**'
            )
            self.assertEqual(
                decode_header(email_message['To'])[0],
                ('[email protected], [email protected]', None)
            )
Example #6
0
    def test_0005_render_email(self):
        """
        Render email
        """
        Mail = POOL.get('mail.mail')

        with Transaction().start(DB_NAME, USER, CONTEXT):
            # Stubbing ``Mail.jinja_loader_func`` as it needs an actual
            # template from filesystem.
            stub(Mail.jinja_loader_func, template_loader)

            with self.assertRaises(Exception):
                # Try rendering mail without passing text
                # or html template
                Mail.render_email(
                    from_email="*****@*****.**",
                    to='*****@*****.**',
                    subject='Dummy subject of email',
                    cc=u'*****@*****.**'
                )

            email_message = Mail.render_email(
                from_email="*****@*****.**",
                to='*****@*****.**',
                subject='Dummy subject of email',
                text_template='mail/base.html',
                cc=u'*****@*****.**'
            )

            self.assertEqual(
                decode_header(email_message['From'])[0],
                ("*****@*****.**", None)
            )

            self.assertEqual(
                decode_header(email_message['Subject'])[0],
                ('Dummy subject of email', None)
            )

            self.assertFalse(email_message.is_multipart())
            self.assertEqual(
                email_message.get_payload(decode=True),
                "|block 1 from base\n|block 2 from base",
            )

            # Email to can take comma separated email addresses.
            email_message = Mail.render_email(
                from_email="*****@*****.**",
                to='[email protected], [email protected]',
                subject='Dummy subject of email',
                text_template='mail/base.html',
                cc=u'*****@*****.**'
            )
            self.assertEqual(
                decode_header(email_message['To'])[0],
                ('[email protected], [email protected]', None)
            )
Example #7
0
 def stub(self, obj, attr=None):
   '''
   Stub an object. If attr is not None, will attempt to stub that attribute
   on the object. Only required for modules and other rare cases where we
   can't determine the binding from the object.
   '''
   s = stub(obj, attr)
   if s not in self._stubs:
     self._stubs.append( s )
   return s
Example #8
0
def stubbed(target, replacement):
    """
    Stubs an object inside a with statement, returning to
    the original implementation in the end.

    :param target: Object to be stubbed-out.
    :param replacement: Stub value/function.

    Example:
    with stubbed(module_under_test.fun_x, lambda _, __: StringIO('hello world')):
        value = module_under_test.function_using_fun_x()
    """
    stubbed_obj = stub(target, replacement)
    try:
        yield
    finally:
        stubbed_obj.unstub()
Example #9
0
    def test_0010_email_with_attachments(self):
        """
        Send an email with text, html and an attachment
        """
        Mail = POOL.get('mail.mail')

        # Stubbing ``Mail.jinja_loader_func`` as it needs an actual
        # template from filesystem.
        stub(Mail.jinja_loader_func, template_loader)

        email_message = Mail.render_email(
            from_email="*****@*****.**",
            to='*****@*****.**',
            subject='Dummy subject of email',
            text_template='mail/base.html',
            html_template='mail/base.html',
            attachments={'filename.pdf': 'some PDF content'},
        )

        self.assertEqual(
            decode_header(email_message['Subject'])[0],
            ('Dummy subject of email', None)
        )

        # Message type should be multipart/alternative
        self.assertTrue(email_message.is_multipart())
        self.assertEqual(
            email_message.get_content_type(), 'multipart/mixed'
        )

        # Ensure that there are two subparts
        self.assertEqual(
            len(email_message.get_payload()), 2
        )

        # Ensure that the subparts are 1 alternative and
        # octet-stream part
        payload_types = set([
            p.get_content_type() for p in email_message.get_payload()
        ])
        self.assertEqual(
            set(['multipart/alternative', 'application/octet-stream']),
            payload_types
        )

        # Drill into the alternative part and ensure that there is
        # both the text part and html part in it.
        for part in email_message.get_payload():
            if part.get_content_type() == 'multipart/alternative':
                # Ensure that there are two subparts
                # 1. text/plain
                # 2. text/html
                self.assertEqual(
                    len(email_message.get_payload()), 2
                )
                payload_types = set([
                    p.get_content_type()
                    for p in part.get_payload()
                ])
                self.assertEqual(
                    set(['text/plain', 'text/html']),
                    payload_types
                )
                break
        else:
            self.fail('Alternative part not found')
Example #10
0
    def test_0010_email_with_attachments(self):
        """
        Send an email with text, html and an attachment
        """
        Mail = POOL.get('mail.mail')

        with Transaction().start(DB_NAME, USER, CONTEXT):
            # Stubbing ``Mail.jinja_loader_func`` as it needs an actual
            # template from filesystem.
            stub(Mail.jinja_loader_func, template_loader)

            email_message = Mail.render_email(
                from_email="*****@*****.**",
                to='*****@*****.**',
                subject='Dummy subject of email',
                text_template='mail/base.html',
                html_template='mail/base.html',
                attachments={'filename.pdf': 'some PDF content'},
            )

            self.assertEqual(
                decode_header(email_message['Subject'])[0],
                ('Dummy subject of email', None)
            )

            # Message type should be multipart/alternative
            self.assertTrue(email_message.is_multipart())
            self.assertEqual(
                email_message.get_content_type(), 'multipart/mixed'
            )

            # Ensure that there are two subparts
            self.assertEqual(
                len(email_message.get_payload()), 2
            )

            # Ensure that the subparts are 1 alternative and
            # octet-stream part
            payload_types = set([
                p.get_content_type() for p in email_message.get_payload()
            ])
            self.assertEqual(
                set(['multipart/alternative', 'application/octet-stream']),
                payload_types
            )

            # Drill into the alternative part and ensure that there is
            # both the text part and html part in it.
            for part in email_message.get_payload():
                if part.get_content_type() == 'multipart/alternative':
                    # Ensure that there are two subparts
                    # 1. text/plain
                    # 2. text/html
                    self.assertEqual(
                        len(email_message.get_payload()), 2
                    )
                    payload_types = set([
                        p.get_content_type()
                        for p in part.get_payload()
                    ])
                    self.assertEqual(
                        set(['text/plain', 'text/html']),
                        payload_types
                    )
                    break
            else:
                self.fail('Alternative part not found')