add test_create_user to tests/test_user.py

- tests.conftest: hardcode REGISTRATIONS to true
 - tests/test_user: better assertions for test_get_me
This commit is contained in:
Luna 2019-03-31 18:15:02 -03:00
parent 4509ede535
commit d458f22967
2 changed files with 56 additions and 0 deletions

View File

@ -43,6 +43,10 @@ def _test_app(unused_tcp_port, event_loop):
main_app.config['WS_PORT'] = ws_port
main_app.config['WEBSOCKET_URL'] = f'localhost:{ws_port}'
# testing user creations requires hardcoding this to true
# on testing
main_app.config['REGISTRATIONS'] = True
# make sure we're calling the before_serving hooks
event_loop.run_until_complete(main_app.startup())

View File

@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import pytest
import secrets
from tests.common import login, get_uid
@ -33,6 +34,14 @@ async def test_get_me(test_cli):
rjson = await resp.json
assert isinstance(rjson, dict)
# incomplete user assertions, but should be enough
assert isinstance(rjson['id'], str)
assert isinstance(rjson['username'], str)
assert isinstance(rjson['discriminator'], str)
assert rjson['avatar'] is None or isinstance(rjson['avatar'], str)
assert isinstance(rjson['flags'], int)
assert isinstance(rjson['bot'], bool)
@pytest.mark.asyncio
async def test_get_me_guilds(test_cli):
@ -63,3 +72,46 @@ async def test_get_profile_self(test_cli):
assert (rjson['premium_since'] is None
or isinstance(rjson['premium_since'], str))
assert isinstance(rjson['mutual_guilds'], list)
@pytest.mark.asyncio
async def test_create_user(test_cli):
"""Test the creation and deletion of a user."""
username = secrets.token_hex(4)
_email = secrets.token_hex(5)
email = f'{_email}@{_email}.com'
password = secrets.token_hex(6)
resp = await test_cli.post('/api/v6/auth/register', json={
'username': username,
'email': email,
'password': password
})
assert resp.status_code == 200
rjson = await resp.json
assert isinstance(rjson, dict)
token = rjson['token']
assert isinstance(token, str)
resp = await test_cli.get('/api/v6/users/@me', headers={
'Authorization': token,
})
assert resp.status_code == 200
rjson = await resp.json
assert rjson['username'] == username
assert rjson['email'] == email
resp = await test_cli.post('/api/v6/users/@me/delete', headers={
'Authorization': token,
}, json={
'password': password
})
assert resp.status_code == 204
await test_cli.app.db.execute("""
DELETE FROM users WHERE id = $1
""", int(rjson['id']))