test_users.py 2.97 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
"""

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/>.

"""

20 21
import secrets

22 23 24
import pytest

from tests.common import login
25 26
from tests.credentials import CREDS

27

28 29 30 31 32 33 34 35 36 37 38 39 40 41
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)


42 43 44
@pytest.mark.asyncio
async def test_list_users(test_cli):
    """Try to list as many users as possible."""
45
    resp = await _search(test_cli, username=CREDS['admin']['username'])
46 47 48 49 50 51 52

    assert resp.status_code == 200
    rjson = await resp.json
    assert isinstance(rjson, list)
    assert rjson


53 54
async def _setup_user(test_cli, *, token=None) -> dict:
    token = token or await login('admin', test_cli)
55 56 57
    genned = secrets.token_hex(7)

    resp = await test_cli.post('/api/v6/admin/users', headers={
58
        'Authorization': token
59 60 61 62
    }, json={
        'username': genned,
        'email': f'{genned}@{genned}.com',
        'password': genned,
63 64
    })

65 66 67 68 69
    assert resp.status_code == 200
    rjson = await resp.json
    assert isinstance(rjson, dict)
    assert rjson['username'] == genned

70
    return rjson
71 72


73 74 75
async def _del_user(test_cli, user_id, *, token=None):
    """Delete a user."""
    token = token or await login('admin', test_cli)
76

77
    resp = await test_cli.delete(f'/api/v6/admin/users/{user_id}', headers={
78 79 80 81 82 83
        'Authorization': token
    })

    assert resp.status_code == 200
    rjson = await resp.json
    assert isinstance(rjson, dict)
84
    assert rjson['new']['id'] == user_id
85
    assert rjson['old']['id'] == rjson['new']['id']
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112


@pytest.mark.asyncio
async def test_create_delete(test_cli):
    """Create a user. Then delete them."""
    token = await login('admin', test_cli)

    rjson = await _setup_user(test_cli, token=token)

    genned = rjson['username']
    genned_uid = rjson['id']

    try:
        # 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
    finally:
        await _del_user(test_cli, genned_uid, token=token)


async def test_user_update(test_cli):
    """Test user update."""
    pass