Add 'login_required' tests

This commit is contained in:
Alex Kuzmenko 2017-02-06 01:51:03 +02:00
parent 19d7ee7b06
commit a8ae6c951d
1 changed files with 50 additions and 1 deletions

View File

@ -1,7 +1,7 @@
import asyncio import asyncio
from aiohttp import web from aiohttp import web
from aiohttp_security import (remember, from aiohttp_security import (remember, login_required,
authorized_userid, permits, authorized_userid, permits,
AbstractAuthorizationPolicy) AbstractAuthorizationPolicy)
from aiohttp_security import setup as _setup from aiohttp_security import setup as _setup
@ -121,3 +121,52 @@ def test_permits_unauthorized(loop, test_client):
resp = yield from client.get('/') resp = yield from client.get('/')
assert 200 == resp.status assert 200 == resp.status
yield from resp.release() 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()