aiohttp-security/tests/test_session_identity.py

111 lines
3.0 KiB
Python
Raw Permalink Normal View History

2015-11-19 13:25:10 +00:00
import pytest
from aiohttp import web
from aiohttp_security import (remember, forget,
AbstractAuthorizationPolicy)
from aiohttp_security import setup as setup_security
from aiohttp_security.session_identity import SessionIdentityPolicy
from aiohttp_security.api import IDENTITY_KEY
from aiohttp_session import SimpleCookieStorage, get_session
from aiohttp_session import setup as setup_session
2015-11-19 13:25:10 +00:00
class Autz(AbstractAuthorizationPolicy):
2017-12-13 14:51:46 +00:00
async def permits(self, identity, permission, context=None):
2015-11-19 13:25:10 +00:00
pass
2017-12-13 14:51:46 +00:00
async def authorized_userid(self, identity):
2015-11-19 13:25:10 +00:00
pass
@pytest.fixture
2018-09-06 10:06:55 +00:00
def make_app():
app = web.Application()
2016-09-27 14:57:11 +00:00
setup_session(app, SimpleCookieStorage())
setup_security(app, SessionIdentityPolicy(), Autz())
return app
2015-11-19 13:25:10 +00:00
2018-09-06 10:06:55 +00:00
async def test_remember(make_app, aiohttp_client):
2015-11-19 13:25:10 +00:00
2017-12-13 14:51:46 +00:00
async def handler(request):
2015-11-19 13:25:10 +00:00
response = web.Response()
2017-12-13 14:51:46 +00:00
await remember(request, response, 'Andrew')
2015-11-19 13:25:10 +00:00
return response
2017-12-13 14:51:46 +00:00
async def check(request):
session = await get_session(request)
2015-11-19 13:25:10 +00:00
assert session['AIOHTTP_SECURITY'] == 'Andrew'
2018-09-06 10:06:55 +00:00
return web.Response()
2015-11-19 13:25:10 +00:00
2016-09-27 14:57:11 +00:00
app = make_app()
2015-11-19 13:25:10 +00:00
app.router.add_route('GET', '/', handler)
app.router.add_route('GET', '/check', check)
2018-09-06 10:06:55 +00:00
client = await aiohttp_client(app)
2017-12-13 14:51:46 +00:00
resp = await client.get('/')
2015-11-19 13:25:10 +00:00
assert 200 == resp.status
2017-12-13 14:51:46 +00:00
resp = await client.get('/check')
2015-11-19 13:25:10 +00:00
assert 200 == resp.status
2018-09-06 10:06:55 +00:00
async def test_identify(make_app, aiohttp_client):
2015-11-19 13:25:10 +00:00
2017-12-13 14:51:46 +00:00
async def create(request):
2015-11-19 13:25:10 +00:00
response = web.Response()
2017-12-13 14:51:46 +00:00
await remember(request, response, 'Andrew')
2015-11-19 13:25:10 +00:00
return response
2017-12-13 14:51:46 +00:00
async def check(request):
2015-11-19 13:25:10 +00:00
policy = request.app[IDENTITY_KEY]
2017-12-13 14:51:46 +00:00
user_id = await policy.identify(request)
2015-11-19 13:25:10 +00:00
assert 'Andrew' == user_id
return web.Response()
2016-09-27 14:57:11 +00:00
app = make_app()
2015-11-19 13:25:10 +00:00
app.router.add_route('GET', '/', check)
app.router.add_route('POST', '/', create)
2018-09-06 10:06:55 +00:00
client = await aiohttp_client(app)
2017-12-13 14:51:46 +00:00
resp = await client.post('/')
2015-11-19 13:25:10 +00:00
assert 200 == resp.status
2017-12-13 14:51:46 +00:00
resp = await client.get('/')
2015-11-19 13:25:10 +00:00
assert 200 == resp.status
2018-09-06 10:06:55 +00:00
async def test_forget(make_app, aiohttp_client):
2015-11-19 13:25:10 +00:00
2017-12-13 14:51:46 +00:00
async def index(request):
session = await get_session(request)
2018-09-06 10:06:55 +00:00
return web.Response(text=session.get('AIOHTTP_SECURITY', ''))
2015-11-19 13:25:10 +00:00
2017-12-13 14:51:46 +00:00
async def login(request):
2015-11-19 13:25:10 +00:00
response = web.HTTPFound(location='/')
2017-12-13 14:51:46 +00:00
await remember(request, response, 'Andrew')
2018-09-06 10:06:55 +00:00
raise response
2015-11-19 13:25:10 +00:00
2017-12-13 14:51:46 +00:00
async def logout(request):
response = web.HTTPFound('/')
2017-12-13 14:51:46 +00:00
await forget(request, response)
2018-09-06 10:06:55 +00:00
raise response
2015-11-19 13:25:10 +00:00
2016-09-27 14:57:11 +00:00
app = make_app()
2015-11-19 13:25:10 +00:00
app.router.add_route('GET', '/', index)
app.router.add_route('POST', '/login', login)
app.router.add_route('POST', '/logout', logout)
2018-09-06 10:06:55 +00:00
client = await aiohttp_client(app)
2016-09-27 14:57:11 +00:00
2017-12-13 14:51:46 +00:00
resp = await client.post('/login')
2015-11-19 13:25:10 +00:00
assert 200 == resp.status
2017-03-21 22:10:14 +00:00
assert str(resp.url).endswith('/')
2017-12-13 14:51:46 +00:00
txt = await resp.text()
2015-11-19 13:25:10 +00:00
assert 'Andrew' == txt
2017-12-13 14:51:46 +00:00
resp = await client.post('/logout')
2015-11-19 13:25:10 +00:00
assert 200 == resp.status
2017-03-21 22:10:14 +00:00
assert str(resp.url).endswith('/')
2017-12-13 14:51:46 +00:00
txt = await resp.text()
2015-11-19 13:25:10 +00:00
assert '' == txt