summaryrefslogtreecommitdiff
path: root/service/test/unit/adapter/mailstore/test_searchable_mailstore.py
blob: 8c5712014b25286e6889ea96a7420e2058f11818 (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
103
104
105
106
107
108
109
110
111
112
#
# Copyright (c) 2015 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 email.parser import Parser
import os
from mockito import verify, mock, when
import pkg_resources
from twisted.internet import defer
from twisted.trial.unittest import TestCase
from pixelated.adapter.mailstore import MailStore
from pixelated.adapter.mailstore.leap_mailstore import LeapMail
from pixelated.adapter.mailstore.searchable_mailstore import SearchableMailStore
from pixelated.adapter.search import SearchEngine


ANY_MAILBOX = 'INBOX'


class TestSearchableMailStore(TestCase):

    def setUp(self):
        super(TestSearchableMailStore, self).setUp()
        self.search_index = mock(mocked_obj=SearchEngine)
        self.delegate_mail_store = mock(mocked_obj=MailStore)
        self.store = SearchableMailStore(self.delegate_mail_store, self.search_index)

    @defer.inlineCallbacks
    def test_add_mail_delegates_to_mail_store_and_updates_index(self):
        mail = self._load_mail_from_file('mbox00000000')
        leap_mail = LeapMail('id', ANY_MAILBOX)
        when(self.delegate_mail_store).add_mail(ANY_MAILBOX, mail).thenReturn(defer.succeed(leap_mail))

        result = yield self.store.add_mail(ANY_MAILBOX, mail)

        verify(self.delegate_mail_store).add_mail(ANY_MAILBOX, mail)
        verify(self.search_index).index_mail(leap_mail)
        self.assertEqual(leap_mail, result)

    @defer.inlineCallbacks
    def test_delete_mail_delegates_to_mail_store_and_updates_index(self):
        when(self.delegate_mail_store).delete_mail('mail id').thenReturn(defer.succeed(None))
        when(self.search_index).remove_from_index('mail id').thenReturn(defer.succeed(None))

        yield self.store.delete_mail('mail id')

        verify(self.delegate_mail_store).delete_mail('mail id')
        verify(self.search_index).remove_from_index('mail id')

    @defer.inlineCallbacks
    def test_update_mail_delegates_to_mail_store_and_updates_index(self):
        leap_mail = LeapMail('id', ANY_MAILBOX)

        yield self.store.update_mail(leap_mail)

        verify(self.delegate_mail_store).update_mail(leap_mail)
        verify(self.search_index).index_mail(leap_mail)

    @defer.inlineCallbacks
    def test_copy_mail_delegates_to_mail_store_and_updates_index(self):
        copied_mail = LeapMail('new id', ANY_MAILBOX)
        when(self.delegate_mail_store).copy_mail_to_mailbox('mail id', ANY_MAILBOX).thenReturn(defer.succeed(copied_mail))

        result = yield self.store.copy_mail_to_mailbox('mail id', ANY_MAILBOX)

        verify(self.search_index).index_mail(copied_mail)
        self.assertEqual(copied_mail, result)

    @defer.inlineCallbacks
    def test_move_mail_delegates_to_mail_store_and_updates_index(self):
        moved_mail = LeapMail('new id', ANY_MAILBOX)
        when(self.delegate_mail_store).move_mail_to_mailbox('mail id', ANY_MAILBOX).thenReturn(defer.succeed(moved_mail))

        result = yield self.store.move_mail_to_mailbox('mail id', ANY_MAILBOX)

        verify(self.search_index).remove_from_index('mail id')
        verify(self.search_index).index_mail(moved_mail)
        self.assertEqual(moved_mail, result)

    @defer.inlineCallbacks
    def test_other_methods_are_delegated(self):
        mail = LeapMail('mail id', ANY_MAILBOX)
        when(self.delegate_mail_store).get_mail('mail id').thenReturn(defer.succeed(mail), defer.succeed(mail))
        result = yield self.store.get_mail('mail id')

        self.assertEqual(mail, result)

    @defer.inlineCallbacks
    def test_delete_mailbox_is_not_implemented(self):
        try:
            yield self.store.delete_mailbox(ANY_MAILBOX)
            self.fail("Should raise NotImplementedError")
        except NotImplementedError:
            pass

    def _load_mail_from_file(self, mail_file):
        mailset_dir = pkg_resources.resource_filename('test.unit.fixtures', 'mailset')
        mail_file = os.path.join(mailset_dir, 'new', mail_file)
        with open(mail_file) as f:
            mail = Parser().parse(f)
        return mail