about summary refs log tree commit diff
path: root/test/python
diff options
context:
space:
mode:
authorPjotr Prins <pjotr.public01@thebird.nl>2026-10-03 12:14:01 +0000
committerPjotr <wrk@bosa.lan>2026-10-03 12:14:01 +0000
commitbb1a44b0c75471b5c004170552540b9632f89810 (patch)
treea8450f9ba19595d22981c4ce514299dac5300915 /test/python
parentc379119d85dc23f855dee005856576cac38126c8 (diff)
downloadgenenetwork4-bb1a44b0c75471b5c004170552540b9632f89810.tar.gz
Add /openapi.json describing the REST API.
* Cargo.toml (dependencies): Add utoipa, feature axum_extras; add
utoipa-axum.
* src/lib.rs: Use utoipa_axum::router::OpenApiRouter and routes!
instead of axum::Router and route!, so paths register into an OpenApi
document as well as the router.
(ApiDoc): New struct, deriving utoipa::OpenApi with title, description
and license.
(app): Add /openapi.json, serving the OpenApi document split out of
OpenApiRouter via split_for_parts.
(hello_world, version, species): Add #[utoipa::path(...)] attributes
describing each endpoint's responses.
(VersionInfo): Derive utoipa::ToSchema in addition to Serialize.
* src/db.rs (Species): Likewise.
* tests/openapi.rs: New file.  Checks /openapi.json's info.title and
that it lists all three endpoints.
* test/python/test_openapi.py: New file.  Likewise.
* README.md: Document /openapi.json.
* rust-package.scm: Regenerate with guix import crate -f Cargo.lock
genenetwork4.  Adds crate-source definitions for utoipa, utoipa-gen,
utoipa-axum and pastey.
* guix.scm (genenetwork4-git)[native-inputs]: Add rust-utoipa-6.0.0,
rust-utoipa-gen-6.0.1, rust-utoipa-axum-0.3.0 and rust-pastey-0.2.3.
Diffstat (limited to 'test/python')
-rw-r--r--test/python/test_openapi.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/test/python/test_openapi.py b/test/python/test_openapi.py
new file mode 100644
index 0000000..03100b9
--- /dev/null
+++ b/test/python/test_openapi.py
@@ -0,0 +1,28 @@
+"""Mirrors tests/openapi.rs."""
+
+import http.client
+import json
+import unittest
+
+from server_test_case import HOST, PORT, ServerTestCase
+
+
+class OpenApiTest(ServerTestCase):
+    def test_openapi_json_describes_all_endpoints(self):
+        conn = http.client.HTTPConnection(HOST, int(PORT), timeout=5)
+        try:
+            conn.request("GET", "/openapi.json")
+            response = conn.getresponse()
+            body = response.read()
+        finally:
+            conn.close()
+        self.assertEqual(response.status, 200)
+
+        doc = json.loads(body)
+        self.assertEqual(doc["info"]["title"], "GeneNetwork4 API")
+        for path in ("/", "/version", "/species"):
+            self.assertIn(path, doc["paths"])
+
+
+if __name__ == "__main__":
+    unittest.main()