diff options
author | Duda Dornelles <ddornell@thoughtworks.com> | 2014-09-29 14:41:03 -0300 |
---|---|---|
committer | Duda Dornelles <ddornell@thoughtworks.com> | 2014-09-29 14:41:03 -0300 |
commit | a40f42b124e8255873b7de62d6b732632fc68408 (patch) | |
tree | f3c22c3d4fc6372dee1a100a4cb751d6abc32957 | |
parent | 34df7f4dde3f481ff9c50dbb2338507ab70b04dd (diff) |
Adding first integration test, w000t
-rw-r--r-- | service/integration/__init__.py | 64 | ||||
-rw-r--r-- | service/integration/mail_fetch_test.py | 56 |
2 files changed, 120 insertions, 0 deletions
diff --git a/service/integration/__init__.py b/service/integration/__init__.py new file mode 100644 index 00000000..910c25d7 --- /dev/null +++ b/service/integration/__init__.py @@ -0,0 +1,64 @@ +from leap.soledad.client import Soledad +import os +import unittest +from mock import Mock +import json + + +def initialize_soledad(tempdir): + uuid = "foobar-uuid" + passphrase = u"verysecretpassphrase" + secret_path = os.path.join(tempdir, "secret.gpg") + local_db_path = os.path.join(tempdir, "soledad.u1db") + server_url = "http://provider" + cert_file = "" + + class MockSharedDB(object): + + get_doc = Mock(return_value=None) + put_doc = Mock() + lock = Mock(return_value=('atoken', 300)) + unlock = Mock(return_value=True) + + def __call__(self): + return self + + Soledad._shared_db = MockSharedDB() + + _soledad = Soledad( + uuid, + passphrase, + secret_path, + 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) + + return _soledad + + +class JSONMailBuilder: + def __init__(self): + self.mail = { + 'header': { + 'to': ['recipient@to.com'], + 'cc': ['recipient@cc.com'], + 'bcc': ['recipient@bcc.com'], + 'subject': 'Hi! This the subject' + }, + 'body': "Hello,\nThis is the body of this message\n\nRegards,\n\n--\nPixelated.\n" + } + + def with_body(self, body): + self.mail['body'] = body + return self + + def with_subject(self, subject): + self.mail['header']['subject'] = subject + return self + + def build(self): + return json.dumps(self.mail)
\ No newline at end of file diff --git a/service/integration/mail_fetch_test.py b/service/integration/mail_fetch_test.py new file mode 100644 index 00000000..bd2823a3 --- /dev/null +++ b/service/integration/mail_fetch_test.py @@ -0,0 +1,56 @@ +import unittest + +from flask import json +from mockito import mock +from pixelated.adapter.mail_service import MailService +from pixelated.adapter.pixelated_mail import PixelatedMail +from pixelated.adapter.pixelated_mailboxes import PixelatedMailBoxes +from pixelated.adapter.soledad_querier import SoledadQuerier +import pixelated.user_agent +from integration import JSONMailBuilder, initialize_soledad + + +class FakeAccount(object): + def __init__(self): + self.mailboxes = ['INBOX', 'DRAFTS', 'SENT', 'TRASH'] + + +class MailFetchTest(unittest.TestCase): + + def setUp(self): + self.soledad_test_folder = "soledad-test" + self.app = pixelated.user_agent.app.test_client() + self.account = FakeAccount() + self.mail_sender = mock() + self.mail_address = "test@pixelated.org" + self.soledad = initialize_soledad(tempdir=self.soledad_test_folder) + + SoledadQuerier.get_instance(soledad=self.soledad) + PixelatedMail.from_email_address = self.mail_address + pixelated_mailboxes = PixelatedMailBoxes(self.account) + pixelated.user_agent.mail_service = MailService(pixelated_mailboxes, self.mail_sender) + pixelated.user_agent.DISABLED_FEATURES = [] + + def tearDown(self): + self.soledad.close() + import shutil + shutil.rmtree(self.soledad_test_folder) + + def get(self, url): + return json.loads(self.app.get(url).data) + + def post_mail(self, data): + self.app.post('/mails', data=data, content_type="application/json") + + def test_get_mails(self): + mail_one = JSONMailBuilder().with_subject("Mail One").build() + mail_two = JSONMailBuilder().with_subject("Mail Two").build() + + self.post_mail(mail_one) + self.post_mail(mail_two) + + response = self.get('/mails?q=tag:drafts') + + # ordered by creation date + self.assertEquals(u'Mail Two', response['mails'][0]['header']['subject']) + self.assertEquals(u'Mail One', response['mails'][1]['header']['subject']) |