Convert test_cookies_identity into pytest style
This commit is contained in:
parent
688df9d2d2
commit
ab2ef11bf1
|
@ -68,6 +68,10 @@ class Client:
|
|||
url += '/'
|
||||
self._url = url
|
||||
|
||||
@property
|
||||
def cookies(self):
|
||||
return self._session.cookies
|
||||
|
||||
def close(self):
|
||||
self._session.close()
|
||||
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
import asyncio
|
||||
import socket
|
||||
import unittest
|
||||
import pytest
|
||||
|
||||
import aiohttp
|
||||
from aiohttp import web
|
||||
from aiohttp_security import (remember, setup, forget,
|
||||
from aiohttp_security import (remember, forget,
|
||||
AbstractAuthorizationPolicy)
|
||||
from aiohttp_security import setup as _setup
|
||||
from aiohttp_security.cookies_identity import CookiesIdentityPolicy
|
||||
from aiohttp_security.api import IDENTITY_KEY
|
||||
|
||||
|
@ -21,42 +20,8 @@ class Autz(AbstractAuthorizationPolicy):
|
|||
pass
|
||||
|
||||
|
||||
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_remember(self):
|
||||
@pytest.mark.run_loop
|
||||
def test_remember(create_app_and_client):
|
||||
|
||||
@asyncio.coroutine
|
||||
def handler(request):
|
||||
|
@ -64,19 +29,17 @@ class TestCookiesIdentity(unittest.TestCase):
|
|||
yield from remember(request, response, 'Andrew')
|
||||
return 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', '/', handler)
|
||||
resp = yield from self.client.get(url+'/')
|
||||
self.assertEqual(200, resp.status)
|
||||
self.assertEqual('Andrew',
|
||||
self.client.cookies['AIOHTTP_SECURITY'].value)
|
||||
resp = yield from client.get('/')
|
||||
assert 200 == resp.status
|
||||
assert 'Andrew' == client.cookies['AIOHTTP_SECURITY'].value
|
||||
yield from resp.release()
|
||||
|
||||
self.loop.run_until_complete(go())
|
||||
|
||||
def test_identify(self):
|
||||
@pytest.mark.run_loop
|
||||
def test_identify(create_app_and_client):
|
||||
|
||||
@asyncio.coroutine
|
||||
def create(request):
|
||||
|
@ -88,24 +51,23 @@ class TestCookiesIdentity(unittest.TestCase):
|
|||
def check(request):
|
||||
policy = request.app[IDENTITY_KEY]
|
||||
user_id = yield from policy.identify(request)
|
||||
self.assertEqual('Andrew', user_id)
|
||||
assert 'Andrew' == user_id
|
||||
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', '/', create)
|
||||
resp = yield from self.client.post(url+'/')
|
||||
self.assertEqual(200, resp.status)
|
||||
resp = yield from client.post('/')
|
||||
assert 200 == resp.status
|
||||
yield from resp.release()
|
||||
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_forget(self):
|
||||
@pytest.mark.run_loop
|
||||
def test_forget(create_app_and_client):
|
||||
|
||||
@asyncio.coroutine
|
||||
def index(request):
|
||||
|
@ -123,22 +85,18 @@ class TestCookiesIdentity(unittest.TestCase):
|
|||
yield from forget(request, response)
|
||||
return 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', '/', index)
|
||||
app.router.add_route('POST', '/login', login)
|
||||
app.router.add_route('POST', '/logout', logout)
|
||||
resp = yield from self.client.post(url+'/login')
|
||||
self.assertEqual(200, resp.status)
|
||||
self.assertEqual(url+'/', resp.url)
|
||||
self.assertEqual('Andrew',
|
||||
self.client.cookies['AIOHTTP_SECURITY'].value)
|
||||
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 self.client.post(url+'/logout')
|
||||
self.assertEqual(200, resp.status)
|
||||
self.assertEqual(url+'/', resp.url)
|
||||
self.assertEqual('', self.client.cookies['AIOHTTP_SECURITY'].value)
|
||||
resp = yield from client.post('/logout')
|
||||
assert 200 == resp.status
|
||||
assert resp.url.endswith('/')
|
||||
assert '' == client.cookies['AIOHTTP_SECURITY'].value
|
||||
yield from resp.release()
|
||||
|
||||
self.loop.run_until_complete(go())
|
||||
|
|
Loading…
Reference in New Issue