Convert test_dict_autz into pytest style
This commit is contained in:
parent
e29666cea9
commit
8f527777f3
|
@ -50,8 +50,11 @@ def create_server(loop, unused_port):
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def finish():
|
def finish():
|
||||||
|
if handler is not None:
|
||||||
yield from handler.finish_connections()
|
yield from handler.finish_connections()
|
||||||
|
if app is not None:
|
||||||
yield from app.finish()
|
yield from app.finish()
|
||||||
|
if srv is not None:
|
||||||
srv.close()
|
srv.close()
|
||||||
yield from srv.wait_closed()
|
yield from srv.wait_closed()
|
||||||
|
|
||||||
|
@ -105,6 +108,7 @@ def create_app_and_client(create_server, loop):
|
||||||
return app, client
|
return app, client
|
||||||
|
|
||||||
yield maker
|
yield maker
|
||||||
|
if client is not None:
|
||||||
client.close()
|
client.close()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import socket
|
import pytest
|
||||||
import unittest
|
|
||||||
|
|
||||||
import aiohttp
|
|
||||||
from aiohttp import web
|
from aiohttp import web
|
||||||
from aiohttp_security import (remember, setup,
|
from aiohttp_security import (remember,
|
||||||
authorized_userid, permits,
|
authorized_userid, permits,
|
||||||
AbstractAuthorizationPolicy)
|
AbstractAuthorizationPolicy)
|
||||||
|
from aiohttp_security import setup as _setup
|
||||||
from aiohttp_security.cookies_identity import CookiesIdentityPolicy
|
from aiohttp_security.cookies_identity import CookiesIdentityPolicy
|
||||||
|
|
||||||
|
|
||||||
|
@ -27,42 +26,8 @@ class Autz(AbstractAuthorizationPolicy):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
class TestCookiesIdentity(unittest.TestCase):
|
@pytest.mark.run_loop
|
||||||
|
def test_authorized_userid(create_app_and_client):
|
||||||
def setUp(self):
|
|
||||||
self.loop = asyncio.new_event_loop()
|
|
||||||
asyncio.set_event_loop(None)
|
|
||||||
self.client = aiohttp.ClientSession(loop=self.loop)
|
|
||||||
|
|
||||||
def tearDown(self):
|
|
||||||
self.client.close()
|
|
||||||
self.loop.run_until_complete(self.handler.finish_connections())
|
|
||||||
self.srv.close()
|
|
||||||
self.loop.run_until_complete(self.srv.wait_closed())
|
|
||||||
self.loop.close()
|
|
||||||
|
|
||||||
def find_unused_port(self):
|
|
||||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
||||||
s.bind(('127.0.0.1', 0))
|
|
||||||
port = s.getsockname()[1]
|
|
||||||
s.close()
|
|
||||||
return port
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
|
||||||
def create_server(self):
|
|
||||||
app = web.Application(loop=self.loop)
|
|
||||||
setup(app, CookiesIdentityPolicy(), Autz())
|
|
||||||
|
|
||||||
port = self.find_unused_port()
|
|
||||||
self.handler = app.make_handler(
|
|
||||||
debug=False, keep_alive_on=False)
|
|
||||||
srv = yield from self.loop.create_server(
|
|
||||||
self.handler, '127.0.0.1', port)
|
|
||||||
url = "http://127.0.0.1:{}".format(port)
|
|
||||||
self.srv = srv
|
|
||||||
return app, srv, url
|
|
||||||
|
|
||||||
def test_authorized_userid(self):
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def login(request):
|
def login(request):
|
||||||
|
@ -73,41 +38,40 @@ class TestCookiesIdentity(unittest.TestCase):
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def check(request):
|
def check(request):
|
||||||
userid = yield from authorized_userid(request)
|
userid = yield from authorized_userid(request)
|
||||||
self.assertEqual('Andrew', userid)
|
assert 'Andrew' == userid
|
||||||
return web.Response(text=userid)
|
return web.Response(text=userid)
|
||||||
|
|
||||||
@asyncio.coroutine
|
app, client = yield from create_app_and_client()
|
||||||
def go():
|
_setup(app, CookiesIdentityPolicy(), Autz())
|
||||||
app, srv, url = yield from self.create_server()
|
|
||||||
app.router.add_route('GET', '/', check)
|
app.router.add_route('GET', '/', check)
|
||||||
app.router.add_route('POST', '/login', login)
|
app.router.add_route('POST', '/login', login)
|
||||||
resp = yield from self.client.post(url+'/login')
|
|
||||||
self.assertEqual(200, resp.status)
|
resp = yield from client.post('/login')
|
||||||
|
assert 200 == resp.status
|
||||||
txt = yield from resp.text()
|
txt = yield from resp.text()
|
||||||
self.assertEqual('Andrew', txt)
|
assert 'Andrew' == txt
|
||||||
yield from resp.release()
|
yield from resp.release()
|
||||||
|
|
||||||
self.loop.run_until_complete(go())
|
|
||||||
|
|
||||||
def test_authorized_userid_not_authorized(self):
|
@pytest.mark.run_loop
|
||||||
|
def test_authorized_userid_not_authorized(create_app_and_client):
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def check(request):
|
def check(request):
|
||||||
userid = yield from authorized_userid(request)
|
userid = yield from authorized_userid(request)
|
||||||
self.assertIsNone(userid)
|
assert userid is None
|
||||||
return web.Response()
|
return web.Response()
|
||||||
|
|
||||||
@asyncio.coroutine
|
app, client = yield from create_app_and_client()
|
||||||
def go():
|
_setup(app, CookiesIdentityPolicy(), Autz())
|
||||||
app, srv, url = yield from self.create_server()
|
|
||||||
app.router.add_route('GET', '/', check)
|
app.router.add_route('GET', '/', check)
|
||||||
resp = yield from self.client.get(url+'/')
|
resp = yield from client.get('/')
|
||||||
self.assertEqual(200, resp.status)
|
assert 200 == resp.status
|
||||||
yield from resp.release()
|
yield from resp.release()
|
||||||
|
|
||||||
self.loop.run_until_complete(go())
|
|
||||||
|
|
||||||
def test_permits(self):
|
@pytest.mark.run_loop
|
||||||
|
def test_permits(create_app_and_client):
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def login(request):
|
def login(request):
|
||||||
|
@ -118,42 +82,38 @@ class TestCookiesIdentity(unittest.TestCase):
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def check(request):
|
def check(request):
|
||||||
ret = yield from permits(request, 'read')
|
ret = yield from permits(request, 'read')
|
||||||
self.assertTrue(ret)
|
assert ret
|
||||||
ret = yield from permits(request, 'write')
|
ret = yield from permits(request, 'write')
|
||||||
self.assertTrue(ret)
|
assert ret
|
||||||
ret = yield from permits(request, 'unknown')
|
ret = yield from permits(request, 'unknown')
|
||||||
self.assertFalse(ret)
|
assert not ret
|
||||||
return web.Response()
|
return web.Response()
|
||||||
|
|
||||||
@asyncio.coroutine
|
app, client = yield from create_app_and_client()
|
||||||
def go():
|
_setup(app, CookiesIdentityPolicy(), Autz())
|
||||||
app, srv, url = yield from self.create_server()
|
|
||||||
app.router.add_route('GET', '/', check)
|
app.router.add_route('GET', '/', check)
|
||||||
app.router.add_route('POST', '/login', login)
|
app.router.add_route('POST', '/login', login)
|
||||||
resp = yield from self.client.post(url+'/login')
|
resp = yield from client.post('/login')
|
||||||
self.assertEqual(200, resp.status)
|
assert 200 == resp.status
|
||||||
yield from resp.release()
|
yield from resp.release()
|
||||||
|
|
||||||
self.loop.run_until_complete(go())
|
|
||||||
|
|
||||||
def test_permits_unauthorized(self):
|
@pytest.mark.run_loop
|
||||||
|
def test_permits_unauthorized(create_app_and_client):
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def check(request):
|
def check(request):
|
||||||
ret = yield from permits(request, 'read')
|
ret = yield from permits(request, 'read')
|
||||||
self.assertFalse(ret)
|
assert not ret
|
||||||
ret = yield from permits(request, 'write')
|
ret = yield from permits(request, 'write')
|
||||||
self.assertFalse(ret)
|
assert not ret
|
||||||
ret = yield from permits(request, 'unknown')
|
ret = yield from permits(request, 'unknown')
|
||||||
self.assertFalse(ret)
|
assert not ret
|
||||||
return web.Response()
|
return web.Response()
|
||||||
|
|
||||||
@asyncio.coroutine
|
app, client = yield from create_app_and_client()
|
||||||
def go():
|
_setup(app, CookiesIdentityPolicy(), Autz())
|
||||||
app, srv, url = yield from self.create_server()
|
|
||||||
app.router.add_route('GET', '/', check)
|
app.router.add_route('GET', '/', check)
|
||||||
resp = yield from self.client.get(url+'/')
|
resp = yield from client.get('/')
|
||||||
self.assertEqual(200, resp.status)
|
assert 200 == resp.status
|
||||||
yield from resp.release()
|
yield from resp.release()
|
||||||
|
|
||||||
self.loop.run_until_complete(go())
|
|
||||||
|
|
Loading…
Reference in New Issue