def test_new_tags_are_added_to_mailbox(self):
        leap_flags = ['\\Deleted', 'tag_custom_one', 'tag_custom_two']
        leap_mailbox_mock = test_helper.leap_mailbox(leap_flags=leap_flags)
        mailbox = PixelatedMailbox(leap_mailbox_mock)
        tags = [Tag('custom_one'), Tag('custom_three')]
        mailbox.update_tags(tags)

        expected = set(('\\Deleted', 'tag_custom_one', 'tag_custom_two', 'tag_custom_three'))
        actual_args = set(leap_mailbox_mock.setFlags.call_args[0][0])

        self.assertEquals(expected, actual_args)
    def test_search_without_query_returns_unfiltered_mailbox(self):
        # given
        mailbox = test_helper.leap_mailbox(leap_flags=['\\Recent'], extra_flags=['tag_custom_tag'])
        account = MagicMock()
        account.getMailbox.return_value = mailbox
        leap_session = MagicMock(account=account)

        # when
        mailservice = MailService(leap_session)
        mails = mailservice.mails({})

        # then
        self.assertEqual(1, len(mails))
    def test_custom_tags_get_created_if_not_exists(self, mockRemoveFlags, mockAppendFlags):
        mailbox = test_helper.leap_mailbox(leap_flags=['\\Recent'])
        account = MagicMock()
        leap_session = MagicMock()
        leap_session.account = account
        leap_session.account.getMailbox.return_value = mailbox

        mailservice = MailService(leap_session)

        new_tags = ['test', 'inbox']
        updated_tags = mailservice.update_tags(6, new_tags)

        self.assertEquals(set([Tag('test'), Tag('inbox')]), set(updated_tags))
        # make sure that special tags are skipped when setting leap flags (eg.: tag_inbox)
        mockAppendFlags.assert_called_with(6, ['tag_test'])
        mockRemoveFlags.assert_called_with(6, [])
    def test_search_for_tags(self):
        # given
        mailbox = test_helper.leap_mailbox(leap_flags=['\\Recent'], extra_flags=['tag_custom_tag'])
        matching_mail = test_helper.leap_mail(uid=6, leap_flags=[], extra_flags=['tag_custom_tag'])
        not_matching_mail = test_helper.leap_mail(uid=6, leap_flags=[], extra_flags=['tag_other'])
        mailbox.messages = [matching_mail, not_matching_mail]
        mailbox.all_tags.return_value = set()
        account = MagicMock()
        account.mailboxes = ['inbox']
        account.getMailbox.return_value = mailbox

        leap_session = MagicMock(account=account)

        # when
        mailservice = MailService(leap_session)
        mails = mailservice.mails({'tags': ['inbox', 'custom_tag']})

        # then
        self.assertEqual(1, len(mails))
        self.assertEqual(set([Tag('custom_tag')]), mails[0].tags)
    def test_retrieve_all_tags_from_mailbox(self):
        leap_flags = ['\\Deleted', '\\Draft', '\\Recent', 'tag_custom', 'should_ignore_all_from_here', 'List']
        mailbox = PixelatedMailbox(test_helper.leap_mailbox(leap_flags=leap_flags))

        self.assertEquals(set([Tag('trash'), Tag('inbox'), Tag('drafts'), Tag('custom')]), mailbox.all_tags())