From ddb56891d1802bff3d59758fd6e88778ffb7d77f Mon Sep 17 00:00:00 2001 From: Luna Date: Sat, 16 Mar 2019 17:38:22 -0300 Subject: [PATCH] add tests.test_admin_api.test_instance_invites - admin_api.instance_invites: fixes --- .../blueprints/admin_api/instance_invites.py | 6 +- tests/test_admin_api/test_instance_invites.py | 87 +++++++++++++++++++ 2 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 tests/test_admin_api/test_instance_invites.py diff --git a/litecord/blueprints/admin_api/instance_invites.py b/litecord/blueprints/admin_api/instance_invites.py index 87279c1..dbd6c78 100644 --- a/litecord/blueprints/admin_api/instance_invites.py +++ b/litecord/blueprints/admin_api/instance_invites.py @@ -65,7 +65,7 @@ async def _all_instance_invites(): rows = [dict(row) for row in rows] for row in rows: - row['timestamp'] = timestamp_(row['timestamp']) + row['created_at'] = timestamp_(row['created_at']) return jsonify(rows) @@ -75,7 +75,7 @@ async def _create_invite(): await admin_check() code = await gen_inv(app) - if not code: + if code is None: return 'failed to make invite', 500 j = validate(await request.get_json(), INSTANCE_INVITE) @@ -103,7 +103,7 @@ async def _del_invite(invite: str): WHERE code = $1 """, invite) - if res.lower() == 'update 0': + if res.lower() == 'delete 0': return 'invite not found', 404 return '', 204 diff --git a/tests/test_admin_api/test_instance_invites.py b/tests/test_admin_api/test_instance_invites.py new file mode 100644 index 0000000..aef9fb8 --- /dev/null +++ b/tests/test_admin_api/test_instance_invites.py @@ -0,0 +1,87 @@ +""" + +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 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 + }) + + assert resp.status_code == 200 + rjson = await resp.json + assert isinstance(rjson, list) + return rjson + + +@pytest.mark.asyncio +async def test_get_invites(test_cli): + """Test the listing of instance invites.""" + await _get_invs(test_cli) + + +@pytest.mark.asyncio +async def test_inv_delete_invalid(test_cli): + """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 + } + ) + + assert resp.status_code == 404 + + +@pytest.mark.asyncio +async def test_create_invite(test_cli): + """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={ + 'max_uses': 1 + }) + + assert resp.status_code == 200 + rjson = await resp.json + assert isinstance(rjson, dict) + code = rjson['code'] + + # assert that the invite is in the list + invites = await _get_invs(test_cli, token) + 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 + } + ) + + assert resp.status_code == 204