Create working demo (#5)

* Add docs example and update demo source code

* Fail with debug

* Fix tests

* Update sql scripts

* Update docs

* Add checking for the password

* Update documentation with launch/usage example

* Launch flake8 instead of pep8 and pyflakes
This commit is contained in:
Misha Behersky
2016-08-30 20:38:59 +03:00
committed by Andrew Svetlov
parent fec22f971c
commit 820dcc8d93
17 changed files with 415 additions and 60 deletions

View File

@@ -1,8 +1,10 @@
import aiohttp
import asyncio
import gc
import pytest
import socket
import asyncio
import pytest
import aiohttp
from aiohttp import web
@@ -79,7 +81,8 @@ class Client:
while path.startswith('/'):
path = path[1:]
url = self._url + path
return self._session.get(url, **kwargs)
resp = self._session.get(url, **kwargs)
return resp
def post(self, path, **kwargs):
while path.startswith('/'):
@@ -97,6 +100,7 @@ class Client:
@pytest.yield_fixture
def create_app_and_client(create_server, loop):
client = None
cookie_jar = aiohttp.CookieJar(loop=loop, unsafe=True)
@asyncio.coroutine
def maker(*, server_params=None, client_params=None):
@@ -108,7 +112,11 @@ def create_app_and_client(create_server, loop):
app, url = yield from create_server(**server_params)
if client_params is None:
client_params = {}
client = Client(aiohttp.ClientSession(loop=loop, **client_params), url)
client = Client(
aiohttp.ClientSession(loop=loop, cookie_jar=cookie_jar),
url
)
return app, client
yield maker

View File

@@ -34,7 +34,7 @@ def test_remember(create_app_and_client):
app.router.add_route('GET', '/', handler)
resp = yield from client.get('/')
assert 200 == resp.status
assert 'Andrew' == client.cookies['AIOHTTP_SECURITY'].value
assert 'Andrew' == resp.cookies['AIOHTTP_SECURITY'].value
yield from resp.release()
@@ -98,5 +98,6 @@ def test_forget(create_app_and_client):
resp = yield from client.post('/logout')
assert 200 == resp.status
assert resp.url.endswith('/')
assert '' == client.cookies['AIOHTTP_SECURITY'].value
with pytest.raises(KeyError):
_ = client.cookies['AIOHTTP_SECURITY'] # noqa
yield from resp.release()

View File

@@ -7,8 +7,8 @@ from aiohttp_security import (remember, forget,
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, session_middleware,
get_session)
from aiohttp_session import SimpleCookieStorage, get_session
from aiohttp_session import setup as setup_session
class Autz(AbstractAuthorizationPolicy):
@@ -27,7 +27,7 @@ def create_app_and_client2(create_app_and_client):
@asyncio.coroutine
def maker(*args, **kwargs):
app, client = yield from create_app_and_client(*args, **kwargs)
app.middlewares.append(session_middleware(SimpleCookieStorage()))
setup_session(app, SimpleCookieStorage())
setup_security(app, SessionIdentityPolicy(), Autz())
return app, client
return maker
@@ -82,6 +82,7 @@ def test_identify(create_app_and_client2):
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()
@@ -103,7 +104,7 @@ def test_forget(create_app_and_client2):
@asyncio.coroutine
def logout(request):
response = web.HTTPFound(location='/')
response = web.HTTPFound('/')
yield from forget(request, response)
return response
@@ -111,12 +112,14 @@ def test_forget(create_app_and_client2):
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('/')
txt = yield from resp.text()
assert 'Andrew' == txt
yield from resp.release()
resp = yield from client.post('/logout')
assert 200 == resp.status
assert resp.url.endswith('/')