diff options
Diffstat (limited to 'service/test/unit/adapter')
-rw-r--r-- | service/test/unit/adapter/__init__.py | 0 | ||||
-rw-r--r-- | service/test/unit/adapter/mail_service_test.py | 60 | ||||
-rw-r--r-- | service/test/unit/adapter/pixelated_mail_sender_test.py | 46 | ||||
-rw-r--r-- | service/test/unit/adapter/pixelated_mail_test.py | 133 | ||||
-rw-r--r-- | service/test/unit/adapter/pixelated_mailbox_test.py | 36 | ||||
-rw-r--r-- | service/test/unit/adapter/pixelated_mailboxes_test.py | 67 | ||||
-rw-r--r-- | service/test/unit/adapter/tag_index_test.py | 88 | ||||
-rw-r--r-- | service/test/unit/adapter/test_status.py | 33 | ||||
-rw-r--r-- | service/test/unit/adapter/test_tag.py | 79 | ||||
-rw-r--r-- | service/test/unit/adapter/test_tag_service.py | 60 |
10 files changed, 602 insertions, 0 deletions
diff --git a/service/test/unit/adapter/__init__.py b/service/test/unit/adapter/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/service/test/unit/adapter/__init__.py diff --git a/service/test/unit/adapter/mail_service_test.py b/service/test/unit/adapter/mail_service_test.py new file mode 100644 index 00000000..549ab05c --- /dev/null +++ b/service/test/unit/adapter/mail_service_test.py @@ -0,0 +1,60 @@ +# +# 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.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.mailboxes.drafts = lambda: mock() + self.mailboxes.trash = lambda: mock() + self.mailboxes.sent = lambda: mock() + + self.mail_sender = mock() + self.mail_service = MailService(self.mailboxes, self.mail_sender) + + def test_send_mail(self): + mail = "mail" + + self.mail_service.send(1, mail) + + verify(self.mail_sender).sendmail(mail) + + def test_mark_as_read(self): + mail = mock() + when(self.mail_service).mail(any()).thenReturn(mail) + self.mail_service.mark_as_read(1) + + verify(mail).mark_as_read() + + def test_create_draft(self): + mail = '' + + self.mail_service.create_draft(mail) + + verify(self.mailboxes).add_draft(mail) + + def test_delete_mail(self): + self.mail_service.delete_mail(1) + + verify(self.mailboxes).move_to_trash(1) diff --git a/service/test/unit/adapter/pixelated_mail_sender_test.py b/service/test/unit/adapter/pixelated_mail_sender_test.py new file mode 100644 index 00000000..207baadb --- /dev/null +++ b/service/test/unit/adapter/pixelated_mail_sender_test.py @@ -0,0 +1,46 @@ +# +# 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.pixelated_mail import PixelatedMail +from pixelated.adapter.pixelated_mail_sender import PixelatedMailSender +from mockito import * +from test.support import test_helper + + +class PixelatedMailSenderTest(unittest.TestCase): + def setUp(self): + self.mail_address = "pixelated@pixelated.org" + self.smtp_client = mock() + self.mail_sender = PixelatedMailSender(self.mail_address, self.smtp_client) + + def test_send_mail_sends_to_To_Cc_and_Bcc(self): + 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_soledad(*test_helper.leap_mail(extra_headers=headers)) + mail.to_smtp_format = lambda: "mail as smtp string" + + self.mail_sender.sendmail(mail) + + expected_recipients = ['to@pixelated.org', 'anotherto@pixelated.org', 'cc@pixelated.org', + 'anothercc@pixelated.org', + 'bcc@pixelated.org', 'anotherbcc@pixelated.org'] + + verify(self.smtp_client).sendmail(self.mail_address, expected_recipients, "mail as smtp string") diff --git a/service/test/unit/adapter/pixelated_mail_test.py b/service/test/unit/adapter/pixelated_mail_test.py new file mode 100644 index 00000000..0ab09a0a --- /dev/null +++ b/service/test/unit/adapter/pixelated_mail_test.py @@ -0,0 +1,133 @@ +# +# 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 + +import os +import pixelated.support.date +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 mockito import * +from test.support import test_helper + + +class TestPixelatedMail(unittest.TestCase): + + 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_soledad(*leap_mail, soledad_querier=self.querier) + + self.assertEqual(str(mail.headers['Date']), leap_mail_date_in_iso_format) + + 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_soledad(*leap_mail, 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) + + current_tags = pixelated_mail.update_tags({'custom_1', 'custom_3'}) + self.assertEquals({'custom_3', 'custom_1'}, current_tags) + + def test_mark_as_read(self): + mail = PixelatedMail.from_soledad(*test_helper.leap_mail(flags=[]), soledad_querier=self.querier) + + mail.mark_as_read() + + self.assertEquals(mail.fdoc.content['flags'], ['\\Seen']) + + def test_mark_as_not_recent(self): + mail = PixelatedMail.from_soledad(*test_helper.leap_mail(flags=['\\Recent']), soledad_querier=self.querier) + + mail.mark_as_not_recent() + + self.assertEquals(mail.fdoc.content['flags'], []) + + def test_update_tags_notifies_tag_service(self): + db_path = '/tmp/test_update_tags_notifies_tag_service' + TagService.instance = 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'), mail.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'] + } + + 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['header']['to'] = '' + mail_dict['header']['bcc'] = '' + mail_dict['header']['cc'] = '' + mail_dict['header']['subject'] = '' + + 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_to_mime_multipart(self): + pixelated.support.date.iso_now = lambda: 'date now' + + mime_multipart = InputMail.from_dict(self.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(), "\nEste \xe9 o corpo") + + def test_smtp_format(self): + PixelatedMail.from_email_address = 'pixelated@org' + + smtp_format = InputMail.from_dict(self.mail_dict()).to_smtp_format() + + self.assertRegexpMatches(smtp_format, "\nFrom: pixelated@org") diff --git a/service/test/unit/adapter/pixelated_mailbox_test.py b/service/test/unit/adapter/pixelated_mailbox_test.py new file mode 100644 index 00000000..d38cef5c --- /dev/null +++ b/service/test/unit/adapter/pixelated_mailbox_test.py @@ -0,0 +1,36 @@ +# +# 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.pixelated_mail import PixelatedMail +from pixelated.adapter.pixelated_mailbox import PixelatedMailbox +from mockito import * +from test.support import test_helper + + +class PixelatedMailboxTest(unittest.TestCase): + def setUp(self): + self.tag_service = mock() + self.querier = mock() + self.mailbox = PixelatedMailbox('INBOX', self.querier, tag_service=self.tag_service) + + def test_remove_message_from_mailbox(self): + mail = PixelatedMail.from_soledad(*test_helper.leap_mail(), soledad_querier=self.querier) + when(self.querier).mail(1).thenReturn(mail) + + self.mailbox.remove(1) + + verify(self.querier).remove_mail(mail) diff --git a/service/test/unit/adapter/pixelated_mailboxes_test.py b/service/test/unit/adapter/pixelated_mailboxes_test.py new file mode 100644 index 00000000..8314f7f8 --- /dev/null +++ b/service/test/unit/adapter/pixelated_mailboxes_test.py @@ -0,0 +1,67 @@ +# +# 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 test.support import test_helper +from mockito import * +import pixelated.adapter.soledad_querier + +querier = mock() +when(pixelated.adapter.soledad_querier).get_soledad_querier_instance().thenReturn(querier) + +from pixelated.adapter.pixelated_mail import InputMail +from pixelated.adapter.pixelated_mailbox import PixelatedMailbox +from pixelated.adapter.pixelated_mailboxes import PixelatedMailBoxes + + +class PixelatedMailboxesTest(unittest.TestCase): + def setUp(self): + + self.account = mock() + self.drafts_mailbox = mock() + self.drafts_mailbox.mailbox_name = 'drafts' + self.mailboxes = PixelatedMailBoxes(self.account) + self.mailboxes.drafts = lambda: self.drafts_mailbox + + def test_search_for_tags(self): + mailbox = mock() + self.account.mailboxes = ['INBOX'] + tags_to_search_for = {'tags': ['inbox', 'custom_tag']} + + 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']) + + self.assertEqual(1, len(mails)) + self.assertEqual("mail", mails[0]) + + def test_add_draft(self): + mail = InputMail() + when(self.drafts_mailbox).add(mail).thenReturn(1) + + self.mailboxes.add_draft(mail) + + verify(self.drafts_mailbox).add(mail) + + def test_update_draft(self): + mail = test_helper.input_mail() + when(self.drafts_mailbox).add(mail).thenReturn(mail) + + self.mailboxes.update_draft(mail.ident, mail) + + inorder.verify(self.drafts_mailbox).add(mail) + inorder.verify(self.drafts_mailbox).remove(mail.ident) diff --git a/service/test/unit/adapter/tag_index_test.py b/service/test/unit/adapter/tag_index_test.py new file mode 100644 index 00000000..21564fc5 --- /dev/null +++ b/service/test/unit/adapter/tag_index_test.py @@ -0,0 +1,88 @@ + +# +# 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 +import os +import uuid + +from pixelated.adapter.tag_index import TagIndex +from pixelated.adapter.tag import Tag + + +class TestTagIndex(unittest.TestCase): + + def setUp(self): + self.db_path = '/tmp/test_tag_index_' + str(uuid.uuid4()) + self.tag_index = TagIndex(self.db_path) + + def tearDown(self): + self.tag_index._close_db() + os.remove(self.db_path + '.db') + + def test_get_and_set_works(self): + tag = Tag('a_tag') + self.tag_index.set(tag) + self.assertEquals(tag, self.tag_index.get('a_tag')) + + def test_values_returns_all_values_in_the_index(self): + tag_a = Tag('tag_a') + self.tag_index.set(tag_a) + tag_b = Tag('tag_b') + self.tag_index.set(tag_b) + tag_c = Tag('tag_c') + self.tag_index.set(tag_c) + + self.assertEquals(set([tag_a, tag_b, tag_c]), self.tag_index.values()) + + def test_changes_are_visible_between_instances_using_same_file(self): + tag = Tag('some_tag') + self.tag_index.set(tag) + + other_tag_index = TagIndex(self.db_path) + self.assertIn(tag, other_tag_index.values()) + + def test_add_does_not_replace_existent_tag_with_same_name(self): + tag = Tag('tag', True) + self.tag_index.set(tag) + + same_name_tag = Tag('tag', False) + self.tag_index.add(same_name_tag) + + self.assertEquals(True, self.tag_index.get('tag').default) + + def test_empty_returns_true_if_there_are_no_values(self): + self.assertTrue(self.tag_index.empty()) + + def test_empty_returns_false_if_there_are_values(self): + self.tag_index.set(Tag('tag')) + self.assertFalse(self.tag_index.empty()) + + def test_remove_deletes_the_tag_with_the_given_key_from_the_index(self): + self.tag_index.set(Tag('tag')) + self.tag_index.remove('tag') + self.assertEquals(None, self.tag_index.get('tag')) + + def test_remove_does_not_raises_exception_if_key_is_not_present(self): + self.tag_index.remove('not_there') + + def test_removals_are_visible_between_instances_using_same_file(self): + tag = Tag('some_tag') + self.tag_index.set(tag) + + other_tag_index = TagIndex(self.db_path) + other_tag_index.remove('some_tag') + + self.assertIsNone(self.tag_index.get('some_tag')) diff --git a/service/test/unit/adapter/test_status.py b/service/test/unit/adapter/test_status.py new file mode 100644 index 00000000..bcdbb360 --- /dev/null +++ b/service/test/unit/adapter/test_status.py @@ -0,0 +1,33 @@ +# +# 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.status import Status + + +class TestStatus(unittest.TestCase): + + def test_leap_seen_flag_is_translated_to_read_status(self): + status = Status.from_flag('\\Seen') + self.assertEquals(Status('read'), status) + + def test_leap_answered_flag_is_translated_to_replied_status(self): + status = Status.from_flag('\\Answered') + self.assertEquals(Status('replied'), status) + + def test_bulk_conversion(self): + statuses = Status.from_flags(['\\Answered', '\\Seen', '\\Recent', 'tag_a_custom']) + self.assertEquals(set([Status('read'), Status('replied'), Status('recent')]), statuses) diff --git a/service/test/unit/adapter/test_tag.py b/service/test/unit/adapter/test_tag.py new file mode 100644 index 00000000..fc14ff49 --- /dev/null +++ b/service/test/unit/adapter/test_tag.py @@ -0,0 +1,79 @@ +# +# 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.tag import Tag + + +class TestTag(unittest.TestCase): + + def test_from_dict_sets_all_tag_attributes(self): + tag_dict = {'name': 'a_tag', + 'default': False, + 'counts': {'total': 3, + 'read': 1, + 'starred': 1, + 'replied': 1}, + 'mails': [1, 2, 3]} + + tag = Tag.from_dict(tag_dict) + + self.assertEquals(tag_dict['name'], tag.name) + self.assertEquals(tag_dict['default'], tag.default) + self.assertEquals(tag_dict['counts']['total'], tag.total) + # Checks if mail ids are aways restored as set() + self.assertEquals(type(tag.mails), type(set())) + self.assertEquals(set(tag_dict['mails']), tag.mails) + + def test_as_dict_puts_all_tag_attributes_in_the_returning_dict(self): + tag = Tag('some_tag', default=True) + tag.counts = {'total': 0, 'read': 0, 'starred': 0, 'replied': 0} + tag.mails = [1, 2, 3] + + tag_dict = tag.as_dict() + + self.assertEquals(tag.name, tag_dict['name']) + self.assertEquals(tag.default, tag_dict['default']) + self.assertEquals(tag.total, tag_dict['counts']['total']) + self.assertEquals(tag.mails, tag_dict['mails']) + + def test_increments_total_count_and_adds_mails_id_to_mails(self): + tag = Tag('another') + tag.increment(12) + + self.assertIn(12, tag.mails) + self.assertEquals(1, tag.total) + + def test_decrement_does_nothing_if_mail_has_not_the_tag(self): + tag = Tag('tag') + tag.decrement(2000) + + self.assertEquals(0, tag.total) + + def test_increment_does_nothing_if_mail_already_has_the_tag(self): + tag = Tag('tag') + tag.mails = set([12]) + tag.increment(12) + + self.assertEquals(1, tag.total) + + def test_decrements_total_count_and_removes_mails_id_from_mails(self): + tag = Tag('one_more') + tag.mails = set([12]) + tag.decrement(12) + + self.assertNotIn(12, tag.mails) + self.assertEquals(0, tag.total) diff --git a/service/test/unit/adapter/test_tag_service.py b/service/test/unit/adapter/test_tag_service.py new file mode 100644 index 00000000..aeb1b503 --- /dev/null +++ b/service/test/unit/adapter/test_tag_service.py @@ -0,0 +1,60 @@ +# +# 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 +import tempfile + +from pixelated.adapter.tag import Tag +from pixelated.adapter.pixelated_mail import PixelatedMail +from pixelated.adapter.tag_index import TagIndex +from pixelated.adapter.tag_service import TagService +from test.support import test_helper + + +class TagServiceTest(unittest.TestCase): + def setUp(self): + self.index_file_handler, self.index_file_path = tempfile.mkstemp() + self.tag_index = TagIndex(self.index_file_path) + self.tag_service = TagService(tag_index=self.tag_index) + + def test_index_is_initialized_with_mail_tags_if_empty(self): + 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) + + self.assertEqual(self.tag_service.all_tags(), {Tag('sent'), Tag('inbox'), Tag('drafts'), Tag('trash'), Tag('tag_1'), Tag('tag_2')}) + + def test_special_tags_always_exists(self): + self.tag_service.load_index([]) + + self.assertEqual(self.tag_service.all_tags(), {Tag('sent'), Tag('inbox'), Tag('drafts'), Tag('trash')}) + + def test_notify_tags_updated_method_properly_changes_tags_state(self): + mail_ident = 12 + tag = Tag('one_tag') + tag.increment(mail_ident) + self.tag_service.load_index([]) + self.tag_service.tag_index.set(tag) + + 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({'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')) |