Convert test_cookies_identity into pytest style

This commit is contained in:
Andrew Svetlov 2015-11-19 14:36:15 +02:00
parent 688df9d2d2
commit ab2ef11bf1
2 changed files with 73 additions and 111 deletions

View File

@ -68,6 +68,10 @@ class Client:
url += '/' url += '/'
self._url = url self._url = url
@property
def cookies(self):
return self._session.cookies
def close(self): def close(self):
self._session.close() self._session.close()

View File

@ -1,11 +1,10 @@
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, forget, from aiohttp_security import (remember, forget,
AbstractAuthorizationPolicy) AbstractAuthorizationPolicy)
from aiohttp_security import setup as _setup
from aiohttp_security.cookies_identity import CookiesIdentityPolicy from aiohttp_security.cookies_identity import CookiesIdentityPolicy
from aiohttp_security.api import IDENTITY_KEY from aiohttp_security.api import IDENTITY_KEY
@ -21,42 +20,8 @@ class Autz(AbstractAuthorizationPolicy):
pass pass
class TestCookiesIdentity(unittest.TestCase): @pytest.mark.run_loop
def test_remember(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_remember(self):
@asyncio.coroutine @asyncio.coroutine
def handler(request): def handler(request):
@ -64,19 +29,17 @@ class TestCookiesIdentity(unittest.TestCase):
yield from remember(request, response, 'Andrew') yield from remember(request, response, 'Andrew')
return response return 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', '/', handler) app.router.add_route('GET', '/', handler)
resp = yield from self.client.get(url+'/') resp = yield from client.get('/')
self.assertEqual(200, resp.status) assert 200 == resp.status
self.assertEqual('Andrew', assert 'Andrew' == client.cookies['AIOHTTP_SECURITY'].value
self.client.cookies['AIOHTTP_SECURITY'].value)
yield from resp.release() 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 @asyncio.coroutine
def create(request): def create(request):
@ -88,24 +51,23 @@ class TestCookiesIdentity(unittest.TestCase):
def check(request): def check(request):
policy = request.app[IDENTITY_KEY] policy = request.app[IDENTITY_KEY]
user_id = yield from policy.identify(request) user_id = yield from policy.identify(request)
self.assertEqual('Andrew', user_id) assert 'Andrew' == user_id
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', '/', create) app.router.add_route('POST', '/', create)
resp = yield from self.client.post(url+'/') resp = yield from client.post('/')
self.assertEqual(200, resp.status) assert 200 == resp.status
yield from resp.release() yield from resp.release()
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_forget(self): @pytest.mark.run_loop
def test_forget(create_app_and_client):
@asyncio.coroutine @asyncio.coroutine
def index(request): def index(request):
@ -123,22 +85,18 @@ class TestCookiesIdentity(unittest.TestCase):
yield from forget(request, response) yield from forget(request, response)
return response return 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', '/', index) app.router.add_route('GET', '/', index)
app.router.add_route('POST', '/login', login) app.router.add_route('POST', '/login', login)
app.router.add_route('POST', '/logout', logout) app.router.add_route('POST', '/logout', logout)
resp = yield from self.client.post(url+'/login') resp = yield from client.post('/login')
self.assertEqual(200, resp.status) assert 200 == resp.status
self.assertEqual(url+'/', resp.url) assert resp.url.endswith('/')
self.assertEqual('Andrew', assert 'Andrew' == client.cookies['AIOHTTP_SECURITY'].value
self.client.cookies['AIOHTTP_SECURITY'].value)
yield from resp.release() yield from resp.release()
resp = yield from self.client.post(url+'/logout') resp = yield from client.post('/logout')
self.assertEqual(200, resp.status) assert 200 == resp.status
self.assertEqual(url+'/', resp.url) assert resp.url.endswith('/')
self.assertEqual('', self.client.cookies['AIOHTTP_SECURITY'].value) assert '' == client.cookies['AIOHTTP_SECURITY'].value
yield from resp.release() yield from resp.release()
self.loop.run_until_complete(go())