forked from kernelci/kernelci-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_user_handler.py
More file actions
203 lines (185 loc) · 6.3 KB
/
test_user_handler.py
File metadata and controls
203 lines (185 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# Copyright (C) 2022, 2023 Collabora Limited
# Author: Jeny Sadadia <jeny.sadadia@collabora.com>
# pylint: disable=unused-argument
"""Unit test function for KernelCI API user handler"""
import json
import pytest
from tests.unit_tests.conftest import (
ADMIN_BEARER_TOKEN,
BEARER_TOKEN,
)
from api.models import UserGroup, UserRead
@pytest.mark.asyncio
async def test_create_regular_user(mock_db_find_one, mock_db_create,
test_async_client):
"""
Test Case : Test KernelCI API /user/register endpoint to create regular
user when requested with admin user's bearer token
Expected Result :
HTTP Response Code 200 OK
JSON with 'id', 'username', 'email', 'groups', 'is_active'
'is_verified' and 'is_superuser' keys
"""
mock_db_find_one.return_value = None
user = UserRead(
id='65265305c74695807499037f',
username='test',
email='test@kernelci.org',
groups=[],
is_active=True,
is_verified=False,
is_superuser=False
)
mock_db_create.return_value = user
response = await test_async_client.post(
"user/register",
headers={
"Accept": "application/json",
"Authorization": ADMIN_BEARER_TOKEN
},
data=json.dumps({
'username': 'test',
'password': 'test',
'email': 'test@kernelci.org'
})
)
print(response.json())
assert response.status_code == 200
assert ('id', 'email', 'is_active', 'is_superuser', 'is_verified',
'username', 'groups') == tuple(response.json().keys())
@pytest.mark.asyncio
async def test_create_admin_user(test_async_client, mock_db_find_one,
mock_db_find_by_id, mock_db_update):
"""
Test Case : Test KernelCI API /user/register endpoint to create admin user
when requested with admin user's bearer token
Expected Result :
HTTP Response Code 200 OK
JSON with 'id', 'username', 'email', 'groups', 'is_active'
'is_verified' and 'is_superuser' keys
"""
user = UserRead(
id='61bda8f2eb1a63d2b7152419',
username='test_admin',
email='test-admin@kernelci.org',
groups=[],
is_active=True,
is_verified=False,
is_superuser=True
)
mock_db_find_one.return_value = None
mock_db_find_by_id.return_value = user
mock_db_update.return_value = user
response = await test_async_client.post(
"user/register",
headers={
"Accept": "application/json",
"Authorization": ADMIN_BEARER_TOKEN
},
data=json.dumps({
'username': 'test_admin',
'password': 'test',
'email': 'test-admin@kernelci.org',
'is_superuser': True
})
)
print(response.json())
assert response.status_code == 200
assert ('id', 'email', 'is_active', 'is_superuser', 'is_verified',
'username', 'groups') == tuple(response.json().keys())
@pytest.mark.asyncio
async def test_create_user_endpoint_negative(test_async_client):
"""
Test Case : Test KernelCI API /user/register endpoint when requested
with regular user's bearer token
Expected Result :
HTTP Response Code 401 Unauthorized
JSON with 'detail' key denoting 'Forbidden' error
"""
response = await test_async_client.post(
"user/register",
headers={
"Accept": "application/json",
"Authorization": BEARER_TOKEN
},
data=json.dumps({
'username': 'test',
'password': 'test',
'email': 'test@kernelci.org'
})
)
print(response.json())
assert response.status_code == 403
assert response.json() == {'detail': 'Forbidden'}
@pytest.mark.asyncio
async def test_create_user_with_group(test_async_client, mock_db_find_one,
mock_db_update, mock_db_find_by_id):
"""
Test Case : Test KernelCI API /user/register endpoint to create a user
with a user group
Expected Result :
HTTP Response Code 200 OK
JSON with 'id', 'username', 'email', 'groups', 'is_active'
'is_verified' and 'is_superuser' keys
"""
user = UserRead(
id='61bda8f2eb1a63d2b7152419',
username='test_admin',
email='test-admin@kernelci.org',
groups=[UserGroup(name='kernelci')],
is_active=True,
is_verified=False,
is_superuser=False
)
mock_db_find_by_id.return_value = user
mock_db_update.return_value = user
mock_db_find_one.side_effect = [None, UserGroup(name='kernelci')]
response = await test_async_client.post(
"user/register",
headers={
"Accept": "application/json",
"Authorization": ADMIN_BEARER_TOKEN
},
data=json.dumps({
'username': 'test',
'password': 'test',
'email': 'test-admin@kernelci.org',
'groups': ['kernelci']
})
)
print(response.json())
assert response.status_code == 200
assert ('id', 'email', 'is_active', 'is_superuser', 'is_verified',
'username', 'groups') == tuple(response.json().keys())
@pytest.mark.asyncio
async def test_get_user_by_id_endpoint(test_async_client,
mock_beanie_get_user_by_id):
"""
Test Case : Test KernelCI API GET /user/{user_id} endpoint with admin
token
Expected Result :
HTTP Response Code 200 OK
JSON with User object attributes
"""
user_obj = UserRead(
id='61bda8f2eb1a63d2b7152418',
username='test',
email='test@kernelci.org',
groups=[],
is_active=True,
is_verified=False,
is_superuser=False
)
mock_beanie_get_user_by_id.return_value = user_obj
response = await test_async_client.get(
"user/61bda8f2eb1a63d2b7152418",
headers={
"Accept": "application/json",
"Authorization": ADMIN_BEARER_TOKEN
})
print("response.json()", response.json())
assert response.status_code == 200
assert ('id', 'email', 'is_active', 'is_superuser', 'is_verified',
'username', 'groups') == tuple(response.json().keys())