forked from snok/drf-openapi-tester
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_openapi_object.py
115 lines (99 loc) · 4.7 KB
/
test_openapi_object.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
import pytest
from openapi_tester import SchemaTester
from openapi_tester.config import OpenAPITestConfig
from openapi_tester.exceptions import DocumentationError
def test_missing_response_key_error():
expected_error_message = (
'The following property was found in the schema definition, but is missing from the response data: "one"'
"\n\nReference:\n\nPOST /endpoint > response > one"
'\n\nResponse body:\n {\n "two": 2\n}'
'\nSchema section:\n {\n "one": {\n "type": "int"\n }\n}'
"\n\nHint: Remove the key from your OpenAPI docs, or include it in your API response"
)
tester = SchemaTester()
with pytest.raises(DocumentationError, match=expected_error_message):
tester.test_openapi_object(
{"required": ["one"], "properties": {"one": {"type": "int"}}},
{"two": 2},
OpenAPITestConfig(reference="POST /endpoint > response"),
)
def test_missing_schema_key_error():
expected_error_message = (
'The following property was found in the response data, but is missing from the schema definition: "two"'
"\n\nReference:"
"\n\nPOST /endpoint > response > two"
'\n\nResponse body:\n {\n "one": 1,\n "two": 2\n}'
'\n\nSchema section:\n {\n "one": {\n "type": "integer"\n }\n}'
"\n\nHint: Remove the key from your API response, or include it in your OpenAPI docs"
)
tester = SchemaTester()
with pytest.raises(DocumentationError, match=expected_error_message):
tester.test_openapi_object(
{"required": ["one"], "properties": {"one": {"type": "integer"}}},
{"one": 1, "two": 2},
OpenAPITestConfig(reference="POST /endpoint > response"),
)
def test_key_in_write_only_properties_error():
expected_error_message = (
'The following property was found in the response, but is documented as being "writeOnly": "one"'
"\n\nReference:"
"\n\nPOST /endpoint > response > one"
'\n\nResponse body:\n {\n "one": 1\n}'
'\nSchema section:\n {\n "one": {\n "type": "integer",\n "writeOnly": true\n }\n}'
'\n\nHint: Remove the key from your API response, or remove the "WriteOnly" restriction'
)
tester = SchemaTester()
with pytest.raises(DocumentationError, match=expected_error_message):
tester.test_openapi_object(
{"properties": {"one": {"type": "integer", "writeOnly": True}}},
{"one": 1},
OpenAPITestConfig(reference="POST /endpoint > response"),
)
def test_key_in_read_only_properties_error():
expected_error_message = (
'The following property was found in the request, but is documented as being "readOnly": "one"'
"\n\nReference:"
"\n\nPOST /endpoint > request > one"
'\n\nRequest body:\n {\n "one": 1\n}'
'\nSchema section:\n {\n "one": {\n "type": "integer",\n "readOnly": true\n }\n}'
'\n\nHint: Remove the key from your API request, or remove the "ReadOnly" restriction'
)
tester = SchemaTester()
with pytest.raises(DocumentationError, match=expected_error_message):
tester.test_openapi_object(
{"properties": {"one": {"type": "integer", "readOnly": True}}},
{"one": 1},
OpenAPITestConfig(
reference="POST /endpoint > request", http_message="request"
),
)
def test_key_in_read_only_properties_response_does_not_raise_error():
tester = SchemaTester()
tester.test_openapi_object(
{"properties": {"one": {"type": "integer", "readOnly": True}}},
{"one": 1},
OpenAPITestConfig(
reference="POST /endpoint > response", http_message="response"
),
)
def test_date_serialization():
tester = SchemaTester()
tester.test_openapi_object(
{"properties": {"updated_at": {"type": "string", "format": "date-time"}}},
{"updated_at": "2021-12-12T12:12:12Z"},
OpenAPITestConfig(reference="POST /endpoint > response"),
)
def test_wrong_date_error():
tester = SchemaTester()
expected_error_message = (
'\n\nExpected: a "date-time" formatted "string" value'
'\n\nReceived: "not-a-date"\n\nReference: '
"\n\nPOST /endpoint > response > updated_at"
"\n\n Response value:\n not-a-date\n Schema description:\n {'type': 'string', 'format': 'date-time'}"
)
with pytest.raises(DocumentationError, match=expected_error_message):
tester.test_openapi_object(
{"properties": {"updated_at": {"type": "string", "format": "date-time"}}},
{"updated_at": "not-a-date"},
OpenAPITestConfig(reference="POST /endpoint > response"),
)