diff options
Diffstat (limited to 'service/test/unit')
-rw-r--r-- | service/test/unit/adapter/search/__init__.py | 15 | ||||
-rw-r--r-- | service/test/unit/adapter/search/test_search.py | 89 | ||||
-rw-r--r-- | service/test/unit/adapter/test_draft_service.py | 2 | ||||
-rw-r--r-- | service/test/unit/adapter/test_email_recepient_normalizer.py | 42 | ||||
-rw-r--r-- | service/test/unit/adapter/test_mail.py | 105 | ||||
-rw-r--r-- | service/test/unit/adapter/test_mail_service.py | 7 | ||||
-rw-r--r-- | service/test/unit/adapter/test_mailbox.py | 9 | ||||
-rw-r--r-- | service/test/unit/adapter/test_mailbox_indexer_listener.py | 2 | ||||
-rw-r--r-- | service/test/unit/adapter/test_soledad_querier.py | 44 | ||||
-rw-r--r-- | service/test/unit/resources/test_sync_info_controller.py | 2 | ||||
-rw-r--r-- | service/test/unit/support/test_ext_keymanager_fetch_key.py | 76 |
11 files changed, 330 insertions, 63 deletions
diff --git a/service/test/unit/adapter/search/__init__.py b/service/test/unit/adapter/search/__init__.py new file mode 100644 index 00000000..2756a319 --- /dev/null +++ b/service/test/unit/adapter/search/__init__.py @@ -0,0 +1,15 @@ +# +# 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/>. diff --git a/service/test/unit/adapter/search/test_search.py b/service/test/unit/adapter/search/test_search.py new file mode 100644 index 00000000..d57b8227 --- /dev/null +++ b/service/test/unit/adapter/search/test_search.py @@ -0,0 +1,89 @@ +# +# 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 unittest +from mockito import mock, when +from pixelated.adapter.search import SearchEngine +from tempdir import TempDir +from test.support import test_helper + +INDEX_KEY = '\xde3?\x87\xff\xd9\xd3\x14\xf0\xa7>\x1f%C{\x16.\\\xae\x8c\x13\xa7\xfb\x04\xd4]+\x8d_\xed\xd1\x8d\x0bI' \ + '\x8a\x0e\xa4tm\xab\xbf\xb4\xa5\x99\x00d\xd5w\x9f\x18\xbc\x1d\xd4_W\xd2\xb6\xe8H\x83\x1b\xd8\x9d\xad' + + +class LockStub(object): + def __init__(self): + self.called = False + + def __enter__(self): + self.called = True + return self + + def __exit__(self, type, value, traceback): + return False + + +class SearchEngineTest(unittest.TestCase): + def setUp(self): + self.tempdir = TempDir() + self.agent_home = self.tempdir.name + + def tearDown(self): + self.tempdir.dissolve() + + def test_index_mail_secured_by_lock(self): + # given + soledad_querier = mock() + lock_stub = LockStub() + when(soledad_querier).get_index_masterkey().thenReturn(INDEX_KEY) + + self.assertEqual(INDEX_KEY, soledad_querier.get_index_masterkey()) + se = SearchEngine(soledad_querier, self.agent_home) + se._write_lock = lock_stub + + headers = { + 'From': 'from@bar.tld', + 'To': 'to@bar.tld', + 'Subject': 'Some test mail', + } + + # when + se.index_mail(test_helper.pixelated_mail(extra_headers=headers)) + + # then + self.assertTrue(lock_stub.called) + + def test_encoding(self): + # given + soledad_querier = mock() + when(soledad_querier).get_index_masterkey().thenReturn(INDEX_KEY) + + se = SearchEngine(soledad_querier, self.agent_home) + + headers = { + 'From': 'foo@bar.tld', + 'To': '=?utf-8?b?IsOEw7zDtiDDlsO8w6QiIDxmb2xrZXJAcGl4ZWxhdGVkLXByb2plY3Qub3Jn?=\n =?utf-8?b?PiwgRsO2bGtlciA8Zm9sa2VyQHBpeGVsYXRlZC1wcm9qZWN0Lm9yZz4=?=', + 'Cc': '=?utf-8?b?IsOEw7zDtiDDlsO8w6QiIDxmb2xrZXJAcGl4ZWxhdGVkLXByb2plY3Qub3Jn?=\n =?utf-8?b?PiwgRsO2bGtlciA8Zm9sa2VyQHBpeGVsYXRlZC1wcm9qZWN0Lm9yZz4=?=', + 'Subject': 'Some test mail', + } + + # when + se.index_mail(test_helper.pixelated_mail(extra_headers=headers, chash='mailid')) + + result = se.search('folker') + + self.assertEqual((['mailid'], 1), result) diff --git a/service/test/unit/adapter/test_draft_service.py b/service/test/unit/adapter/test_draft_service.py index baa07ce0..0dd6cd2a 100644 --- a/service/test/unit/adapter/test_draft_service.py +++ b/service/test/unit/adapter/test_draft_service.py @@ -3,7 +3,7 @@ import unittest from pixelated.adapter.model.mail import InputMail from pixelated.adapter.services.draft_service import DraftService import test.support.test_helper as test_helper -from mockito import * +from mockito import mock, verify, inorder, when class DraftServiceTest(unittest.TestCase): diff --git a/service/test/unit/adapter/test_email_recepient_normalizer.py b/service/test/unit/adapter/test_email_recepient_normalizer.py deleted file mode 100644 index 79d50273..00000000 --- a/service/test/unit/adapter/test_email_recepient_normalizer.py +++ /dev/null @@ -1,42 +0,0 @@ -# -# 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 unittest - -from pixelated.adapter.model.mail import PixelatedMail -from pixelated.adapter.services.mailbox import Mailbox -from pixelated.adapter.services.mail_sender import MailSender -from mockito import * -from test.support import test_helper - - -class PixelatedDuplicateEmailTest(unittest.TestCase): - def setUp(self): - self.mail_sender = MailSender(self, "random@gmail.com") - - def test_remove_duplicate_mail_recepients(self): - mail_list = ['simba@gmail.com', 'simba@gmail.com', 'fabio@gmail.com'] - normalized_recepients = self.mail_sender.recepients_normalizer(mail_list) - self.assertEquals(normalized_recepients, set(['simba@gmail.com', 'fabio@gmail.com'])) - - def test_get_email_addresses(self): - mail_list = ['simbarashe<simba@gmail.com>', 'vic@gmail.com', 'Fabio<fabio@gmail.com>', 'slick@gmail.com'] - selected_recepients = self.mail_sender.get_email_addresses(mail_list) - self.assertEquals(selected_recepients, set(['simba@gmail.com', 'vic@gmail.com', 'fabio@gmail.com', 'slick@gmail.com'])) - - def test_remove_duplicate_emails_with_routing_format(self): - mail_list = ['simbarashe<simba@gmail.com>', 'simba<simba@gmail.com>', 'Fabio<fabio@gmail.com>', 'Fabinho<fabio@gmail.com>'] - selected_recepients = self.mail_sender.get_email_addresses(mail_list) - self.assertEquals(selected_recepients, set(['simba@gmail.com', 'fabio@gmail.com'])) diff --git a/service/test/unit/adapter/test_mail.py b/service/test/unit/adapter/test_mail.py index 54c421c7..c7910b7f 100644 --- a/service/test/unit/adapter/test_mail.py +++ b/service/test/unit/adapter/test_mail.py @@ -17,7 +17,7 @@ import unittest import pixelated.support.date from pixelated.adapter.model.mail import PixelatedMail, InputMail -from mockito import * +from mockito import mock, unstub, when from test.support import test_helper import dateutil.parser as dateparser import base64 @@ -31,6 +31,9 @@ class TestPixelatedMail(unittest.TestCase): def setUp(self): self.querier = mock() + def tearDown(self): + unstub() + def test_parse_date_from_soledad_uses_date_header_if_available(self): leap_mail_date = 'Wed, 3 Sep 2014 12:36:17 -0300' leap_mail_date_in_iso_format = "2014-09-03T12:36:17-03:00" @@ -52,6 +55,17 @@ class TestPixelatedMail(unittest.TestCase): self.assertEqual(str(mail.headers['Date']), leap_mail_date_in_iso_format) + def test_parse_date_from_soledad_fallback_to_now_if_neither_date_nor_received_header(self): + leap_mail_date_in_iso_format = "2014-09-03T13:11:15-03:00" + + when(pixelated.support.date).iso_now().thenReturn(leap_mail_date_in_iso_format) + fdoc, hdoc, bdoc = test_helper.leap_mail() + del hdoc.content['date'] + + mail = PixelatedMail.from_soledad(fdoc, hdoc, bdoc, soledad_querier=self.querier) + + self.assertEqual(str(mail.headers['Date']), leap_mail_date_in_iso_format) + def test_update_tags_return_a_set_with_the_current_tags(self): soledad_docs = test_helper.leap_mail(extra_headers={'X-tags': '["custom_1", "custom_2"]'}) pixelated_mail = PixelatedMail.from_soledad(*soledad_docs, soledad_querier=self.querier) @@ -174,9 +188,20 @@ class TestPixelatedMail(unittest.TestCase): self.assertRegexpMatches(mail.html_body, '([\s\S]*100%)') def test_content_type_header_of_mail_part_is_used(self): - plain_headers = {'Content-Type': 'text/plain; charset=utf-8', 'Content-Transfer-Encoding': 'quoted-printable'} + plain_headers = {'Content-Type': 'text/plain; charset=iso-8859-1', 'Content-Transfer-Encoding': 'quoted-printable'} html_headers = {'Content-Type': 'text/html; charset=utf-8', 'Content-Transfer-Encoding': 'quoted-printable'} - parts = {'alternatives': [{'content': 'H=C3=A4llo', 'headers': plain_headers}, {'content': '<p>H=C3=A4llo</p>', 'headers': html_headers}]} + parts = {'alternatives': [{'content': 'H=E4llo', 'headers': plain_headers}, {'content': '<p>H=C3=A4llo</p>', 'headers': html_headers}]} + + mail = PixelatedMail.from_soledad(None, None, self._create_bdoc(raw='some raw body'), parts=parts, soledad_querier=None) + + self.assertEqual(2, len(mail.alternatives)) + self.assertEquals(u'H\xe4llo', mail.text_plain_body) + self.assertEquals(u'<p>H\xe4llo</p>', mail.html_body) + + def test_multi_line_content_type_header_is_supported(self): + plain_headers = {'Content-Type': 'text/plain;\ncharset=iso-8859-1', 'Content-Transfer-Encoding': 'quoted-printable'} + html_headers = {'Content-Type': 'text/html;\ncharset=utf-8', 'Content-Transfer-Encoding': 'quoted-printable'} + parts = {'alternatives': [{'content': 'H=E4llo', 'headers': plain_headers}, {'content': '<p>H=C3=A4llo</p>', 'headers': html_headers}]} mail = PixelatedMail.from_soledad(None, None, self._create_bdoc(raw='some raw body'), parts=parts, soledad_querier=None) @@ -211,6 +236,28 @@ class TestPixelatedMail(unittest.TestCase): self.assertEquals(body, mail.text_plain_body) + def test_that_body_understands_7bit(self): + body = u'testtext' + encoded_body = body + + fdoc, hdoc, bdoc = test_helper.leap_mail() + parts = {'alternatives': []} + parts['alternatives'].append({'content': encoded_body, 'headers': {'Content-Transfer-Encoding': '7bit'}}) + mail = PixelatedMail.from_soledad(fdoc, hdoc, bdoc, soledad_querier=self.querier, parts=parts) + + self.assertEquals(body, mail.text_plain_body) + + def test_that_body_understands_8bit(self): + body = u'testtext' + encoded_body = body + + fdoc, hdoc, bdoc = test_helper.leap_mail() + parts = {'alternatives': []} + parts['alternatives'].append({'content': encoded_body, 'headers': {'Content-Transfer-Encoding': '8bit'}}) + mail = PixelatedMail.from_soledad(fdoc, hdoc, bdoc, soledad_querier=self.querier, parts=parts) + + self.assertEquals(body, mail.text_plain_body) + def test_bounced_mails_are_recognized(self): bounced_mail_hdoc = os.path.join(os.path.dirname(__file__), '..', 'fixtures', 'bounced_mail_hdoc.json') with open(bounced_mail_hdoc) as f: @@ -256,9 +303,40 @@ class TestPixelatedMail(unittest.TestCase): self.content = {'raw': raw} return FakeBDoc(raw) + def test_encoding_special_character_on_header(self): + subject = "=?UTF-8?Q?test_encoding_St=C3=A4ch?=" + email_from = "=?UTF-8?Q?St=C3=A4ch_<stach@pixelated-project.org>?=" + email_to = "=?utf-8?b?IsOEw7zDtiDDlsO8w6QiIDxmb2xrZXJAcGl4ZWxhdGVkLXByb2plY3Qub3Jn?=\n =?utf-8?b?PiwgRsO2bGtlciA8Zm9sa2VyQHBpeGVsYXRlZC1wcm9qZWN0Lm9yZz4=?=" -class InputMailTest(unittest.TestCase): - mail_dict = lambda x: { + pixel_mail = PixelatedMail() + + self.assertEqual(pixel_mail._decode_header(subject), 'test encoding St\xc3\xa4ch') + self.assertEqual(pixel_mail._decode_header(email_from), 'St\xc3\xa4ch <stach@pixelated-project.org>') + self.assertEqual(pixel_mail._decode_header(email_to), '"\xc3\x84\xc3\xbc\xc3\xb6 \xc3\x96\xc3\xbc\xc3\xa4" <folker@pixelated-project.org>, F\xc3\xb6lker <folker@pixelated-project.org>') + self.assertEqual(pixel_mail._decode_header(None), None) + + def test_headers_are_encoded_right(self): + subject = "=?UTF-8?Q?test_encoding_St=C3=A4ch?=" + email_from = "=?UTF-8?Q?St=C3=A4ch_<stach@pixelated-project.org>?=" + email_to = "=?utf-8?b?IsOEw7zDtiDDlsO8w6QiIDxmb2xrZXJAcGl4ZWxhdGVkLXByb2plY3Qub3Jn?=\n =?utf-8?b?PiwgRsO2bGtlciA8Zm9sa2VyQHBpeGVsYXRlZC1wcm9qZWN0Lm9yZz4=?=" + email_cc = "=?UTF-8?Q?St=C3=A4ch_<stach@pixelated-project.org>?=" + email_bcc = "=?UTF-8?Q?St=C3=A4ch_<stach@pixelated-project.org>?=" + + leap_mail = test_helper.leap_mail(extra_headers={'Subject': subject, 'From': email_from, 'To': email_to, 'Cc': email_cc, 'Bcc': email_bcc}) + + mail = PixelatedMail.from_soledad(*leap_mail, soledad_querier=self.querier) + + self.assertEqual(str(mail.headers['Subject']), 'test encoding St\xc3\xa4ch') + self.assertEqual(str(mail.headers['From']), 'St\xc3\xa4ch <stach@pixelated-project.org>') + self.assertEqual(mail.headers['To'], ['"\xc3\x84\xc3\xbc\xc3\xb6 \xc3\x96\xc3\xbc\xc3\xa4" <folker@pixelated-project.org>', 'F\xc3\xb6lker <folker@pixelated-project.org>']) + self.assertEqual(mail.headers['Cc'], ['St\xc3\xa4ch <stach@pixelated-project.org>']) + self.assertEqual(mail.headers['Bcc'], ['St\xc3\xa4ch <stach@pixelated-project.org>']) + + mail.as_dict() + + +def simple_mail_dict(): + return { 'body': 'Este \xe9 o corpo', 'header': { 'cc': ['cc@pixelated.org', 'anothercc@pixelated.org'], @@ -270,7 +348,9 @@ class InputMailTest(unittest.TestCase): 'tags': ['sent'] } - multipart_mail_dict = lambda x: { + +def multipart_mail_dict(): + return { 'body': [{'content-type': 'plain', 'raw': 'Hello world!'}, {'content-type': 'html', 'raw': '<p>Hello html world!</p>'}], 'header': { @@ -283,10 +363,13 @@ class InputMailTest(unittest.TestCase): 'tags': ['sent'] } + +class InputMailTest(unittest.TestCase): + def test_to_mime_multipart_should_add_blank_fields(self): pixelated.support.date.iso_now = lambda: 'date now' - mail_dict = self.mail_dict() + mail_dict = simple_mail_dict() mail_dict['header']['to'] = '' mail_dict['header']['bcc'] = '' mail_dict['header']['cc'] = '' @@ -302,24 +385,24 @@ class InputMailTest(unittest.TestCase): def test_to_mime_multipart(self): pixelated.support.date.iso_now = lambda: 'date now' - mime_multipart = InputMail.from_dict(self.mail_dict()).to_mime_multipart() + mime_multipart = InputMail.from_dict(simple_mail_dict()).to_mime_multipart() self.assertRegexpMatches(mime_multipart.as_string(), "\nTo: to@pixelated.org, anotherto@pixelated.org\n") self.assertRegexpMatches(mime_multipart.as_string(), "\nCc: cc@pixelated.org, anothercc@pixelated.org\n") self.assertRegexpMatches(mime_multipart.as_string(), "\nBcc: bcc@pixelated.org, anotherbcc@pixelated.org\n") self.assertRegexpMatches(mime_multipart.as_string(), "\nDate: date now\n") self.assertRegexpMatches(mime_multipart.as_string(), "\nSubject: Oi\n") - self.assertRegexpMatches(mime_multipart.as_string(), base64.b64encode(self.mail_dict()['body'])) + self.assertRegexpMatches(mime_multipart.as_string(), base64.b64encode(simple_mail_dict()['body'])) def test_smtp_format(self): InputMail.FROM_EMAIL_ADDRESS = 'pixelated@org' - smtp_format = InputMail.from_dict(self.mail_dict()).to_smtp_format() + smtp_format = InputMail.from_dict(simple_mail_dict()).to_smtp_format() self.assertRegexpMatches(smtp_format, "\nFrom: pixelated@org") def test_to_mime_multipart_handles_alternative_bodies(self): - mime_multipart = InputMail.from_dict(self.multipart_mail_dict()).to_mime_multipart() + mime_multipart = InputMail.from_dict(multipart_mail_dict()).to_mime_multipart() part_one = 'Content-Type: text/plain; charset="us-ascii"\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7bit\n\nHello world!' part_two = 'Content-Type: text/html; charset="us-ascii"\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7bit\n\n<p>Hello html world!</p>' diff --git a/service/test/unit/adapter/test_mail_service.py b/service/test/unit/adapter/test_mail_service.py index 98ead126..34fec708 100644 --- a/service/test/unit/adapter/test_mail_service.py +++ b/service/test/unit/adapter/test_mail_service.py @@ -18,25 +18,22 @@ from pixelated.adapter.model.mail import InputMail, PixelatedMail from pixelated.adapter.services.mail_service import MailService from test.support.test_helper import mail_dict, leap_mail -from mockito import * +from mockito import mock, unstub, when, verify, verifyNoMoreInteractions, any from twisted.internet.defer import Deferred -from twisted.internet import defer - class TestMailService(unittest.TestCase): def setUp(self): self.drafts = mock() self.querier = mock() self.mailboxes = mock() - self.tag_service = mock() self.mailboxes.drafts = lambda: self.drafts self.mailboxes.trash = lambda: mock() self.mailboxes.sent = lambda: mock() self.mail_sender = mock() self.search_engine = mock() - self.mail_service = MailService(self.mailboxes, self.mail_sender, self.tag_service, self.querier, self.search_engine) + self.mail_service = MailService(self.mailboxes, self.mail_sender, self.querier, self.search_engine) def tearDown(self): unstub() diff --git a/service/test/unit/adapter/test_mailbox.py b/service/test/unit/adapter/test_mailbox.py index b44f507b..ed634648 100644 --- a/service/test/unit/adapter/test_mailbox.py +++ b/service/test/unit/adapter/test_mailbox.py @@ -17,13 +17,12 @@ import unittest from pixelated.adapter.model.mail import PixelatedMail from pixelated.adapter.services.mailbox import Mailbox -from mockito import * +from mockito import mock, when, verify from test.support import test_helper class PixelatedMailboxTest(unittest.TestCase): def setUp(self): - self.tag_service = mock() self.querier = mock() self.search_engine = mock() self.mailbox = Mailbox('INBOX', self.querier, self.search_engine) @@ -35,3 +34,9 @@ class PixelatedMailboxTest(unittest.TestCase): self.mailbox.remove(1) verify(self.querier).remove_mail(mail) + + def test_fresh_mailbox_checking_lastuid(self): + when(self.querier).get_lastuid('INBOX').thenReturn(0) + self.assertTrue(self.mailbox.fresh) + when(self.querier).get_lastuid('INBOX').thenReturn(1) + self.assertFalse(self.mailbox.fresh) diff --git a/service/test/unit/adapter/test_mailbox_indexer_listener.py b/service/test/unit/adapter/test_mailbox_indexer_listener.py index 65ba8966..71c9cd15 100644 --- a/service/test/unit/adapter/test_mailbox_indexer_listener.py +++ b/service/test/unit/adapter/test_mailbox_indexer_listener.py @@ -15,7 +15,7 @@ # along with Pixelated. If not, see <http://www.gnu.org/licenses/>. import unittest -from mockito import * +from mockito import mock, when, verify from pixelated.adapter.listeners.mailbox_indexer_listener import MailboxIndexerListener diff --git a/service/test/unit/adapter/test_soledad_querier.py b/service/test/unit/adapter/test_soledad_querier.py index 2cc23750..e5ea457d 100644 --- a/service/test/unit/adapter/test_soledad_querier.py +++ b/service/test/unit/adapter/test_soledad_querier.py @@ -104,3 +104,47 @@ class SoledadQuerierTest(unittest.TestCase): attachment = querier.attachment(u'0400BEBACAFE', 'quoted-printable') self.assertEquals('esse papo seu ta qualquer coisa', attachment['content']) + + def test_empty_or_null_queries_are_ignored(self): + soledad = mock() + when(soledad).get_from_index(any(), any(), any()).thenReturn(['nonempty', 'list']) + querier = SoledadQuerier(soledad) + + test_parameters = ['', None] + + def call_with_bad_parameters(funct): + for param in test_parameters: + self.assertFalse(funct(param)) + + call_with_bad_parameters(querier.get_all_flags_by_mbox) + call_with_bad_parameters(querier.get_content_by_phash) + call_with_bad_parameters(querier.get_flags_by_chash) + call_with_bad_parameters(querier.get_header_by_chash) + call_with_bad_parameters(querier.get_recent_by_mbox) + call_with_bad_parameters(querier.idents_by_mailbox) + call_with_bad_parameters(querier.get_mbox) + + def test_get_lastuid(self): + soledad = mock() + mbox = mock() + mbox.content = {'lastuid': 0} + when(soledad).get_from_index('by-type-and-mbox', 'mbox', 'INBOX').thenReturn([mbox]) + querier = SoledadQuerier(soledad) + + self.assertEquals(querier.get_lastuid(querier.get_mbox('INBOX')[0]), 0) + mbox.content = {'lastuid': 1} + self.assertEquals(querier.get_lastuid(querier.get_mbox('INBOX')[0]), 1) + + def test_create_mail_increments_uid(self): + soledad = mock() + mbox = mock() + mail = mock() + when(mail).get_for_save(next_uid=any(), mailbox='INBOX').thenReturn([]) + mbox.content = {'lastuid': 0} + when(soledad).get_from_index('by-type-and-mbox', 'mbox', 'INBOX').thenReturn([mbox]) + querier = SoledadQuerier(soledad) + when(querier).mail(any()).thenReturn([]) + + self.assertEquals(querier.get_lastuid(querier.get_mbox('INBOX')[0]), 0) + querier.create_mail(mail, 'INBOX') + self.assertEquals(querier.get_lastuid(querier.get_mbox('INBOX')[0]), 1) diff --git a/service/test/unit/resources/test_sync_info_controller.py b/service/test/unit/resources/test_sync_info_controller.py index a91dd386..1285237b 100644 --- a/service/test/unit/resources/test_sync_info_controller.py +++ b/service/test/unit/resources/test_sync_info_controller.py @@ -18,7 +18,7 @@ import json from test.support.test_helper import request_mock from pixelated.resources.sync_info_resource import SyncInfoResource -from mockito import * +from mockito import mock class SyncInfoResourceTest(unittest.TestCase): diff --git a/service/test/unit/support/test_ext_keymanager_fetch_key.py b/service/test/unit/support/test_ext_keymanager_fetch_key.py new file mode 100644 index 00000000..8998198d --- /dev/null +++ b/service/test/unit/support/test_ext_keymanager_fetch_key.py @@ -0,0 +1,76 @@ +# +# 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 unittest +from mock import MagicMock, patch + +from leap.keymanager import KeyManager +from leap.keymanager.keys import KEY_ADDRESS_KEY, KEY_TYPE_KEY, KEY_ID_KEY, KEY_FINGERPRINT_KEY, KEY_DATA_KEY, KEY_PRIVATE_KEY, KEY_LENGTH_KEY, KEY_EXPIRY_DATE_KEY, KEY_FIRST_SEEN_AT_KEY, KEY_LAST_AUDITED_AT_KEY, KEY_VALIDATION_KEY, KEY_TAGS_KEY +from leap.keymanager.openpgp import OpenPGPKey +from leap.keymanager.errors import KeyNotFound +import pixelated.support.ext_keymanager_fetch_key +from requests.exceptions import HTTPError + + +class TestDoc(object): + def __init__(self, encryption_key): + self.content = encryption_key + +sample_key = { + KEY_ADDRESS_KEY: 'foo@bar.de', + KEY_TYPE_KEY: 'type', + KEY_ID_KEY: 'key_id', + KEY_FINGERPRINT_KEY: 'fingerprint', + KEY_DATA_KEY: 'key_data', + KEY_PRIVATE_KEY: None, + KEY_LENGTH_KEY: 'length', + KEY_EXPIRY_DATE_KEY: 'expiry_date', + KEY_FIRST_SEEN_AT_KEY: 'first_seen_at', + KEY_LAST_AUDITED_AT_KEY: 'last_audited_at', + KEY_VALIDATION_KEY: 'validation', + KEY_TAGS_KEY: 'tags', +} + + +class TestExtKeyManagerFetchKey(unittest.TestCase): + + @patch('leap.keymanager.requests') + def test_retrieves_key(self, requests_mock): + nickserver_url = 'http://some/nickserver/uri' + soledad = MagicMock() + soledad.get_from_index.side_effect = [[], [TestDoc(sample_key)]] + + km = KeyManager('me@bar.de', nickserver_url, soledad, ca_cert_path='some path') + + result = km.get_key('foo@bar.de', OpenPGPKey) + + self.assertEqual(str(OpenPGPKey('foo@bar.de', key_id='key_id')), str(result)) + + @patch('leap.keymanager.requests') + def test_http_error_500(self, requests_mock): + def do_request(one, data=None, verify=None): + response = MagicMock() + response.raise_for_status = MagicMock() + response.raise_for_status.side_effect = HTTPError + return response + + nickserver_url = 'http://some/nickserver/uri' + soledad = MagicMock() + soledad.get_from_index.side_effect = [[], []] + requests_mock.get.side_effect = do_request + + km = KeyManager('me@bar.de', nickserver_url, soledad, ca_cert_path='some path') + + self.assertRaises(KeyNotFound, km.get_key, 'foo@bar.de', OpenPGPKey) |