summaryrefslogtreecommitdiff
path: root/service/test/integration/test_contacts.py
blob: 98c2e6f64cadb241910a958af1c79a723dbba21c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#
# 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 test.support.integration import SoledadTestBase, MailBuilder
from twisted.internet import defer
import json
import pkg_resources


class ContactsTest(SoledadTestBase):

    @defer.inlineCallbacks
    def test_TO_CC_and_BCC_fields_are_being_searched(self):
        input_mail = MailBuilder().with_tags(['important']).build_input_mail()
        yield self.add_mail_to_inbox(input_mail)

        contacts = yield self.get_contacts(query='recipient')

        self.assertTrue('recipient@to.com' in contacts)
        self.assertTrue('recipient@cc.com' in contacts)
        self.assertTrue('recipient@bcc.com' in contacts)

    @defer.inlineCallbacks
    def test_FROM_address_is_being_searched(self):
        input_mail = MailBuilder().with_tags(['important']).with_from('Formatted Sender <sender@from.com>').build_input_mail()
        yield self.add_mail_to_inbox(input_mail)

        contacts = yield self.get_contacts(query='Sender')

        self.assertIn('Formatted Sender <sender@from.com>', contacts)

    @defer.inlineCallbacks
    def test_trash_and_drafts_mailboxes_are_being_ignored(self):
        yield self.add_multiple_to_mailbox(1, mailbox='INBOX', to='recipient@inbox.com')
        yield self.add_multiple_to_mailbox(1, mailbox='DRAFTS', to='recipient@drafts.com')
        yield self.add_multiple_to_mailbox(1, mailbox='SENT', to='recipient@sent.com')
        yield self.add_multiple_to_mailbox(1, mailbox='TRASH', to='recipient@trash.com')

        contacts = yield self.get_contacts(query='recipient')

        self.assertTrue('recipient@inbox.com' in contacts)
        self.assertTrue('recipient@sent.com' in contacts)
        self.assertFalse('recipient@drafts.com' in contacts)
        self.assertFalse('recipient@trash.com' in contacts)

    @defer.inlineCallbacks
    def test_deduplication_on_same_mail_address_using_largest(self):
        input_mail = MailBuilder().with_tags(['important']).build_input_mail()

        formatted_input_mail = MailBuilder().with_tags(['important'])
        formatted_input_mail.with_to('Recipient Principal <recipient@to.com>')
        formatted_input_mail.with_cc('Recipient Copied <recipient@cc.com>')
        formatted_input_mail.with_bcc('Recipient Carbon <recipient@bcc.com>')
        formatted_input_mail = formatted_input_mail.build_input_mail()

        yield self.add_mail_to_inbox(input_mail)
        yield self.add_mail_to_inbox(formatted_input_mail)

        contacts = yield self.get_contacts(query='Recipient')

        self.assertEquals(3, len(contacts))
        self.assertTrue('Recipient Principal <recipient@to.com>' in contacts)
        self.assertTrue('Recipient Copied <recipient@cc.com>' in contacts)
        self.assertTrue('Recipient Carbon <recipient@bcc.com>' in contacts)

    @defer.inlineCallbacks
    def test_bounced_addresses_are_ignored(self):
        self.skipTest("bounced mails filter still needs to be migrated")
        to_be_bounced = MailBuilder().with_to('this_mail_was_bounced@domain.com').build_input_mail()
        yield self.add_mail_to_inbox(to_be_bounced)

        bounced_hdoc = self._bounced_mail_hdoc_content()
        bounced_mail_template = MailBuilder().build_input_mail()
        bounced_mail_template.headers.update(bounced_hdoc["headers"])

        not_bounced_mail = MailBuilder(
        ).with_tags(['important']).with_to('this_mail_was_not@bounced.com').build_input_mail()
        yield self.add_mail_to_inbox(not_bounced_mail)

        contacts = yield self.get_contacts(query='this')

        self.assertNotIn('this_mail_was_bounced@domain.com', contacts)
        self.assertNotIn("MAILER-DAEMON@domain.org (Mail Delivery System)", contacts)
        self.assertIn('this_mail_was_not@bounced.com', contacts)

    def _bounced_mail_hdoc_content(self):
        hdoc_file = pkg_resources.resource_filename('test.unit.fixtures', 'bounced_mail_hdoc.json')
        with open(hdoc_file) as f:
            hdoc = json.loads(f.read())
        return hdoc