Convert test_no_identity into pytest style

This commit is contained in:
Andrew Svetlov 2015-11-19 14:17:34 +02:00
parent 8f527777f3
commit 688df9d2d2
1 changed files with 28 additions and 69 deletions

View File

@ -1,82 +1,41 @@
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, forget from aiohttp_security import remember, forget
class TestNoAuth(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 @asyncio.coroutine
def create_server(self): def do_remember(request):
app = web.Application(loop=self.loop) response = web.Response()
yield from remember(request, response, 'Andrew')
port = self.find_unused_port() app, client = yield from create_app_and_client()
self.handler = app.make_handler( app.router.add_route('POST', '/', do_remember)
debug=False, keep_alive_on=False) resp = yield from client.post('/')
srv = yield from self.loop.create_server( assert 500 == resp.status
self.handler, '127.0.0.1', port) assert (('Security subsystem is not initialized, '
url = "http://127.0.0.1:{}/".format(port) 'call aiohttp_security.setup(...) first') ==
self.srv = srv resp.reason)
return app, srv, url yield from resp.release()
def test_remember(self):
@asyncio.coroutine @pytest.mark.run_loop
def do_remember(request): def test_forget(create_app_and_client):
response = web.Response()
yield from remember(request, response, 'Andrew')
@asyncio.coroutine @asyncio.coroutine
def go(): def do_forget(request):
app, srv, url = yield from self.create_server() response = web.Response()
app.router.add_route('POST', '/', do_remember) yield from forget(request, response)
resp = yield from self.client.post(url)
self.assertEqual(500, resp.status)
self.assertEqual(('Security subsystem is not initialized, '
'call aiohttp_security.setup(...) first'),
resp.reason)
yield from resp.release()
self.loop.run_until_complete(go()) app, client = yield from create_app_and_client()
app.router.add_route('POST', '/', do_forget)
def test_forget(self): resp = yield from client.post('/')
assert 500 == resp.status
@asyncio.coroutine assert (('Security subsystem is not initialized, '
def do_forget(request): 'call aiohttp_security.setup(...) first') ==
response = web.Response() resp.reason)
yield from forget(request, response) yield from resp.release()
@asyncio.coroutine
def go():
app, srv, url = yield from self.create_server()
app.router.add_route('POST', '/', do_forget)
resp = yield from self.client.post(url)
self.assertEqual(500, resp.status)
self.assertEqual(('Security subsystem is not initialized, '
'call aiohttp_security.setup(...) first'),
resp.reason)
yield from resp.release()
self.loop.run_until_complete(go())