From 30902d6e8fe9f963c99305921feadb6237175cc4 Mon Sep 17 00:00:00 2001 From: Luna Date: Sun, 21 Jul 2019 12:51:40 -0300 Subject: [PATCH] update many tests to the new TestClient fixture --- tests/test_admin_api/test_guilds.py | 50 ++++++----------- tests/test_admin_api/test_instance_invites.py | 41 ++++---------- tests/test_admin_api/test_users.py | 54 +++++++------------ tests/test_gateway.py | 10 +--- tests/test_guild.py | 1 - tests/test_no_tracking.py | 11 +--- tests/test_user.py | 26 +++------ tests/test_websocket.py | 22 ++++---- 8 files changed, 68 insertions(+), 147 deletions(-) diff --git a/tests/test_admin_api/test_guilds.py b/tests/test_admin_api/test_guilds.py index e3183f5..e87309e 100644 --- a/tests/test_admin_api/test_guilds.py +++ b/tests/test_admin_api/test_guilds.py @@ -21,18 +21,13 @@ import secrets import pytest -from tests.common import login from litecord.blueprints.guilds import delete_guild from litecord.errors import GuildNotFound -async def _create_guild(test_cli, *, token=None): - token = token or await login('admin', test_cli) - +async def _create_guild(test_cli_staff): genned_name = secrets.token_hex(6) - resp = await test_cli.post('/api/v6/guilds', headers={ - 'Authorization': token - }, json={ + resp = await test_cli_staff.post('/api/v6/guilds', json={ 'name': genned_name, 'region': None }) @@ -45,12 +40,8 @@ async def _create_guild(test_cli, *, token=None): return rjson -async def _fetch_guild(test_cli, guild_id, *, token=None, ret_early=False): - token = token or await login('admin', test_cli) - - resp = await test_cli.get(f'/api/v6/admin/guilds/{guild_id}', headers={ - 'Authorization': token - }) +async def _fetch_guild(test_cli_staff, guild_id, *, ret_early=False): + resp = await test_cli_staff.get(f'/api/v6/admin/guilds/{guild_id}') if ret_early: return resp @@ -64,23 +55,21 @@ async def _fetch_guild(test_cli, guild_id, *, token=None, ret_early=False): @pytest.mark.asyncio -async def test_guild_fetch(test_cli): +async def test_guild_fetch(test_cli_staff): """Test the creation and fetching of a guild via the Admin API.""" - token = await login('admin', test_cli) - rjson = await _create_guild(test_cli, token=token) + rjson = await _create_guild(test_cli_staff) guild_id = rjson['id'] try: - await _fetch_guild(test_cli, guild_id) + await _fetch_guild(test_cli_staff, guild_id) finally: - await delete_guild(int(guild_id), app_=test_cli.app) + await delete_guild(int(guild_id), app_=test_cli_staff.test_cli.app) @pytest.mark.asyncio -async def test_guild_update(test_cli): +async def test_guild_update(test_cli_staff): """Test the update of a guild via the Admin API.""" - token = await login('admin', test_cli) - rjson = await _create_guild(test_cli, token=token) + rjson = await _create_guild(test_cli_staff, token=token) guild_id = rjson['id'] assert not rjson['unavailable'] @@ -103,30 +92,25 @@ async def test_guild_update(test_cli): assert rjson['id'] == guild_id assert rjson['unavailable'] - rjson = await _fetch_guild(test_cli, guild_id, token=token) + rjson = await _fetch_guild(test_cli_staff, guild_id) assert rjson['unavailable'] finally: - await delete_guild(int(guild_id), app_=test_cli.app) + await delete_guild(int(guild_id), app_=test_cli_staff.test_cli.app) @pytest.mark.asyncio -async def test_guild_delete(test_cli): +async def test_guild_delete(test_cli_staff): """Test the update of a guild via the Admin API.""" - token = await login('admin', test_cli) - rjson = await _create_guild(test_cli, token=token) + rjson = await _create_guild(test_cli_staff) guild_id = rjson['id'] try: - resp = await test_cli.delete( - f'/api/v6/admin/guilds/{guild_id}', - headers={ - 'Authorization': token - }) + resp = await test_cli_staff.delete(f'/api/v6/admin/guilds/{guild_id}') assert resp.status_code == 204 - resp = await _fetch_guild(test_cli, guild_id, token=token, - ret_early=True) + resp = await _fetch_guild( + test_cli_staff, guild_id, ret_early=True) assert resp.status_code == 404 rjson = await resp.json diff --git a/tests/test_admin_api/test_instance_invites.py b/tests/test_admin_api/test_instance_invites.py index aef9fb8..9bdeba5 100644 --- a/tests/test_admin_api/test_instance_invites.py +++ b/tests/test_admin_api/test_instance_invites.py @@ -19,15 +19,9 @@ along with this program. If not, see . import pytest -from tests.common import login -async def _get_invs(test_cli, token=None): - if token is None: - token = await login('admin', test_cli) - - resp = await test_cli.get('/api/v6/admin/instance/invites', headers={ - 'Authorization': token - }) +async def _get_invs(test_cli): + resp = await test_cli.get('/api/v6/admin/instance/invites') assert resp.status_code == 200 rjson = await resp.json @@ -36,34 +30,25 @@ async def _get_invs(test_cli, token=None): @pytest.mark.asyncio -async def test_get_invites(test_cli): +async def test_get_invites(test_cli_staff): """Test the listing of instance invites.""" - await _get_invs(test_cli) + await _get_invs(test_cli_staff) @pytest.mark.asyncio -async def test_inv_delete_invalid(test_cli): +async def test_inv_delete_invalid(test_cli_staff): """Test errors happen when trying to delete a non-existing instance invite.""" - token = await login('admin', test_cli) - resp = await test_cli.delete( - '/api/v6/admin/instance/invites/aaaaaaaaaa', - headers={ - 'Authorization': token - } - ) + resp = await test_cli_staff.delete('/api/v6/admin/instance/invites/aaaaaa') assert resp.status_code == 404 @pytest.mark.asyncio -async def test_create_invite(test_cli): +async def test_create_invite(test_cli_staff): """Test the creation of an instance invite, then listing it, then deleting it.""" - token = await login('admin', test_cli) - resp = await test_cli.put('/api/v6/admin/instance/invites', headers={ - 'Authorization': token - }, json={ + resp = await test_cli_staff.put('/api/v6/admin/instance/invites', json={ 'max_uses': 1 }) @@ -73,15 +58,11 @@ async def test_create_invite(test_cli): code = rjson['code'] # assert that the invite is in the list - invites = await _get_invs(test_cli, token) + invites = await _get_invs(test_cli_staff) assert any(inv['code'] == code for inv in invites) # delete it, and assert it worked - resp = await test_cli.delete( - f'/api/v6/admin/instance/invites/{code}', - headers={ - 'Authorization': token - } - ) + resp = await test_cli_staff.delete( + f'/api/v6/admin/instance/invites/{code}') assert resp.status_code == 204 diff --git a/tests/test_admin_api/test_users.py b/tests/test_admin_api/test_users.py index fc911cb..841711a 100644 --- a/tests/test_admin_api/test_users.py +++ b/tests/test_admin_api/test_users.py @@ -21,28 +21,24 @@ import secrets import pytest -from tests.common import login from tests.credentials import CREDS from litecord.enums import UserFlags -async def _search(test_cli, *, username='', discrim='', token=None): - token = token or await login('admin', test_cli) - +async def _search(test_cli, *, username='', discrim=''): query_string = { 'username': username, 'discriminator': discrim } - return await test_cli.get('/api/v6/admin/users', headers={ - 'Authorization': token - }, query_string=query_string) + return await test_cli.get('/api/v6/admin/users', query_string=query_string) @pytest.mark.asyncio -async def test_list_users(test_cli): +async def test_list_users(test_cli_staff): """Try to list as many users as possible.""" - resp = await _search(test_cli, username=CREDS['admin']['username']) + resp = await _search( + test_cli_staff, username=test_cli_staff.user['username']) assert resp.status_code == 200 rjson = await resp.json @@ -50,13 +46,10 @@ async def test_list_users(test_cli): assert rjson -async def _setup_user(test_cli, *, token=None) -> dict: - token = token or await login('admin', test_cli) +async def _setup_user(test_cli) -> dict: genned = secrets.token_hex(7) - resp = await test_cli.post('/api/v6/admin/users', headers={ - 'Authorization': token - }, json={ + resp = await test_cli.post('/api/v6/admin/users', json={ 'username': genned, 'email': f'{genned}@{genned}.com', 'password': genned, @@ -70,13 +63,9 @@ async def _setup_user(test_cli, *, token=None) -> dict: return rjson -async def _del_user(test_cli, user_id, *, token=None): +async def _del_user(test_cli, user_id): """Delete a user.""" - token = token or await login('admin', test_cli) - - resp = await test_cli.delete(f'/api/v6/admin/users/{user_id}', headers={ - 'Authorization': token - }) + resp = await test_cli.delete(f'/api/v6/admin/users/{user_id}') assert resp.status_code == 200 rjson = await resp.json @@ -93,32 +82,29 @@ async def _del_user(test_cli, user_id, *, token=None): @pytest.mark.asyncio -async def test_create_delete(test_cli): +async def test_create_delete(test_cli_staff): """Create a user. Then delete them.""" - token = await login('admin', test_cli) - - rjson = await _setup_user(test_cli, token=token) + rjson = await _setup_user(test_cli_staff) genned = rjson['username'] genned_uid = rjson['id'] try: # check if side-effects went through with a search - resp = await _search(test_cli, username=genned, token=token) + resp = await _search(test_cli_staff, username=genned) assert resp.status_code == 200 rjson = await resp.json assert isinstance(rjson, list) assert rjson[0]['id'] == genned_uid finally: - await _del_user(test_cli, genned_uid, token=token) + await _del_user(test_cli_staff, genned_uid, token=token) @pytest.mark.asyncio -async def test_user_update(test_cli): +async def test_user_update(test_cli_staff): """Test user update.""" - token = await login('admin', test_cli) - rjson = await _setup_user(test_cli, token=token) + rjson = await _setup_user(test_cli_staff) user_id = rjson['id'] @@ -126,11 +112,11 @@ async def test_user_update(test_cli): try: # set them as partner flag - resp = await test_cli.patch(f'/api/v6/admin/users/{user_id}', headers={ - 'Authorization': token - }, json={ - 'flags': UserFlags.partner, - }) + resp = await test_cli_staff.patch( + f'/api/v6/admin/users/{user_id}', + json={ + 'flags': UserFlags.partner, + }) assert resp.status_code == 200 rjson = await resp.json diff --git a/tests/test_gateway.py b/tests/test_gateway.py index e18d8e4..a160b88 100644 --- a/tests/test_gateway.py +++ b/tests/test_gateway.py @@ -23,8 +23,6 @@ sys.path.append(os.getcwd()) import pytest -from tests.common import login - @pytest.mark.asyncio async def test_gw(test_cli): @@ -38,13 +36,9 @@ async def test_gw(test_cli): @pytest.mark.asyncio -async def test_gw_bot(test_cli): +async def test_gw_bot(test_cli_user): """Test the Get Bot Gateway route""" - token = await login('normal', test_cli) - - resp = await test_cli.get('/api/v6/gateway/bot', headers={ - 'Authorization': token - }) + resp = await test_cli_user.get('/api/v6/gateway/bot') assert resp.status_code == 200 rjson = await resp.json diff --git a/tests/test_guild.py b/tests/test_guild.py index dcb850e..c91e676 100644 --- a/tests/test_guild.py +++ b/tests/test_guild.py @@ -20,7 +20,6 @@ import secrets import pytest -from tests.common import login @pytest.mark.asyncio async def test_guild_create(test_cli_user): diff --git a/tests/test_no_tracking.py b/tests/test_no_tracking.py index 8ac1af2..efdac93 100644 --- a/tests/test_no_tracking.py +++ b/tests/test_no_tracking.py @@ -19,8 +19,6 @@ along with this program. If not, see . import pytest -from tests.common import login - @pytest.mark.asyncio async def test_science_empty(test_cli): @@ -37,15 +35,10 @@ async def test_harvest_empty(test_cli): @pytest.mark.asyncio -async def test_consent_non_consenting(test_cli): +async def test_consent_non_consenting(test_cli_user): """Test the consent route to see if we're still on a non-consent status regarding data collection.""" - token = await login('normal', test_cli) - - resp = await test_cli.get('/api/v6/users/@me/consent', headers={ - 'Authorization': token - }) - + resp = await test_cli_user.get('/api/v6/users/@me/consent') assert resp.status_code == 200 rjson = await resp.json diff --git a/tests/test_user.py b/tests/test_user.py index 7a851a0..a83de77 100644 --- a/tests/test_user.py +++ b/tests/test_user.py @@ -20,15 +20,10 @@ along with this program. If not, see . import pytest import secrets -from tests.common import login, get_uid - @pytest.mark.asyncio -async def test_get_me(test_cli): - token = await login('normal', test_cli) - resp = await test_cli.get('/api/v6/users/@me', headers={ - 'Authorization': token - }) +async def test_get_me(test_cli_user): + resp = await test_cli_user.get('/api/v6/users/@me') assert resp.status_code == 200 rjson = await resp.json @@ -44,11 +39,8 @@ async def test_get_me(test_cli): @pytest.mark.asyncio -async def test_get_me_guilds(test_cli): - token = await login('normal', test_cli) - resp = await test_cli.get('/api/v6/users/@me/guilds', headers={ - 'Authorization': token - }) +async def test_get_me_guilds(test_cli_user): + resp = await test_cli_user.get('/api/v6/users/@me/guilds') assert resp.status_code == 200 rjson = await resp.json @@ -56,13 +48,9 @@ async def test_get_me_guilds(test_cli): @pytest.mark.asyncio -async def test_get_profile_self(test_cli): - token = await login('normal', test_cli) - user_id = await get_uid(token, test_cli) - - resp = await test_cli.get(f'/api/v6/users/{user_id}/profile', headers={ - 'Authorization': token - }) +async def test_get_profile_self(test_cli_user): + user_id = test_cli_user.user['id'] + resp = await test_cli_user.get(f'/api/v6/users/{user_id}/profile') assert resp.status_code == 200 rjson = await resp.json diff --git a/tests/test_websocket.py b/tests/test_websocket.py index 826fe58..86dba0f 100644 --- a/tests/test_websocket.py +++ b/tests/test_websocket.py @@ -22,7 +22,6 @@ import json import pytest import websockets -from tests.common import login from litecord.gateway.opcodes import OP from litecord.gateway.websocket import decode_etf @@ -87,9 +86,8 @@ async def test_gw(test_cli): @pytest.mark.asyncio -async def test_ready(test_cli): - token = await login('normal', test_cli) - conn = await gw_start(test_cli) +async def test_ready(test_cli_user): + conn = await gw_start(test_cli_user.test_cli) # get the hello frame but ignore it await _json(conn) @@ -97,7 +95,7 @@ async def test_ready(test_cli): await _json_send(conn, { 'op': OP.IDENTIFY, 'd': { - 'token': token, + 'token': test_cli_user.user['token'], } }) @@ -112,9 +110,8 @@ async def test_ready(test_cli): @pytest.mark.asyncio -async def test_ready_fields(test_cli): - token = await login('normal', test_cli) - conn = await gw_start(test_cli) +async def test_ready_fields(test_cli_user): + conn = await gw_start(test_cli_user.test_cli) # get the hello frame but ignore it await _json(conn) @@ -122,7 +119,7 @@ async def test_ready_fields(test_cli): await _json_send(conn, { 'op': OP.IDENTIFY, 'd': { - 'token': token, + 'token': test_cli_user.user['token'], } }) @@ -153,9 +150,8 @@ async def test_ready_fields(test_cli): @pytest.mark.asyncio -async def test_heartbeat(test_cli): - token = await login('normal', test_cli) - conn = await gw_start(test_cli) +async def test_heartbeat(test_cli_user): + conn = await gw_start(test_cli_user.test_cli) # get the hello frame but ignore it await _json(conn) @@ -163,7 +159,7 @@ async def test_heartbeat(test_cli): await _json_send(conn, { 'op': OP.IDENTIFY, 'd': { - 'token': token, + 'token': test_cli_user.user['token'], } })