From 86c39e5792392c9e42b84e0e441b993a2d202f9f Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Tue, 11 Jan 2022 21:14:04 -0800 Subject: [PATCH] test_openapi: Make testing.yml a full conformant specification. Signed-off-by: Anders Kaseorg --- zerver/openapi/testing.yaml | 219 +++++++++++++++++------------------ zerver/tests/test_openapi.py | 38 +++--- 2 files changed, 126 insertions(+), 131 deletions(-) diff --git a/zerver/openapi/testing.yaml b/zerver/openapi/testing.yaml index c2d56b08bc..a897496b0a 100644 --- a/zerver/openapi/testing.yaml +++ b/zerver/openapi/testing.yaml @@ -1,118 +1,107 @@ -test1: - responses: - "200": - content: - application/json: - schema: - type: object - additionalProperties: false - properties: - top_array: - type: array - items: - oneOf: - - type: object - properties: - obj: - oneOf: - - type: array - items: +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: + type: object + additionalProperties: false + properties: + top_array: + type: array + items: + oneOf: + - type: object + properties: + obj: + oneOf: + - type: array + items: + type: string + - type: object + properties: + str3: + type: string + - type: array + items: + type: object + properties: + str1: type: string - - type: object - properties: - str3: - type: string - - type: array - items: - type: object - properties: - str1: - type: string - str2: - type: string - example: - { - "top_array": - [ - {"obj": {"str3": "test"}}, - [{"str1": "success", "str2": "success"}], - ], - } -test2: - responses: - "200": - content: - application/json: - schema: - type: object - additionalProperties: false - properties: - top_array: - type: array - items: - oneOf: - - type: object - properties: - obj: - oneOf: - - type: array - items: + str2: type: string - - type: object - additionalProperties: false - properties: - str3: - type: string - - type: array - items: - type: object - properties: - str1: - type: string - str2: - type: string - example: - { - "top_array": - [ - {"obj": {"str3": "test", "str4": "extraneous"}}, - [{"str1": "success", "str2": "success"}], - ], - } -test3: - responses: - "200": - content: - application/json: - schema: - type: object - additionalProperties: false - properties: - top_array: - type: array - items: - oneOf: - - type: object - properties: - obj: - oneOf: - - type: array - items: + /test2: + get: + responses: + "200": + description: OK + content: + application/json: + schema: + type: object + additionalProperties: false + properties: + top_array: + type: array + items: + oneOf: + - type: object + properties: + obj: + oneOf: + - type: array + items: + type: string + - type: object + additionalProperties: false + properties: + str3: + type: string + - type: array + items: + type: object + properties: + str1: + type: string + str2: + type: string + /test3: + get: + responses: + "200": + description: OK + content: + application/json: + schema: + type: object + additionalProperties: false + properties: + top_array: + type: array + items: + oneOf: + - type: object + properties: + obj: + oneOf: + - type: array + items: + type: string + - type: object + - type: array + items: + type: object + properties: + str1: + type: string + str2: type: string - - type: object - - type: array - items: - type: object - properties: - str1: - type: string - str2: - type: string - example: - { - "top_array": - [ - {"obj": {"str3": "test"}}, - [{"str1": "success", "str2": "success"}], - ], - } diff --git a/zerver/tests/test_openapi.py b/zerver/tests/test_openapi.py index a733c54e7b..e5a91d1a4c 100644 --- a/zerver/tests/test_openapi.py +++ b/zerver/tests/test_openapi.py @@ -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