summaryrefslogtreecommitdiff
path: root/service/test/unit/resources/test_auth.py
blob: ac2529ddd95506ecc419f748cc6682171b672846 (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
from mockito import mock, when, any as ANY
from pixelated.resources.auth import PixelatedAuthSessionWrapper
from pixelated.resources.login_resource import LoginResource
from pixelated.resources.root_resource import RootResource
from test.unit.resources import DummySite
from twisted.cred.checkers import ANONYMOUS
from twisted.internet.defer import succeed
from twisted.trial import unittest
from twisted.web.resource import IResource
from twisted.web.test.requesthelper import DummyRequest


class TestPixelatedAuthSessionWrapper(unittest.TestCase):

    def setUp(self):
        self.portal_mock = mock()
        self.root_resource_mock = mock()
        self.anonymous_resource_mock = mock()
        credential_factories_mock = mock()

        self.session_wrapper = PixelatedAuthSessionWrapper(self.portal_mock, self.root_resource_mock, self.anonymous_resource_mock, credential_factories_mock)

    def test_should_use_login_resource_when_the_user_is_not_logged_in(self):
        request = DummyRequest([])
        request.prepath = ['']
        request.path = '/'
        when(self.portal_mock).login(ANY(), None, IResource).thenReturn(succeed((IResource, ANONYMOUS, lambda: None)))

        deferred_resource = self.session_wrapper.getChildWithDefault('/', request)
        d = deferred_resource.d

        def assert_anonymous_resource(resource):
            self.assertIs(resource, self.anonymous_resource_mock)

        d.addCallback(assert_anonymous_resource)
        return d