test_admin_api.test_users: add impl for test_create_delete

- admin_api.users: fix bug
This commit is contained in:
Luna 2019-03-16 20:31:28 -03:00
parent 2dda7c1bf9
commit 169d22ad72
2 changed files with 60 additions and 6 deletions

View File

@ -35,7 +35,7 @@ async def _create_user():
j = validate(await request.get_json(), USER_CREATE) j = validate(await request.get_json(), USER_CREATE)
user_id = await create_user(j['username'], j['email'], j['password']) user_id, _ = await create_user(j['username'], j['email'], j['password'])
return jsonify( return jsonify(
await app.storage.get_user(user_id) await app.storage.get_user(user_id)

View File

@ -17,21 +17,75 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
""" """
import secrets
import pytest import pytest
from tests.common import login from tests.common import login
async def _search(test_cli, *, username='', discrim='', token=None):
if token is None:
token = await login('admin', test_cli)
query_string = {
'username': username,
'discriminator': discrim
}
return await test_cli.get('/api/v6/admin/users', headers={
'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):
"""Try to list as many users as possible.""" """Try to list as many users as possible."""
token = await login('admin', test_cli)
# NOTE: replace here if admin username changes # NOTE: replace here if admin username changes
resp = await test_cli.get('/api/v6/admin/users?username=big_girl', headers={ resp = await _search(test_cli, username='big_girl')
'Authorization': token
})
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 assert rjson
@pytest.mark.asyncio
async def test_create_delete(test_cli):
"""Create a user. Then delete them."""
token = await login('admin', test_cli)
genned = secrets.token_hex(7)
resp = await test_cli.post('/api/v6/admin/users', headers={
'Authorization': token
}, json={
'username': genned,
'email': f'{genned}@{genned}.com',
'password': genned,
})
assert resp.status_code == 200
rjson = await resp.json
assert isinstance(rjson, dict)
assert rjson['username'] == genned
genned_uid = rjson['id']
# check if side-effects went through with a search
resp = await _search(test_cli, username=genned, token=token)
assert resp.status_code == 200
rjson = await resp.json
assert isinstance(rjson, list)
assert rjson[0]['id'] == genned_uid
# delete
resp = await test_cli.delete(f'/api/v6/admin/users/{genned_uid}', headers={
'Authorization': token
})
assert resp.status_code == 200
rjson = await resp.json
assert isinstance(rjson, dict)
assert rjson['new']['id'] == genned_uid
assert rjson['old']['id'] == rjson['new']['id']