<commit_before>from mockito import mock, when, any as ANY
from pixelated.resources.auth import SessionChecker, PixelatedRealm, PixelatedAuthSessionWrapper
from pixelated.resources.login_resource import LoginResource
from pixelated.resources.root_resource import RootResource
from test.unit.resources import DummySite
from twisted.cred import error
from twisted.cred.checkers import ANONYMOUS, AllowAnonymousAccess
from twisted.cred.portal import Portal
from twisted.internet.defer import succeed, fail
from twisted.python import failure
from twisted.trial import unittest
from twisted.web._auth.wrapper import UnauthorizedResource
from twisted.web.resource import IResource, getChildForRequest
from twisted.web.test.requesthelper import DummyRequest


class TestPixelatedRealm(unittest.TestCase):

    def setUp(self):
        self.authenticated_root_resource = mock()
        self.public_root_resource = mock()
        self.realm = PixelatedRealm(self.authenticated_root_resource, self.public_root_resource)

    def test_anonymous_user_gets_anonymous_resource(self):
        interface, avatar, logout_handler = self.realm.requestAvatar(ANONYMOUS, None, IResource)
        self.assertEqual(interface, IResource)
        self.assertIs(avatar, self.public_root_resource)

    def test_authenticated_user_gets_root_resource(self):
        interface, avatar, logout_handler = self.realm.requestAvatar('username', None, IResource)
        self.assertEqual(interface, IResource)
        self.assertIs(avatar, self.authenticated_root_resource)


class TestPixelatedAuthSessionWrapper(unittest.TestCase):

    def setUp(self):
        self.realm_mock = mock()
        services_factory = mock()
        session_checker = SessionChecker(services_factory)
        self.portal = Portal(self.realm_mock, [session_checker, AllowAnonymousAccess()])
        self.user_uuid_mock = mock()
        self.root_resource = RootResource(services_factory)
        self.anonymous_resource_mock = mock()

        self.session_wrapper = PixelatedAuthSessionWrapper(self.portal, self.root_resource, self.anonymous_resource_mock)
        self.request = DummyRequest([])
        self.request.prepath = ['']
        self.request.path = '/'

    def test_should_proxy_to_login_resource_when_the_user_is_not_logged_in(self):
        when(self.realm_mock).requestAvatar(ANONYMOUS, None, IResource).thenReturn((IResource, self.anonymous_resource_mock, lambda: None))

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

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

        d.addCallback(assert_anonymous_resource)
        return d

    def test_should_proxy_to_root_resource_when_the_user_is_logged_in(self):
        when(self.realm_mock).requestAvatar(ANY(), None, IResource).thenReturn((IResource, self.root_resource, lambda: None))

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

        def assert_root_resource(resource):
            self.assertIs(resource, self.root_resource)

        d.addCallback(assert_root_resource)
        return d

    def test_should_X_when_unauthenticated_user_requests_non_public_resource(self):
        when(self.realm_mock).requestAvatar(ANONYMOUS, None, IResource).thenReturn((IResource, self.anonymous_resource_mock, lambda: None))

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

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

        d.addCallback(assert_unauthorized_resource)
        return d<commit_msg>test root resource delegation on a bit higher level<commit_after>from mockito import mock, when, any as ANY
from pixelated.resources.auth import SessionChecker, PixelatedRealm, PixelatedAuthSessionWrapper
from pixelated.resources.login_resource import LoginResource
from pixelated.resources.root_resource import PublicRootResource, RootResource
from test.unit.resources import DummySite
from twisted.cred import error
from twisted.cred.checkers import ANONYMOUS, AllowAnonymousAccess
from twisted.cred.portal import Portal
from twisted.internet.defer import succeed, fail
from twisted.python import failure
from twisted.trial import unittest
from twisted.web._auth.wrapper import UnauthorizedResource
from twisted.web.resource import IResource, getChildForRequest
from twisted.web.test.requesthelper import DummyRequest


class TestPixelatedRealm(unittest.TestCase):

    def setUp(self):
        self.authenticated_root_resource = mock()
        self.public_root_resource = mock()
        self.realm = PixelatedRealm(self.authenticated_root_resource, self.public_root_resource)

    def test_anonymous_user_gets_anonymous_resource(self):
        interface, avatar, logout_handler = self.realm.requestAvatar(ANONYMOUS, None, IResource)
        self.assertEqual(interface, IResource)
        self.assertIs(avatar, self.public_root_resource)

    def test_authenticated_user_gets_root_resource(self):
        interface, avatar, logout_handler = self.realm.requestAvatar('username', None, IResource)
        self.assertEqual(interface, IResource)
        self.assertIs(avatar, self.authenticated_root_resource)


class TestPixelatedAuthSessionWrapper(unittest.TestCase):

    def setUp(self):
        self.realm_mock = mock()
        services_factory = mock()
        session_checker = SessionChecker(services_factory)
        self.portal = Portal(self.realm_mock, [session_checker, AllowAnonymousAccess()])
        self.user_uuid_mock = mock()
        self.root_resource = RootResource(services_factory)
        self.anonymous_resource = PublicRootResource(services_factory)

        self.session_wrapper = PixelatedAuthSessionWrapper(self.portal, self.root_resource, self.anonymous_resource)
        self.request = DummyRequest([])
        self.request.prepath = ['']
        self.request.path = '/'

    def test_root_url_should_delegate_to_public_root_resource_for_unauthenticated_user(self):
        when(self.realm_mock).requestAvatar(ANONYMOUS, None, IResource).thenReturn((IResource, self.anonymous_resource, lambda: None))
        request = DummyRequest([''])
        deferred_resource = getChildForRequest(self.session_wrapper, request)
        d = deferred_resource.d

        def assert_public_root_resource(resource):
            self.assertIsInstance(resource, PublicRootResource)

        return d.addCallback(assert_public_root_resource)

    def test_root_url_should_delegate_to_protected_root_resource_for_authenticated_user(self):
        when(self.realm_mock).requestAvatar(ANY(), None, IResource).thenReturn((IResource, self.root_resource, lambda: None))
        request = DummyRequest([''])
        deferred_resource = getChildForRequest(self.session_wrapper, request)
        d = deferred_resource.d

        def assert_protected_root_resource(resource):
            self.assertIsInstance(resource, RootResource)

        return d.addCallback(assert_protected_root_resource)
{'fraction_non_alphanumeric': 0.0777824326211423, 'fraction_numerical': 0.0, 'mean_word_length': 4.564879564879565, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6802521', 'n_tokens_mistral': 2033, 'n_tokens_neox': 1965, 'n_words': 366}