forked from kernelci/kernelci-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_user_creation.py
152 lines (138 loc) · 4.78 KB
/
test_user_creation.py
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
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# Copyright (C) 2023 Collabora Limited
# Author: Jeny Sadadia <jeny.sadadia@collabora.com>
"""End-to-end test functions for KernelCI API user creation"""
import json
import pytest
from api.models import User, UserGroup, UserProfile
from api.db import Database
from e2e_tests.conftest import db_create
@pytest.mark.dependency(
depends=["e2e_tests/test_user_group_creation.py::test_create_user_groups"],
scope="session")
@pytest.mark.dependency()
@pytest.mark.order(2)
@pytest.mark.asyncio
async def test_create_admin_user(test_async_client):
"""
Test Case : Get hashed password using '/hash' endpoint to create an admin
user. Create the admin user using database create method.
Request authentication token using '/token' endpoint for the user and
store it in pytest global variable 'ADMIN_BEARER_TOKEN'.
"""
username = 'admin'
password = 'test'
response = await test_async_client.post(
"hash",
data=json.dumps({'password': password})
)
hashed_password = response.json()
assert response.status_code == 200
profile = UserProfile(
username=username,
hashed_password=hashed_password,
groups=[UserGroup(name="admin")]
)
obj = await db_create(
Database.COLLECTIONS[User],
User(profile=profile)
)
assert obj is not None
response = await test_async_client.post(
"token",
headers={
"Accept": "application/json",
"Content-Type": "application/x-www-form-urlencoded"
},
data={
'username': username, 'password': password, 'scope': 'admin users'
}
)
assert response.status_code == 200
assert response.json().keys() == {
'access_token',
'token_type',
}
pytest.ADMIN_BEARER_TOKEN = response.json()['access_token']
@pytest.mark.dependency(depends=["test_create_admin_user"])
@pytest.mark.order(3)
@pytest.mark.asyncio
async def test_create_regular_user(test_async_client):
"""
Test Case : Test KernelCI API '/user' endpoint to create regular user
when requested with admin user's bearer token. Request '/token' endpoint
for the user and store it in pytest global variable 'BEARER_TOKEN'.
"""
username = 'test_user'
password = 'test'
response = await test_async_client.post(
f"user/{username}",
headers={
"Accept": "application/json",
"Authorization": f"Bearer {pytest.ADMIN_BEARER_TOKEN}"
},
data=json.dumps({'password': password})
)
assert response.status_code == 200
assert ('id', 'active',
'profile') == tuple(response.json().keys())
assert ('username', 'hashed_password',
'groups') == tuple(response.json()['profile'].keys())
response = await test_async_client.post(
"token",
headers={
"Accept": "application/json",
"Content-Type": "application/x-www-form-urlencoded"
},
data={'username': username, 'password': password}
)
assert response.status_code == 200
assert response.json().keys() == {
'access_token',
'token_type',
}
pytest.BEARER_TOKEN = response.json()['access_token']
@pytest.mark.asyncio
@pytest.mark.dependency(depends=["test_create_regular_user"])
async def test_whoami(test_async_client):
"""
Test Case : Test KernelCI API /whoami endpoint
Expected Result :
HTTP Response Code 200 OK
JSON with 'id', 'username', 'hashed_password'
and 'active' keys
"""
response = await test_async_client.get(
"whoami",
headers={
"Accept": "application/json",
"Authorization": f"Bearer {pytest.BEARER_TOKEN}"
},
)
assert response.status_code == 200
assert ('id', 'active',
'profile') == tuple(response.json().keys())
assert ('username', 'hashed_password',
'groups') == tuple(response.json()['profile'].keys())
assert response.json()['profile']['username'] == 'test_user'
@pytest.mark.asyncio
@pytest.mark.dependency(depends=["test_create_regular_user"])
async def test_create_user_negative(test_async_client):
"""
Test Case : Test KernelCI API /user endpoint when requested
with regular user's bearer token.
Expected Result :
HTTP Response Code 401 Unauthorized
JSON with 'detail' key denoting 'Access denied' error
"""
response = await test_async_client.post(
"user/test",
headers={
"Accept": "application/json",
"Authorization": f"Bearer {pytest.BEARER_TOKEN}"
},
data=json.dumps({'password': 'test'})
)
assert response.status_code == 401
assert response.json() == {'detail': 'Access denied'}