-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_main.py
269 lines (233 loc) · 7.22 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
from fastapi.testclient import TestClient
import json
import randominfo
from api.main import app
import api.schemas as schemas
from api.helpers.checks import schema_check
from key import credentials
good_bearer = {'Authorization': f"Bearer {credentials['KEY']}"}
bad_bearer = {'Authorization': f"Bearer {credentials['KEY']}-BAD"}
no_bearer = {}
# Generate Random Author / Site / Post Data
author_fn = randominfo.get_first_name()
author_ln = randominfo.get_last_name()
author_topic = randominfo.get_hobbies()[0]
site_url = f"{author_fn.lower()}{author_ln.lower()}.com"
site_name = f"All About {author_topic.title()}"
site_desc = f"This iste will teach you all about {author_topic}."
post_title = f"How to {author_topic}"
post_excerpt = f"Today, we will talk all about {author_topic} and how to get started."
author_email = f"{author_fn.lower()}@{site_url}.com"
author_disp = f"{author_fn} {author_ln}"
author_lnaddr = f"{author_fn.lower()}{author_ln.lower()}@getalby.com"
author_data = {
"email": author_email,
"first_name": author_fn,
"last_name": author_ln,
"display": author_disp,
"image": "https://theblogindex.org/img/no-image.svg",
"avatar": "https://theblogindex.org/img/no-image.svg",
"flags": [],
"rating": 100,
"disabled": False,
"value": [author_lnaddr]
}
bad_author_id = 99999
bad_site_id = bad_author_id
client = TestClient(app)
# /author/create
def test_author_create():
response = client.post(
"/author/create",
headers= good_bearer,
json = author_data
)
return_json = json.loads(response.text)
assert response.status_code == 200
for key in return_json:
data = return_json[key]
if key == "id":
global author_id
author_id = data
assert isinstance(author_id,int)
else:
assert data == author_data[key]
global site_data
site_data = {
"url": site_url,
"name": site_name,
"description": site_desc,
"rating": 100,
"flags": [],
"disabled": False,
"value": [author_lnaddr],
"user_id": author_id
}
# /author/get/all
def test_author_get_all():
response = client.get(
"/author/get/all",
headers = good_bearer
)
assert response.status_code == 200
assert schema_check(schemas.Author(**response.json()[0]))
def test_author_get_all_no_apikey():
response = client.get(
"/author/get/all"
)
assert response.status_code == 403
def test_author_get_all_bad_apikey():
response = client.get(
"/author/get/all",
headers = bad_bearer
)
assert response.status_code == 400
# /author/get/by-email
def test_author_get_by_email():
response = client.get(
f"/author/get/by-email?email={author_data['email']}",
headers = good_bearer
)
assert response.status_code == 200
assert schema_check(schemas.Author(**response.json()[0]))
def test_author_get_by_email_bad_email():
response = client.get(
"/author/get/[email protected]",
headers = good_bearer
)
assert response.status_code == 400
assert response.json() == {
"detail": "no records"
}
def test_author_get_by_email_no_apikey():
response = client.get(
f"/author/get/by-email?email={author_data['email']}"
)
assert response.status_code == 403
def test_author_get_by_email_bad_apikey():
response = client.get(
f"/author/get/by-email?email={author_data['email']}",
headers = bad_bearer
)
assert response.status_code == 400
# /author/get/by-id
def test_author_get_by_id():
response = client.get(
f"/author/get/by-id?id={author_id}",
headers = good_bearer
)
assert response.status_code == 200
assert schema_check(schemas.Author(**response.json()[0]))
def test_author_get_by_id_bad_id():
response = client.get(
f"/author/get/by-id?id={bad_author_id}",
headers = good_bearer
)
assert response.status_code == 400
assert response.json() == {
"detail": "no records"
}
def test_author_get_by_id_no_apikey():
response = client.get(
f"/author/get/by-id?id={author_id}",
)
assert response.status_code == 403
def test_author_get_by_id_bad_apikey():
response = client.get(
f"/author/get/by-id?id={author_id}",
headers = bad_bearer
)
assert response.status_code == 400
# /site/create
def test_site_create():
response = client.post(
"/site/create",
headers= good_bearer,
json = site_data
)
return_json = json.loads(response.text)
assert response.status_code == 200
for key in return_json:
assert key in return_json
data = return_json[key]
if key == "id":
global site_id
site_id = data
assert isinstance(site_id,int)
else:
assert data == site_data[key]
# /site/get/all
def test_site_get_all():
response = client.get(
"/site/get/all",
headers = good_bearer
)
assert response.status_code == 200
assert schema_check(schemas.Site(**response.json()[0]))
def test_site_get_all_no_apikey():
response = client.get(
"/site/get/all"
)
assert response.status_code == 403
def test_site_get_all_bad_apikey():
response = client.get(
"/site/get/all",
headers = bad_bearer
)
assert response.status_code == 400
# /site/get/by-author_email
def test_site_get_by_email():
response = client.get(
f"/site/get/by-author_email?author_email={author_data['email']}",
headers = good_bearer
)
assert response.status_code == 200
assert schema_check(schemas.Site(**response.json()[0]))
def test_site_get_by_email_bad_email():
response = client.get(
"/site/get/[email protected]",
headers = good_bearer
)
assert response.status_code == 400
assert response.json() == {
"detail": "no records"
}
def test_site_get_by_email_no_apikey():
response = client.get(
f"/site/get/by-author_email?author_email={author_data['email']}"
)
assert response.status_code == 403
def test_site_get_by_email_bad_apikey():
response = client.get(
f"/site/get/by-author_email?author_email={author_data['email']}",
headers = bad_bearer
)
assert response.status_code == 400
# /site/get/by-id
def test_site_get_by_id():
response = client.get(
f"/site/get/by-id?id={site_id}",
headers = good_bearer
)
assert response.status_code == 200
assert schema_check(schemas.Site(**response.json()))
def test_site_get_by_id_bad_id():
response = client.get(
f"/site/get/by-id?id={bad_site_id}",
headers = good_bearer
)
assert response.status_code == 400
assert response.json() == {
"detail": "no records"
}
def test_site_get_by_id_no_apikey():
response = client.get(
f"/site/get/by-id?id={site_id}",
)
assert response.status_code == 403
def test_site_get_by_id_bad_apikey():
response = client.get(
f"/site/get/by-id?id={site_id}",
headers = bad_bearer
)
assert response.status_code == 400