summaryrefslogtreecommitdiff
path: root/service/test/unit/resources/test_attachments_resource.py
blob: 0424c4816069ee701cd6736dfee503e16b66e8c6 (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
import json
import unittest
import logging

from mock import patch, MagicMock
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.attachments_resource import AttachmentsResource
from test.unit.resources import DummySite

logging.basicConfig(level=logging.CRITICAL)
logging.getLogger('pixelated.resources.attachments_resource').addHandler(logging.NullHandler())


class AttachmentsResourceTest(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_factory._services_by_user = {'someuserid': self.mail_service}
        when(self.services_factory).services(ANY()).thenReturn(self.services)

        self.mails_resource = AttachmentsResource(self.services_factory)
        self.mails_resource.isLeaf = True
        self.web = DummySite(self.mails_resource)

    @patch('cgi.FieldStorage')
    def test_post_new_attachment(self, mock_fields):
        request = DummyRequest(['/attachment'])
        request.method = 'POST'
        request.content = 'mocked'
        attachment_id = 'B5B4ED80AC3B894523D72E375DACAA2FC6606C18EDF680FE95903086C8B5E14A'
        _file = MagicMock()
        _file.value = 'some mocked value'
        _file.type = 'some mocked type'
        _file.filename = 'filename.txt'
        mock_fields.return_value = {'attachment': _file}
        when(self.mail_service).save_attachment('some mocked value', 'some mocked type').thenReturn(defer.succeed(attachment_id))

        d = self.web.get(request)

        def assert_response(_):
            self.assertEqual(201, request.code)
            self.assertEqual('/attachment/%s' % attachment_id, request.responseHeaders.getRawHeaders("location")[0])
            response_json = {'ident': attachment_id, 'content-type': 'some mocked type',
                             'name': 'filename.txt', 'size': 17, 'encoding': 'base64'}
            self.assertEqual(response_json, json.loads(request.written[0]))
            verify(self.mail_service).save_attachment('some mocked value', 'some mocked type')

        d.addCallback(assert_response)
        return d

    @patch('cgi.FieldStorage')
    def test_post_attachment_fails(self, mock_fields):
        request = DummyRequest(['/attachment'])
        request.method = 'POST'
        request.content = 'mocked'

        _file = MagicMock()
        _file.value = 'some mocked value'
        _file.type = 'some mocked type'
        mock_fields.return_value = {'attachment': _file}
        when(self.mail_service).save_attachment('some mocked value', 'some mocked type').thenReturn(defer.fail(Exception))

        d = self.web.get(request)

        def assert_response(_):
            self.assertEqual(500, request.code)
            self.assertFalse(request.responseHeaders.hasHeader('Location'.lower()))
            self.assertIn("message", json.loads(request.written[0]))
            verify(self.mail_service).save_attachment('some mocked value', 'some mocked type')

        d.addCallback(assert_response)
        return d