From ee6a3fe8b310ea5fed1e7bf625a84e9d663d35bc Mon Sep 17 00:00:00 2001 From: drebs Date: Mon, 17 Jun 2013 17:32:32 -0300 Subject: Adapt IMAP to latest Soledad api. * Also fix some tests that where not up-to-date with code. --- src/leap/mail/imap/tests/__init__.py | 81 +++++++++++++++++++++++++---------- src/leap/mail/imap/tests/test_imap.py | 79 ++++++++++++++++++---------------- 2 files changed, 100 insertions(+), 60 deletions(-) (limited to 'src/leap/mail/imap/tests') diff --git a/src/leap/mail/imap/tests/__init__.py b/src/leap/mail/imap/tests/__init__.py index 315d649..fdeda76 100644 --- a/src/leap/mail/imap/tests/__init__.py +++ b/src/leap/mail/imap/tests/__init__.py @@ -17,13 +17,13 @@ def run(): """xxx fill me in""" pass +import os import u1db from leap.common.testing.basetest import BaseLeapTest from leap.soledad import Soledad -from leap.soledad.util import GPGWrapper -from leap.soledad.backends.leap_backend import LeapDocument +from leap.soledad.document import SoledadDocument #----------------------------------------------------------------------------- @@ -38,30 +38,65 @@ class BaseSoledadIMAPTest(BaseLeapTest): """ def setUp(self): - # config info - self.gnupg_home = "%s/gnupg" % self.tempdir - self.db1_file = "%s/db1.u1db" % self.tempdir - self.db2_file = "%s/db2.u1db" % self.tempdir - self.email = 'leap@leap.se' # open test dbs + self.db1_file = os.path.join( + self.tempdir, "db1.u1db") + self.db2_file = os.path.join( + self.tempdir, "db2.u1db") + self._db1 = u1db.open(self.db1_file, create=True, - document_factory=LeapDocument) + document_factory=SoledadDocument) self._db2 = u1db.open(self.db2_file, create=True, - document_factory=LeapDocument) - - # initialize soledad by hand so we can control keys - self._soledad = Soledad(self.email, gnupg_home=self.gnupg_home, - bootstrap=False, - prefix=self.tempdir) - self._soledad._init_dirs() - self._soledad._gpg = GPGWrapper(gnupghome=self.gnupg_home) - - if not self._soledad._has_privkey(): - self._soledad._set_privkey(PRIVATE_KEY) - if not self._soledad._has_symkey(): - self._soledad._gen_symkey() - self._soledad._load_symkey() - self._soledad._init_db() + document_factory=SoledadDocument) + + # soledad config info + self.email = 'leap@leap.se' + secrets_path = os.path.join( + self.tempdir, Soledad.STORAGE_SECRETS_FILE_NAME) + local_db_path = os.path.join( + self.tempdir, Soledad.LOCAL_DATABASE_FILE_NAME) + server_url = '' + cert_file = None + + self._soledad = self._soledad_instance( + self.email, '123', + secrets_path=secrets_path, + local_db_path=local_db_path, + server_url=server_url, + cert_file=cert_file) + + def _soledad_instance(self, uuid, passphrase, secrets_path, local_db_path, + server_url, cert_file): + """ + Return a Soledad instance for tests. + """ + # mock key fetching and storing so Soledad doesn't fail when trying to + # reach the server. + Soledad._fetch_keys_from_shared_db = Mock(return_value=None) + Soledad._assert_keys_in_shared_db = Mock(return_value=None) + + # instantiate soledad + def _put_doc_side_effect(doc): + self._doc_put = doc + + class MockSharedDB(object): + + get_doc = Mock(return_value=None) + put_doc = Mock(side_effect=_put_doc_side_effect) + + def __call__(self): + return self + + Soledad._shared_db = MockSharedDB() + + return Soledad( + uuid, + passphrase, + secrets_path=secrets_path, + local_db_path=local_db_path, + server_url=server_url, + cert_file=cert_file, + ) def tearDown(self): self._db1.close() diff --git a/src/leap/mail/imap/tests/test_imap.py b/src/leap/mail/imap/tests/test_imap.py index 6b6c24e..8804fe0 100644 --- a/src/leap/mail/imap/tests/test_imap.py +++ b/src/leap/mail/imap/tests/test_imap.py @@ -31,24 +31,18 @@ try: except ImportError: from StringIO import StringIO -#import codecs -#import locale import os import types import tempfile import shutil -#from zope.interface import implements +from mock import Mock + -#from twisted.mail.imap4 import MessageSet from twisted.mail import imap4 from twisted.protocols import loopback from twisted.internet import defer -#from twisted.internet import error -#from twisted.internet import reactor -#from twisted.internet import interfaces -#from twisted.internet.task import Clock from twisted.trial import unittest from twisted.python import util, log from twisted.python import failure @@ -59,8 +53,6 @@ import twisted.cred.checkers import twisted.cred.credentials import twisted.cred.portal -#from twisted.test.proto_helpers import StringTransport, StringTransportWithDisconnection - #import u1db @@ -68,8 +60,6 @@ from leap.common.testing.basetest import BaseLeapTest from leap.mail.imap.server import SoledadMailbox from leap.mail.imap.server import SoledadBackedAccount from leap.mail.imap.server import MessageCollection -#from leap.mail.imap.tests import PUBLIC_KEY -#from leap.mail.imap.tests import PRIVATE_KEY from leap.soledad import Soledad from leap.soledad import SoledadCrypto @@ -107,19 +97,23 @@ def initialize_soledad(email, gnupg_home, tempdir): server_url = "http://provider" cert_file = "" + class MockSharedDB(object): + + get_doc = Mock(return_value=None) + put_doc = Mock() + + def __call__(self): + return self + + Soledad._shared_db = MockSharedDB() + _soledad = Soledad( - uuid, # user's uuid, obtained through signal events - passphrase, # how to get this? - secret_path, # how to get this? - local_db_path, # how to get this? - server_url, # can be None for now - cert_file, - bootstrap=False) - _soledad._init_dirs() - _soledad._crypto = SoledadCrypto(_soledad) - _soledad._shared_db = None - _soledad._init_keys() - _soledad._init_db() + uuid, + passphrase, + secret_path, + local_db_path, + server_url, + cert_file) return _soledad @@ -253,9 +247,9 @@ class IMAP4HelperMixin(BaseLeapTest): #cls.db2_file = "%s/db2.u1db" % cls.tempdir # open test dbs #cls._db1 = u1db.open(cls.db1_file, create=True, - #document_factory=LeapDocument) + #document_factory=SoledadDocument) #cls._db2 = u1db.open(cls.db2_file, create=True, - #document_factory=LeapDocument) + #document_factory=SoledadDocument) # initialize soledad by hand so we can control keys cls._soledad = initialize_soledad( @@ -401,12 +395,21 @@ class MessageCollectionTestCase(IMAP4HelperMixin, unittest.TestCase): """ Test empty message and collection """ - em = self.messages.get_empty_msg() - self.assertEqual(em, - {"subject": "", "seen": False, - "flags": [], "mailbox": "inbox", - "mbox-uid": 1, - "raw": ""}) + em = self.messages._get_empty_msg() + self.assertEqual( + em, + { + "date": '', + "flags": [], + "headers": {}, + "mbox": "inbox", + "raw": "", + "recent": True, + "seen": False, + "subject": "", + "type": "msg", + "uid": 1, + }) self.assertEqual(self.messages.count(), 0) def testFilterByMailbox(self): @@ -419,13 +422,13 @@ class MessageCollectionTestCase(IMAP4HelperMixin, unittest.TestCase): mc.add_msg('', subject="test3") self.assertEqual(self.messages.count(), 3) - newmsg = mc.get_empty_msg() + newmsg = mc._get_empty_msg() newmsg['mailbox'] = "mailbox/foo" newmsg['subject'] = "test another mailbox" - mc.db.create_doc(newmsg) + mc._soledad.create_doc(newmsg) self.assertEqual(mc.count(), 3) - self.assertEqual(len(mc.db.get_from_index(mc.MAILBOX_INDEX, "*")), - 4) + self.assertEqual( + len(mc._soledad.get_from_index(mc.TYPE_IDX, "*")), 4) class LeapIMAP4ServerTestCase(IMAP4HelperMixin, unittest.TestCase): @@ -561,10 +564,13 @@ class LeapIMAP4ServerTestCase(IMAP4HelperMixin, unittest.TestCase): """ Try deleting a mailbox with sub-folders, and \NoSelect flag set. An exception is expected + + Obs: this test will fail if SoledadMailbox returns hardcoded flags. """ SimpleLEAPServer.theAccount.addMailbox('delete') to_delete = SimpleLEAPServer.theAccount.getMailbox('delete') to_delete.setFlags((r'\Noselect',)) + to_delete.getFlags() SimpleLEAPServer.theAccount.addMailbox('delete/me') def login(): @@ -1180,7 +1186,6 @@ class LeapIMAP4ServerTestCase(IMAP4HelperMixin, unittest.TestCase): """ name = 'mailbox-close' self.server.theAccount.addMailbox(name) - #import ipdb; ipdb.set_trace() m = SimpleLEAPServer.theAccount.getMailbox(name) m.messages.add_msg('', subject="Message 1", -- cgit v1.2.3