tests.test_main: fix testing

This commit is contained in:
Luna Mendes 2018-11-12 17:39:30 -03:00
parent 7bd8b456a2
commit 1711192ac9
2 changed files with 28 additions and 11 deletions

6
run.py
View File

@ -63,6 +63,9 @@ def make_app():
def set_blueprints(app_): def set_blueprints(app_):
"""Set the blueprints for a given app instance"""
log.debug('blueprint setup')
bps = { bps = {
gateway: None, gateway: None,
auth: '/auth', auth: '/auth',
@ -91,6 +94,8 @@ def set_blueprints(app_):
suffix = suffix or '' suffix = suffix or ''
app_.register_blueprint(bp, url_prefix=f'/api/v6{suffix}') app_.register_blueprint(bp, url_prefix=f'/api/v6{suffix}')
log.debug('blueprint setup: OK')
app = make_app() app = make_app()
set_blueprints(app) set_blueprints(app)
@ -148,6 +153,7 @@ async def app_set_ratelimit_headers(resp):
async def init_app_db(app): async def init_app_db(app):
"""Connect to databases""" """Connect to databases"""
log.info('db connect')
app.db = await asyncpg.create_pool(**app.config['POSTGRES']) app.db = await asyncpg.create_pool(**app.config['POSTGRES'])

View File

@ -4,37 +4,48 @@ import os
import pytest import pytest
# this is very hacky.
sys.path.append(os.getcwd()) sys.path.append(os.getcwd())
from run import make_app, init_app_db, init_app_managers, set_blueprints from run import app as main_app, set_blueprints
@pytest.fixture(name='app') @pytest.fixture(name='app')
def _test_app(): def _test_app(unused_tcp_port):
app = make_app() set_blueprints(main_app)
set_blueprints(app)
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
loop.run_until_complete(init_app_db(app))
init_app_managers(app) # reassign an unused tcp port for websockets
return app # since the config might give a used one.
main_app.config['WS_PORT'] = unused_tcp_port
# make sure we're calling the before_serving hooks
loop.run_until_complete(main_app.startup())
return main_app
@pytest.fixture(name='test_cli') @pytest.fixture(name='test_cli')
def _test_cli(app): def _test_cli(app):
"""Give a test client."""
return app.test_client() return app.test_client()
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_index(test_cli): async def test_index(test_cli):
"""Test if the main index page works."""
resp = await test_cli.get('/') resp = await test_cli.get('/')
assert resp.status_code == 200 assert resp.status_code == 200
assert (await resp.get_data()).decode() == 'hewwo'
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_gw(test_cli): async def test_gw(test_cli):
resp = await test_cli.get('/api/v6/gateway/') """Test if the gateway route is sane."""
resp = await test_cli.get('/api/v6/gateway')
assert resp.status_code == 200 assert resp.status_code == 200
rjson = await resp.json() rjson = await resp.json
assert isinstance(rjson, dict) assert isinstance(rjson, dict)
assert 'gateway' in rjson assert 'url' in rjson
assert isinstance(rjson['gateway'], str) assert isinstance(rjson['url'], str)