summaryrefslogtreecommitdiff
path: root/service/test/unit/resources/test_users_resource.py
blob: 4ca578ae2cb9e864f25f87c79e831d479dfae5be (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
from mockito import mock, when, verify
from twisted.trial import unittest
from twisted.web.test.requesthelper import DummyRequest

from pixelated.config.services import Services, ServicesFactory
from pixelated.resources.users import UsersResource
from test.unit.resources import DummySite


class TestUsersResource(unittest.TestCase):

    def setUp(self):
        self.services_factory = mock()
        self.resource = UsersResource(self.services_factory)
        self.web = DummySite(self.resource)

    def test_numbers_of_users_online(self):
        number_of_users_online = 6
        self.services_factory.online_sessions = lambda: number_of_users_online
        self.resource.is_admin = lambda _: True
        request = DummyRequest([''])

        d = self.web.get(request)

        def assert_users_count(_):
            self.assertEqual(200, request.code)
            self.assertEqual('{"count": %d}' % number_of_users_online, request.written[0])

        d.addCallback(assert_users_count)
        return d

    def test_numbers_of_users_online_is_only_available_only_for_admin(self):
        self.resource.is_admin = lambda _: False
        request = DummyRequest([''])
        d = self.web.get(request)

        def assert_is_forbidden(_):
            self.assertEqual(401, request.responseCode)
            self.assertEqual('Unauthorized!', request.written[0])

        d.addCallback(assert_is_forbidden)
        return d

    def test_is_admin_is_queried_from_leap_auth(self):
        leap_session = mock()
        auth = mock()
        auth.uuid = 'some_id1'
        leap_session.user_auth = auth
        leap_session.config = mock()
        services = Services(leap_session)
        service_factory = ServicesFactory(mock())
        service_factory.add_session('some_id1', services)

        when(auth).is_admin().thenReturn(True)
        request = mock()
        resource = UsersResource(service_factory)

        when(resource)._get_user_id_from_request(request).thenReturn('some_id1')

        self.assertTrue(resource.is_admin(request))
        verify(auth).is_admin()