update many tests to the new TestClient fixture

This commit is contained in:
Luna 2019-07-21 12:51:40 -03:00
parent 7b07254741
commit 30902d6e8f
8 changed files with 68 additions and 147 deletions

View File

@ -21,18 +21,13 @@ import secrets
import pytest import pytest
from tests.common import login
from litecord.blueprints.guilds import delete_guild from litecord.blueprints.guilds import delete_guild
from litecord.errors import GuildNotFound from litecord.errors import GuildNotFound
async def _create_guild(test_cli, *, token=None): async def _create_guild(test_cli_staff):
token = token or await login('admin', test_cli)
genned_name = secrets.token_hex(6) genned_name = secrets.token_hex(6)
resp = await test_cli.post('/api/v6/guilds', headers={ resp = await test_cli_staff.post('/api/v6/guilds', json={
'Authorization': token
}, json={
'name': genned_name, 'name': genned_name,
'region': None 'region': None
}) })
@ -45,12 +40,8 @@ async def _create_guild(test_cli, *, token=None):
return rjson return rjson
async def _fetch_guild(test_cli, guild_id, *, token=None, ret_early=False): async def _fetch_guild(test_cli_staff, guild_id, *, ret_early=False):
token = token or await login('admin', test_cli) resp = await test_cli_staff.get(f'/api/v6/admin/guilds/{guild_id}')
resp = await test_cli.get(f'/api/v6/admin/guilds/{guild_id}', headers={
'Authorization': token
})
if ret_early: if ret_early:
return resp return resp
@ -64,23 +55,21 @@ async def _fetch_guild(test_cli, guild_id, *, token=None, ret_early=False):
@pytest.mark.asyncio @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.""" """Test the creation and fetching of a guild via the Admin API."""
token = await login('admin', test_cli) rjson = await _create_guild(test_cli_staff)
rjson = await _create_guild(test_cli, token=token)
guild_id = rjson['id'] guild_id = rjson['id']
try: try:
await _fetch_guild(test_cli, guild_id) await _fetch_guild(test_cli_staff, guild_id)
finally: 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 @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.""" """Test the update of a guild via the Admin API."""
token = await login('admin', test_cli) rjson = await _create_guild(test_cli_staff, token=token)
rjson = await _create_guild(test_cli, token=token)
guild_id = rjson['id'] guild_id = rjson['id']
assert not rjson['unavailable'] assert not rjson['unavailable']
@ -103,30 +92,25 @@ async def test_guild_update(test_cli):
assert rjson['id'] == guild_id assert rjson['id'] == guild_id
assert rjson['unavailable'] 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'] assert rjson['unavailable']
finally: 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 @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.""" """Test the update of a guild via the Admin API."""
token = await login('admin', test_cli) rjson = await _create_guild(test_cli_staff)
rjson = await _create_guild(test_cli, token=token)
guild_id = rjson['id'] guild_id = rjson['id']
try: try:
resp = await test_cli.delete( resp = await test_cli_staff.delete(f'/api/v6/admin/guilds/{guild_id}')
f'/api/v6/admin/guilds/{guild_id}',
headers={
'Authorization': token
})
assert resp.status_code == 204 assert resp.status_code == 204
resp = await _fetch_guild(test_cli, guild_id, token=token, resp = await _fetch_guild(
ret_early=True) test_cli_staff, guild_id, ret_early=True)
assert resp.status_code == 404 assert resp.status_code == 404
rjson = await resp.json rjson = await resp.json

View File

@ -19,15 +19,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import pytest import pytest
from tests.common import login
async def _get_invs(test_cli, token=None): async def _get_invs(test_cli):
if token is None: resp = await test_cli.get('/api/v6/admin/instance/invites')
token = await login('admin', test_cli)
resp = await test_cli.get('/api/v6/admin/instance/invites', headers={
'Authorization': token
})
assert resp.status_code == 200 assert resp.status_code == 200
rjson = await resp.json rjson = await resp.json
@ -36,34 +30,25 @@ async def _get_invs(test_cli, token=None):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_get_invites(test_cli): async def test_get_invites(test_cli_staff):
"""Test the listing of instance invites.""" """Test the listing of instance invites."""
await _get_invs(test_cli) await _get_invs(test_cli_staff)
@pytest.mark.asyncio @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 """Test errors happen when trying to delete a
non-existing instance invite.""" non-existing instance invite."""
token = await login('admin', test_cli) resp = await test_cli_staff.delete('/api/v6/admin/instance/invites/aaaaaa')
resp = await test_cli.delete(
'/api/v6/admin/instance/invites/aaaaaaaaaa',
headers={
'Authorization': token
}
)
assert resp.status_code == 404 assert resp.status_code == 404
@pytest.mark.asyncio @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, """Test the creation of an instance invite, then listing it,
then deleting it.""" then deleting it."""
token = await login('admin', test_cli) resp = await test_cli_staff.put('/api/v6/admin/instance/invites', json={
resp = await test_cli.put('/api/v6/admin/instance/invites', headers={
'Authorization': token
}, json={
'max_uses': 1 'max_uses': 1
}) })
@ -73,15 +58,11 @@ async def test_create_invite(test_cli):
code = rjson['code'] code = rjson['code']
# assert that the invite is in the list # 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) assert any(inv['code'] == code for inv in invites)
# delete it, and assert it worked # delete it, and assert it worked
resp = await test_cli.delete( resp = await test_cli_staff.delete(
f'/api/v6/admin/instance/invites/{code}', f'/api/v6/admin/instance/invites/{code}')
headers={
'Authorization': token
}
)
assert resp.status_code == 204 assert resp.status_code == 204

View File

@ -21,28 +21,24 @@ import secrets
import pytest import pytest
from tests.common import login
from tests.credentials import CREDS from tests.credentials import CREDS
from litecord.enums import UserFlags from litecord.enums import UserFlags
async def _search(test_cli, *, username='', discrim='', token=None): async def _search(test_cli, *, username='', discrim=''):
token = token or await login('admin', test_cli)
query_string = { query_string = {
'username': username, 'username': username,
'discriminator': discrim 'discriminator': discrim
} }
return await test_cli.get('/api/v6/admin/users', headers={ return await test_cli.get('/api/v6/admin/users', query_string=query_string)
'Authorization': token
}, query_string=query_string)
@pytest.mark.asyncio @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.""" """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 assert resp.status_code == 200
rjson = await resp.json rjson = await resp.json
@ -50,13 +46,10 @@ async def test_list_users(test_cli):
assert rjson assert rjson
async def _setup_user(test_cli, *, token=None) -> dict: async def _setup_user(test_cli) -> dict:
token = token or await login('admin', test_cli)
genned = secrets.token_hex(7) genned = secrets.token_hex(7)
resp = await test_cli.post('/api/v6/admin/users', headers={ resp = await test_cli.post('/api/v6/admin/users', json={
'Authorization': token
}, json={
'username': genned, 'username': genned,
'email': f'{genned}@{genned}.com', 'email': f'{genned}@{genned}.com',
'password': genned, 'password': genned,
@ -70,13 +63,9 @@ async def _setup_user(test_cli, *, token=None) -> dict:
return rjson return rjson
async def _del_user(test_cli, user_id, *, token=None): async def _del_user(test_cli, user_id):
"""Delete a user.""" """Delete a user."""
token = token or await login('admin', test_cli) resp = await test_cli.delete(f'/api/v6/admin/users/{user_id}')
resp = await test_cli.delete(f'/api/v6/admin/users/{user_id}', headers={
'Authorization': token
})
assert resp.status_code == 200 assert resp.status_code == 200
rjson = await resp.json rjson = await resp.json
@ -93,32 +82,29 @@ async def _del_user(test_cli, user_id, *, token=None):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_create_delete(test_cli): async def test_create_delete(test_cli_staff):
"""Create a user. Then delete them.""" """Create a user. Then delete them."""
token = await login('admin', test_cli) rjson = await _setup_user(test_cli_staff)
rjson = await _setup_user(test_cli, token=token)
genned = rjson['username'] genned = rjson['username']
genned_uid = rjson['id'] genned_uid = rjson['id']
try: try:
# check if side-effects went through with a search # 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 assert resp.status_code == 200
rjson = await resp.json rjson = await resp.json
assert isinstance(rjson, list) assert isinstance(rjson, list)
assert rjson[0]['id'] == genned_uid assert rjson[0]['id'] == genned_uid
finally: finally:
await _del_user(test_cli, genned_uid, token=token) await _del_user(test_cli_staff, genned_uid, token=token)
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_user_update(test_cli): async def test_user_update(test_cli_staff):
"""Test user update.""" """Test user update."""
token = await login('admin', test_cli) rjson = await _setup_user(test_cli_staff)
rjson = await _setup_user(test_cli, token=token)
user_id = rjson['id'] user_id = rjson['id']
@ -126,11 +112,11 @@ async def test_user_update(test_cli):
try: try:
# set them as partner flag # set them as partner flag
resp = await test_cli.patch(f'/api/v6/admin/users/{user_id}', headers={ resp = await test_cli_staff.patch(
'Authorization': token f'/api/v6/admin/users/{user_id}',
}, json={ json={
'flags': UserFlags.partner, 'flags': UserFlags.partner,
}) })
assert resp.status_code == 200 assert resp.status_code == 200
rjson = await resp.json rjson = await resp.json

View File

@ -23,8 +23,6 @@ sys.path.append(os.getcwd())
import pytest import pytest
from tests.common import login
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_gw(test_cli): async def test_gw(test_cli):
@ -38,13 +36,9 @@ async def test_gw(test_cli):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_gw_bot(test_cli): async def test_gw_bot(test_cli_user):
"""Test the Get Bot Gateway route""" """Test the Get Bot Gateway route"""
token = await login('normal', test_cli) resp = await test_cli_user.get('/api/v6/gateway/bot')
resp = await test_cli.get('/api/v6/gateway/bot', headers={
'Authorization': token
})
assert resp.status_code == 200 assert resp.status_code == 200
rjson = await resp.json rjson = await resp.json

View File

@ -20,7 +20,6 @@ import secrets
import pytest import pytest
from tests.common import login
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_guild_create(test_cli_user): async def test_guild_create(test_cli_user):

View File

@ -19,8 +19,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import pytest import pytest
from tests.common import login
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_science_empty(test_cli): async def test_science_empty(test_cli):
@ -37,15 +35,10 @@ async def test_harvest_empty(test_cli):
@pytest.mark.asyncio @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 """Test the consent route to see if we're still on
a non-consent status regarding data collection.""" a non-consent status regarding data collection."""
token = await login('normal', test_cli) resp = await test_cli_user.get('/api/v6/users/@me/consent')
resp = await test_cli.get('/api/v6/users/@me/consent', headers={
'Authorization': token
})
assert resp.status_code == 200 assert resp.status_code == 200
rjson = await resp.json rjson = await resp.json

View File

@ -20,15 +20,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import pytest import pytest
import secrets import secrets
from tests.common import login, get_uid
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_get_me(test_cli): async def test_get_me(test_cli_user):
token = await login('normal', test_cli) resp = await test_cli_user.get('/api/v6/users/@me')
resp = await test_cli.get('/api/v6/users/@me', headers={
'Authorization': token
})
assert resp.status_code == 200 assert resp.status_code == 200
rjson = await resp.json rjson = await resp.json
@ -44,11 +39,8 @@ async def test_get_me(test_cli):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_get_me_guilds(test_cli): async def test_get_me_guilds(test_cli_user):
token = await login('normal', test_cli) resp = await test_cli_user.get('/api/v6/users/@me/guilds')
resp = await test_cli.get('/api/v6/users/@me/guilds', headers={
'Authorization': token
})
assert resp.status_code == 200 assert resp.status_code == 200
rjson = await resp.json rjson = await resp.json
@ -56,13 +48,9 @@ async def test_get_me_guilds(test_cli):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_get_profile_self(test_cli): async def test_get_profile_self(test_cli_user):
token = await login('normal', test_cli) user_id = test_cli_user.user['id']
user_id = await get_uid(token, test_cli) resp = await test_cli_user.get(f'/api/v6/users/{user_id}/profile')
resp = await test_cli.get(f'/api/v6/users/{user_id}/profile', headers={
'Authorization': token
})
assert resp.status_code == 200 assert resp.status_code == 200
rjson = await resp.json rjson = await resp.json

View File

@ -22,7 +22,6 @@ import json
import pytest import pytest
import websockets import websockets
from tests.common import login
from litecord.gateway.opcodes import OP from litecord.gateway.opcodes import OP
from litecord.gateway.websocket import decode_etf from litecord.gateway.websocket import decode_etf
@ -87,9 +86,8 @@ async def test_gw(test_cli):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_ready(test_cli): async def test_ready(test_cli_user):
token = await login('normal', test_cli) conn = await gw_start(test_cli_user.test_cli)
conn = await gw_start(test_cli)
# get the hello frame but ignore it # get the hello frame but ignore it
await _json(conn) await _json(conn)
@ -97,7 +95,7 @@ async def test_ready(test_cli):
await _json_send(conn, { await _json_send(conn, {
'op': OP.IDENTIFY, 'op': OP.IDENTIFY,
'd': { 'd': {
'token': token, 'token': test_cli_user.user['token'],
} }
}) })
@ -112,9 +110,8 @@ async def test_ready(test_cli):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_ready_fields(test_cli): async def test_ready_fields(test_cli_user):
token = await login('normal', test_cli) conn = await gw_start(test_cli_user.test_cli)
conn = await gw_start(test_cli)
# get the hello frame but ignore it # get the hello frame but ignore it
await _json(conn) await _json(conn)
@ -122,7 +119,7 @@ async def test_ready_fields(test_cli):
await _json_send(conn, { await _json_send(conn, {
'op': OP.IDENTIFY, 'op': OP.IDENTIFY,
'd': { 'd': {
'token': token, 'token': test_cli_user.user['token'],
} }
}) })
@ -153,9 +150,8 @@ async def test_ready_fields(test_cli):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_heartbeat(test_cli): async def test_heartbeat(test_cli_user):
token = await login('normal', test_cli) conn = await gw_start(test_cli_user.test_cli)
conn = await gw_start(test_cli)
# get the hello frame but ignore it # get the hello frame but ignore it
await _json(conn) await _json(conn)
@ -163,7 +159,7 @@ async def test_heartbeat(test_cli):
await _json_send(conn, { await _json_send(conn, {
'op': OP.IDENTIFY, 'op': OP.IDENTIFY,
'd': { 'd': {
'token': token, 'token': test_cli_user.user['token'],
} }
}) })