Exemplo n.º 1
0
    def test_repoze_sendmail_send_to_queue_functional(self):
        # functest that emulates the interaction between tgext.mailer and
        # repoze.maildir.add and queuedelivery.send.
        
        import tempfile
        from email.generator import Generator
        from email.parser import Parser
        from tgext.mailer.message import Message
        from tgext.mailer.message import Attachment
        from repoze.sendmail.encoding import cleanup_message
        from repoze.sendmail.delivery import copy_message

        def checkit(msg):
            self.assertEqual(
                msg['Content-Type'],
                'text/plain; charset="iso-8859-1"'
                )
            self.assertEqual(
                msg['Content-Transfer-Encoding'], transfer_encoding)

            payload = msg.get_payload()
            self.assertEqual(payload, expected)

        charset = 'iso-8859-1'
        text_encoded = b'LaPe\xf1a'
        text = text_encoded.decode(charset)
        expected = _qencode(text_encoded).decode('ascii')
        transfer_encoding = 'quoted-printable'
        body = Attachment(
            data=text,
            transfer_encoding=transfer_encoding
            )
        msg = Message(
            subject="testing",
            sender="*****@*****.**",
            recipients=["*****@*****.**"],
            body=body
            )

        # done in tgext.mailer via mailer/send_to_queue
        msg = msg.to_message()
        msg.as_string()

        checkit(msg)

        # done in repoze.sendmail via delivery/AbstractMailDelivery/send
        cleanup_message(msg)

        checkit(msg)

        # done in repoze.sendmail via
        # delivery/AbstractMailDelivery/createDataManager
        msg_copy = copy_message(msg)

        checkit(msg_copy)

        try:
            # emulate what repoze.sendmail maildir.py/add does
            fn = tempfile.mktemp()
            fd = os.open(fn,
                         os.O_CREAT|os.O_EXCL|os.O_WRONLY,
                         0o600
                         )
            with os.fdopen(fd, 'w') as f:
                writer = Generator(f)
                writer.flatten(msg_copy)

            # emulate what repoze.sendmail.queue _parseMessage does
            with open(fn) as foo:
                parser = Parser()
                reconstituted = parser.parse(foo)
                checkit(reconstituted)
                
        finally: # pragma: no cover
            try:
                os.remove(fn)
            except:
                pass
Exemplo n.º 2
0
    def test_repoze_sendmail_send_to_queue_functional(self):
        # functest that emulates the interaction between pyramid_mailer and
        # repoze.maildir.add and queuedelivery.send.
        
        import tempfile
        from email.generator import Generator
        from email.parser import Parser
        from pyramid_mailer.message import Message
        from pyramid_mailer.message import Attachment
        from repoze.sendmail.encoding import cleanup_message
        from repoze.sendmail.delivery import copy_message

        def checkit(msg):
            self.assertEqual(
                msg['Content-Type'],
                'text/plain; charset="iso-8859-1"'
                )
            self.assertEqual(
                msg['Content-Transfer-Encoding'], transfer_encoding)

            payload = msg.get_payload()
            self.assertEqual(payload, expected)

        charset = 'iso-8859-1'
        text_encoded = b'LaPe\xf1a'
        text = text_encoded.decode(charset)
        expected = _qencode(text_encoded).decode('ascii')
        transfer_encoding = 'quoted-printable'
        body = Attachment(
            data=text,
            transfer_encoding=transfer_encoding
            )
        msg = Message(
            subject="testing",
            sender="*****@*****.**",
            recipients=["*****@*****.**"],
            body=body
            )

        # done in pyramid_mailer via mailer/send_to_queue
        msg = msg.to_message()
        msg.as_string()

        checkit(msg)

        # done in repoze.sendmail via delivery/AbstractMailDelivery/send
        cleanup_message(msg)

        checkit(msg)

        # done in repoze.sendmail via
        # delivery/AbstractMailDelivery/createDataManager
        msg_copy = copy_message(msg)

        checkit(msg_copy)

        try:
            # emulate what repoze.sendmail maildir.py/add does
            fn = tempfile.mktemp()
            fd = os.open(fn,
                         os.O_CREAT|os.O_EXCL|os.O_WRONLY,
                         0o600
                         )
            with os.fdopen(fd, 'w') as f:
                writer = Generator(f)
                writer.flatten(msg_copy)

            # emulate what repoze.sendmail.queue _parseMessage does
            with open(fn) as foo:
                parser = Parser()
                reconstituted = parser.parse(foo)
                checkit(reconstituted)
                
        finally: # pragma: no cover
            try:
                os.remove(fn)
            except:
                pass