Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
openapi-tests: Add coverage for validating example events.
Lauryn Menard6f58994ef470
diff --git a/zerver/openapi/python_examples.py b/zerver/openapi/python_examples.pyindex b4ae8229495..70df59461c9 100644--- a/zerver/openapi/python_examples.py+++ b/zerver/openapi/python_examples.py@@ -1599,11 +1599,15 @@ def test_streams(client: Client, nonadmin_client: Client) -> None:def test_queues(client: Client) -> None:- # Note that the example for api/get-events is not tested.+ # Note that the example for api/get-events is not tested here.+ ## Since, methods such as client.get_events() or client.call_on_each_message# are blocking calls and since the event queue backend is already# thoroughly tested in zerver/tests/test_event_queue.py, it is not worth# the effort to come up with asynchronous logic for testing those here.+ #+ # We do validate endpoint example responses in zerver/tests/test_openapi.py,+ # as well as the example events returned by api/get-events.queue_id = register_queue(client)get_queue(client, queue_id)deregister_queue(client, queue_id)diff --git a/zerver/tests/test_openapi.py b/zerver/tests/test_openapi.pyindex 1e25e8998f6..3f5d6578c13 100644--- a/zerver/tests/test_openapi.py+++ b/zerver/tests/test_openapi.py@@ -910,6 +910,7 @@ class OpenAPIAttributesTest(ZulipTestCase):Checks:* All endpoints have `operationId` and `tag` attributes.* All example responses match their schema.+ * All example events in `/get-events` match an event schema.* That no opaque object exists."""EXCLUDE = ["/real-time"]@@ -929,15 +930,28 @@ class OpenAPIAttributesTest(ZulipTestCase):if path in EXCLUDE:continuefor method, operation in path_item.items():- # Check if every file has an operationIdassert "operationId" in operationassert "tags" in operationtag = operation["tags"][0]assert tag in VALID_TAGSfor status_code, response in operation["responses"].items():schema = response["content"]["application/json"]["schema"]+ # Validate the documented examples for each event type+ # in api/get-events for the documented event schemas.+ if path == "/events" and method == "get" and status_code == "200":+ for event_type in schema["properties"]["events"]["items"]["oneOf"]:+ event_array = [event_type["example"]]+ content = {+ "queue_id": "fb67bf8a-c031-47cc-84cf-ed80accacda8",+ "events": event_array,+ "msg": "",+ "result": "success",+ }+ assert validate_against_openapi_schema(+ content, path, method, status_code+ )if "oneOf" in schema:- for _, subschema in enumerate(schema["oneOf"]):+ for subschema in schema["oneOf"]:validate_schema(subschema)assert validate_against_openapi_schema(subschema["example"],