From 8b61b34f1ed71c04afbeeb45f08a65d35a18423d Mon Sep 17 00:00:00 2001 From: mnandri Date: Fri, 18 Dec 2015 18:24:52 +0100 Subject: adapting mail controllers POST and PUT to work with attachements Issue #548 --- service/test/unit/adapter/test_mail.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'service/test/unit/adapter') diff --git a/service/test/unit/adapter/test_mail.py b/service/test/unit/adapter/test_mail.py index 5c5d4c59..006bde77 100644 --- a/service/test/unit/adapter/test_mail.py +++ b/service/test/unit/adapter/test_mail.py @@ -14,6 +14,9 @@ # # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see . +from email.mime.nonmultipart import MIMENonMultipart +from email.mime.multipart import MIMEMultipart + from twisted.trial import unittest import pixelated.support.date @@ -50,6 +53,23 @@ def multipart_mail_dict(): } +def with_attachment_mail_dict(): + return { + 'attachments': [{'content-type': 'text/plain', 'filename': 'ayoyo.txt', 'raw': 'Hamburg Ayoyoyooooo!!!', 'id': 'some_attachment_id'}, + {'content-type': 'text/html', 'filename': 'hello.html', 'raw': '

Hello html Hamburg!

', 'id': 'other_attachment_id'}], + 'body': [{'content-type': 'plain', 'raw': 'Hello world!'}, + {'content-type': 'html', 'raw': '

Hello html world!

'}], + 'header': { + 'cc': ['cc@pixelated.org', 'anothercc@pixelated.org'], + 'to': ['to@pixelated.org', 'anotherto@pixelated.org'], + 'bcc': ['bcc@pixelated.org', 'anotherbcc@pixelated.org'], + 'subject': 'Oi', + }, + 'ident': '', + 'tags': ['sent'] + } + + class InputMailTest(unittest.TestCase): def test_to_mime_multipart_should_add_blank_fields(self): @@ -120,3 +140,17 @@ class InputMailTest(unittest.TestCase): self.assertRegexpMatches(mime_multipart.as_string(), part_one) self.assertRegexpMatches(mime_multipart.as_string(), part_two) + + def test_raw_with_attachment_data(self): + input_mail = InputMail.from_dict(with_attachment_mail_dict()) + + attachment = MIMENonMultipart('text', 'plain', Content_Disposition='attachment; filename=ayoyo.txt') + attachment.set_payload('Hello World') + mail = MIMEMultipart() + mail.attach(attachment) + + part_one = 'Content-Type: text/plain; Content-Disposition="attachment; filename=ayoyo.txt"\nMIME-Version: 1.0\n\nHamburg Ayoyoyooooo!!!' + part_two = 'Content-Type: text/html; Content-Disposition="attachment; filename=hello.html"\nMIME-Version: 1.0\n\n

Hello html Hamburg!

' + + self.assertRegexpMatches(input_mail.raw, part_one) + self.assertRegexpMatches(input_mail.raw, part_two) -- cgit v1.2.3