summaryrefslogtreecommitdiff
path: root/service/test/unit/resources/test_mails_resource.py
blob: 2d9cb33c5e09b766278eaa48a06530f57def7c05 (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
#  -*- coding: utf-8 -*-
#
# 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 mock import patch
from mockito import mock, when, verify, any as ANY
from twisted.internet import defer
from twisted.web.test.requesthelper import DummyRequest

from pixelated.application import UserAgentMode
from pixelated.resources.mails_resource import MailsResource
from test.unit.resources import DummySite


class TestMailsResource(unittest.TestCase):
    def setUp(self):
        self.mail_service = mock()
        self.services_factory = mock()
        self.services_factory.mode = UserAgentMode(is_single_user=True)
        self.services = mock()
        self.services.mail_service = self.mail_service
        self.services.draft_service = mock()
        self.services_factory._services_by_user = {'someuserid': self.mail_service}
        when(self.services_factory).services(ANY()).thenReturn(self.services)

    @patch('leap.common.events.register')
    def test_render_GET_should_unicode_mails_search_query(self, mock_register):
        request = DummyRequest(['/mails'])
        non_unicode_search_term = 'coração'
        request.addArg('q', non_unicode_search_term)
        request.addArg('w', 25)
        request.addArg('p', 1)

        unicodified_search_term = u'coração'
        when(self.mail_service).mails(unicodified_search_term, 25, 1).thenReturn(defer.Deferred())

        mails_resource = MailsResource(self.services_factory)
        mails_resource.isLeaf = True
        web = DummySite(mails_resource)
        d = web.get(request)

        def assert_response(_):
            verify(self.mail_service).mails(unicodified_search_term, 25, 1)

        d.addCallback(assert_response)
        return d

    @patch('leap.common.events.register')
    def test_render_PUT_should_store_draft_with_attachments(self, mock_register):
        request = DummyRequest(['/mails'])
        request.method = 'PUT'
        content = mock()
        when(content).read().thenReturn('{"attachments": [{"ident": "some fake attachment id"}]}')
        when(self.mail_service).attachment('some fake attachment id').thenReturn(defer.Deferred())
        request.content = content

        mails_resource = MailsResource(self.services_factory)
        mails_resource.isLeaf = True
        web = DummySite(mails_resource)
        d = web.get(request)

        def assert_response(_):
            verify(self.mail_service).attachment('some fake attachment id')

        d.addCallback(assert_response)
        return d

    @patch('leap.common.events.register')
    def test_render_POST_should_send_email_with_attachments(self, mock_register):
        request = DummyRequest(['/mails'])
        request.method = 'POST'
        content = mock()
        when(content).read().thenReturn('{"attachments": [{"ident": "some fake attachment id"}]}')
        when(self.mail_service).attachment('some fake attachment id').thenReturn(defer.succeed({"content": "some content"}))
        as_dictable = mock()
        when(as_dictable).as_dict().thenReturn({})
        when(self.mail_service).send_mail({"attachments": [{"ident": "some fake attachment id", "raw": "some content"}]})\
            .thenReturn(defer.succeed(as_dictable))
        request.content = content

        mails_resource = MailsResource(self.services_factory)
        mails_resource.isLeaf = True
        web = DummySite(mails_resource)
        d = web.get(request)

        def assert_response(_):
            verify(self.mail_service).attachment('some fake attachment id')
            verify(self.mail_service).send_mail({"attachments": [{"ident": "some fake attachment id", "raw": "some content"}]})

        d.addCallback(assert_response)
        return d