diff options
-rw-r--r-- | service/pixelated/adapter/soledad_querier.py | 21 | ||||
-rw-r--r-- | service/test/integration/soledad_querier_test.py | 57 | ||||
-rw-r--r-- | service/test/support/integration_helper.py | 34 |
3 files changed, 76 insertions, 36 deletions
diff --git a/service/pixelated/adapter/soledad_querier.py b/service/pixelated/adapter/soledad_querier.py index 6a8f7d97..ae518f00 100644 --- a/service/pixelated/adapter/soledad_querier.py +++ b/service/pixelated/adapter/soledad_querier.py @@ -28,16 +28,14 @@ class SoledadQuerier: mailboxes = [d for d in self.soledad.get_from_index('by-type', 'mbox') if d.content['mbox'] == mailbox_name] if len(mailboxes) == 0: return - sorted(mailboxes, key=lambda x: x.content['lastuid'], reverse=True) - mailboxes_to_remove = mailboxes[1:len(mailboxes)] + mailboxes_to_remove = sorted(mailboxes, key=lambda x: x.content['created'])[1:len(mailboxes)] self._remove_many(mailboxes_to_remove) def _remove_dup_recent(self, mailbox_name): rct = [d for d in self.soledad.get_from_index('by-type', 'rct') if d.content['mbox'] == mailbox_name] if len(rct) == 0: return - sorted(rct, key=lambda x: len(x.content['rct']), reverse=True) - rct_to_remove = rct[1:len(rct)] + rct_to_remove = sorted(rct, key=lambda x: len(x.content['rct']), reverse=True)[1:len(rct)] self._remove_many(rct_to_remove) def remove_duplicates(self): @@ -76,9 +74,15 @@ class SoledadQuerier: self._update_index([mail.fdoc, mail.hdoc]) def create_mail(self, mail, mailbox_name): - uid = self._next_uid_for_mailbox(mailbox_name) + mbox = [m for m in self.soledad.get_from_index('by-type', 'mbox') if m.content['mbox'] == 'INBOX'][0] + + uid = mbox.content['lastuid'] + 1 new_docs = [self.soledad.create_doc(doc) for doc in mail._get_for_save(next_uid=uid, mailbox=mailbox_name)] + mbox.content['lastuid'] = uid + + self.soledad.put_doc(mbox) self._update_index(new_docs) + return self.mail(mail.ident) def mail(self, ident): @@ -103,13 +107,6 @@ class SoledadQuerier: def idents_by_mailbox(self, mailbox_name): return set(doc.content['chash'] for doc in self.soledad.get_from_index('by-type-and-mbox-and-deleted', 'flags', mailbox_name, '0')) - def _next_uid_for_mailbox(self, mailbox_name): - mails = self.all_mails_by_mailbox(mailbox_name) - mails.sort(key=lambda x: x.uid, reverse=True) - if len(mails) == 0: - return 1 - return mails[0].uid + 1 - def _update_index(self, docs): db = self.soledad._db diff --git a/service/test/integration/soledad_querier_test.py b/service/test/integration/soledad_querier_test.py new file mode 100644 index 00000000..3c1ce6e1 --- /dev/null +++ b/service/test/integration/soledad_querier_test.py @@ -0,0 +1,57 @@ +# +# Copyright (c) 2014 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/>. + +import copy +import unittest +import time +from test.support.integration_helper import SoledadTestBase +from leap.mail.imap.fields import WithMsgFields + + +class SoledadQuerierTest(unittest.TestCase, SoledadTestBase, WithMsgFields): + + def setUp(self): + self.setup_soledad() + + def tearDown(self): + self.teardown_soledad() + + def _get_empty_mailbox(self): + return copy.deepcopy(self.EMPTY_MBOX) + + def _create_mailbox(self, mailbox_name): + new_mailbox = self._get_empty_mailbox() + new_mailbox['mbox'] = mailbox_name + new_mailbox['created'] = int(time.time() * 10E2) + return self.soledad.create_doc(new_mailbox) + + def _get_mailboxes_from_soledad(self, mailbox_name): + return [m for m in self.soledad.get_from_index('by-type', 'mbox') if m.content['mbox'] == mailbox_name] + + def test_remove_dup_mailboxes_keeps_the_one_with_the_highest_last_uid(self): + self.add_multiple_to_mailbox(3, 'INBOX') # by now we already have one inbox with 3 mails + self._create_mailbox('INBOX') # now we have a duplicate + + # make sure we have two + inboxes = self._get_mailboxes_from_soledad('INBOX') + self.assertEqual(2, len(inboxes)) + + self.soledad_querier.remove_duplicates() + + # make sure we only have one, and the one with the right lastuid + inboxes = self._get_mailboxes_from_soledad('INBOX') + self.assertEqual(1, len(inboxes)) + self.assertEqual(3, inboxes[0].content['lastuid']) diff --git a/service/test/support/integration_helper.py b/service/test/support/integration_helper.py index 0bb88e52..4691af2d 100644 --- a/service/test/support/integration_helper.py +++ b/service/test/support/integration_helper.py @@ -18,7 +18,7 @@ import shutil from leap.soledad.client import Soledad from mockito import mock import os -from mock import Mock +from mock import Mock, MagicMock from pixelated.adapter.mail_service import MailService from pixelated.adapter.search import SearchEngine from pixelated.adapter.status import Status @@ -30,27 +30,12 @@ from pixelated.adapter.mailboxes import Mailboxes from pixelated.adapter.soledad_querier import SoledadQuerier from pixelated.controllers import * import pixelated.config.app_factory as app_factory +from leap.mail.imap.account import SoledadBackedAccount soledad_test_folder = "soledad-test" -class FakeLeapMailboxWithListeners: - def __init__(self): - self.listeners = set() - - def addListener(self, listener): - self.listeners.add(listener) - - -class FakeAccount: - def __init__(self): - self.mailboxes = ['INBOX', 'DRAFTS', 'SENT', 'TRASH'] - - def getMailbox(self, name): - return FakeLeapMailboxWithListeners() - - def initialize_soledad(tempdir): if os.path.isdir(soledad_test_folder): shutil.rmtree(soledad_test_folder) @@ -80,12 +65,12 @@ def initialize_soledad(tempdir): local_db_path, server_url, cert_file) - - from leap.mail.imap.fields import fields - - for name, expression in fields.INDEXES.items(): - _soledad.create_index(name, *expression) - + # + # from leap.mail.imap.fields import fields + # + # for name, expression in fields.INDEXES.items(): + # _soledad.create_index(name, *expression) + # return _soledad @@ -158,7 +143,8 @@ class SoledadTestBase: self._reset_routes(self.client.application) self.soledad_querier = SoledadQuerier(self.soledad) - self.account = FakeAccount() + + self.account = SoledadBackedAccount('test', self.soledad, MagicMock()) self.mailboxes = Mailboxes(self.account, self.soledad_querier) self.mail_sender = mock() self.tag_service = TagService() |