diff options
Diffstat (limited to 'service/test')
| -rw-r--r-- | service/test/integration/test_draft_service.py | 19 | ||||
| -rw-r--r-- | service/test/unit/adapter/test_mail.py | 34 | ||||
| -rw-r--r-- | service/test/unit/resources/test_mails_resource.py | 47 | 
3 files changed, 97 insertions, 3 deletions
| diff --git a/service/test/integration/test_draft_service.py b/service/test/integration/test_draft_service.py index 00b1fcfe..4f4a9181 100644 --- a/service/test/integration/test_draft_service.py +++ b/service/test/integration/test_draft_service.py @@ -24,9 +24,24 @@ class DraftServiceTest(SoledadTestBase):      @defer.inlineCallbacks      def test_store_and_load_draft(self):          input_mail = MailBuilder().with_body('some test text').build_input_mail() - -        stored_draft = yield self.draft_service.create_draft(input_mail) +        draft_id = None +        stored_draft = yield self.draft_service.process_draft(draft_id, input_mail)          draft = yield self.mail_store.get_mail(stored_draft.ident, include_body=True)          self.assertEqual('some test text', draft.body) + +    @defer.inlineCallbacks +    def test_update_draft(self): +        input_mail = MailBuilder().with_body('some test text').build_input_mail() +        saved_mail = yield self.mail_store.add_mail('DRAFTS', input_mail.raw) +        draft_id = saved_mail.ident +        new_email = MailBuilder().with_body('other test text').with_ident(draft_id).build_input_mail() + +        stored_draft = yield self.draft_service.process_draft(draft_id, new_email) + +        old_draft = yield self.mail_store.get_mail(draft_id, include_body=True) +        draft = yield self.mail_store.get_mail(stored_draft.ident, include_body=True) + +        self.assertIsNone(old_draft) +        self.assertEqual('other test text', draft.body) 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 <http://www.gnu.org/licenses/>. +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': '<p>Hello html Hamburg!</p>', 'id': 'other_attachment_id'}], +        'body': [{'content-type': 'plain', 'raw': 'Hello world!'}, +                 {'content-type': 'html', 'raw': '<p>Hello html world!</p>'}], +        '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<p>Hello html Hamburg!</p>' + +        self.assertRegexpMatches(input_mail.raw, part_one) +        self.assertRegexpMatches(input_mail.raw, part_two) diff --git a/service/test/unit/resources/test_mails_resource.py b/service/test/unit/resources/test_mails_resource.py index 02b17bf1..44aebd4d 100644 --- a/service/test/unit/resources/test_mails_resource.py +++ b/service/test/unit/resources/test_mails_resource.py @@ -24,7 +24,7 @@ from twisted.internet import defer  from mock import patch -class TestArchiveResource(unittest.TestCase): +class TestMailsResource(unittest.TestCase):      def setUp(self):          self.mail_service = mock() @@ -49,3 +49,48 @@ class TestArchiveResource(unittest.TestCase):          d.addCallback(assert_response)          return d + +    @patch('leap.common.events.register') +    def test_render_PUT_should_store_draft_with_attachments(self, mock_register): +        request = DummyRequest(['/mails']) +        request.method = 'PUT' +        content = mock() +        when(content).read().thenReturn('{"attachments": [{"id": "some fake attachment id"}]}') +        when(self.mail_service).attachment('some fake attachment id').thenReturn(defer.Deferred()) +        request.content = content + +        mails_resource = MailsResource(self.mail_service, mock()) +        mails_resource.isLeaf = True +        web = DummySite(mails_resource) +        d = web.get(request) + +        def assert_response(_): +            verify(self.mail_service).attachment('some fake attachment id') + +        d.addCallback(assert_response) +        return d + +    @patch('leap.common.events.register') +    def test_render_POST_should_send_email_with_attachments(self, mock_register): +        request = DummyRequest(['/mails']) +        request.method = 'POST' +        content = mock() +        when(content).read().thenReturn('{"attachments": [{"id": "some fake attachment id"}]}') +        when(self.mail_service).attachment('some fake attachment id').thenReturn(defer.succeed({"content": "some content"})) +        as_dictable = mock() +        when(as_dictable).as_dict().thenReturn({}) +        when(self.mail_service).send_mail({"attachments": [{"id": "some fake attachment id", "raw": "some content"}]})\ +            .thenReturn(defer.succeed(as_dictable)) +        request.content = content + +        mails_resource = MailsResource(self.mail_service, mock()) +        mails_resource.isLeaf = True +        web = DummySite(mails_resource) +        d = web.get(request) + +        def assert_response(_): +            verify(self.mail_service).attachment('some fake attachment id') +            verify(self.mail_service).send_mail({"attachments": [{"id": "some fake attachment id", "raw": "some content"}]}) + +        d.addCallback(assert_response) +        return d | 
