diff --git a/litecord/blueprints/guilds.py b/litecord/blueprints/guilds.py index 36c8f11..781b1be 100644 --- a/litecord/blueprints/guilds.py +++ b/litecord/blueprints/guilds.py @@ -351,7 +351,8 @@ async def _update_guild(guild_id): @bp.route('/', methods=['DELETE']) -@bp.route('//delete', methods=['POST']) # this one is not actually documented, but it's used by Discord client +# this endpoint is not documented, but used by the official client. +@bp.route('//delete', methods=['POST']) async def delete_guild(guild_id): """Delete a guild.""" user_id = await token_check() diff --git a/tests/test_guild.py b/tests/test_guild.py new file mode 100644 index 0000000..f4aee70 --- /dev/null +++ b/tests/test_guild.py @@ -0,0 +1,92 @@ +""" + +Litecord +Copyright (C) 2018-2019 Luna Mendes + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 3 of the License. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . + +""" +import secrets + +import pytest + +from tests.common import login + +@pytest.mark.asyncio +async def test_guild_create(test_cli): + """Test the creation of a guild, in three stages: + - creating it + - checking the list + - deleting it + """ + token = await login('normal', test_cli) + g_name = secrets.token_hex(5) + + # stage 1: create + resp = await test_cli.post('/api/v6/guilds', headers={ + 'Authorization': token + }, json={ + 'name': g_name, + 'region': None, + }) + + assert resp.status_code == 200 + rjson = await resp.json + + # we won't assert a full guild object. + assert isinstance(rjson['id'], str) + assert isinstance(rjson['owner_id'], str) + assert isinstance(rjson['name'], str) + assert rjson['name'] == g_name + + created = rjson + guild_id = created['id'] + + # stage 2: test + resp = await test_cli.get('/api/v6/users/@me/guilds', headers={ + 'Authorization': token + }) + + assert resp.status_code == 200 + rjson = await resp.json + + assert isinstance(rjson, list) + + # it MUST be 1 as we'll delete the guild later on. + # plus the test user never starts with any guild. + assert len(rjson) == 1 + + for guild in rjson: + assert isinstance(guild, dict) + assert isinstance(guild['id'], str) + assert isinstance(guild['name'], str) + assert isinstance(guild['owner'], bool) + assert guild['icon'] is None or isinstance(guild['icon'], str) + + try: + our_guild = next(filter( + lambda guild: guild['id'] == guild_id, + rjson + )) + except StopIteration: + raise Exception('created guild not found in user guild list') + + assert our_guild['id'] == created['id'] + assert our_guild['name'] == created['name'] + + # stage 3: deletion + resp = await test_cli.delete(f'/api/v6/guilds/{guild_id}', headers={ + 'Authorization': token + }) + + assert resp.status_code == 204