add tests/test_admin_api/test_guilds.py

- guilds: decouple endpoint logic into a delete_guild() function
This commit is contained in:
Luna 2019-04-22 02:13:53 -03:00
parent 717c02bdd7
commit d8b889c1a9
3 changed files with 80 additions and 12 deletions

View File

@ -350,21 +350,17 @@ async def _update_guild(guild_id):
return jsonify(guild) return jsonify(guild)
@bp.route('/<int:guild_id>', methods=['DELETE']) async def delete_guild(guild_id: int, *, app_=None):
# this endpoint is not documented, but used by the official client. """Delete a single guild."""
@bp.route('/<int:guild_id>/delete', methods=['POST']) app_ = app_ or app
async def delete_guild(guild_id):
"""Delete a guild."""
user_id = await token_check()
await guild_owner_check(user_id, guild_id)
await app.db.execute(""" await app_.db.execute("""
DELETE FROM guilds DELETE FROM guilds
WHERE guilds.id = $1 WHERE guilds.id = $1
""", guild_id) """, guild_id)
# Discord's client expects IDs being string # Discord's client expects IDs being string
await app.dispatcher.dispatch('guild', guild_id, 'GUILD_DELETE', { await app_.dispatcher.dispatch('guild', guild_id, 'GUILD_DELETE', {
'guild_id': str(guild_id), 'guild_id': str(guild_id),
'id': str(guild_id), 'id': str(guild_id),
# 'unavailable': False, # 'unavailable': False,
@ -373,8 +369,17 @@ async def delete_guild(guild_id):
# remove from the dispatcher so nobody # remove from the dispatcher so nobody
# becomes the little memer that tries to fuck up with # becomes the little memer that tries to fuck up with
# everybody's gateway # everybody's gateway
await app.dispatcher.remove('guild', guild_id) await app_.dispatcher.remove('guild', guild_id)
@bp.route('/<int:guild_id>', methods=['DELETE'])
# this endpoint is not documented, but used by the official client.
@bp.route('/<int:guild_id>/delete', methods=['POST'])
async def delete_guild_handler(guild_id):
"""Delete a guild."""
user_id = await token_check()
await guild_owner_check(user_id, guild_id)
await delete_guild(guild_id)
return '', 204 return '', 204

View File

@ -0,0 +1,64 @@
"""
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 <http://www.gnu.org/licenses/>.
"""
import secrets
import pytest
from tests.common import login
from litecord.blueprints.guilds import delete_guild
async def _create_guild(test_cli, *, token=None):
token = token or await login('admin', test_cli)
genned_name = secrets.token_hex(6)
resp = await test_cli.post('/api/v6/guilds', headers={
'Authorization': token
}, json={
'name': genned_name,
'region': None
})
assert resp.status_code == 200
rjson = await resp.json
assert isinstance(rjson, dict)
assert rjson['name'] == genned_name
return rjson
@pytest.mark.asyncio
async def test_guild_fetch(test_cli):
"""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)
guild_id = rjson['id']
try:
resp = await test_cli.get(f'/api/v6/admin/guilds/{guild_id}', headers={
'Authorization': token
})
assert resp.status_code == 200
rjson = await resp.json
assert isinstance(rjson, dict)
assert rjson['id'] == guild_id
finally:
await delete_guild(int(guild_id), app_=test_cli.app)

View File

@ -27,8 +27,7 @@ from litecord.enums import UserFlags
async def _search(test_cli, *, username='', discrim='', token=None): async def _search(test_cli, *, username='', discrim='', token=None):
if token is None: token = token or await login('admin', test_cli)
token = await login('admin', test_cli)
query_string = { query_string = {
'username': username, 'username': username,