diff options
author | Duda Dornelles <ddornell@thoughtworks.com> | 2014-09-26 13:11:00 -0300 |
---|---|---|
committer | Duda Dornelles <ddornell@thoughtworks.com> | 2014-09-26 13:11:00 -0300 |
commit | 1082f233962953c0927b73e7acac2665d420857b (patch) | |
tree | e6bd8fef98930e5f1bb18041643da261a606fdc4 | |
parent | b4333d2d0955b3f5774bbd1ea97cfdabd5578fd1 (diff) |
Fixing tests
-rw-r--r-- | service/pixelated/adapter/pixelated_mail.py | 94 | ||||
-rw-r--r-- | service/pixelated/adapter/pixelated_mailbox.py | 26 | ||||
-rw-r--r-- | service/pixelated/adapter/pixelated_mailboxes.py | 19 | ||||
-rw-r--r-- | service/pixelated/adapter/soledad_querier.py | 31 | ||||
-rw-r--r-- | service/test/adapter/mail_service_test.py | 4 | ||||
-rw-r--r-- | service/test/adapter/pixelated_mail_sender_test.py | 11 | ||||
-rw-r--r-- | service/test/adapter/pixelated_mail_test.py | 158 | ||||
-rw-r--r-- | service/test/adapter/pixelated_mailbox_test.py | 53 | ||||
-rw-r--r-- | service/test/adapter/pixelated_mailboxes_test.py | 31 | ||||
-rw-r--r-- | service/test/adapter/test_helper.py | 37 | ||||
-rw-r--r-- | service/test/adapter/test_tag_service.py | 6 | ||||
-rw-r--r-- | service/test/user_agent_test.py | 35 |
12 files changed, 236 insertions, 269 deletions
diff --git a/service/pixelated/adapter/pixelated_mail.py b/service/pixelated/adapter/pixelated_mail.py index 9014cb4d..b603ba70 100644 --- a/service/pixelated/adapter/pixelated_mail.py +++ b/service/pixelated/adapter/pixelated_mail.py @@ -49,13 +49,8 @@ class InputMail: @staticmethod def from_dict(mail_dict): - input_mail = InputMail() - input_mail.headers = {key.capitalize(): value for key, value in mail_dict.get('header', {}).items()} - input_mail.headers['Date'] = pixelated.support.date.iso_now() - input_mail.body = mail_dict.get('body', '') - input_mail.tags = set(mail_dict.get('tags', [])) - input_mail.status = set(mail_dict.get('status', [])) - return input_mail + return input_mail_from_dict(mail_dict) + @property def _mime_multipart(self): @@ -73,13 +68,14 @@ class InputMail: return self._get_chash() def _raw(self): - if self._raw_message: - return self._raw_message - self._raw_message = self._mime_multipart.as_string() + if not self._raw_message: + self._raw_message = self._mime_multipart.as_string() return self._raw_message def _get_chash(self): - return sha256.SHA256(self._raw()).hexdigest() + if not self._chash: + self._chash = sha256.SHA256(self._raw()).hexdigest() + return self._chash def _get_for_save(self, next_uid): docs = [self._fdoc(next_uid), self._hdoc()] @@ -133,7 +129,7 @@ class InputMail: if self.headers[header]: mime_multipart[header] = ", ".join(self.headers[header]) - if self.headers['subject']: + if self.headers['Subject']: mime_multipart['Subject'] = self.headers['Subject'] mime_multipart['Date'] = self.headers['Date'] @@ -152,7 +148,7 @@ class PixelatedMail: self.tag_service = tag_service @staticmethod - def from_soledad(fdoc, hdoc, bdoc, soledad_querier): + def from_soledad(fdoc, hdoc, bdoc, soledad_querier=None): mail = PixelatedMail() mail.bdoc = bdoc mail.fdoc = fdoc @@ -166,10 +162,18 @@ class PixelatedMail: @property def headers(self): - _headers = ['From', 'Date', 'To', 'Subject', 'Cc', 'Bcc'] + _headers = ['From', 'To', 'Subject', 'Cc', 'Bcc'] _headers = {header: self.hdoc.content['headers'].get(header) for header in _headers} + _headers['Date'] = self._get_date() return _headers + def _get_date(self): + date = self.hdoc.content.get('date', None) + if not date: + + date = self.hdoc.content['received'].split(";")[-1].strip() + return dateparser.parse(date).isoformat() + @property def status(self): return Status.from_flags(self.fdoc.content.get('flags')) @@ -180,8 +184,8 @@ class PixelatedMail: @property def tags(self): - _tags = self.headers.get('x-tags', '[]') - return set(_tags) if type(_tags) is list else set(json.loads(_tags)) + _tags = self.hdoc.content.get('X-Tags', '[]') + return set(_tags) if type(_tags) is list or type(_tags) is set else set(json.loads(_tags)) @property def ident(self): @@ -203,45 +207,37 @@ class PixelatedMail: return self.querier.save_mail(self) def set_from(self, _from): - self.headers['from'] = [_from] + self.headers['From'] = [_from] def get_to(self): - return self.headers['to'] + return self.headers['To'] def get_cc(self): - return self.headers['cc'] + return self.headers['Cc'] def get_bcc(self): - return self.headers['bcc'] - - def mark_as_deleted(self): - # self.remove_all_tags() - # self.leap_mail.setFlags((Status.PixelatedStatus.DELETED,), 1) - pass + return self.headers['Bcc'] def remove_all_tags(self): self.update_tags(set([])) def update_tags(self, tags): old_tags = self.tags - self.tags = tags + self._persist_mail_tags(tags) removed = old_tags.difference(tags) added = tags.difference(old_tags) - self._persist_mail_tags(tags) self.tag_service.notify_tags_updated(added, removed, self.ident) return self.tags def mark_as_read(self): - # self.leap_mail.setFlags((Status.PixelatedStatus.SEEN,), 1) - # self.status = self._extract_status() - # return self - pass + self.fdoc.content['flags'].append(Status.PixelatedStatus.SEEN) + #self.status = self._extract_status() + return self def mark_as_not_recent(self): - # self.leap_mail.setFlags((Status.PixelatedStatus.RECENT,), -1) - # self.status = self._extract_status() - # return self - pass + self.fdoc.content['flags'].remove(Status.PixelatedStatus.RECENT) + #self.status = self._extract_status() + return self def _persist_mail_tags(self, current_tags): self.hdoc.content['headers']['X-Tags'] = json.dumps(list(current_tags)) @@ -261,24 +257,12 @@ class PixelatedMail: 'body': self.body } - @staticmethod - def from_dict(mail_dict): - return from_dict(mail_dict) - - @classmethod - def _get_date(cls, headers): - date = headers.get('date', None) - if not date: - date = headers['received'].split(";")[-1].strip() - return dateparser.parse(date).isoformat() - -def from_dict(mail_dict): - mail = PixelatedMail() - mail.headers = mail_dict.get('header', {}) - mail.headers['date'] = pixelated.support.date.iso_now() - mail.body = mail_dict.get('body', '') - mail._ident = mail_dict.get('ident', None) - mail.tags = set(mail_dict.get('tags', [])) - mail.status = set(mail_dict.get('status', [])) - return mail +def input_mail_from_dict(mail_dict): + input_mail = InputMail() + input_mail.headers = {key.capitalize(): value for key, value in mail_dict.get('header', {}).items()} + input_mail.headers['Date'] = pixelated.support.date.iso_now() + input_mail.body = mail_dict.get('body', '') + input_mail.tags = set(mail_dict.get('tags', [])) + input_mail.status = set(mail_dict.get('status', [])) + return input_mail diff --git a/service/pixelated/adapter/pixelated_mailbox.py b/service/pixelated/adapter/pixelated_mailbox.py index aa00a24f..85d965d1 100644 --- a/service/pixelated/adapter/pixelated_mailbox.py +++ b/service/pixelated/adapter/pixelated_mailbox.py @@ -16,24 +16,15 @@ from pixelated.adapter.tag_service import TagService from pixelated.adapter.soledad_querier import SoledadQuerier -from crochet import wait_for class PixelatedMailbox: - def __init__(self, leap_mailbox, tag_service=TagService.get_instance()): + def __init__(self, mailbox_name, querier, tag_service=TagService.get_instance()): self.tag_service = tag_service - self.leap_mailbox = leap_mailbox - self.mailbox_tag = self.leap_mailbox.mbox.lower() - self.querier = SoledadQuerier.get_instance() - - @property - def messages(self): - return self.leap_mailbox.messages - - @property - def mailbox_name(self): - return self.leap_mailbox.mbox + self.mailbox_name = mailbox_name + self.mailbox_tag = mailbox_name.lower() + self.querier = querier def add_mailbox_tag_if_not_there(self, pixelated_mail): if not pixelated_mail.has_tag(self.mailbox_tag): @@ -42,7 +33,7 @@ class PixelatedMailbox: pixelated_mail.mark_as_not_recent() def mails(self): - _mails = self.querier.all_mails_by_mailbox(self.leap_mailbox.mbox) + _mails = self.querier.all_mails_by_mailbox(self.mailbox_name) result = [] for mail in _mails: @@ -60,14 +51,13 @@ class PixelatedMailbox: if message.ident == mail_id: return message - def add(self, mail, use_smtp_format=False): + def add(self, mail): self.querier.create_mail(mail, self.mailbox_name) def remove(self, ident): mail = self.querier.mail(ident) self.querier.remove_mail(mail) - self.leap_mailbox.expunge() @classmethod - def create(cls, account, mailbox_name='INBOX'): - return PixelatedMailbox(account.getMailbox(mailbox_name)) + def create(cls, mailbox_name='INBOX'): + return PixelatedMailbox(mailbox_name, SoledadQuerier.get_instance()) diff --git a/service/pixelated/adapter/pixelated_mailboxes.py b/service/pixelated/adapter/pixelated_mailboxes.py index 890e86e1..f3efe544 100644 --- a/service/pixelated/adapter/pixelated_mailboxes.py +++ b/service/pixelated/adapter/pixelated_mailboxes.py @@ -1,3 +1,18 @@ +# +# 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/>. from pixelated.adapter.pixelated_mailbox import PixelatedMailbox @@ -10,7 +25,7 @@ class PixelatedMailBoxes(): mailbox_name = mailbox_name.upper() if mailbox_name not in self.account.mailboxes: self.account.addMailbox(mailbox_name) - return PixelatedMailbox.create(self.account, mailbox_name) + return PixelatedMailbox.create(mailbox_name) def drafts(self): return self._create_or_get('DRAFTS') @@ -20,7 +35,7 @@ class PixelatedMailBoxes(): @property def mailboxes(self): - return [PixelatedMailbox.create(self.account, leap_mailbox_name) for leap_mailbox_name in + return [PixelatedMailbox.create(leap_mailbox_name) for leap_mailbox_name in self.account.mailboxes] def mails_by_tag(self, query_tags): diff --git a/service/pixelated/adapter/soledad_querier.py b/service/pixelated/adapter/soledad_querier.py index 179d10f1..7f617767 100644 --- a/service/pixelated/adapter/soledad_querier.py +++ b/service/pixelated/adapter/soledad_querier.py @@ -1,6 +1,29 @@ +# +# 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/>. from pixelated.adapter.pixelated_mail import PixelatedMail +def get_soledad_querier_instance(cls, soledad=None): + if not cls.instance: + if not soledad: + raise Exception("Need a soledad for the first time you call this") + cls.instance = SoledadQuerier(soledad) + return cls.instance + + class SoledadQuerier: instance = None @@ -10,11 +33,7 @@ class SoledadQuerier: @classmethod def get_instance(cls, soledad=None): - if not cls.instance: - if not soledad: - raise Exception("Need a soledad for the first time you call this") - cls.instance = SoledadQuerier(soledad) - return cls.instance + return get_soledad_querier_instance(cls, soledad) def all_mails(self): fdocs_chash = [(fdoc, fdoc.content['chash']) for fdoc in self.soledad.get_from_index('by-type', 'flags')] @@ -48,10 +67,12 @@ class SoledadQuerier: fdoc = self.soledad.get_from_index('by-type-and-contenthash', 'flags', ident)[0] hdoc = self.soledad.get_from_index('by-type-and-contenthash', 'head', ident)[0] bdoc = self.soledad.get_from_index('by-type-and-payloadhash', 'cnt', hdoc.content['body'])[0] + return PixelatedMail.from_soledad(fdoc, hdoc, bdoc, soledad_querier=self) def remove_mail(self, mail): _mail = self.mail(mail.ident) + # FIX-ME: Must go through all the part_map phash to delete all the cdocs self.soledad.delete_doc(_mail.bdoc) self.soledad.delete_doc(_mail.hdoc) self.soledad.delete_doc(_mail.fdoc) diff --git a/service/test/adapter/mail_service_test.py b/service/test/adapter/mail_service_test.py index 517368e2..38542cac 100644 --- a/service/test/adapter/mail_service_test.py +++ b/service/test/adapter/mail_service_test.py @@ -17,10 +17,14 @@ import unittest from pixelated.adapter.mail_service import MailService from mockito import * +import pixelated.adapter.soledad_querier class TestMailService(unittest.TestCase): def setUp(self): + self.querier = mock() + pixelated.adapter.soledad_querier.get_soledad_querier_instance = lambda x, y: self.querier + self.mailboxes = mock() self.mail_sender = mock() self.mail_service = MailService(self.mailboxes, self.mail_sender) diff --git a/service/test/adapter/pixelated_mail_sender_test.py b/service/test/adapter/pixelated_mail_sender_test.py index b41f1d0c..d75aa57c 100644 --- a/service/test/adapter/pixelated_mail_sender_test.py +++ b/service/test/adapter/pixelated_mail_sender_test.py @@ -27,12 +27,13 @@ class PixelatedMailSenderTest(unittest.TestCase): self.mail_sender = PixelatedMailSender(self.mail_address, self.smtp_client) def test_send_mail_sends_to_To_Cc_and_Bcc(self): - mail_dict = test_helper.mail_dict() - mail_dict['header']['to'] = ['to@pixelated.org', 'anotherto@pixelated.org'] - mail_dict['header']['cc'] = ['cc@pixelated.org', 'anothercc@pixelated.org'] - mail_dict['header']['bcc'] = ['bcc@pixelated.org', 'anotherbcc@pixelated.org'] + headers = { + 'To': ['to@pixelated.org', 'anotherto@pixelated.org'], + 'Cc': ['cc@pixelated.org', 'anothercc@pixelated.org'], + 'Bcc': ['bcc@pixelated.org', 'anotherbcc@pixelated.org'] + } - mail = PixelatedMail.from_dict(mail_dict) + mail = PixelatedMail.from_soledad(*test_helper.leap_mail(extra_headers=headers)) mail.to_smtp_format = lambda: "mail as smtp string" self.mail_sender.sendmail(mail) diff --git a/service/test/adapter/pixelated_mail_test.py b/service/test/adapter/pixelated_mail_test.py index 0a103cce..c8c23b8a 100644 --- a/service/test/adapter/pixelated_mail_test.py +++ b/service/test/adapter/pixelated_mail_test.py @@ -18,81 +18,83 @@ import os import pixelated.support.date import test_helper -from pixelated.adapter.pixelated_mail import PixelatedMail +from pixelated.adapter.pixelated_mail import PixelatedMail, InputMail from pixelated.adapter.tag_service import TagService from pixelated.adapter.tag_index import TagIndex from pixelated.adapter.tag import Tag -from mock import Mock - +from mockito import * class TestPixelatedMail(unittest.TestCase): - mail_dict = lambda x: { - 'body': 'Este \xe9 o corpo', - 'header': { - 'cc': ['cc@pixelated.org', 'anothercc@pixelated.org'], - 'to': ['to@pixelated.org', 'anotherto@pixelated.org'], - 'bcc': ['bcc@pixelated.org', 'anotherbcc@pixelated.org'], - 'subject': 'Oi' - }, - 'ident': '', - 'tags': ['sent'] - } - def test_parse_date_from_leap_mail_uses_date_header_if_available(self): + def setUp(self): + self.querier = mock() + + 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" leap_mail = test_helper.leap_mail(headers={'date': leap_mail_date}) - mail = PixelatedMail.from_leap_mail(leap_mail) + mail = PixelatedMail.from_soledad(*leap_mail, soledad_querier=self.querier) - self.assertEqual(str(mail.headers['date']), leap_mail_date_in_iso_format) + self.assertEqual(str(mail.headers['Date']), leap_mail_date_in_iso_format) - def test_parse_date_from_leap_mail_fallback_to_received_header_if_date_header_isnt_available(self): + def test_parse_date_from_soledad_fallback_to_received_header_if_date_header_isnt_available(self): leap_mail_date = "Wed, 03 Sep 2014 13:11:15 -0300" leap_mail_date_in_iso_format = "2014-09-03T13:11:15-03:00" leap_mail_received_header = "by bitmask.local from 127.0.0.1 with ESMTP ;\n " + leap_mail_date leap_mail = test_helper.leap_mail(headers={'received': leap_mail_received_header}) - mail = PixelatedMail.from_leap_mail(leap_mail) + mail = PixelatedMail.from_soledad(*leap_mail, soledad_querier=self.querier) - self.assertEqual(str(mail.headers['date']), leap_mail_date_in_iso_format) + self.assertEqual(str(mail.headers['Date']), leap_mail_date_in_iso_format) - def test_from_dict(self): - mail = PixelatedMail.from_dict(self.mail_dict()) + 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) - self.assertEqual(mail.headers['cc'], ['cc@pixelated.org', 'anothercc@pixelated.org']) - self.assertEqual(mail.headers['to'], ['to@pixelated.org', 'anotherto@pixelated.org']) - self.assertEqual(mail.headers['bcc'], ['bcc@pixelated.org', 'anotherbcc@pixelated.org']) - self.assertEqual(mail.headers['subject'], 'Oi') - self.assertEqual(mail.ident, None) - self.assertEqual(mail.tags, set(['sent'])) - self.assertEqual(mail.body, 'Este \xe9 o corpo') + current_tags = pixelated_mail.update_tags({'custom_1', 'custom_3'}) + self.assertEquals({'custom_3', 'custom_1'}, current_tags) - def test_from_dict_adds_current_date(self): - pixelated.support.date.iso_now = lambda: 'date now' + def test_mark_as_read(self): + mail = PixelatedMail.from_soledad(*test_helper.leap_mail(flags=[]), soledad_querier=self.querier) - mail = PixelatedMail.from_dict(self.mail_dict()) + mail.mark_as_read() - self.assertEqual('date now', mail.headers['date']) + self.assertEquals(mail.fdoc.content['flags'], ['\\Seen']) - def test_update_tags_return_a_set_with_the_current_tags(self): - pixelated_mail = PixelatedMail.from_leap_mail(test_helper.leap_mail(extra_headers={'X-tags': '["custom_1", "custom_2"]'}), Mock()) - current_tags = pixelated_mail.update_tags(set(['custom_1', 'custom_3'])) - self.assertEquals(set(['custom_3', 'custom_1']), current_tags) + def test_mark_as_not_recent(self): + mail = PixelatedMail.from_soledad(*test_helper.leap_mail(flags=['\\Recent']), soledad_querier=self.querier) - def test_to_mime_multipart(self): - pixelated.support.date.iso_now = lambda: 'date now' + mail.mark_as_not_recent() - mime_multipart = PixelatedMail.from_dict(self.mail_dict()).to_mime_multipart() + self.assertEquals(mail.fdoc.content['flags'], []) + + def test_update_tags_notifies_tag_service(self): + db_path = '/tmp/test_update_tags_notifies_tag_service' + tag_service = TagService(TagIndex(db_path)) + + mail = PixelatedMail.from_soledad(*test_helper.leap_mail(), soledad_querier=self.querier) + + mail.update_tags({'new_tag'}) + self.assertIn(Tag('new_tag'), tag_service.all_tags()) + + os.remove(db_path + '.db') + +class InputMailTest(unittest.TestCase): + mail_dict = lambda x: { + 'body': 'Este \xe9 o corpo', + 'header': { + 'cc': ['cc@pixelated.org', 'anothercc@pixelated.org'], + 'to': ['to@pixelated.org', 'anotherto@pixelated.org'], + 'bcc': ['bcc@pixelated.org', 'anotherbcc@pixelated.org'], + 'subject': 'Oi' + }, + 'ident': '', + 'tags': ['sent'] + } - 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(), "\nEste \xe9 o corpo") def test_to_mime_multipart_should_add_blank_fields(self): pixelated.support.date.iso_now = lambda: 'date now' @@ -103,66 +105,30 @@ class TestPixelatedMail(unittest.TestCase): mail_dict['header']['cc'] = '' mail_dict['header']['subject'] = '' - mime_multipart = PixelatedMail.from_dict(mail_dict).to_mime_multipart() + mime_multipart = InputMail.from_dict(mail_dict).to_mime_multipart() self.assertNotRegexpMatches(mime_multipart.as_string(), "\nTo: \n") self.assertNotRegexpMatches(mime_multipart.as_string(), "\nBcc: \n") self.assertNotRegexpMatches(mime_multipart.as_string(), "\nCc: \n") self.assertNotRegexpMatches(mime_multipart.as_string(), "\nSubject: \n") - def test_smtp_format(self): - PixelatedMail.from_email_address = 'pixelated@org' - mail = PixelatedMail.from_dict(self.mail_dict()) - - smtp_format = mail.to_smtp_format() - - self.assertRegexpMatches(smtp_format, "\nFrom: pixelated@org") - - def test_extract_headers_should_break_header_in_multiple_recipients(self): - headers = test_helper.DEFAULT_HEADERS.copy() - - headers['to'] = "nlima@example.com, Duda Dornelles <ddornelles@example.com>" - headers['bcc'] = "ddornelles@example.com, Neissi Lima <nlima@example.com>" - headers['cc'] = "nlima@example.com, Duda Dornelles <ddornelles@example.com>" - - leap_mail = test_helper.leap_mail(headers=headers) - - pixelated_mail = PixelatedMail.from_leap_mail(leap_mail) - - self.assertEquals(pixelated_mail.headers['to'], ["nlima@example.com", "Duda Dornelles <ddornelles@example.com>"]) - self.assertEquals(pixelated_mail.headers['bcc'], ["ddornelles@example.com", "Neissi Lima <nlima@example.com>"]) - self.assertEquals(pixelated_mail.headers['cc'], ["nlima@example.com", "Duda Dornelles <ddornelles@example.com>"]) - - def test_mark_as_read(self): - mail = PixelatedMail.from_leap_mail(test_helper.leap_mail(flags=[])) - - mail.mark_as_read() - - self.assertEquals(mail.leap_mail.setFlags.call_args[0], (('\\Seen',), 1)) - def test_mark_as_not_recent(self): - mail = PixelatedMail.from_leap_mail(test_helper.leap_mail(flags=[])) - - mail.mark_as_not_recent() - - self.assertEquals(mail.leap_mail.setFlags.call_args[0], (('\\Recent',), -1)) - - def test_mark_as_deleted(self): - mail = PixelatedMail.from_leap_mail(test_helper.leap_mail(extra_headers={'X-Tags': '["skinka", "altoids"]'}, flags=[]), Mock()) + def test_to_mime_multipart(self): + pixelated.support.date.iso_now = lambda: 'date now' - self.assertEquals(set(['skinka', 'altoids']), mail.tags) + mime_multipart = InputMail.from_dict(self.mail_dict()).to_mime_multipart() - mail.mark_as_deleted() + 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(), "\nEste \xe9 o corpo") - self.assertEquals(set([]), mail.tags) - self.assertEquals(mail.leap_mail.setFlags.call_args[0], ((u'\\Deleted',), 1)) - def test_update_tags_notifies_tag_service(self): - db_path = '/tmp/test_update_tags_notifies_tag_service' - tag_service = TagService(TagIndex(db_path)) - mail = PixelatedMail.from_leap_mail(test_helper.leap_mail(), tag_service) + def test_smtp_format(self): + PixelatedMail.from_email_address = 'pixelated@org' - mail.update_tags(set(['new_tag'])) - self.assertIn(Tag('new_tag'), tag_service.all_tags()) + smtp_format = InputMail.from_dict(self.mail_dict()).to_smtp_format() - os.remove(db_path + '.db') + self.assertRegexpMatches(smtp_format, "\nFrom: pixelated@org")
\ No newline at end of file diff --git a/service/test/adapter/pixelated_mailbox_test.py b/service/test/adapter/pixelated_mailbox_test.py index 847ce20c..87f7b4f3 100644 --- a/service/test/adapter/pixelated_mailbox_test.py +++ b/service/test/adapter/pixelated_mailbox_test.py @@ -14,58 +14,35 @@ # 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.pixelated_mail import PixelatedMail -from pixelated.adapter.status import Status -import test_helper from pixelated.adapter.pixelated_mailbox import PixelatedMailbox +import test_helper from mockito import * class PixelatedMailboxTest(unittest.TestCase): def setUp(self): - mail_one = test_helper.leap_mail(uid=0, mbox='SENT') - leap_mailbox = test_helper.leap_mailbox(messages=[mail_one], mailbox_name='SENT') - self.tag_service = mock() - self.mailbox = PixelatedMailbox(leap_mailbox, self.tag_service) + self.querier = mock() + self.mailbox = PixelatedMailbox('INBOX', self.querier, tag_service=self.tag_service) def test_mailbox_tag_is_added_when_recent_mail_arrives(self): - recent_leap_mail = test_helper.leap_mail(uid=0, mbox='SPAM', flags=['\\Recent']) - mailbox = PixelatedMailbox(test_helper.leap_mailbox(messages=[recent_leap_mail], mailbox_name='SPAM')) - self.assertIn('spam', mailbox.mails()[0].tags) - - def test_mailbox_tag_is_ignored_for_non_recent_mail(self): - recent_leap_mail = test_helper.leap_mail(uid=0, mbox='SPAM', flags=[]) - mailbox = PixelatedMailbox(test_helper.leap_mailbox(messages=[recent_leap_mail], mailbox_name='SPAM')) - self.assertNotIn('spam', mailbox.mails()[0].tags) + recent_leap_mail = test_helper.leap_mail(uid=0, mbox='INBOX', flags=['\\Recent']) + when(self.querier).all_mails_by_mailbox('INBOX').thenReturn([PixelatedMail.from_soledad(*recent_leap_mail, soledad_querier=self.querier)]) - def test_add_message_to_mailbox_with_raw_message(self): - mail = PixelatedMail.from_dict(test_helper.mail_dict()) - mail.raw_message = lambda: 'raw mail' + self.assertIn('inbox', self.mailbox.mails()[0].tags) - leap_mailbox_messages = mock() - self.mailbox.leap_mailbox.messages = leap_mailbox_messages - - self.mailbox._do_add_async.wrapped_function(self.mailbox, mail, use_smtp_format=False) - - verify(leap_mailbox_messages).add_msg('raw mail') - - def test_add_message_to_mailbox_with_smtp_format(self): - mail = PixelatedMail.from_dict(test_helper.mail_dict()) - mail.to_smtp_format = lambda: 'smtp format mail' - - leap_mailbox_messages = mock() - self.mailbox.leap_mailbox.messages = leap_mailbox_messages - - self.mailbox._do_add_async.wrapped_function(self.mailbox, mail, use_smtp_format=True) + def test_mailbox_tag_is_ignored_for_non_recent_mail(self): + recent_leap_mail = test_helper.leap_mail(uid=0, mbox='INBOX', flags=[]) + when(self.querier).all_mails_by_mailbox('INBOX').thenReturn([PixelatedMail.from_soledad(*recent_leap_mail, soledad_querier=self.querier)]) - verify(leap_mailbox_messages).add_msg('smtp format mail') + self.assertNotIn('spam', self.mailbox.mails()[0].tags) def test_remove_message_from_mailbox(self): - mail = mock() - self.mailbox.leap_mailbox = mock() + mail = PixelatedMail() + when(self.querier).mail(1).thenReturn(mail) - self.mailbox.remove(mail) + self.mailbox.remove(1) - verify(mail).mark_as_deleted() - verify(self.mailbox.leap_mailbox).expunge() + verify(self.querier).remove_mail(mail) diff --git a/service/test/adapter/pixelated_mailboxes_test.py b/service/test/adapter/pixelated_mailboxes_test.py index e188354e..e8f76e0f 100644 --- a/service/test/adapter/pixelated_mailboxes_test.py +++ b/service/test/adapter/pixelated_mailboxes_test.py @@ -14,15 +14,22 @@ # 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.pixelated_mail import PixelatedMail +from test.adapter import test_helper +from mockito import * +import pixelated.adapter.soledad_querier +querier = mock() +global querier +pixelated.adapter.soledad_querier.get_soledad_querier_instance = lambda x, y: querier + +from pixelated.adapter.pixelated_mail import PixelatedMail from pixelated.adapter.pixelated_mailbox import PixelatedMailbox from pixelated.adapter.pixelated_mailboxes import PixelatedMailBoxes -from mockito import * class PixelatedMailboxesTest(unittest.TestCase): def setUp(self): + self.account = mock() self.drafts_mailbox = mock() self.drafts_mailbox.mailbox_name = 'drafts' @@ -34,7 +41,7 @@ class PixelatedMailboxesTest(unittest.TestCase): self.account.mailboxes = ['INBOX'] tags_to_search_for = {'tags': ['inbox', 'custom_tag']} - when(PixelatedMailbox).create(self.account, 'INBOX').thenReturn(mailbox) + when(PixelatedMailbox).create('INBOX').thenReturn(mailbox) when(mailbox).mails_by_tags(any(list)).thenReturn(["mail"]) mails = self.mailboxes.mails_by_tag(tags_to_search_for['tags']) @@ -44,22 +51,18 @@ class PixelatedMailboxesTest(unittest.TestCase): def test_add_draft(self): mail = PixelatedMail() - when(self.drafts_mailbox).add(mail, use_smtp_format=True).thenReturn(1) + when(self.drafts_mailbox).add(mail).thenReturn(1) self.mailboxes.add_draft(mail) - verify(self.drafts_mailbox).add(mail, use_smtp_format=True) - self.assertEqual('drafts', mail.mailbox_name) - self.assertEqual(1, mail.uid) + verify(self.drafts_mailbox).add(mail) def test_update_draft(self): - mail = PixelatedMail() - when(self.drafts_mailbox).add(mail, use_smtp_format=True).thenReturn(1) + mail = test_helper.input_mail() + when(self.drafts_mailbox).add(mail).thenReturn(mail) - self.mailboxes.update_draft(mail) + self.mailboxes.update_draft(mail.ident, mail) - inorder.verify(self.drafts_mailbox).add(mail, use_smtp_format=True) - inorder.verify(self.drafts_mailbox).remove(mail) + inorder.verify(self.drafts_mailbox).add(mail) + inorder.verify(self.drafts_mailbox).remove(mail.ident) - self.assertEqual('drafts', mail.mailbox_name) - self.assertEqual(1, mail.uid) diff --git a/service/test/adapter/test_helper.py b/service/test/adapter/test_helper.py index ad39f343..c8593fc1 100644 --- a/service/test/adapter/test_helper.py +++ b/service/test/adapter/test_helper.py @@ -15,6 +15,7 @@ # along with Pixelated. If not, see <http://www.gnu.org/licenses/>. from mock import Mock from datetime import datetime +from pixelated.adapter.pixelated_mail import InputMail LEAP_FLAGS = ['\\Seen', '\\Answered', @@ -41,16 +42,30 @@ def mail_dict(): } -def leap_mail(uid=0, flags=LEAP_FLAGS, headers=DEFAULT_HEADERS, extra_headers={}, mbox='INBOX'): - headers = dict(headers.items() + extra_headers.items()) - return Mock(getUID=Mock(return_value=uid), - _mbox=mbox, - getFlags=Mock(return_value=flags), - bdoc=Mock(content={'raw': 'test'}), - hdoc=Mock(content={'headers': headers})) +def doc(content): + class TestDoc: + def __init__(self, content): + self.content = content + return TestDoc(content) + + +def leap_mail(uid=0, flags=LEAP_FLAGS, headers=DEFAULT_HEADERS, extra_headers={}, mbox='INBOX', body='body', + chash='chash'): + fdoc = doc({'flags': flags, 'mbox': mbox, 'type': 'flags', 'uid': uid, 'chash': chash}) + + headers['headers'] = extra_headers + hdoc = doc(headers) + + bdoc = doc({'raw': body, 'type': 'cnt'}) + + return (fdoc, hdoc, bdoc) + + +def input_mail(): + mail = InputMail() + mail.fdoc = doc({}) + mail._chash = "123" + mail.as_dict = lambda: None + return mail -def leap_mailbox(messages=[leap_mail(uid=6)], mailbox_name='INBOX'): - return Mock(_get_mbox_doc=Mock(return_value=None), - mbox=mailbox_name, - messages=messages) diff --git a/service/test/adapter/test_tag_service.py b/service/test/adapter/test_tag_service.py index 027e6113..5a70e82b 100644 --- a/service/test/adapter/test_tag_service.py +++ b/service/test/adapter/test_tag_service.py @@ -30,8 +30,8 @@ class TagServiceTest(unittest.TestCase): self.tag_service = TagService(tag_index=self.tag_index) def test_index_is_initialized_with_mail_tags_if_empty(self): - mail_one = PixelatedMail.from_leap_mail(test_helper.leap_mail(uid=0, extra_headers={'X-Tags': '["tag_1"]'})) - mail_two = PixelatedMail.from_leap_mail(test_helper.leap_mail(uid=1, extra_headers={'X-Tags': '["tag_2"]'})) + mail_one = PixelatedMail.from_soledad(*test_helper.leap_mail(uid=0, extra_headers={'X-Tags': '["tag_1"]'})) + mail_two = PixelatedMail.from_soledad(*test_helper.leap_mail(uid=1, extra_headers={'X-Tags': '["tag_2"]'})) mails = [mail_one, mail_two] self.tag_service.load_index(mails) @@ -53,7 +53,7 @@ class TagServiceTest(unittest.TestCase): self.assertEquals(0, self.tag_service.tag_index.get('inbox').total) self.assertEquals(1, self.tag_service.tag_index.get('one_tag').total) - self.tag_service.notify_tags_updated(set(['inbox']), set(['one_tag']), mail_ident) + self.tag_service.notify_tags_updated({'inbox'}, {'one_tag'}, mail_ident) self.assertEquals(1, self.tag_service.tag_index.get('inbox').total) self.assertIsNone(self.tag_service.tag_index.get('one_tag')) diff --git a/service/test/user_agent_test.py b/service/test/user_agent_test.py index 2ae4c97d..d5432cbd 100644 --- a/service/test/user_agent_test.py +++ b/service/test/user_agent_test.py @@ -19,6 +19,8 @@ import pixelated.user_agent from pixelated.adapter.pixelated_mail import PixelatedMail from pixelated.adapter.pixelated_mail import InputMail from mockito import * +import test.adapter.test_helper as test_helper +import json import pixelated.adapter.pixelated_mail @@ -30,44 +32,33 @@ class UserAgentTest(unittest.TestCase): pixelated.user_agent.DISABLED_FEATURES = [] pixelated.user_agent.mail_service = self.mail_service + self.input_mail = None + pixelated.adapter.pixelated_mail.input_mail_from_dict = lambda x: self.input_mail def test_create_or_send_draft_should_create_draft_if_mail_has_no_ident(self): - mail = self.draft() - InputMail.from_dict = mock(return_value=mail) # has no ident + self.input_mail = self.draft() self.app.post('/mails', data='{}', content_type="application/json") - verify(self.mail_service).create_draft(mail) + verify(self.mail_service).create_draft(self.input_mail) - @unittest.expectedFailure def test_create_or_send_draft_should_send_draft_if_mail_has_ident(self): - mail = self.mail_with_ident() - pixelated.adapter.pixelated_mail.from_dict = lambda x: mail # does have ident + self.input_mail = self.draft() self.app.post('/mails', data='{"ident":1}', content_type="application/json") - verify(self.mail_service).send_draft(mail) + verify(self.mail_service).send_draft(self.input_mail) def test_update_draft(self): - mail = self.draft() - when(InputMail).from_dict().thenReturn(mail) - when(self.mail_service).update_draft().thenReturn(mail) + self.input_mail = self.draft() + + when(self.mail_service).update_draft(1, self.input_mail).thenReturn(self.input_mail) self.app.put('/mails', data='{"ident":1}', content_type="application/json") - verify(self.mail_service).update_draft(1, mail) + verify(self.mail_service).update_draft(1, self.input_mail) - def mail_without_ident(self): - mail = PixelatedMail() - mail.ident = '' - return mail - def mail_with_ident(self): - mail = PixelatedMail() - mail.ident = 1 - return mail def draft(self): - mail = InputMail() - mail.ident = 1 - return mail + return test_helper.input_mail() |