2015-07-08 17:30:24 +00:00
|
|
|
import asyncio
|
2015-11-19 12:36:15 +00:00
|
|
|
import pytest
|
2015-07-08 17:30:24 +00:00
|
|
|
|
|
|
|
from aiohttp import web
|
2015-11-19 12:36:15 +00:00
|
|
|
from aiohttp_security import (remember, forget,
|
2015-07-08 17:30:24 +00:00
|
|
|
AbstractAuthorizationPolicy)
|
2015-11-19 12:36:15 +00:00
|
|
|
from aiohttp_security import setup as _setup
|
2015-07-08 17:30:24 +00:00
|
|
|
from aiohttp_security.cookies_identity import CookiesIdentityPolicy
|
|
|
|
from aiohttp_security.api import IDENTITY_KEY
|
|
|
|
|
|
|
|
|
|
|
|
class Autz(AbstractAuthorizationPolicy):
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def permits(self, identity, permission, context=None):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def authorized_userid(self, identity):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-11-19 12:36:15 +00:00
|
|
|
@pytest.mark.run_loop
|
|
|
|
def test_remember(create_app_and_client):
|
2015-07-08 17:30:24 +00:00
|
|
|
|
2015-11-19 12:36:15 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def handler(request):
|
|
|
|
response = web.Response()
|
|
|
|
yield from remember(request, response, 'Andrew')
|
|
|
|
return response
|
|
|
|
|
|
|
|
app, client = yield from create_app_and_client()
|
|
|
|
_setup(app, CookiesIdentityPolicy(), Autz())
|
|
|
|
app.router.add_route('GET', '/', handler)
|
|
|
|
resp = yield from client.get('/')
|
|
|
|
assert 200 == resp.status
|
2016-08-30 17:38:59 +00:00
|
|
|
assert 'Andrew' == resp.cookies['AIOHTTP_SECURITY'].value
|
2015-11-19 12:36:15 +00:00
|
|
|
yield from resp.release()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.run_loop
|
|
|
|
def test_identify(create_app_and_client):
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def create(request):
|
|
|
|
response = web.Response()
|
|
|
|
yield from remember(request, response, 'Andrew')
|
|
|
|
return response
|
2015-07-08 17:30:24 +00:00
|
|
|
|
2015-11-19 12:36:15 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def check(request):
|
|
|
|
policy = request.app[IDENTITY_KEY]
|
|
|
|
user_id = yield from policy.identify(request)
|
|
|
|
assert 'Andrew' == user_id
|
|
|
|
return web.Response()
|
|
|
|
|
|
|
|
app, client = yield from create_app_and_client()
|
|
|
|
_setup(app, CookiesIdentityPolicy(), Autz())
|
|
|
|
app.router.add_route('GET', '/', check)
|
|
|
|
app.router.add_route('POST', '/', create)
|
|
|
|
resp = yield from client.post('/')
|
|
|
|
assert 200 == resp.status
|
|
|
|
yield from resp.release()
|
|
|
|
resp = yield from client.get('/')
|
|
|
|
assert 200 == resp.status
|
|
|
|
yield from resp.release()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.run_loop
|
|
|
|
def test_forget(create_app_and_client):
|
2015-07-08 17:30:24 +00:00
|
|
|
|
2015-11-19 12:36:15 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def index(request):
|
|
|
|
return web.Response()
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def login(request):
|
|
|
|
response = web.HTTPFound(location='/')
|
|
|
|
yield from remember(request, response, 'Andrew')
|
|
|
|
return response
|
2015-07-08 17:30:24 +00:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
2015-11-19 12:36:15 +00:00
|
|
|
def logout(request):
|
|
|
|
response = web.HTTPFound(location='/')
|
|
|
|
yield from forget(request, response)
|
|
|
|
return response
|
|
|
|
|
|
|
|
app, client = yield from create_app_and_client()
|
|
|
|
_setup(app, CookiesIdentityPolicy(), Autz())
|
|
|
|
app.router.add_route('GET', '/', index)
|
|
|
|
app.router.add_route('POST', '/login', login)
|
|
|
|
app.router.add_route('POST', '/logout', logout)
|
|
|
|
resp = yield from client.post('/login')
|
|
|
|
assert 200 == resp.status
|
|
|
|
assert resp.url.endswith('/')
|
|
|
|
assert 'Andrew' == client.cookies['AIOHTTP_SECURITY'].value
|
|
|
|
yield from resp.release()
|
|
|
|
resp = yield from client.post('/logout')
|
|
|
|
assert 200 == resp.status
|
|
|
|
assert resp.url.endswith('/')
|
2016-08-30 17:38:59 +00:00
|
|
|
with pytest.raises(KeyError):
|
|
|
|
_ = client.cookies['AIOHTTP_SECURITY'] # noqa
|
2015-11-19 12:36:15 +00:00
|
|
|
yield from resp.release()
|