From 6b588bb4cb93f8e265d044ea784cb18b7021b26c Mon Sep 17 00:00:00 2001 From: Folker Bernitt Date: Fri, 7 Aug 2015 09:30:51 +0200 Subject: Changed test_delete_mail.py integration test to new mail store. - intermediate state, some tests are broken --- .../pixelated/adapter/mailstore/leap_mailstore.py | 21 ++++++++++++++++- service/pixelated/adapter/services/mail_service.py | 21 ++++++++++------- service/pixelated/resources/__init__.py | 11 +++++++-- service/pixelated/resources/mails_resource.py | 6 ++++- service/test/integration/test_delete_mail.py | 5 ++-- .../test/support/integration/app_test_client.py | 27 ++++++++++++++-------- .../unit/adapter/mailstore/test_leap_mailstore.py | 8 ++++--- 7 files changed, 73 insertions(+), 26 deletions(-) diff --git a/service/pixelated/adapter/mailstore/leap_mailstore.py b/service/pixelated/adapter/mailstore/leap_mailstore.py index c3c93cc3..c34d2b16 100644 --- a/service/pixelated/adapter/mailstore/leap_mailstore.py +++ b/service/pixelated/adapter/mailstore/leap_mailstore.py @@ -13,6 +13,7 @@ # # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see . +from uuid import uuid4 from leap.mail.adaptors.soledad import SoledadMailAdaptor from twisted.internet import defer from pixelated.adapter.mailstore.mailstore import MailStore, underscore_uuid @@ -31,6 +32,10 @@ class LeapMail(Mail): self.tags = tags self._flags = flags + @property + def ident(self): + return self._mail_id + @property def mail_id(self): return self._mail_id @@ -84,6 +89,8 @@ class LeapMailStore(MailStore): defer.returnValue(leap_mail) except AttributeError, e: + import traceback + traceback.print_exc() defer.returnValue(None) def get_mails(self, mail_ids): @@ -199,12 +206,24 @@ class LeapMailStore(MailStore): map = (yield self._mailbox_uuid_to_name_map()) defer.returnValue(map[uuid]) + @defer.inlineCallbacks def _get_or_create_mailbox(self, mailbox_name): - return SoledadMailAdaptor().get_or_create_mbox(self.soledad, mailbox_name) + mailbox_name_upper = mailbox_name.upper() + mbx = yield SoledadMailAdaptor().get_or_create_mbox(self.soledad, mailbox_name_upper) + if mbx.uuid is None: + mbx.uuid = str(uuid4()) + yield mbx.update(self.soledad) + defer.returnValue(mbx) def _fetch_msg_from_soledad(self, mail_id, load_body=False): return SoledadMailAdaptor().get_msg_from_mdoc_id(Message, self.soledad, mail_id, get_cdocs=load_body) + @defer.inlineCallbacks + def _dump_soledad(self): + gen, docs = yield self.soledad.get_all_docs() + for doc in docs: + print '\n%s\n' % doc + def _is_empty_message(message): return (message is None) or (message.get_wrapper().mdoc.doc_id is None) diff --git a/service/pixelated/adapter/services/mail_service.py b/service/pixelated/adapter/services/mail_service.py index 23cbc5f7..5e4d7a6d 100644 --- a/service/pixelated/adapter/services/mail_service.py +++ b/service/pixelated/adapter/services/mail_service.py @@ -29,15 +29,20 @@ class MailService(object): @defer.inlineCallbacks def all_mails(self): - defer.returnValue((yield self.querier.all_mails())) + mails = yield self.mail_store.all_mails() + defer.returnValue(mails) @defer.inlineCallbacks def mails(self, query, window_size, page): mail_ids, total = self.search_engine.search(query, window_size, page) - mails = yield self.querier.mails(mail_ids) - - defer.returnValue((mails, total)) + try: + mails = yield self.mail_store.get_mails(mail_ids) + defer.returnValue((mails, total)) + except Exception, e: + import traceback + traceback.print_exc() + raise @defer.inlineCallbacks def update_tags(self, mail_id, new_tags): @@ -65,7 +70,7 @@ class MailService(object): return [_use_current_casing(new_tag.lower()) if new_tag.lower() in current_tags_lower else new_tag for new_tag in new_tags] def mail(self, mail_id): - return self.querier.mail(mail_id) + return self.mail_store.get_mail(mail_id) def attachment(self, attachment_id, encoding): return self.querier.attachment(attachment_id, encoding) @@ -104,10 +109,10 @@ class MailService(object): @defer.inlineCallbacks def delete_mail(self, mail_id): mail = yield self.mail(mail_id) - if mail.mailbox_name == 'TRASH': - yield self.delete_permanent(mail_id) + if mail.mailbox_name.upper() == u'TRASH': + yield self.mail_store.delete_mail(mail_id) else: - trashed_mail = yield self.mailboxes.move_to_trash(mail_id) + trashed_mail = yield self.mail_store.move_mail_to_mailbox(mail_id, 'TRASH') self.search_engine.index_mail(trashed_mail) def recover_mail(self, mail_id): diff --git a/service/pixelated/resources/__init__.py b/service/pixelated/resources/__init__.py index b244900a..c65e19f3 100644 --- a/service/pixelated/resources/__init__.py +++ b/service/pixelated/resources/__init__.py @@ -17,15 +17,22 @@ import json +class SetEncoder(json.JSONEncoder): + def default(self, obj): + if isinstance(obj, set): + return list(obj) + return super(SetEncoder, self).default(obj) + + def respond_json(entity, request, status_code=200): - json_response = json.dumps(entity) + json_response = json.dumps(entity, cls=SetEncoder) request.responseHeaders.addRawHeader(b"content-type", b"application/json") request.code = status_code return json_response def respond_json_deferred(entity, request, status_code=200): - json_response = json.dumps(entity) + json_response = json.dumps(entity, cls=SetEncoder) request.responseHeaders.addRawHeader(b"content-type", b"application/json") request.code = status_code request.write(json_response) diff --git a/service/pixelated/resources/mails_resource.py b/service/pixelated/resources/mails_resource.py index d4e8372e..9658bd82 100644 --- a/service/pixelated/resources/mails_resource.py +++ b/service/pixelated/resources/mails_resource.py @@ -123,6 +123,11 @@ class MailsResource(Resource): }) d.addCallback(lambda res: respond_json_deferred(res, request)) + def error_handler(error): + print error + + d.addErrback(error_handler) + return NOT_DONE_YET def render_POST(self, request): @@ -146,7 +151,6 @@ class MailsResource(Resource): return server.NOT_DONE_YET def render_PUT(self, request): - print '\nrender_PUT\n' content_dict = json.loads(request.content.read()) _mail = InputMail.from_dict(content_dict) draft_id = content_dict.get('ident') diff --git a/service/test/integration/test_delete_mail.py b/service/test/integration/test_delete_mail.py index 58c9e33b..9e5143e1 100644 --- a/service/test/integration/test_delete_mail.py +++ b/service/test/integration/test_delete_mail.py @@ -22,12 +22,12 @@ class DeleteMailTest(SoledadTestBase): @defer.inlineCallbacks def test_move_mail_to_trash_when_deleting(self): input_mail = MailBuilder().with_subject('Mail with tags').build_input_mail() - yield self.add_mail_to_inbox(input_mail) + mail = yield self.add_mail_to_inbox(input_mail) inbox_mails = yield self.get_mails_by_tag('inbox') self.assertEquals(1, len(inbox_mails)) - yield self.delete_mail(input_mail.ident) + yield self.delete_mail(mail.mail_id) inbox_mails = yield self.get_mails_by_tag('inbox') self.assertEquals(0, len(inbox_mails)) @@ -45,6 +45,7 @@ class DeleteMailTest(SoledadTestBase): @defer.inlineCallbacks def test_move_mail_to_trash_when_delete_multiple(self): + yield self.add_multiple_to_mailbox(1, 'trash') mails = yield self.add_multiple_to_mailbox(5, 'inbox') mail_idents = [m.ident for m in mails] diff --git a/service/test/support/integration/app_test_client.py b/service/test/support/integration/app_test_client.py index 28d378b3..ee305c69 100644 --- a/service/test/support/integration/app_test_client.py +++ b/service/test/support/integration/app_test_client.py @@ -141,24 +141,33 @@ class AppTestClient(object): @defer.inlineCallbacks def add_mail_to_inbox(self, input_mail): - inbox = yield self.mailboxes.inbox - mail = yield inbox.add(input_mail) - if input_mail.tags: - mail.update_tags(input_mail.tags) - self.search_engine.index_mail(mail) + mail = yield self.mail_store.add_mail('INBOX', input_mail.raw) + defer.returnValue(mail) + # inbox = yield self.mailboxes.inbox + # mail = yield inbox.add(input_mail) + # if input_mail.tags: + # mail.update_tags(input_mail.tags) + # self.search_engine.index_mail(mail) @defer.inlineCallbacks def add_multiple_to_mailbox(self, num, mailbox='', flags=[], tags=[], to='recipient@to.com', cc='recipient@cc.com', bcc='recipient@bcc.com'): mails = [] + yield self.mail_store.add_mailbox(mailbox) for _ in range(num): builder = MailBuilder().with_status(flags).with_tags(tags).with_to(to).with_cc(cc).with_bcc(bcc) builder.with_body(str(random.random())) input_mail = builder.build_input_mail() - mbx = yield self.mailboxes._create_or_get(mailbox) - mail = yield mbx.add(input_mail) + mail = yield self.mail_store.add_mail(mailbox, input_mail.raw) + if tags: + mail.tags.add(tags) + yield self.mail_store.update_mail(mail) mails.append(mail) - mail.update_tags(input_mail.tags) if tags else None - self.search_engine.index_mails(mails) if tags else None + + # mbx = yield self.mailboxes._create_or_get(mailbox) + # mail = yield mbx.add(input_mail) + # mails.append(mail) + # mail.update_tags(input_mail.tags) if tags else None + # self.search_engine.index_mails(mails) if tags else None defer.returnValue(mails) diff --git a/service/test/unit/adapter/mailstore/test_leap_mailstore.py b/service/test/unit/adapter/mailstore/test_leap_mailstore.py index 4565cc6d..429f1683 100644 --- a/service/test/unit/adapter/mailstore/test_leap_mailstore.py +++ b/service/test/unit/adapter/mailstore/test_leap_mailstore.py @@ -23,7 +23,7 @@ from twisted.internet.defer import FirstError from twisted.trial.unittest import TestCase from leap.mail import constants from twisted.internet import defer -from mockito import mock, when, verify, any +from mockito import mock, when, verify, any as ANY import test.support.mockito from leap.mail.adaptors.soledad import SoledadMailAdaptor, MailboxWrapper import pkg_resources @@ -31,7 +31,7 @@ from leap.mail.mail import Message from pixelated.adapter.mailstore import underscore_uuid from pixelated.adapter.mailstore.leap_mailstore import LeapMailStore, LeapMail - +import test.support.mockito class TestLeapMail(TestCase): def test_leap_mail(self): @@ -203,8 +203,9 @@ class TestLeapMailStore(TestCase): def test_add_mailbox(self): when(self.soledad).list_indexes().thenReturn(defer.succeed(MAIL_INDEXES)).thenReturn(defer.succeed(MAIL_INDEXES)) when(self.soledad).get_from_index('by-type-and-mbox', 'mbox', 'TEST').thenReturn(defer.succeed([])) - when(self.soledad).create_doc(any()).thenReturn(defer.succeed(None)) self._mock_create_doc(self.mbox_uuid, MailboxWrapper(mbox='TEST')) + when(self.soledad).get_doc(self.mbox_uuid).thenAnswer(lambda: defer.succeed(self.doc_by_id[self.mbox_uuid])) + when(self.soledad).put_doc(ANY()).thenAnswer(lambda: defer.succeed(None)) store = LeapMailStore(self.soledad) mbox = yield store.add_mailbox('TEST') @@ -212,6 +213,7 @@ class TestLeapMailStore(TestCase): self.assertIsNotNone(mbox) self.assertEqual(self.mbox_uuid, mbox.doc_id) self.assertEqual('TEST', mbox.mbox) + self.assertIsNotNone(mbox.uuid) # assert index got updated @defer.inlineCallbacks -- cgit v1.2.3