forked from snok/drf-openapi-tester
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_clients.py
229 lines (170 loc) · 7.35 KB
/
test_clients.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
import functools
from typing import TYPE_CHECKING
import orjson
import pytest
from django.test.testcases import SimpleTestCase
from rest_framework import status
from openapi_tester.clients import OpenAPIClient, OpenAPINinjaClient
from openapi_tester.exceptions import (
APIFrameworkNotInstalledError,
DocumentationError,
UndocumentedSchemaSectionError,
)
from openapi_tester.schema_tester import SchemaTester
if TYPE_CHECKING:
from pathlib import Path
@pytest.fixture
def openapi_client(settings) -> OpenAPIClient:
"""Sample ``OpenAPIClient`` instance to use in tests."""
# use `drf-yasg` schema loader in tests
settings.INSTALLED_APPS = [
app for app in settings.INSTALLED_APPS if app != "drf_spectacular"
]
return OpenAPIClient()
def test_init_schema_tester_passed():
"""Ensure passed ``SchemaTester`` instance is used."""
schema_tester = SchemaTester()
client = OpenAPIClient(schema_tester=schema_tester)
assert client.schema_tester is schema_tester
def test_init_schema_tester_passed_ninja():
"""Ensure passed ``SchemaTester`` instance is used."""
schema_tester = SchemaTester()
client = OpenAPINinjaClient(router_or_app=None, schema_tester=schema_tester)
assert client.schema_tester is schema_tester
def test_get_request(cars_api_schema: "Path"):
schema_tester = SchemaTester(schema_file_path=str(cars_api_schema))
openapi_client = OpenAPIClient(schema_tester=schema_tester)
response = openapi_client.get(path="/api/v1/cars/correct")
assert response.status_code == status.HTTP_200_OK
def test_post_request(openapi_client):
response = openapi_client.post(
path="/api/v1/vehicles",
data={"vehicle_type": "suv"},
content_type="application/json",
)
assert response.status_code == status.HTTP_201_CREATED
def test_post_request_no_content_type(openapi_client):
response = openapi_client.post(
path="/api/v1/vehicles",
data={"vehicle_type": "suv"},
)
assert response.status_code == status.HTTP_201_CREATED
def test_request_validation_is_not_triggered_for_bad_requests(pets_api_schema: "Path"):
schema_tester = SchemaTester(schema_file_path=str(pets_api_schema))
openapi_client = OpenAPIClient(schema_tester=schema_tester)
response = openapi_client.post(path="/api/pets", data={})
assert response.status_code == status.HTTP_400_BAD_REQUEST
def test_request_body_extra_non_documented_field(pets_api_schema: "Path"):
"""Ensure ``SchemaTester`` raises exception when request is successful but an
extra field non-documented was sent."""
schema_tester = SchemaTester(schema_file_path=str(pets_api_schema))
openapi_client = OpenAPIClient(schema_tester=schema_tester)
with pytest.raises(DocumentationError):
openapi_client.post(
path="/api/pets",
data={"name": "doggie", "age": 1},
content_type="application/json",
)
def test_request_with_write_only_field(pets_api_schema: "Path"):
"""Ensure validation doesn't raise exception when request a write-only field is
included in the request, and not in the response."""
schema_tester = SchemaTester(schema_file_path=str(pets_api_schema))
openapi_client = OpenAPIClient(schema_tester=schema_tester)
openapi_client.post(
path="/api/pets",
data={"name": "doggie", "tag": "Bulldog"},
content_type="application/json",
)
def test_request_with_read_only_field(pets_api_schema: "Path"):
"""Ensure validation doesn't raise exception when request a write-only field is
included in the request, and not in the response."""
schema_tester = SchemaTester(schema_file_path=str(pets_api_schema))
openapi_client = OpenAPIClient(schema_tester=schema_tester)
with pytest.raises(DocumentationError):
openapi_client.post(
path="/api/pets",
data={"id": 1, "name": "doggie", "tag": "Bulldog"},
content_type="application/json",
)
def test_response_with_write_only_field(pets_api_schema: "Path"):
"""Ensure validation raises exception when response includes a write-only field."""
schema_tester = SchemaTester(schema_file_path=str(pets_api_schema))
openapi_client = OpenAPIClient(schema_tester=schema_tester)
with pytest.raises(DocumentationError):
openapi_client.get(path="/api/pets")
def test_request_body_non_null_fields(pets_api_schema: "Path"):
schema_tester = SchemaTester(schema_file_path=str(pets_api_schema))
openapi_client = OpenAPIClient(schema_tester=schema_tester)
with pytest.raises(DocumentationError):
openapi_client.post(
path="/api/pets",
data={"name": "doggie", "tag": None},
content_type="application/json",
)
def test_request_multiple_types_supported(pets_api_schema: "Path"):
schema_tester = SchemaTester(schema_file_path=str(pets_api_schema))
openapi_client = OpenAPIClient(schema_tester=schema_tester)
openapi_client.post(path="/api/pets", data={"name": "doggie", "tag": "pet"})
def test_request_multiple_types_null_type_allowed(pets_api_schema: "Path"):
schema_tester = SchemaTester(schema_file_path=str(pets_api_schema))
openapi_client = OpenAPIClient(schema_tester=schema_tester)
openapi_client.post(path="/api/pets", data={"name": None, "tag": "pet"})
def test_request_on_empty_list(openapi_client):
"""Ensure ``SchemaTester`` doesn't raise exception when response is empty list."""
response = openapi_client.generic(
method="GET",
path="/api/v1/empty-names",
content_type="application/json",
)
assert response.status_code == status.HTTP_200_OK, response.data
@pytest.mark.parametrize(
("generic_kwargs", "raises_kwargs"),
[
(
{
"method": "POST",
"path": "/api/v1/vehicles",
"data": orjson.dumps({"vehicle_type": "1" * 50}).decode("utf-8"),
"content_type": "application/json",
},
{
"expected_exception": UndocumentedSchemaSectionError,
"match": "Undocumented status code: 400",
},
),
(
{"method": "PUT", "path": "/api/v1/animals"},
{
"expected_exception": UndocumentedSchemaSectionError,
"match": "Undocumented method: put",
},
),
],
)
def test_request_invalid_response(
openapi_client,
generic_kwargs,
raises_kwargs,
):
"""Ensure ``SchemaTester`` raises an exception when response is invalid."""
with pytest.raises(**raises_kwargs): # noqa: PT010
openapi_client.generic(**generic_kwargs)
@pytest.mark.parametrize(
"openapi_client_class",
[
OpenAPIClient,
functools.partial(OpenAPIClient, schema_tester=SchemaTester()),
],
)
def test_django_testcase_client_class(openapi_client_class):
"""Ensure example from README.md about Django test case works fine."""
class DummyTestCase(SimpleTestCase):
"""Django ``TestCase`` with ``OpenAPIClient`` client."""
client_class = openapi_client_class
test_case = DummyTestCase()
test_case._pre_setup()
assert isinstance(test_case.client, OpenAPIClient)
def test_ninja_not_installed(ninja_not_installed):
OpenAPIClient()
with pytest.raises(APIFrameworkNotInstalledError):
OpenAPINinjaClient(router_or_app=None)