-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_main.py
150 lines (118 loc) · 3.47 KB
/
test_main.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
import time
import json
import requests
from faker import Faker
def fake_users(number=10):
fake = Faker()
users = []
for _ in range(number):
users.append(
{
"username": fake.user_name(),
"password": fake.password(),
"full_name": fake.name(),
"email": fake.email(),
"id": 0,
}
)
return users
users = fake_users() # unique?
class Admin:
token = ""
cred = {"username": "admin", "password": "admin123"}
admin = Admin()
def test_add_users():
def request(user):
return requests.post(
"http://localhost:8000/users/",
data=json.dumps(user),
headers={"accept": "application/json", "Content-Type": "application/json"},
)
res_map = map(
request,
users,
)
class bcolors:
HEADER = "\033[95m"
OKBLUE = "\033[94m"
OKCYAN = "\033[96m"
OKGREEN = "\033[92m"
WARNING = "\033[93m"
FAIL = "\033[91m"
ENDC = "\033[0m"
BOLD = "\033[1m"
UNDERLINE = "\033[4m"
print(
bcolors.OKGREEN, "START", bcolors.ENDC, flush=True, end=""
) # .2sec/req... HOWWWWWWW what? localhost??
start_time = time.perf_counter()
for res, user in zip(res_map, users):
assert res.status_code == 200
user["id"] = res.json()["id"]
assert res.json()["username"] == user["username"]
process_time = time.perf_counter() - start_time
print(
bcolors.OKCYAN,
"{:.2f}".format(process_time) + " sec",
bcolors.ENDC,
end="",
flush=True,
)
# for user in users:
# print(user["id"])
# test duplicate (email & password are unique)
dup_res = request(users[0])
assert dup_res.status_code == 400
def test_admin_token():
res = requests.post(
"http://localhost:8000/token",
admin.cred,
)
[admin.token, type] = res.json().values()
assert res.status_code == 200
assert type == "bearer"
def test_admin_get_me():
admin_res = requests.get(
"http://localhost:8000/users/me",
headers={"Authorization": "Bearer " + admin.token},
)
assert admin_res.status_code == 200
def test_admin_get_users():
res = requests.get(
"http://localhost:8000/users/",
headers={"Authorization": "Bearer " + admin.token},
)
# print(res.json())
def test_admin_update():
url = f"http://localhost:8000/users/{users[0]['id']}"
before_res = requests.get(
url,
headers={"Authorization": "Bearer " + admin.token},
)
update_res = requests.patch(
url,
headers={
"Authorization": "Bearer " + admin.token,
"accept": "application/json",
"Content-Type": "application/json",
},
data=json.dumps({"username": "aaaaaaaaa"}),
)
assert before_res.json()["username"] != update_res.json()["username"]
# print(update_res.json())
def test_admin_delete():
url = f"http://localhost:8000/users/{users[0]['id']}"
before_res = requests.get(
url,
headers={"Authorization": "Bearer " + admin.token},
)
requests.delete(
url,
headers={"Authorization": "Bearer " + admin.token},
)
update_res = requests.get(
url,
headers={"Authorization": "Bearer " + admin.token},
)
assert before_res.status_code == 200
assert update_res.status_code == 404