diff options
author | Folker Bernitt <fbernitt@thoughtworks.com> | 2015-07-24 10:05:56 +0200 |
---|---|---|
committer | Folker Bernitt <fbernitt@thoughtworks.com> | 2015-08-11 17:00:27 +0200 |
commit | 634fd37726f753ea9928fb629c6cccec1f9d5885 (patch) | |
tree | 3a4472bbeaae40c8abcc3afb117e66f399af9aae | |
parent | ca2be15686064d3ece97261df72560a4f91ade4d (diff) |
Added leap mailstore integration test.
-rw-r--r-- | service/pixelated/adapter/mailstore/leap_mailstore.py | 28 | ||||
-rw-r--r-- | service/test/integration/test_leap_mailstore.py | 61 | ||||
-rw-r--r-- | service/test/unit/adapter/mailstore/test_leap_mailstore.py | 60 |
3 files changed, 119 insertions, 30 deletions
diff --git a/service/pixelated/adapter/mailstore/leap_mailstore.py b/service/pixelated/adapter/mailstore/leap_mailstore.py index 893eb52b..038163f3 100644 --- a/service/pixelated/adapter/mailstore/leap_mailstore.py +++ b/service/pixelated/adapter/mailstore/leap_mailstore.py @@ -23,11 +23,15 @@ from pixelated.adapter.model.mail import Mail class LeapMail(Mail): - def __init__(self, mail_id, headers, tags=tuple(), body=None): + def __init__(self, mail_id, headers, tags=set(), body=None): self._mail_id = mail_id self.headers = headers self._body = body - self._tags = tags + self.tags = tags + + @property + def mail_id(self): + return self._mail_id @property def body(self): @@ -37,22 +41,21 @@ class LeapMail(Mail): return { 'header': {k.lower(): v for k, v in self.headers.items()}, 'ident': self._mail_id, - 'tags': self._tags, + 'tags': self.tags, 'body': self._body } class LeapMailStore(MailStore): - __slots__ = ('account', 'soledad') + __slots__ = ('soledad') - def __init__(self, imapAccount, soledad): - self.account = imapAccount + def __init__(self, soledad): self.soledad = soledad @defer.inlineCallbacks def get_mail(self, mail_id, include_body=False): try: - message = yield SoledadMailAdaptor().get_msg_from_mdoc_id(Message, self.soledad, mail_id) + message = yield self._fetch_msg_from_soledad(mail_id) leap_mail = yield self._leap_message_to_leap_mail(mail_id, message, include_body) defer.returnValue(leap_mail) @@ -67,12 +70,21 @@ class LeapMailStore(MailStore): return defer.gatherResults(deferreds, consumeErrors=True) @defer.inlineCallbacks + def update_mail(self, mail): + message = yield self._fetch_msg_from_soledad(mail.mail_id) + message.get_wrapper().set_tags(tuple(mail.tags)) + message.get_wrapper().update(self.soledad) + pass + + @defer.inlineCallbacks def _leap_message_to_leap_mail(self, mail_id, message, include_body): if include_body: body = (yield message._wrapper.get_body(self.soledad)).raw else: body = None - mail = LeapMail(mail_id, message.get_headers(), message.get_tags(), body=body) + mail = LeapMail(mail_id, message.get_headers(), set(message.get_tags()), body=body) defer.returnValue(mail) + def _fetch_msg_from_soledad(self, mail_id): + return SoledadMailAdaptor().get_msg_from_mdoc_id(Message, self.soledad, mail_id) diff --git a/service/test/integration/test_leap_mailstore.py b/service/test/integration/test_leap_mailstore.py new file mode 100644 index 00000000..612e2082 --- /dev/null +++ b/service/test/integration/test_leap_mailstore.py @@ -0,0 +1,61 @@ +# +# Copyright (c) 2015 ThoughtWorks, Inc. +# +# Pixelated is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pixelated is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# 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.parser import Parser +from leap.mail.mail import Message +import pkg_resources +import os +from pixelated.adapter.mailstore.leap_mailstore import LeapMailStore +from test.support.integration import SoledadTestBase +from leap.mail.adaptors.soledad import SoledadMailAdaptor +from twisted.internet import defer +from uuid import uuid4 + + +class LeapMailStoreTest(SoledadTestBase): + + def setUp(self): + super(LeapMailStoreTest, self).setUp() + self.adaptor = SoledadMailAdaptor() + self.mbox_uuid = str(uuid4()) + + @defer.inlineCallbacks + def test_get_mail_with_body(self): + expected_mail_dict = {'body': u'Dignissimos ducimus veritatis. Est tenetur consequatur quia occaecati. Vel sit sit voluptas.\n\nEarum distinctio eos. Accusantium qui sint ut quia assumenda. Facere dignissimos inventore autem sit amet. Pariatur voluptatem sint est.\n\nUt recusandae praesentium aspernatur. Exercitationem amet placeat deserunt quae consequatur eum. Unde doloremque suscipit quia.\n\n', 'header': {u'date': u'Tue, 21 Apr 2015 08:43:27 +0000 (UTC)', u'to': u'carmel@murazikortiz.name', u'x-tw-pixelated-tags': u'nite, macro, trash', u'from': u'darby.senger@zemlak.biz', u'subject': u'Itaque consequatur repellendus provident sunt quia.'}, 'ident': mail_id, 'tags': set([])} + mail = _load_mail_from_file('mbox00000000') + mail_id = yield self._create_mail_in_soledad(mail) + + store = LeapMailStore(self.soledad) + + result = yield store.get_mail(mail_id, include_body=True) + self.assertIsNotNone(result) + self.assertEqual(expected_mail_dict, result.as_dict()) + + @defer.inlineCallbacks + def _create_mail_in_soledad(self, mail): + message = self.adaptor.get_msg_from_string(Message, mail.as_string()) + message.get_wrapper().set_mbox_uuid(self.mbox_uuid) + yield self.adaptor.initialize_store(self.soledad) + yield self.adaptor.create_msg(self.soledad, message) + + defer.returnValue(message.get_wrapper().mdoc.doc_id) + + +def _load_mail_from_file(mail_file): + mailset_dir = pkg_resources.resource_filename('test.unit.fixtures', 'mailset') + mail_file = os.path.join(mailset_dir, 'new', mail_file) + with open(mail_file) as f: + mail = Parser().parse(f) + return mail diff --git a/service/test/unit/adapter/mailstore/test_leap_mailstore.py b/service/test/unit/adapter/mailstore/test_leap_mailstore.py index cee1dfd2..aa4e62fd 100644 --- a/service/test/unit/adapter/mailstore/test_leap_mailstore.py +++ b/service/test/unit/adapter/mailstore/test_leap_mailstore.py @@ -22,9 +22,8 @@ from leap.soledad.common.document import SoledadDocument from twisted.internet.defer import FirstError from twisted.trial.unittest import TestCase from leap.mail import constants -from leap.mail.imap.account import IMAPAccount from twisted.internet import defer -from mockito import mock, when +from mockito import mock, when, verify, any from leap.mail.adaptors.soledad import SoledadMailAdaptor import pkg_resources from leap.mail.mail import Message @@ -66,13 +65,12 @@ class TestLeapMail(TestCase): class TestLeapMailStore(TestCase): def setUp(self): - self.account = mock(mocked_obj=IMAPAccount) self.soledad = mock() self.mbox_uuid = str(uuid4()) @defer.inlineCallbacks def test_get_mail_not_exist(self): - store = LeapMailStore(self.account, self.soledad) + store = LeapMailStore(self.soledad) mail = yield store.get_mail(_format_mdoc_id(uuid4(), 1)) @@ -80,9 +78,9 @@ class TestLeapMailStore(TestCase): @defer.inlineCallbacks def test_get_mail(self): - mdoc_id = self._add_mail_fixture_to_soledad('mbox00000000') + mdoc_id, _ = self._add_mail_fixture_to_soledad('mbox00000000') - store = LeapMailStore(self.account, self.soledad) + store = LeapMailStore(self.soledad) mail = yield store.get_mail(mdoc_id) @@ -94,10 +92,10 @@ class TestLeapMailStore(TestCase): @defer.inlineCallbacks def test_get_two_different_mails(self): - first_mdoc_id = self._add_mail_fixture_to_soledad('mbox00000000') - second_mdoc_id = self._add_mail_fixture_to_soledad('mbox00000001') + first_mdoc_id, _ = self._add_mail_fixture_to_soledad('mbox00000000') + second_mdoc_id, _ = self._add_mail_fixture_to_soledad('mbox00000001') - store = LeapMailStore(self.account, self.soledad) + store = LeapMailStore(self.soledad) mail1 = yield store.get_mail(first_mdoc_id) mail2 = yield store.get_mail(second_mdoc_id) @@ -108,12 +106,12 @@ class TestLeapMailStore(TestCase): @defer.inlineCallbacks def test_get_mails(self): - first_mdoc_id = self._add_mail_fixture_to_soledad('mbox00000000') - second_mdoc_id = self._add_mail_fixture_to_soledad('mbox00000001') + first_mdoc_id, _ = self._add_mail_fixture_to_soledad('mbox00000000') + second_mdoc_id, _ = self._add_mail_fixture_to_soledad('mbox00000001') - store = LeapMailStore(self.account, self.soledad) + store = LeapMailStore(self.soledad) - mails = yield store.get_mails([first_mdoc_id,second_mdoc_id]) + mails = yield store.get_mails([first_mdoc_id, second_mdoc_id]) self.assertEqual(2, len(mails)) self.assertEqual('Itaque consequatur repellendus provident sunt quia.', mails[0].subject) @@ -121,7 +119,7 @@ class TestLeapMailStore(TestCase): @defer.inlineCallbacks def test_get_mails_fails_for_invalid_mail_id(self): - store = LeapMailStore(self.account, self.soledad) + store = LeapMailStore(self.soledad) try: yield store.get_mails(['invalid']) @@ -131,16 +129,31 @@ class TestLeapMailStore(TestCase): @defer.inlineCallbacks def test_get_mail_with_body(self): - mdoc_id = self._add_mail_fixture_to_soledad('mbox00000000') + expeted_body = 'Dignissimos ducimus veritatis. Est tenetur consequatur quia occaecati. Vel sit sit voluptas.\n\nEarum distinctio eos. Accusantium qui sint ut quia assumenda. Facere dignissimos inventore autem sit amet. Pariatur voluptatem sint est.\n\nUt recusandae praesentium aspernatur. Exercitationem amet placeat deserunt quae consequatur eum. Unde doloremque suscipit quia.\n\n' + mdoc_id, _ = self._add_mail_fixture_to_soledad('mbox00000000') - store = LeapMailStore(self.account, self.soledad) + store = LeapMailStore(self.soledad) mail = yield store.get_mail(mdoc_id, include_body=True) - expeted_body = 'Dignissimos ducimus veritatis. Est tenetur consequatur quia occaecati. Vel sit sit voluptas.\n\nEarum distinctio eos. Accusantium qui sint ut quia assumenda. Facere dignissimos inventore autem sit amet. Pariatur voluptatem sint est.\n\nUt recusandae praesentium aspernatur. Exercitationem amet placeat deserunt quae consequatur eum. Unde doloremque suscipit quia.\n\n' - self.assertEqual(expeted_body, mail.body) + @defer.inlineCallbacks + def test_update_mail(self): + mdoc_id, flags_doc = self._add_mail_fixture_to_soledad('mbox00000000') + when(self.soledad).put_doc(flags_doc).thenReturn(defer.succeed(None)) + + store = LeapMailStore(self.soledad) + + mail = yield store.get_mail(mdoc_id) + + mail.tags.add('new_tag') + + yield store.update_mail(mail) + + verify(self.soledad).put_doc(flags_doc) + self.assertTrue('new_tag' in flags_doc.content['tags']) + def _add_mail_fixture_to_soledad(self, mail_file): mail = self._load_mail_from_file(mail_file) @@ -154,15 +167,18 @@ class TestLeapMailStore(TestCase): cdoc_id = msg.get_wrapper().mdoc.cdocs[0] when(self.soledad).get_doc(mdoc_id).thenReturn(defer.succeed(msg.get_wrapper().mdoc.serialize())) - when(self.soledad).get_doc(fdoc_id).thenReturn(defer.succeed(msg.get_wrapper().fdoc.serialize())) + + flags_doc = SoledadDocument(doc_id=fdoc_id, json=json.dumps(msg.get_wrapper().fdoc.serialize())) + when(self.soledad).get_doc(fdoc_id).thenReturn(defer.succeed(flags_doc)) + + # when(self.soledad).get_doc(fdoc_id).thenReturn(defer.succeed(msg.get_wrapper().fdoc.serialize())) when(self.soledad).get_doc(hdoc_id).thenReturn(defer.succeed(msg.get_wrapper().hdoc.serialize())) content = SoledadDocument(doc_id=cdoc_id, json=json.dumps(msg.get_wrapper().cdocs[1].serialize())) when(self.soledad).get_doc(cdoc_id).thenReturn(defer.succeed(content)) - # when(self.soledad).get_doc(cdoc_id).thenReturn(defer.succeed(msg.get_wrapper().cdocs[1].serialize())) - return mdoc_id + return mdoc_id, flags_doc def _load_mail_from_file(self, mail_file): mailset_dir = pkg_resources.resource_filename('test.unit.fixtures', 'mailset') @@ -173,4 +189,4 @@ class TestLeapMailStore(TestCase): def _format_mdoc_id(mbox_uuid, chash): - return constants.METAMSGID.format(mbox_uuid=mbox_uuid, chash=chash)
\ No newline at end of file + return constants.METAMSGID.format(mbox_uuid=mbox_uuid, chash=chash) |