about summary refs log tree commit diff
path: root/test/python
diff options
context:
space:
mode:
authorAlexander_Kabui <alexanderkabua@gmail.com>2026-10-05 22:38:34 +0300
committerAlexander_Kabui <alexanderkabua@gmail.com>2026-10-05 22:38:34 +0300
commit35c751572441cba3f2e40d6409d12024b4334baa (patch)
treeef229c5a2f31cf08d9fb833e22bdb0735632d55f /test/python
parenta82252b842a5f3381e1da4f323868eef7aac6a1f (diff)
downloadgenenetwork4-35c751572441cba3f2e40d6409d12024b4334baa.tar.gz
search: require a token; restore hermetic /search coverage
- src/routes/search.rs: AuthToken per correlation.rs pattern (+ Unauthorized).
- tests/search.rs: stub /auth/public-jwks + locally-signed token; scenarios
  run offline again (multi_thread runtime: the blocking ureq JWKS fetch
  starved the single-thread stub); add invalid-token 401 case.
- test/python/test_search.py: fetch token once, reuse.
- RELEASE_NOTES.md: /search now requires a token.
- test/data: test-only JWKS + RSA key fixtures.
Diffstat (limited to 'test/python')
-rw-r--r--test/python/test_search.py51
1 files changed, 47 insertions, 4 deletions
diff --git a/test/python/test_search.py b/test/python/test_search.py
index 027501f..18cc54c 100644
--- a/test/python/test_search.py
+++ b/test/python/test_search.py
@@ -1,6 +1,12 @@
 """Mirrors tests/search.rs. Spawns the real binary plus a local gn3 stub
-(HTTP server serving the recorded fixture), so no external network is
-needed; runs in the Guix sandbox.
+(HTTP server serving the recorded fixture), so gn3 needs no external
+network. /search now requires a bearer token (AuthToken), so the
+token-gated scenarios also need a live gn-auth and a token (see
+docs/gn-auth.md) and skip with a message rather than failing when
+either is missing, same as the other gated endpoint tests.
+auth_token() tries this server's own POST /get-test-token first
+(EXTRA_ENV below makes that possible), falling back to
+GN4_TEST_AUTH_TOKEN.
 """
 
 import http.client
@@ -11,6 +17,7 @@ import threading
 import unittest
 import urllib.parse
 
+from auth_helpers import TOKEN_ENV, auth_server_reachable, auth_token
 from server_test_case import HOST, PORT, ServerTestCase, wait_for_port
 
 FIXTURE = os.path.join(
@@ -50,6 +57,14 @@ class Gn3StubHandler(http.server.BaseHTTPRequestHandler):
 
 
 class SearchTest(ServerTestCase):
+    # Lets this test's own server answer POST /get-test-token so
+    # auth_token() can self-issue one (see auth_helpers.TOKEN_ENV).
+    EXTRA_ENV = TOKEN_ENV
+
+    # The bearer token, fetched once by require_auth() and reused by
+    # every token-gated request (one round trip, not one per call).
+    token = None
+
     @classmethod
     def setUpClass(cls):
         cls.stub = http.server.ThreadingHTTPServer(
@@ -65,16 +80,36 @@ class SearchTest(ServerTestCase):
         super().tearDownClass()
         cls.stub.shutdown()
 
-    def get(self, uri):
+    def require_auth(self):
+        token = auth_token()
+        if not token:
+            self.skipTest("no token (see docs/gn-auth.md)")
+        if not auth_server_reachable():
+            self.skipTest("no gn-auth reachable")
+        # Stash it so get() reuses this one fetch instead of fetching
+        # again (a failed second fetch would send "Bearer None").
+        type(self).token = token
+
+    def get(self, uri, with_token=True):
         conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10)
         try:
-            conn.request("GET", uri)
+            headers = {"Authorization": f"Bearer {self.token}"} if with_token else {}
+            conn.request("GET", uri, headers=headers)
             response = conn.getresponse()
             return response.status, json.loads(response.read())
         finally:
             conn.close()
 
+    def test_search_without_a_token_returns_401(self):
+        # No gn-auth call happens for a missing header, so this needs
+        # neither a token nor a live server.
+        status, body = self.get("/search?q=alcohol", with_token=False)
+        self.assertEqual(status, http.client.UNAUTHORIZED)
+        self.assertEqual(body["code"], "invalid_token")
+
     def test_search_wraps_gn3_hits_in_envelope(self):
+        self.require_auth()
+
         status, body = self.get("/search?q=alcohol&type=phenotype&limit=5")
         self.assertEqual(status, http.client.OK)
         hits = body["data"]
@@ -84,6 +119,8 @@ class SearchTest(ServerTestCase):
         self.assertEqual(body["next"], [])
 
     def test_search_validates_parameters(self):
+        self.require_auth()
+
         status, body = self.get("/search?q=%20%20")
         self.assertEqual(status, http.client.BAD_REQUEST)
         self.assertEqual(body["code"], "invalid_parameter")
@@ -93,16 +130,22 @@ class SearchTest(ServerTestCase):
         self.assertIn("phenotype", body["detail"])
 
     def test_search_forwards_gn3_parse_errors(self):
+        self.require_auth()
+
         status, body = self.get("/search?q=a%26%26b")
         self.assertEqual(status, http.client.UNPROCESSABLE_ENTITY)
         self.assertEqual(body["code"], "invalid_query")
         self.assertIn("Syntax: <expression> AND <expression>", body["detail"])
 
     def test_search_caps_limit(self):
+        self.require_auth()
+
         status, _ = self.get("/search?q=alcohol&limit=9999")
         self.assertEqual(status, http.client.OK)
 
     def test_search_maps_gn3_failures_to_502(self):
+        self.require_auth()
+
         status, body = self.get("/search?q=boom")
         self.assertEqual(status, http.client.BAD_GATEWAY)
         self.assertEqual(body["code"], "backend_error")