summaryrefslogtreecommitdiff
path: root/service/test/unit/resources/test_logout_resources.py
blob: 6d5fe33a356ee4f7f898488516f690903d8648c8 (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
#
# 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 mock import patch, MagicMock
from twisted.internet import defer
from twisted.trial import unittest
from twisted.web.error import UnsupportedMethod
from twisted.web.test.requesthelper import DummyRequest

from pixelated.resources.logout_resource import LogoutResource
from test.unit.resources import DummySite


class TestLogoutResource(unittest.TestCase):
    def setUp(self):
        self.services_factory = MagicMock()
        self.resource = LogoutResource(self.services_factory)
        self.services_factory.log_out_user.return_value = defer.succeed(None)
        self.web = DummySite(self.resource)

    @patch('twisted.web.util.redirectTo')
    def test_logout(self, mock_redirect):
        request = DummyRequest(['/logout'])
        request.method = 'POST'

        session = self.resource.get_session(request)
        session.expire = MagicMock()
        mock_redirect.return_value = 'some redirect response'

        d = self.web.get(request)

        def expire_session_and_redirect(_):
            session = self.resource.get_session(request)
            self.services_factory.destroy_session.assert_called_once_with(session.user_uuid)
            session.expire.assert_called_once_with()
            mock_redirect.assert_called_once_with('/login', request)

        d.addCallback(expire_session_and_redirect)
        return d

    def test_get_is_not_supported_for_logout(self):
        request = DummyRequest(['/logout'])
        request.method = 'GET'

        self.assertRaises(UnsupportedMethod, self.web.get, request)

    def test_errback_is_called(self):
        request = DummyRequest(['/logout'])
        request.method = 'POST'

        session = self.resource.get_session(request)
        exception = Exception('')
        session.expire = MagicMock(side_effect=exception)

        d = self.web.get(request)

        def assert_500_when_exception_is_thrown(_):
            self.assertEqual(500, request.responseCode)
            self.assertEqual('Something went wrong!', request.written[0])

        d.addCallback(assert_500_when_exception_is_thrown)
        return d