예제 #1
0
    def _middleware(self, acl_callback):
        """Returns the middlewares used in the test"""
        if acl_callback:
            return [
                session_middleware(self.storage),
                auth_middleware(self.auth),
                acl_middleware(acl_callback)
            ]

        return [session_middleware(self.storage), auth_middleware(self.auth)]
예제 #2
0
    def _middleware(self, acl_callback):
        """Returns the middlewares used in the test"""
        if acl_callback:
            return [
                session_middleware(self.storage),
                auth_middleware(self.auth),
                acl_middleware(acl_callback)]

        return [
            session_middleware(self.storage),
            auth_middleware(self.auth)]
예제 #3
0
    async def test_middleware_installed_no_session(self):
        middlewares = [
            session_middleware(SimpleCookieStorage()),
            auth_middleware(auth.SessionTktAuthentication(urandom(16), 15))]

        request = await make_request('GET', '/', middlewares)
        user_id = await auth.get_auth(request)
        self.assertIsNone(user_id)
예제 #4
0
    async def test_middleware_stores_auth_in_cookie(self):
        secret = b'01234567890abcdef'
        auth_ = auth.CookieTktAuthentication(secret, 15, cookie_name='auth')
        middlewares = [auth_middleware(auth_)]

        request = await make_request('GET', '/', middlewares)
        await auth.remember(request, 'some_user')
        response = await make_response(request, middlewares)
        self.assertTrue(auth_.cookie_name in response.cookies)
예제 #5
0
    async def test_middleware_installed_no_session(self):
        middlewares = [
            session_middleware(SimpleCookieStorage()),
            auth_middleware(auth.SessionTktAuthentication(urandom(16), 15))
        ]

        request = await make_request('GET', '/', middlewares)
        user_id = await auth.get_auth(request)
        self.assertIsNone(user_id)
예제 #6
0
    async def test_middleware_stores_auth_in_cookie(self):
        secret = b'01234567890abcdef'
        auth_ = auth.CookieTktAuthentication(secret, 15, cookie_name='auth')
        middlewares = [
            auth_middleware(auth_)]

        request = await make_request('GET', '/', middlewares)
        await auth.remember(request, 'some_user')
        response = await make_response(request, middlewares)
        self.assertTrue(auth_.cookie_name in response.cookies)
예제 #7
0
    async def test_middleware_stores_auth_in_session(self):
        secret = b'01234567890abcdef'
        storage = SimpleCookieStorage()
        auth_ = auth.SessionTktAuthentication(secret, 15, cookie_name='auth')
        middlewares = [session_middleware(storage), auth_middleware(auth_)]

        request = await make_request('GET', '/', middlewares)
        await auth.remember(request, 'some_user')
        response = await make_response(request, middlewares)
        self.assertTrue(auth_.cookie_name in \
            response.cookies.get(storage.cookie_name).value)
예제 #8
0
    async def test_middleware_gets_auth_from_session(self):
        secret = b'01234567890abcdef'
        storage = SimpleCookieStorage()
        auth_ = auth.SessionTktAuthentication(secret, 15, cookie_name='auth')
        middlewares = [session_middleware(storage), auth_middleware(auth_)]

        session_data = make_auth_session(secret, 'some_user',
                                         auth_.cookie_name)
        request = await make_request('GET', '/', middlewares, \
            [(storage.cookie_name, json.dumps(session_data))])

        user_id = await auth.get_auth(request)
        self.assertEqual(user_id, 'some_user')
예제 #9
0
    async def test_middleware_stores_auth_in_session(self):
        secret = b'01234567890abcdef'
        storage = SimpleCookieStorage()
        auth_ = auth.SessionTktAuthentication(secret, 15, cookie_name='auth')
        middlewares = [
            session_middleware(storage),
            auth_middleware(auth_)]

        request = await make_request('GET', '/', middlewares)
        await auth.remember(request, 'some_user')
        response = await make_response(request, middlewares)
        self.assertTrue(auth_.cookie_name in \
            response.cookies.get(storage.cookie_name).value)
예제 #10
0
    async def test_middleware_gets_auth_from_cookie(self):
        secret = b'01234567890abcdef'
        auth_ = auth.CookieTktAuthentication(secret, 15, 2, cookie_name='auth')
        middlewares = [auth_middleware(auth_)]

        session_data = TicketFactory(secret).new('some_user')
        request = await make_request('GET', '/', middlewares, \
            [(auth_.cookie_name, session_data)])

        user_id = await auth.get_auth(request)
        self.assertEqual(user_id, 'some_user')

        response = await make_response(request, middlewares)
        self.assertFalse(auth_.cookie_name in response.cookies)
예제 #11
0
    async def test_middleware_gets_auth_from_session(self):
        secret = b'01234567890abcdef'
        storage = SimpleCookieStorage()
        auth_ = auth.SessionTktAuthentication(secret, 15, cookie_name='auth')
        middlewares = [
            session_middleware(storage),
            auth_middleware(auth_)]

        session_data = make_auth_session(secret, 'some_user', auth_.cookie_name)
        request = await make_request('GET', '/', middlewares, \
            [(storage.cookie_name, json.dumps(session_data))])

        user_id = await auth.get_auth(request)
        self.assertEqual(user_id, 'some_user')
예제 #12
0
    async def test_middleware_gets_auth_from_cookie(self):
        secret = b'01234567890abcdef'
        auth_ = auth.CookieTktAuthentication(secret, 15, 2, cookie_name='auth')
        middlewares = [
            auth_middleware(auth_)]

        session_data = TicketFactory(secret).new('some_user')
        request = await make_request('GET', '/', middlewares, \
            [(auth_.cookie_name, session_data)])

        user_id = await auth.get_auth(request)
        self.assertEqual(user_id, 'some_user')

        response = await make_response(request, middlewares)
        self.assertFalse(auth_.cookie_name in response.cookies)
예제 #13
0
    async def test_middleware_reissues_ticket_auth(self):
        secret = b'01234567890abcdef'
        auth_ = auth.CookieTktAuthentication(secret, 15, 0, cookie_name='auth')
        middlewares = [auth_middleware(auth_)]

        valid_until = time.time() + 15
        session_data = TicketFactory(secret).new('some_user',
                                                 valid_until=valid_until)
        request = await make_request('GET', '/', middlewares, \
            [(auth_.cookie_name, session_data)])

        user_id = await auth.get_auth(request)
        self.assertEqual(user_id, 'some_user')

        response = await make_response(request, middlewares)
        self.assertTrue(auth_.cookie_name in response.cookies)
        self.assertNotEqual(response.cookies[auth_.cookie_name], session_data)
예제 #14
0
    async def test_middleware_doesnt_reissue_on_bad_response(self):
        secret = b'01234567890abcdef'
        auth_ = auth.CookieTktAuthentication(secret, 15, 0, cookie_name='auth')
        middlewares = [
            auth_middleware(auth_)]

        valid_until = time.time() + 15
        session_data = TicketFactory(secret).new('some_user',
                                                 valid_until=valid_until)
        request = await make_request('GET', '/', middlewares, \
            [(auth_.cookie_name, session_data)])

        user_id = await auth.get_auth(request)
        self.assertEqual(user_id, 'some_user')

        response = await make_response(request, middlewares, web.Response(status=400))
        self.assertFalse(auth_.cookie_name in response.cookies)