about summary refs log tree commit diff
path: root/test/python
diff options
context:
space:
mode:
authorPjotr Prins <pjotr.public01@thebird.nl>2026-10-04 13:32:08 +0000
committerPjotr <wrk@bosa.lan>2026-10-04 13:32:08 +0000
commit21c69c1051715e6964fe9d4c8e06a326344fe6eb (patch)
tree5e8fbf9892de09c9d095ae32c39de2e149195292 /test/python
parentdfbf105652140d52e50a96853f0a7ab91fec0776 (diff)
downloadgenenetwork4-21c69c1051715e6964fe9d4c8e06a326344fe6eb.tar.gz
Gate GET /datasets/{dataset}/probesets/{probeset}/values on gn-auth
Same treatment as dataset_probeset_info: require a valid AuthToken
unconditionally, and for a dataset that isn't publicly readable, check
group:resource:view-resource via authorise_trait -- denied is a 403,
gn-auth unreachable is a 502.

Unify DatasetProbesetLookup/DatasetProbesetInfoLookup back into one
generic enum (with the new Forbidden variant) now that both
dataset_probeset_info and dataset_probeset_values need it, rather than
keeping two near-identical enums.

tests/dataset_probeset_values.rs and its Python mirror gain the same
401/403 cases as dataset_probeset_info's, run against a real gn-auth
instance; the 403 case reuses UTHSC_BXD_Cochlea_0225, confirmed to
have both ProbeSet metadata and ProbeSetData values rows so the test
exercises the authorization check rather than ProbesetNotFound.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XDDpXvZBQat9uSgDeEMJ8d
Diffstat (limited to 'test/python')
-rw-r--r--test/python/test_dataset_probeset_values.py92
1 files changed, 71 insertions, 21 deletions
diff --git a/test/python/test_dataset_probeset_values.py b/test/python/test_dataset_probeset_values.py
index 3477fa7..b9f0f0e 100644
--- a/test/python/test_dataset_probeset_values.py
+++ b/test/python/test_dataset_probeset_values.py
@@ -1,15 +1,22 @@
 """Mirrors tests/dataset_probeset_values.rs. Needs a live MariaDB
 (SQL_URI); skips with a message rather than failing when none is
-reachable, same as test_group_by_name.py.
+reachable, same as test_group_by_name.py. Also needs a live gn-auth
+(AUTH_SERVER_URL) and GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md), since
+this endpoint now requires a bearer token; skips when either is
+missing.
 """
 
 import http.client
 import json
+import os
 import socket
 import unittest
+from urllib.parse import urlparse
 
 from server_test_case import HOST, PORT, ServerTestCase
 
+AUTH_SERVER_URL = os.environ.get("AUTH_SERVER_URL", "http://127.0.0.1:8081/")
+
 
 def database_reachable():
     try:
@@ -19,18 +26,45 @@ def database_reachable():
         return False
 
 
+def auth_server_reachable():
+    url = urlparse(AUTH_SERVER_URL)
+    conn = http.client.HTTPConnection(url.hostname, url.port or 80, timeout=2)
+    try:
+        conn.request("GET", url.path.rstrip("/") + "/auth/public-jwks")
+        return conn.getresponse().status == 200
+    except OSError:
+        return False
+    finally:
+        conn.close()
+
+
+def auth_token():
+    return os.environ.get("GN4_TEST_AUTH_TOKEN")
+
+
 class DatasetProbesetValuesTest(ServerTestCase):
-    def test_dataset_probeset_values_returns_strain_values(self):
+    def require_fixtures(self):
         if not database_reachable():
             self.skipTest("no database on 127.0.0.1:3306")
+        if not auth_token():
+            self.skipTest("set GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md)")
+        if not auth_server_reachable():
+            self.skipTest(f"no gn-auth reachable at {AUTH_SERVER_URL}")
 
+    def get(self, path, with_token=True):
         conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10)
         try:
-            conn.request("GET", "/datasets/Br_U_0803_M/probesets/100001_at/values")
+            headers = {"Authorization": f"Bearer {auth_token()}"} if with_token else {}
+            conn.request("GET", path, headers=headers)
             response = conn.getresponse()
-            body = response.read()
+            return response, response.read()
         finally:
             conn.close()
+
+    def test_dataset_probeset_values_returns_strain_values(self):
+        self.require_fixtures()
+
+        response, body = self.get("/datasets/Br_U_0803_M/probesets/100001_at/values")
         self.assertEqual(response.status, 200)
 
         values = json.loads(body)
@@ -38,17 +72,21 @@ class DatasetProbesetValuesTest(ServerTestCase):
         bxd1 = next(v for v in values if v["name"] == "BXD1")
         self.assertIsInstance(bxd1["value"], float)
 
-    def test_dataset_probeset_values_unknown_dataset_returns_404(self):
+    def test_dataset_probeset_values_without_a_token_returns_401(self):
         if not database_reachable():
             self.skipTest("no database on 127.0.0.1:3306")
 
-        conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10)
-        try:
-            conn.request("GET", "/datasets/not-a-real-dataset/probesets/100001_at/values")
-            response = conn.getresponse()
-            body = response.read()
-        finally:
-            conn.close()
+        response, _body = self.get(
+            "/datasets/Br_U_0803_M/probesets/100001_at/values", with_token=False
+        )
+        self.assertEqual(response.status, 401)
+
+    def test_dataset_probeset_values_unknown_dataset_returns_404(self):
+        self.require_fixtures()
+
+        response, body = self.get(
+            "/datasets/not-a-real-dataset/probesets/100001_at/values"
+        )
         self.assertEqual(response.status, 404)
 
         error = json.loads(body)
@@ -57,16 +95,11 @@ class DatasetProbesetValuesTest(ServerTestCase):
         self.assertEqual(error["operation_id"], "dataset_probeset_values")
 
     def test_dataset_probeset_values_unknown_probeset_in_dataset_returns_404(self):
-        if not database_reachable():
-            self.skipTest("no database on 127.0.0.1:3306")
+        self.require_fixtures()
 
-        conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10)
-        try:
-            conn.request("GET", "/datasets/Br_U_0803_M/probesets/not-a-real-probeset/values")
-            response = conn.getresponse()
-            body = response.read()
-        finally:
-            conn.close()
+        response, body = self.get(
+            "/datasets/Br_U_0803_M/probesets/not-a-real-probeset/values"
+        )
         self.assertEqual(response.status, 404)
 
         error = json.loads(body)
@@ -74,6 +107,23 @@ class DatasetProbesetValuesTest(ServerTestCase):
         self.assertEqual(error["code"], "probeset_not_found")
         self.assertEqual(error["operation_id"], "dataset_probeset_values")
 
+    def test_dataset_probeset_values_non_public_unlinked_dataset_returns_403(self):
+        """UTHSC_BXD_Cochlea_0225 (public=1, confidentiality=1, created
+        2025-02-13) is neither publicly readable by the flags nor old
+        enough for the embargo-age rule, and has never been linked to a
+        gn-auth resource -- so even the token's owner has no privilege
+        on it.
+        """
+        self.require_fixtures()
+
+        response, body = self.get(
+            "/datasets/UTHSC_BXD_Cochlea_0225/probesets/ENSMUSG00000028180/values"
+        )
+        self.assertEqual(response.status, 403)
+
+        error = json.loads(body)
+        self.assertEqual(error["code"], "forbidden")
+
 
 if __name__ == "__main__":
     unittest.main()