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
|
||||
def finish():
|
||||
if handler is not None:
|
||||
yield from handler.finish_connections()
|
||||
if app is not None:
|
||||
yield from app.finish()
|
||||
if srv is not None:
|
||||
srv.close()
|
||||
yield from srv.wait_closed()
|
||||
|
||||
|
@ -105,6 +108,7 @@ def create_app_and_client(create_server, loop):
|
|||
return app, client
|
||||
|
||||
yield maker
|
||||
if client is not None:
|
||||
client.close()
|
||||
|
||||
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
import asyncio
|
||||
import socket
|
||||
import unittest
|
||||
import pytest
|
||||
|
||||
import aiohttp
|
||||
from aiohttp import web
|
||||
from aiohttp_security import (remember, setup,
|
||||
from aiohttp_security import (remember,
|
||||
authorized_userid, permits,
|
||||
AbstractAuthorizationPolicy)
|
||||
from aiohttp_security import setup as _setup
|
||||
from aiohttp_security.cookies_identity import CookiesIdentityPolicy
|
||||
|
||||
|
||||
|
@ -27,42 +26,8 @@ class Autz(AbstractAuthorizationPolicy):
|
|||
return None
|
||||
|
||||
|
||||
class TestCookiesIdentity(unittest.TestCase):
|
||||
|
||||
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):
|
||||
@pytest.mark.run_loop
|
||||
def test_authorized_userid(create_app_and_client):
|
||||
|
||||
@asyncio.coroutine
|
||||
def login(request):
|
||||
|
@ -73,41 +38,40 @@ class TestCookiesIdentity(unittest.TestCase):
|
|||
@asyncio.coroutine
|
||||
def check(request):
|
||||
userid = yield from authorized_userid(request)
|
||||
self.assertEqual('Andrew', userid)
|
||||
assert 'Andrew' == userid
|
||||
return web.Response(text=userid)
|
||||
|
||||
@asyncio.coroutine
|
||||
def go():
|
||||
app, srv, url = yield from self.create_server()
|
||||
app, client = yield from create_app_and_client()
|
||||
_setup(app, CookiesIdentityPolicy(), Autz())
|
||||
app.router.add_route('GET', '/', check)
|
||||
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()
|
||||
self.assertEqual('Andrew', txt)
|
||||
assert 'Andrew' == txt
|
||||
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
|
||||
def check(request):
|
||||
userid = yield from authorized_userid(request)
|
||||
self.assertIsNone(userid)
|
||||
assert userid is None
|
||||
return web.Response()
|
||||
|
||||
@asyncio.coroutine
|
||||
def go():
|
||||
app, srv, url = yield from self.create_server()
|
||||
app, client = yield from create_app_and_client()
|
||||
_setup(app, CookiesIdentityPolicy(), Autz())
|
||||
app.router.add_route('GET', '/', check)
|
||||
resp = yield from self.client.get(url+'/')
|
||||
self.assertEqual(200, resp.status)
|
||||
resp = yield from client.get('/')
|
||||
assert 200 == resp.status
|
||||
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
|
||||
def login(request):
|
||||
|
@ -118,42 +82,38 @@ class TestCookiesIdentity(unittest.TestCase):
|
|||
@asyncio.coroutine
|
||||
def check(request):
|
||||
ret = yield from permits(request, 'read')
|
||||
self.assertTrue(ret)
|
||||
assert ret
|
||||
ret = yield from permits(request, 'write')
|
||||
self.assertTrue(ret)
|
||||
assert ret
|
||||
ret = yield from permits(request, 'unknown')
|
||||
self.assertFalse(ret)
|
||||
assert not ret
|
||||
return web.Response()
|
||||
|
||||
@asyncio.coroutine
|
||||
def go():
|
||||
app, srv, url = yield from self.create_server()
|
||||
app, client = yield from create_app_and_client()
|
||||
_setup(app, CookiesIdentityPolicy(), Autz())
|
||||
app.router.add_route('GET', '/', check)
|
||||
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
|
||||
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
|
||||
def check(request):
|
||||
ret = yield from permits(request, 'read')
|
||||
self.assertFalse(ret)
|
||||
assert not ret
|
||||
ret = yield from permits(request, 'write')
|
||||
self.assertFalse(ret)
|
||||
assert not ret
|
||||
ret = yield from permits(request, 'unknown')
|
||||
self.assertFalse(ret)
|
||||
assert not ret
|
||||
return web.Response()
|
||||
|
||||
@asyncio.coroutine
|
||||
def go():
|
||||
app, srv, url = yield from self.create_server()
|
||||
app, client = yield from create_app_and_client()
|
||||
_setup(app, CookiesIdentityPolicy(), Autz())
|
||||
app.router.add_route('GET', '/', check)
|
||||
resp = yield from self.client.get(url+'/')
|
||||
self.assertEqual(200, resp.status)
|
||||
resp = yield from client.get('/')
|
||||
assert 200 == resp.status
|
||||
yield from resp.release()
|
||||
|
||||
self.loop.run_until_complete(go())
|
||||
|
|
Loading…
Reference in New Issue