about summary refs log tree commit diff
path: root/test/python/test_envelope.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/python/test_envelope.py')
-rw-r--r--test/python/test_envelope.py135
1 files changed, 135 insertions, 0 deletions
diff --git a/test/python/test_envelope.py b/test/python/test_envelope.py
new file mode 100644
index 0000000..160a50c
--- /dev/null
+++ b/test/python/test_envelope.py
@@ -0,0 +1,135 @@
+"""Mirrors tests/envelope.rs and the envelope check of tests/openapi.rs:
+the success envelope (`data` next to `meta`, `notes` and `next`) on
+every endpoint except `/` and `/openapi.json`, and that every `next`
+href resolves. The `/version` and OpenAPI checks need nothing else; the
+`next` crawl needs a live MariaDB and a token (see docs/gn-auth.md) and
+skips with a message when either is missing.
+"""
+
+import http.client
+import json
+import unittest
+
+from auth_helpers import TOKEN_ENV, auth_token
+from server_test_case import HOST, PORT, ServerTestCase, database_reachable
+
+# Hrefs not followed: lists of thousands of rows (every trait of a
+# group, every probeset of a dataset).
+TOO_LARGE_SUFFIXES = ("/traits", "/probesets")
+
+SEEDS = [
+    "/species",
+    "/species/mouse",
+    "/species/mouse/datasets",
+    "/groups",
+    "/groups/BXD",
+    "/groups/BXD/strains",
+    "/groups/BXD/case-attributes",
+    "/groups/BXD/datasets",
+    "/groups/BXD/traits?abbreviation=5HT_MB_M",
+    "/groups/BXD/traits/17985",
+    "/groups/BXD/traits/17985/values",
+    "/datasets/Br_U_0803_M",
+    "/datasets/Br_U_0803_M/probesets/100001_at",
+    "/datasets/Br_U_0803_M/probesets/100001_at/values",
+    "/traits/BXDPublish/17985",
+    "/traits/BXDPublish/17985/values",
+    "/publications/24102824/traits",
+]
+
+
+class EnvelopeTest(ServerTestCase):
+    EXTRA_ENV = TOKEN_ENV
+
+    def get(self, path, token=None, timeout=60):
+        conn = http.client.HTTPConnection(HOST, int(PORT), timeout=timeout)
+        try:
+            headers = {"Authorization": f"Bearer {token}"} if token else {}
+            conn.request("GET", path, headers=headers)
+            response = conn.getresponse()
+            return response, response.read()
+        finally:
+            conn.close()
+
+    def test_version_is_wrapped_in_the_envelope(self):
+        response, body = self.get("/version")
+        self.assertEqual(response.status, 200)
+        envelope = json.loads(body)
+        self.assertEqual(envelope["data"]["name"], "GeneNetwork4")
+        self.assertEqual(envelope["meta"]["state"], "complete")
+        self.assertIsInstance(envelope["meta"]["took_ms"], int)
+        self.assertEqual(
+            envelope["meta"]["request_id"], response.getheader("x-request-id")
+        )
+        self.assertEqual(envelope["notes"], [])
+        self.assertEqual(envelope["next"], [])
+
+    def test_errors_carry_the_request_id_and_took_ms(self):
+        # The token extractor's 401: needs no database or gn-auth.
+        response, body = self.get("/species")
+        self.assertEqual(response.status, 401)
+        error = json.loads(body)
+        self.assertEqual(error["code"], "invalid_token")
+        self.assertEqual(error["request_id"], response.getheader("x-request-id"))
+        self.assertIsInstance(error["took_ms"], int)
+
+    def test_the_greeting_and_the_openapi_document_are_not_wrapped(self):
+        response, body = self.get("/")
+        self.assertEqual(response.status, 200)
+        self.assertEqual(body, b"Hello, world!")
+
+        response, body = self.get("/openapi.json")
+        doc = json.loads(body)
+        self.assertIn("openapi", doc)
+        self.assertNotIn("meta", doc)
+
+    def test_every_json_success_response_is_an_envelope(self):
+        _, body = self.get("/openapi.json")
+        checked = 0
+        for path, item in json.loads(body)["paths"].items():
+            for method, operation in item.items():
+                content = operation["responses"].get("200", {}).get("content", {})
+                body_schema = content.get("application/json")
+                if body_schema is None:
+                    continue
+                schema = body_schema["schema"].get("$ref", "")
+                self.assertTrue(
+                    schema.startswith("#/components/schemas/Envelope_"),
+                    f"{method} {path}: 200 response is {schema!r}, not an envelope",
+                )
+                checked += 1
+        self.assertGreaterEqual(checked, 20)
+
+    def test_next_links_resolve(self):
+        if not database_reachable():
+            self.skipTest("no database on 127.0.0.1:3306")
+        token = auth_token()
+        if not token:
+            self.skipTest("no token (see docs/gn-auth.md)")
+
+        followed = 0
+        for seed in SEEDS:
+            response, body = self.get(seed, token)
+            self.assertEqual(response.status, 200, f"GET {seed}: {body[:200]}")
+            envelope = json.loads(body)
+            self.assertEqual(
+                envelope["meta"]["request_id"], response.getheader("x-request-id"), seed
+            )
+            self.assertIsInstance(envelope["meta"]["took_ms"], int, seed)
+            for nxt in envelope["next"]:
+                href = nxt["href"]
+                self.assertTrue(href.startswith("/"), f"{seed}: relative href {href}")
+                self.assertIsInstance(nxt["description"], str)
+                if href.endswith(TOO_LARGE_SUFFIXES):
+                    continue
+                response, linked = self.get(href, token)
+                self.assertEqual(
+                    response.status, 200, f"{seed} suggests {href}: {linked[:200]}"
+                )
+                self.assertIn("state", json.loads(linked)["meta"])
+                followed += 1
+        self.assertGreaterEqual(followed, 20)
+
+
+if __name__ == "__main__":
+    unittest.main()