-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_api.py
327 lines (246 loc) · 12.6 KB
/
test_api.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import base64
import json
import pytest
from app import create_app
from data.db_session import global_init, SqlAlchemyBase, create_session
from models.users import User
__engine = None
app = create_app("secret_key")
def clear_db(engine):
SqlAlchemyBase.metadata.drop_all(engine)
SqlAlchemyBase.metadata.create_all(engine)
@pytest.fixture
def client():
global __engine
app.config['TESTING'] = True
with app.test_client() as client:
with app.app_context():
__engine = global_init('db/api_test.db')
yield client
def test_users_wrong_registration_fields(client):
user = User(name="admin",
email="[email protected]",
is_admin=1)
user.set_password("admin12345")
db_sess = create_session()
db_sess.add(user)
db_sess.commit()
json_request = {"data": [{"name": "Саша",
"email": "[email protected]",
"password": "password1"},
{"name": "Маша",
"password": "password1"},
{"name": "Саша",
"email": "[email protected]",
"password": "password1"}
]}
credentials = base64.b64encode(b"[email protected]:admin12345").decode("utf-8")
response = client.post("/api/users", json=json_request,
headers={"Authorization": "Basic " + credentials})
assert response.status_code == 400
assert json.loads(response.data) == {
"validation_error": {"bad_params": [{"name": "Маша", "password": "password1"}]}
}
def test_correct_registration_users(client):
json_request = {"data": [{"name": "Саша",
"email": "[email protected]",
"password": "password1"},
{"name": "Маша",
"email": "[email protected]",
"password": "password2"},
{"name": "Саша",
"email": "[email protected]",
"password": "password3"}
]}
credentials = base64.b64encode(b"[email protected]:admin12345").decode("utf-8")
response = client.post("/api/users", json=json_request,
headers={"Authorization": "Basic " + credentials})
assert response.status_code == 201
assert json.loads(response.data) == {"users": [{"email": "[email protected]"},
{"email": "[email protected]"},
{"email": "[email protected]"}]}
def test_patch_user_wrong_id(client):
json_request = {"name": "Саша"}
credentials = base64.b64encode(b"[email protected]:admin12345").decode("utf-8")
response = client.patch("/api/users/666", json=json_request,
headers={"Authorization": "Basic " + credentials})
assert response.status_code == 404
def test_user_patch(client):
json_request = {"name": "Саша"}
credentials = base64.b64encode(b"[email protected]:password2").decode("utf-8")
response = client.patch("/api/users/3", json=json_request,
headers={"Authorization": "Basic " + credentials})
assert response.status_code == 200
assert json.loads(response.data) == {"user": {"name": "Саша",
"email": "[email protected]",
"id": 3}}
def test_user_patch_forbidden(client):
json_request = {"name": "Саша"}
credentials = base64.b64encode(b"[email protected]:password2").decode("utf-8")
response = client.patch("/api/users/4", json=json_request,
headers={"Authorization": "Basic " + credentials})
assert response.status_code == 403
def test_user_delete_wrong_id(client):
credentials = base64.b64encode(b"[email protected]:admin12345").decode("utf-8")
response = client.delete("/api/users/666",
headers={"Authorization": "Basic " + credentials})
assert response.status_code == 404
def test_user_delete_correct(client):
credentials = base64.b64encode(b"[email protected]:admin12345").decode("utf-8")
response = client.delete("/api/users/3",
headers={"Authorization": "Basic " + credentials})
assert response.status_code == 200
assert json.loads(response.data) == {"banned_id": 3}
def test_user_get_wrong_id(client):
credentials = base64.b64encode(b"[email protected]:admin12345").decode("utf-8")
response = client.get("/api/users/666",
headers={"Authorization": "Basic " + credentials})
assert response.status_code == 404
def test_user_correct_get(client):
credentials = base64.b64encode(b"[email protected]:password3").decode("utf-8")
response = client.get("/api/users/4",
headers={"Authorization": "Basic " + credentials})
assert response.status_code == 200
def test_user_register_same_email(client):
json_data = {"name": "Даша",
"email": "[email protected]",
"password": "password1"}
response = client.post("/api/users/personal_register", json=json_data)
assert response.status_code == 400
assert json.loads(response.data) == {"error": "email have been used"}
def test_correct_user_register(client):
json_data = {"name": "Даша",
"email": "[email protected]",
"password": "password4"}
response = client.post("/api/users/personal_register", json=json_data)
assert response.status_code == 201
assert json.loads(response.data) == {"email": "[email protected]"}
def test_wrong_params_category(client):
credentials = base64.b64encode(b"[email protected]:admin12345").decode("utf-8")
response = client.post("/api/categories",
headers={"Authorization": "Basic " + credentials})
assert response.status_code == 400
def test_correct_params_category(client):
json_data = {"title": "Улитка в баре"}
credentials = base64.b64encode(b"[email protected]:admin12345").decode("utf-8")
response = client.post("/api/categories", json=json_data,
headers={"Authorization": "Basic " + credentials})
assert response.status_code == 201
assert json.loads(response.data) == {"title": "Улитка в баре"}
def test_incorect_param_anecdotes_adding(client):
json_data = {"data": [{"name": "Test1",
"text": "test",
"category": "Улитка в баре"},
{"name": "Test2",
"text": "test",
"category": "Улитка"},
{"name": "Test1",
"category": "Улитка в баре"}]}
credentials = base64.b64encode(b"[email protected]:password3").decode("utf-8")
response = client.post("/api/anecdotes", json=json_data,
headers={"Authorization": "Basic " + credentials})
assert response.status_code == 400
assert json.loads(response.data) == {"validation_error": {
"bad_params": [{"name": "Test2",
"text": "test",
"category": "Улитка"},
{"name": "Test1",
"category": "Улитка в баре"}]
}}
def test_correct_anecdotes_adding(client):
json_data = {"data": [{"name": "Test1",
"text": "test",
"category": "Улитка в баре"},
{"name": "Test2",
"text": "test",
"category": "Улитка в баре"},
{"name": "Test3",
"text": "test",
"category": "Улитка в баре"}]}
credentials = base64.b64encode(b"[email protected]:password3").decode("utf-8")
response = client.post("/api/anecdotes", json=json_data,
headers={"Authorization": "Basic " + credentials})
assert response.status_code == 201
assert json.loads(response.data) == {"anecdotes": [{"name": "Test1"},
{"name": "Test2"},
{"name": "Test3"}]}
def test_one_anecdote_adding(client):
json_data = {"name": "Test4",
"text": "test",
"category": "Улитка в баре"}
credentials = base64.b64encode(b"[email protected]:password3").decode("utf-8")
response = client.post("/api/anecdote", json=json_data,
headers={"Authorization": "Basic " + credentials})
assert response.status_code == 201
def test_get_not_moderated_anecdotes(client):
response = client.get("/api/anecdotes/moderate")
return response.status_code == 200
def test_incorrect_id_moderation(client):
json_data = {"anecdote_id": 66666,
"is_published": 1}
credentials = base64.b64encode(b"[email protected]:admin12345").decode("utf-8")
response = client.patch("/api/anecdotes/moderate", json=json_data,
headers={"Authorization": "Basic " + credentials})
assert response.status_code == 404
def test_correct_moderation(client):
json_data = {"anecdote_id": 1,
"is_published": 1}
credentials = base64.b64encode(b"[email protected]:admin12345").decode("utf-8")
response = client.patch("/api/anecdotes/moderate", json=json_data,
headers={"Authorization": "Basic " + credentials})
assert response.status_code == 200
assert json.loads(response.data) == {"id": 1}
def test_get_random_anecdote(client):
response = client.get("/api/anecdote")
assert response.status_code == 200
def test_create_comment(client):
json_data = {"anecdote_id": 1,
"text": "good anec"}
credentials = base64.b64encode(b"[email protected]:admin12345").decode("utf-8")
response = client.post("/api/comments", json=json_data,
headers={"Authorization": "Basic " + credentials})
assert response.status_code == 201
def test_create_comment_wrong_id(client):
json_data = {"anecdote_id": 666,
"text": "good anec"}
credentials = base64.b64encode(b"[email protected]:admin12345").decode("utf-8")
response = client.post("/api/comments", json=json_data,
headers={"Authorization": "Basic " + credentials})
assert response.status_code == 404
def test_create_comment_not_pudlished(client):
json_data = {"anecdote_id": 2,
"text": "good anec"}
credentials = base64.b64encode(b"[email protected]:admin12345").decode("utf-8")
response = client.post("/api/comments", json=json_data,
headers={"Authorization": "Basic " + credentials})
assert response.status_code == 400
assert json.loads(response.data) == {"error": "anecdote haven't been published"}
def test_del_comment_wrong_id(client):
json_data = {"comment_id": 666}
credentials = base64.b64encode(b"[email protected]:admin12345").decode("utf-8")
response = client.delete("/api/comments", json=json_data,
headers={"Authorization": "Basic " + credentials})
assert response.status_code == 404
def test_del_comment(client):
json_data = {"comment_id": 1}
credentials = base64.b64encode(b"[email protected]:admin12345").decode("utf-8")
response = client.delete("/api/comments", json=json_data,
headers={"Authorization": "Basic " + credentials})
assert response.status_code == 200
def test_get_likes(client):
credentials = base64.b64encode(b"[email protected]:admin12345").decode("utf-8")
response = client.get("/api/likes",
headers={"Authorization": "Basic " + credentials})
assert response.status_code == 200
def test_put_like(client):
json_data = {"anecdote_id": 1,
"value": -1}
credentials = base64.b64encode(b"[email protected]:admin12345").decode("utf-8")
response = client.post("/api/likes", json=json_data,
headers={"Authorization": "Basic " + credentials})
assert response.status_code == 201
# Не отбирайте костылик. Я инвалид, ножки болят(Александр)
def test_for_clearing_db():
assert isinstance("Not test", str)
SqlAlchemyBase.metadata.drop_all(__engine)
SqlAlchemyBase.metadata.create_all(__engine)