test_users.py 3.99 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
from tests.credentials import CREDS
26
from litecord.enums import UserFlags
27

28

29
async def _search(test_cli, *, username='', discrim='', token=None):
30
    token = token or await login('admin', test_cli)
31 32 33 34 35 36 37 38 39 40 41

    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
    # delete the original record since the DELETE endpoint will just
    # replace the user by a "Deleted User <random hex>", and we don't want
    # to have obsolete users filling up our db every time we run tests
    await test_cli.app.db.execute("""
    DELETE FROM users WHERE id = $1
    """, int(user_id))
93

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

@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)


117
@pytest.mark.asyncio
118 119
async def test_user_update(test_cli):
    """Test user update."""
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
    token = await login('admin', test_cli)
    rjson = await _setup_user(test_cli, token=token)

    user_id = rjson['id']

    # test update

    try:
        # set them as partner flag
        resp = await test_cli.patch(f'/api/v6/admin/users/{user_id}', headers={
            'Authorization': token
        }, json={
            'flags': UserFlags.partner,
        })

        assert resp.status_code == 200
        rjson = await resp.json
        assert rjson['id'] == user_id
        assert rjson['flags'] == UserFlags.partner

        # TODO: maybe we can check for side effects by fetching the
        # user manually too...
    finally:
        await _del_user(test_cli, user_id, token=token)