test_openapi: Make testing.yml a full conformant specification.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2022-01-11 21:14:04 -08:00 committed by Tim Abbott
parent 465ea4ac51
commit 86c39e5792
2 changed files with 126 additions and 131 deletions

View File

@ -1,6 +1,15 @@
test1:
openapi: 3.0.1
info:
title: Test API
version: 1.0.0
servers:
- url: "http://localhost:9991/api/v1"
paths:
/test1:
get:
responses:
"200":
description: OK
content:
application/json:
schema:
@ -30,17 +39,11 @@ test1:
type: string
str2:
type: string
example:
{
"top_array":
[
{"obj": {"str3": "test"}},
[{"str1": "success", "str2": "success"}],
],
}
test2:
/test2:
get:
responses:
"200":
description: OK
content:
application/json:
schema:
@ -71,17 +74,11 @@ test2:
type: string
str2:
type: string
example:
{
"top_array":
[
{"obj": {"str3": "test", "str4": "extraneous"}},
[{"str1": "success", "str2": "success"}],
],
}
test3:
/test3:
get:
responses:
"200":
description: OK
content:
application/json:
schema:
@ -108,11 +105,3 @@ test3:
type: string
str2:
type: string
example:
{
"top_array":
[
{"obj": {"str3": "test"}},
[{"str1": "success", "str2": "success"}],
],
}

View File

@ -132,14 +132,19 @@ class OpenAPIToolsTest(ZulipTestCase):
# 'deep' opaque object. Also the parameters are a heterogeneous
# mix of arrays and objects to verify that our descent logic
# correctly gets to the the deeply nested objects.
with open(os.path.join(os.path.dirname(OPENAPI_SPEC_PATH), "testing.yaml")) as test_file:
test_filename = os.path.join(os.path.dirname(OPENAPI_SPEC_PATH), "testing.yaml")
with open(test_filename) as test_file:
test_dict = yaml.safe_load(test_file)
openapi_spec.openapi()["paths"]["testing"] = test_dict
try:
with patch("zerver.openapi.openapi.openapi_spec", OpenAPISpec(test_filename)):
validate_against_openapi_schema(
(test_dict["test1"]["responses"]["200"]["content"]["application/json"]["example"]),
"testing",
"test1",
{
"top_array": [
{"obj": {"str3": "test"}},
[{"str1": "success", "str2": "success"}],
],
},
"/test1",
"get",
"200",
)
with self.assertRaisesRegex(
@ -147,13 +152,14 @@ class OpenAPIToolsTest(ZulipTestCase):
r"\{'obj': \{'str3': 'test', 'str4': 'extraneous'\}\} is not valid under any of the given schemas",
):
validate_against_openapi_schema(
(
test_dict["test2"]["responses"]["200"]["content"]["application/json"][
"example"
]
),
"testing",
"test2",
{
"top_array": [
{"obj": {"str3": "test", "str4": "extraneous"}},
[{"str1": "success", "str2": "success"}],
],
},
"/test2",
"get",
"200",
)
with self.assertRaisesRegex(
@ -162,10 +168,10 @@ class OpenAPIToolsTest(ZulipTestCase):
):
# Checks for opaque objects
validate_schema(
test_dict["test3"]["responses"]["200"]["content"]["application/json"]["schema"]
test_dict["paths"]["/test3"]["get"]["responses"]["200"]["content"][
"application/json"
]["schema"]
)
finally:
openapi_spec.openapi()["paths"].pop("testing", None)
def test_live_reload(self) -> None:
# Force the reload by making the last update date < the file's last