summaryrefslogtreecommitdiff
path: root/service
diff options
context:
space:
mode:
Diffstat (limited to 'service')
-rw-r--r--service/pixelated/adapter/mailstore/leap_mailstore.py27
-rw-r--r--service/test/unit/adapter/mailstore/test_leap_mailstore.py38
-rw-r--r--service/test/unit/adapter/test_mailboxes.py2
3 files changed, 50 insertions, 17 deletions
diff --git a/service/pixelated/adapter/mailstore/leap_mailstore.py b/service/pixelated/adapter/mailstore/leap_mailstore.py
index 60068322..7ba02519 100644
--- a/service/pixelated/adapter/mailstore/leap_mailstore.py
+++ b/service/pixelated/adapter/mailstore/leap_mailstore.py
@@ -23,8 +23,9 @@ from pixelated.adapter.model.mail import Mail
class LeapMail(Mail):
- def __init__(self, mail_id, headers=None, tags=set(), body=None):
+ def __init__(self, mail_id, mailbox_name, headers=None, tags=set(), body=None):
self._mail_id = mail_id
+ self._mailbox_name = mailbox_name
self.headers = headers if headers is not None else {}
self._body = body
self.tags = tags
@@ -37,6 +38,10 @@ class LeapMail(Mail):
def body(self):
return self._body
+ @property
+ def mailbox_name(self):
+ return self._mailbox_name
+
def as_dict(self):
return {
'header': {k.lower(): v for k, v in self.headers.items()},
@@ -94,16 +99,16 @@ class LeapMailStore(MailStore):
@defer.inlineCallbacks
def get_mailbox_names(self):
- mbox_map = set((yield self._mailbox_uuid_to_name()).values())
+ mbox_map = set((yield self._mailbox_uuid_to_name_map()).values())
defer.returnValue(mbox_map.union({'INBOX'}))
@defer.inlineCallbacks
- def _mailbox_uuid_to_name(self):
+ def _mailbox_uuid_to_name_map(self):
map = {}
mbox_docs = yield self.soledad.get_from_index('by-type', 'mbox')
for doc in mbox_docs:
- map[doc.doc_id] = doc.content.get('mbox')
+ map[underscore_uuid(doc.content.get('uuid'))] = doc.content.get('mbox')
defer.returnValue(map)
@@ -161,13 +166,23 @@ class LeapMailStore(MailStore):
@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
+ body = (yield message.get_wrapper().get_body(self.soledad)).raw
else:
body = None
- mail = LeapMail(mail_id, message.get_headers(), set(message.get_tags()), body=body)
+
+ # fetch mailbox name by mbox_uuid
+ mbox_uuid = message.get_wrapper().fdoc.mbox_uuid
+ mbox_name = yield self._mailbox_name_from_uuid(mbox_uuid)
+
+ mail = LeapMail(mail_id, mbox_name, message.get_headers(), set(message.get_tags()), body=body)
defer.returnValue(mail)
+ @defer.inlineCallbacks
+ def _mailbox_name_from_uuid(self, uuid):
+ map = (yield self._mailbox_uuid_to_name_map())
+ defer.returnValue(map[uuid])
+
def _get_or_create_mailbox(self, mailbox_name):
return SoledadMailAdaptor().get_or_create_mbox(self.soledad, mailbox_name)
diff --git a/service/test/unit/adapter/mailstore/test_leap_mailstore.py b/service/test/unit/adapter/mailstore/test_leap_mailstore.py
index 89b19f18..ed5fce84 100644
--- a/service/test/unit/adapter/mailstore/test_leap_mailstore.py
+++ b/service/test/unit/adapter/mailstore/test_leap_mailstore.py
@@ -35,14 +35,14 @@ from pixelated.adapter.mailstore.leap_mailstore import LeapMailStore, LeapMail
class TestLeapMail(TestCase):
def test_leap_mail(self):
- mail = LeapMail('', {'From': 'test@example.test', 'Subject': 'A test Mail', 'To': 'receiver@example.test'})
+ mail = LeapMail('', 'INBOX', {'From': 'test@example.test', 'Subject': 'A test Mail', 'To': 'receiver@example.test'})
self.assertEqual('test@example.test', mail.from_sender)
self.assertEqual('receiver@example.test', mail.to)
self.assertEqual('A test Mail', mail.subject)
def test_as_dict(self):
- mail = LeapMail('doc id', {'From': 'test@example.test', 'Subject': 'A test Mail', 'To': 'receiver@example.test'}, ('foo', 'bar'))
+ mail = LeapMail('doc id', 'INBOX', {'From': 'test@example.test', 'Subject': 'A test Mail', 'To': 'receiver@example.test'}, ('foo', 'bar'))
expected = {
'header': {
@@ -60,7 +60,7 @@ class TestLeapMail(TestCase):
def test_as_dict_with_body(self):
body = 'some body content'
- mail = LeapMail('doc id', {'From': 'test@example.test', 'Subject': 'A test Mail', 'To': 'receiver@example.test'}, ('foo', 'bar'), body=body)
+ mail = LeapMail('doc id', 'INBOX', {'From': 'test@example.test', 'Subject': 'A test Mail', 'To': 'receiver@example.test'}, ('foo', 'bar'), body=body)
self.assertEqual(body, mail.as_dict()['body'])
@@ -74,6 +74,7 @@ class TestLeapMailStore(TestCase):
self.mbox_soledad_docs = []
when(self.soledad).get_from_index('by-type', 'mbox').thenAnswer(lambda: defer.succeed(self.mbox_soledad_docs))
+ self._mock_get_mailbox('INBOX')
@defer.inlineCallbacks
def test_get_mail_not_exist(self):
@@ -96,6 +97,18 @@ class TestLeapMailStore(TestCase):
self.assertEqual('carmel@murazikortiz.name', mail.to)
self.assertEqual('Itaque consequatur repellendus provident sunt quia.', mail.subject)
self.assertIsNone(mail.body)
+ self.assertEqual('INBOX', mail.mailbox_name)
+
+ @defer.inlineCallbacks
+ def test_get_mail_from_mailbox(self):
+ other, _ = self._mock_get_mailbox('OTHER', create_new_uuid=True)
+ mdoc_id, _ = self._add_mail_fixture_to_soledad('mbox00000000', other.uuid)
+
+ store = LeapMailStore(self.soledad)
+
+ mail = yield store.get_mail(mdoc_id)
+
+ self.assertEqual('OTHER', mail.mailbox_name)
@defer.inlineCallbacks
def test_get_two_different_mails(self):
@@ -296,19 +309,20 @@ class TestLeapMailStore(TestCase):
mbox_uuid = self.mbox_uuid if not create_new_uuid else str(uuid4())
when(self.soledad).list_indexes().thenReturn(defer.succeed(MAIL_INDEXES)).thenReturn(
defer.succeed(MAIL_INDEXES))
- mbox = MailboxWrapper(doc_id=mbox_uuid, mbox=mailbox_name, uuid=mbox_uuid)
- soledad_doc = SoledadDocument(mbox_uuid, json=json.dumps(mbox.serialize()))
+ doc_id = str(uuid4())
+ mbox = MailboxWrapper(doc_id=doc_id, mbox=mailbox_name, uuid=mbox_uuid)
+ soledad_doc = SoledadDocument(doc_id, json=json.dumps(mbox.serialize()))
when(self.soledad).get_from_index('by-type-and-mbox', 'mbox', mailbox_name).thenReturn(defer.succeed([soledad_doc]))
- self._mock_soledad_doc(mbox_uuid, mbox)
+ self._mock_soledad_doc(doc_id, mbox)
self.mbox_uuid_by_name[mailbox_name] = mbox_uuid
self.mbox_soledad_docs.append(soledad_doc)
return mbox, soledad_doc
- def _add_mail_fixture_to_soledad(self, mail_file):
+ def _add_mail_fixture_to_soledad(self, mail_file, mbox_uuid=None):
mail = self._load_mail_from_file(mail_file)
- msg = self._convert_mail_to_leap_message(mail)
+ msg = self._convert_mail_to_leap_message(mail, mbox_uuid)
wrapper = msg.get_wrapper()
mdoc_id = wrapper.mdoc.future_doc_id
@@ -340,9 +354,13 @@ class TestLeapMailStore(TestCase):
return msg
- def _convert_mail_to_leap_message(self, mail):
+ def _convert_mail_to_leap_message(self, mail, mbox_uuid=None):
msg = SoledadMailAdaptor().get_msg_from_string(Message, mail.as_string())
- msg.get_wrapper().set_mbox_uuid(self.mbox_uuid)
+ if mbox_uuid is None:
+ msg.get_wrapper().set_mbox_uuid(self.mbox_uuid)
+ else:
+ msg.get_wrapper().set_mbox_uuid(mbox_uuid)
+
return msg
def _mock_soledad_doc(self, doc_id, doc):
diff --git a/service/test/unit/adapter/test_mailboxes.py b/service/test/unit/adapter/test_mailboxes.py
index daa37c0a..4072893f 100644
--- a/service/test/unit/adapter/test_mailboxes.py
+++ b/service/test/unit/adapter/test_mailboxes.py
@@ -33,7 +33,7 @@ class PixelatedMailboxesTest(unittest.TestCase):
@defer.inlineCallbacks
def test_move_to_inbox(self):
- when(self.mail_store).move_mail_to_mailbox(1, 'INBOX').thenReturn(defer.succeed(LeapMail('2', None)))
+ when(self.mail_store).move_mail_to_mailbox(1, 'INBOX').thenReturn(defer.succeed(LeapMail('2', None, 'OTHER')))
moved_mail = yield self.mailboxes.move_to_inbox(1)