summaryrefslogtreecommitdiff
path: root/service/test/unit/resources/test_mails_resource.py
blob: bdd15657b897f178afd00943fb65d4457afc50b0 (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
#  -*- 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/>.
from twisted.trial 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([])
        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.succeed(([], 0)))

        mails_resource = MailsResource(self.services_factory)
        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([])
        request.method = 'PUT'
        request.content = mock()
        when(request.content).read().thenReturn('{"attachments": [{"ident": "some fake attachment id"}]}')
        when(self.mail_service).attachment('some fake attachment id').thenReturn(defer.succeed({'content': mock()}))

        mails_resource = MailsResource(self.services_factory)
        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([])
        request.method = 'POST'
        request.content = mock()
        when(request.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))

        mails_resource = MailsResource(self.services_factory)
        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