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
|
2016-08-30 17:38:59 +00:00
|
|
|
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
|
2016-09-27 14:57:11 +00:00
|
|
|
def make_app(loop):
|
|
|
|
app = web.Application(loop=loop)
|
|
|
|
setup_session(app, SimpleCookieStorage())
|
|
|
|
setup_security(app, SessionIdentityPolicy(), Autz())
|
|
|
|
return app
|
2015-11-19 13:25:10 +00:00
|
|
|
|
|
|
|
|
2017-12-13 14:51:46 +00:00
|
|
|
async def test_remember(make_app, test_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'
|
|
|
|
return web.HTTPOk()
|
|
|
|
|
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)
|
2017-12-13 14:51:46 +00:00
|
|
|
client = await test_client(app)
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2017-12-13 14:51:46 +00:00
|
|
|
async def test_identify(make_app, test_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)
|
2017-12-13 14:51:46 +00:00
|
|
|
client = await test_client(app)
|
|
|
|
resp = await client.post('/')
|
2015-11-19 13:25:10 +00:00
|
|
|
assert 200 == resp.status
|
2016-08-30 17:38:59 +00:00
|
|
|
|
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
|
|
|
async def test_forget(make_app, test_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)
|
2015-11-19 13:25:10 +00:00
|
|
|
return web.HTTPOk(text=session.get('AIOHTTP_SECURITY', ''))
|
|
|
|
|
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')
|
2015-11-19 13:25:10 +00:00
|
|
|
return response
|
|
|
|
|
2017-12-13 14:51:46 +00:00
|
|
|
async def logout(request):
|
2016-08-30 17:38:59 +00:00
|
|
|
response = web.HTTPFound('/')
|
2017-12-13 14:51:46 +00:00
|
|
|
await forget(request, response)
|
2015-11-19 13:25:10 +00:00
|
|
|
return 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', '/', index)
|
|
|
|
app.router.add_route('POST', '/login', login)
|
|
|
|
app.router.add_route('POST', '/logout', logout)
|
2016-08-30 17:38:59 +00:00
|
|
|
|
2017-12-13 14:51:46 +00:00
|
|
|
client = await test_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
|
2016-08-30 17:38:59 +00:00
|
|
|
|
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
|