From a8ae6c951d8652b85b22a63b6a877cab6dff438c Mon Sep 17 00:00:00 2001 From: Alex Kuzmenko Date: Mon, 6 Feb 2017 01:51:03 +0200 Subject: [PATCH] Add 'login_required' tests --- tests/test_dict_autz.py | 51 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/tests/test_dict_autz.py b/tests/test_dict_autz.py index e866dc9..e1470b1 100644 --- a/tests/test_dict_autz.py +++ b/tests/test_dict_autz.py @@ -1,7 +1,7 @@ import asyncio from aiohttp import web -from aiohttp_security import (remember, +from aiohttp_security import (remember, login_required, authorized_userid, permits, AbstractAuthorizationPolicy) from aiohttp_security import setup as _setup @@ -121,3 +121,52 @@ def test_permits_unauthorized(loop, test_client): resp = yield from client.get('/') assert 200 == resp.status yield from resp.release() + + +@asyncio.coroutine +def test_login_required(loop, test_client): + + @asyncio.coroutine + def login(request): + response = web.Response() + yield from remember(request, response, 'UserID') + return response + + @login_required('read') + @asyncio.coroutine + def check_read(request): + return web.Response() + + @login_required('write') + @asyncio.coroutine + def check_write(request): + return web.Response() + + @login_required('unknown') + @asyncio.coroutine + def check_unknown(request): + return web.Response() + + app = web.Application(loop=loop) + _setup(app, CookiesIdentityPolicy(), Autz()) + + app.router.add_get('/check_read', check_read) + app.router.add_get('/check_write', check_write) + app.router.add_get('/check_unknown', check_unknown) + app.router.add_post('/login', login) + + client = yield from test_client(app) + + resp = yield from client.post('/login') + assert 200 == resp.status + + resp = yield from client.get('/check_read') + assert 200 == resp.status + + resp = yield from client.get('/check_write') + assert 200 == resp.status + + resp = yield from client.get('/check_unknown') + assert 403 == resp.status + + yield from resp.release()