-
-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathtest_django_project.py
397 lines (352 loc) · 11.7 KB
/
test_django_project.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import os
import sys
from base64 import b64encode
from json import dumps
from unittest import mock
import pytest
from django.test.utils import override_settings
class BaseTestDjangoProject:
api_key = "12345"
@property
def api_key_encoded(self):
api_key_bytes = self.api_key.encode("utf8")
api_key_bytes_enc = b64encode(api_key_bytes)
return str(api_key_bytes_enc, "utf8")
@pytest.fixture(autouse=True, scope="module")
def django_setup(self):
directory = os.path.abspath(os.path.dirname(__file__))
django_project_dir = os.path.join(directory, "data/v3.0")
sys.path.insert(0, django_project_dir)
with mock.patch.dict(
os.environ,
{
"DJANGO_SETTINGS_MODULE": "djangoproject.settings",
},
):
import django
django.setup()
yield
sys.path.remove(django_project_dir)
@pytest.fixture
def client(self):
from django.test import Client
return Client()
class TestPetListView(BaseTestDjangoProject):
def test_get_no_required_param(self, client):
headers = {
"HTTP_AUTHORIZATION": "Basic testuser",
"HTTP_HOST": "petstore.swagger.io",
}
with pytest.warns(DeprecationWarning):
response = client.get("/v1/pets", **headers)
expected_data = {
"errors": [
{
"type": (
"<class 'openapi_core.validation.request.exceptions."
"MissingRequiredParameter'>"
),
"status": 400,
"title": "Missing required query parameter: limit",
}
]
}
assert response.status_code == 400
assert response.json() == expected_data
def test_get_valid(self, client):
data_json = {
"limit": 12,
}
headers = {
"HTTP_AUTHORIZATION": "Basic testuser",
"HTTP_HOST": "petstore.swagger.io",
}
with pytest.warns(DeprecationWarning):
response = client.get("/v1/pets", data_json, **headers)
expected_data = {
"data": [
{
"id": 12,
"name": "Cat",
"ears": {
"healthy": True,
},
},
],
}
assert response.status_code == 200
assert response.json() == expected_data
def test_post_server_invalid(self, client):
headers = {
"HTTP_HOST": "petstore.swagger.io",
}
response = client.post("/v1/pets", **headers)
expected_data = {
"errors": [
{
"type": (
"<class 'openapi_core.templating.paths.exceptions."
"ServerNotFound'>"
),
"status": 400,
"title": (
"Server not found for "
"http://petstore.swagger.io/v1/pets"
),
}
]
}
assert response.status_code == 400
assert response.json() == expected_data
def test_post_required_header_param_missing(self, client):
client.cookies.load({"user": 1})
pet_name = "Cat"
pet_tag = "cats"
pet_street = "Piekna"
pet_city = "Warsaw"
pet_healthy = False
data_json = {
"name": pet_name,
"tag": pet_tag,
"position": 2,
"address": {
"street": pet_street,
"city": pet_city,
},
"healthy": pet_healthy,
"wings": {
"healthy": pet_healthy,
},
}
content_type = "application/json"
headers = {
"HTTP_AUTHORIZATION": "Basic testuser",
"HTTP_HOST": "staging.gigantic-server.com",
}
response = client.post(
"/v1/pets", data_json, content_type, secure=True, **headers
)
expected_data = {
"errors": [
{
"type": (
"<class 'openapi_core.validation.request.exceptions."
"MissingRequiredParameter'>"
),
"status": 400,
"title": "Missing required header parameter: api-key",
}
]
}
assert response.status_code == 400
assert response.json() == expected_data
def test_post_media_type_invalid(self, client):
client.cookies.load({"user": 1})
data = "data"
content_type = "text/html"
headers = {
"HTTP_AUTHORIZATION": "Basic testuser",
"HTTP_HOST": "staging.gigantic-server.com",
"HTTP_API_KEY": self.api_key_encoded,
}
response = client.post(
"/v1/pets", data, content_type, secure=True, **headers
)
expected_data = {
"errors": [
{
"type": (
"<class 'openapi_core.templating.media_types."
"exceptions.MediaTypeNotFound'>"
),
"status": 415,
"title": (
"Content for the following mimetype not found: "
"text/html. "
"Valid mimetypes: ['application/json', 'text/plain']"
),
}
]
}
assert response.status_code == 415
assert response.json() == expected_data
def test_post_required_cookie_param_missing(self, client):
data_json = {
"id": 12,
"name": "Cat",
"ears": {
"healthy": True,
},
}
content_type = "application/json"
headers = {
"HTTP_AUTHORIZATION": "Basic testuser",
"HTTP_HOST": "staging.gigantic-server.com",
"HTTP_API_KEY": self.api_key_encoded,
}
response = client.post(
"/v1/pets", data_json, content_type, secure=True, **headers
)
expected_data = {
"errors": [
{
"type": (
"<class 'openapi_core.validation.request.exceptions."
"MissingRequiredParameter'>"
),
"status": 400,
"title": "Missing required cookie parameter: user",
}
]
}
assert response.status_code == 400
assert response.json() == expected_data
@pytest.mark.parametrize(
"data_json",
[
{
"id": 12,
"name": "Cat",
"ears": {
"healthy": True,
},
},
{
"id": 12,
"name": "Bird",
"wings": {
"healthy": True,
},
},
],
)
def test_post_valid(self, client, data_json):
client.cookies.load({"user": 1})
content_type = "application/json"
headers = {
"HTTP_AUTHORIZATION": "Basic testuser",
"HTTP_HOST": "staging.gigantic-server.com",
"HTTP_API_KEY": self.api_key_encoded,
}
response = client.post(
"/v1/pets", data_json, content_type, secure=True, **headers
)
assert response.status_code == 201
assert not response.content
class TestPetDetailView(BaseTestDjangoProject):
def test_get_server_invalid(self, client):
response = client.get("/v1/pets/12")
expected_data = (
b"You may need to add 'testserver' to ALLOWED_HOSTS."
)
assert response.status_code == 400
assert expected_data in response.content
def test_get_unauthorized(self, client):
headers = {
"HTTP_HOST": "petstore.swagger.io",
}
response = client.get("/v1/pets/12", **headers)
expected_data = {
"errors": [
{
"type": (
"<class 'openapi_core.templating.security.exceptions."
"SecurityNotFound'>"
),
"status": 403,
"title": (
"Security not found. Schemes not valid for any "
"requirement: [['petstore_auth']]"
),
}
]
}
assert response.status_code == 403
assert response.json() == expected_data
def test_delete_method_invalid(self, client):
headers = {
"HTTP_AUTHORIZATION": "Basic testuser",
"HTTP_HOST": "petstore.swagger.io",
}
response = client.delete("/v1/pets/12", **headers)
expected_data = {
"errors": [
{
"type": (
"<class 'openapi_core.templating.paths.exceptions."
"OperationNotFound'>"
),
"status": 405,
"title": (
"Operation delete not found for "
"http://petstore.swagger.io/v1/pets/12"
),
}
]
}
assert response.status_code == 405
assert response.json() == expected_data
def test_get_valid(self, client):
headers = {
"HTTP_AUTHORIZATION": "Basic testuser",
"HTTP_HOST": "petstore.swagger.io",
}
response = client.get("/v1/pets/12", **headers)
expected_data = {
"data": {
"id": 12,
"name": "Cat",
"ears": {
"healthy": True,
},
},
}
assert response.status_code == 200
assert response.json() == expected_data
class BaseTestDRF(BaseTestDjangoProject):
@pytest.fixture
def api_client(self):
from rest_framework.test import APIClient
return APIClient()
class TestDRFPetListView(BaseTestDRF):
def test_post_valid(self, api_client):
api_client.cookies.load({"user": 1})
content_type = "application/json"
data_json = {
"id": 12,
"name": "Cat",
"ears": {
"healthy": True,
},
}
headers = {
"HTTP_AUTHORIZATION": "Basic testuser",
"HTTP_HOST": "staging.gigantic-server.com",
"HTTP_API_KEY": self.api_key_encoded,
}
response = api_client.post(
"/v1/pets",
dumps(data_json),
content_type=content_type,
secure=True,
**headers,
)
assert response.status_code == 201
assert not response.content
class TestDRFTagListView(BaseTestDRF):
def test_get_response_invalid(self, client):
headers = {
"HTTP_AUTHORIZATION": "Basic testuser",
"HTTP_HOST": "petstore.swagger.io",
}
response = client.get("/v1/tags", **headers)
assert response.status_code == 415
def test_get_skip_response_validation(self, client):
headers = {
"HTTP_AUTHORIZATION": "Basic testuser",
"HTTP_HOST": "petstore.swagger.io",
}
with override_settings(OPENAPI_RESPONSE_CLS=None):
response = client.get("/v1/tags", **headers)
assert response.status_code == 200
assert response.content == b"success"