diff options
Diffstat (limited to 'test/python')
| -rw-r--r-- | test/python/auth_helpers.py | 58 | ||||
| -rw-r--r-- | test/python/test_dataset_by_name.py | 34 | ||||
| -rw-r--r-- | test/python/test_dataset_probeset_info.py | 35 | ||||
| -rw-r--r-- | test/python/test_dataset_probeset_values.py | 35 | ||||
| -rw-r--r-- | test/python/test_dataset_probesets.py | 33 | ||||
| -rw-r--r-- | test/python/test_get_test_token.py (renamed from test/python/test_dev_token.py) | 36 | ||||
| -rw-r--r-- | test/python/test_group_by_name.py | 42 | ||||
| -rw-r--r-- | test/python/test_group_datasets.py | 34 | ||||
| -rw-r--r-- | test/python/test_group_strains.py | 42 | ||||
| -rw-r--r-- | test/python/test_group_trait_info.py | 42 | ||||
| -rw-r--r-- | test/python/test_group_trait_values.py | 42 | ||||
| -rw-r--r-- | test/python/test_group_traits.py | 38 | ||||
| -rw-r--r-- | test/python/test_groups.py | 14 | ||||
| -rw-r--r-- | test/python/test_species.py | 15 | ||||
| -rw-r--r-- | test/python/test_species_by_name.py | 34 | ||||
| -rw-r--r-- | test/python/test_species_datasets.py | 34 | ||||
| -rw-r--r-- | test/python/test_traits.py | 35 |
17 files changed, 315 insertions, 288 deletions
diff --git a/test/python/auth_helpers.py b/test/python/auth_helpers.py new file mode 100644 index 0000000..8b3e80d --- /dev/null +++ b/test/python/auth_helpers.py @@ -0,0 +1,58 @@ +"""Shared helpers for tests that need a gn-auth bearer token. Mirrors +tests/common/mod.rs's auth_token()/auth_server_available() on the Rust +side. + +A test class that uses auth_token() must set EXTRA_ENV = TOKEN_ENV (see +server_test_case.py) so its own spawned server can answer +POST /get-test-token (src/routes/dev.rs) with a real one. +""" + +import http.client +import json +import os +from urllib.parse import urlparse + +from server_test_case import HOST, PORT + +AUTH_SERVER_URL = os.environ.get("AUTH_SERVER_URL", "http://127.0.0.1:8081/") + +# Every endpoint needs a token now (see docs/gn-auth.md) -- a test class +# that calls auth_token() needs its server started with this so it can +# self-issue one. +TOKEN_ENV = {"GENENETWORK4_DEV_MODE": "true", "GENENETWORK4_BIND": "127.0.0.1"} + + +def auth_server_reachable(): + """True only when gn-auth is actually reachable at AUTH_SERVER_URL.""" + 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(): + """A bearer token to use against the already-running test server + (started with TOKEN_ENV). Tries POST /get-test-token first -- this + just works once the server was started with TOKEN_ENV, no + externally managed token needed -- falling back to the + GN4_TEST_AUTH_TOKEN env var (see docs/gn-auth.md) for a gn-auth + setup that doesn't want dev mode on even for tests. Returns None, + rather than raising, if neither produces a token; callers should + skipTest() in that case. + """ + conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10) + try: + conn.request("POST", "/get-test-token") + response = conn.getresponse() + if response.status == 200: + return json.loads(response.read())["access_token"] + except OSError: + pass + finally: + conn.close() + return os.environ.get("GN4_TEST_AUTH_TOKEN") diff --git a/test/python/test_dataset_by_name.py b/test/python/test_dataset_by_name.py index 082f758..51c1879 100644 --- a/test/python/test_dataset_by_name.py +++ b/test/python/test_dataset_by_name.py @@ -1,6 +1,7 @@ -"""Mirrors tests/dataset_by_name.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. +"""Mirrors tests/dataset_by_name.rs. Needs a live MariaDB (SQL_URI) and +a valid token (every endpoint needs one now -- see docs/gn-auth.md); +skips with a message rather than failing when either is missing, same +as test_group_by_name.py. """ import http.client @@ -8,6 +9,7 @@ import json import socket import unittest +from auth_helpers import TOKEN_ENV, auth_token from server_test_case import HOST, PORT, ServerTestCase @@ -20,17 +22,25 @@ def database_reachable(): class DatasetByNameTest(ServerTestCase): - def test_dataset_by_name_returns_dataset_row(self): - if not database_reachable(): - self.skipTest("no database on 127.0.0.1:3306") + EXTRA_ENV = TOKEN_ENV + def get(self, path): + token = auth_token() + if not token: + self.skipTest("no token (see docs/gn-auth.md)") conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10) try: - conn.request("GET", "/datasets/Br_U_0803_M") + conn.request("GET", path, headers={"Authorization": f"Bearer {token}"}) response = conn.getresponse() - body = response.read() + return response, response.read() finally: conn.close() + + def test_dataset_by_name_returns_dataset_row(self): + if not database_reachable(): + self.skipTest("no database on 127.0.0.1:3306") + + response, body = self.get("/datasets/Br_U_0803_M") self.assertEqual(response.status, 200) dataset = json.loads(body) @@ -42,13 +52,7 @@ class DatasetByNameTest(ServerTestCase): 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") - response = conn.getresponse() - body = response.read() - finally: - conn.close() + response, body = self.get("/datasets/not-a-real-dataset") self.assertEqual(response.status, 404) error = json.loads(body) diff --git a/test/python/test_dataset_probeset_info.py b/test/python/test_dataset_probeset_info.py index 569e9f0..32d25a6 100644 --- a/test/python/test_dataset_probeset_info.py +++ b/test/python/test_dataset_probeset_info.py @@ -1,22 +1,21 @@ """Mirrors tests/dataset_probeset_info.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. 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. +(AUTH_SERVER_URL) and a token (see docs/gn-auth.md), since this +endpoint now requires a bearer token; skips when either is missing. +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 import json -import os import socket import unittest -from urllib.parse import urlparse +from auth_helpers import TOKEN_ENV, auth_server_reachable, auth_token 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: @@ -26,30 +25,16 @@ 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 DatasetProbesetInfoTest(ServerTestCase): + EXTRA_ENV = TOKEN_ENV + 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)") + self.skipTest("no token (see docs/gn-auth.md)") if not auth_server_reachable(): - self.skipTest(f"no gn-auth reachable at {AUTH_SERVER_URL}") + self.skipTest("no gn-auth reachable") def get(self, path, with_token=True): conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10) diff --git a/test/python/test_dataset_probeset_values.py b/test/python/test_dataset_probeset_values.py index b9f0f0e..7a934d0 100644 --- a/test/python/test_dataset_probeset_values.py +++ b/test/python/test_dataset_probeset_values.py @@ -1,22 +1,21 @@ """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. 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. +(AUTH_SERVER_URL) and a token (see docs/gn-auth.md), since this +endpoint now requires a bearer token; skips when either is missing. +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 import json -import os import socket import unittest -from urllib.parse import urlparse +from auth_helpers import TOKEN_ENV, auth_server_reachable, auth_token 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: @@ -26,30 +25,16 @@ 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): + EXTRA_ENV = TOKEN_ENV + 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)") + self.skipTest("no token (see docs/gn-auth.md)") if not auth_server_reachable(): - self.skipTest(f"no gn-auth reachable at {AUTH_SERVER_URL}") + self.skipTest("no gn-auth reachable") def get(self, path, with_token=True): conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10) diff --git a/test/python/test_dataset_probesets.py b/test/python/test_dataset_probesets.py index 64cebff..dcfcd99 100644 --- a/test/python/test_dataset_probesets.py +++ b/test/python/test_dataset_probesets.py @@ -1,21 +1,20 @@ """Mirrors tests/dataset_probesets.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. 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. +and a token (see docs/gn-auth.md), since this endpoint now requires a +bearer token; skips when either is missing. 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 import json -import os import socket import unittest -from urllib.parse import urlparse +from auth_helpers import TOKEN_ENV, auth_server_reachable, auth_token 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: @@ -25,30 +24,16 @@ 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 DatasetProbesetsTest(ServerTestCase): + EXTRA_ENV = TOKEN_ENV + 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)") + self.skipTest("no token (see docs/gn-auth.md)") if not auth_server_reachable(): - self.skipTest(f"no gn-auth reachable at {AUTH_SERVER_URL}") + self.skipTest("no gn-auth reachable") def get(self, path, with_token=True): conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10) diff --git a/test/python/test_dev_token.py b/test/python/test_get_test_token.py index 8b31395..2426ebd 100644 --- a/test/python/test_dev_token.py +++ b/test/python/test_get_test_token.py @@ -1,7 +1,7 @@ -"""Mirrors tests/dev_token.rs. POST /dev/token only exists when -GENENETWORK4_DEV_MODE is on and GENENETWORK4_BIND resolves to loopback -(see Config::dev_token_enabled, src/routes/dev.rs). Each test class -below spawns its own server subprocess with its own environment +"""Mirrors tests/get_test_token.rs. POST /get-test-token only exists +when GENENETWORK4_DEV_MODE is on and GENENETWORK4_BIND resolves to +loopback (see Config::dev_token_enabled, src/routes/dev.rs). Each test +class below spawns its own server subprocess with its own environment (ServerTestCase.EXTRA_ENV), so -- unlike the Rust test, which has to keep its env mutations in one sequential test function -- these can be separate test classes safely. @@ -30,24 +30,26 @@ def auth_server_reachable(): conn.close() -def post_dev_token(): +def post_test_token(): conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10) try: - conn.request("POST", "/dev/token") + conn.request("POST", "/get-test-token") response = conn.getresponse() return response, response.read() finally: conn.close() -class DevTokenDisabledByDefaultTest(ServerTestCase): +class GetTestTokenDisabledByDefaultTest(ServerTestCase): """No EXTRA_ENV -- the out-of-the-box default (dev_mode off).""" - def test_dev_token_returns_404_by_default(self): - response, _body = post_dev_token() + def test_get_test_token_returns_404_by_default(self): + response, _body = post_test_token() self.assertEqual(response.status, 404) - def test_dev_token_not_in_openapi_json_by_default(self): + def test_get_test_token_not_in_openapi_json_by_default(self): + # /openapi.json itself is one of the few endpoints that stays + # ungated (see lib.rs) -- no token needed here. conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10) try: conn.request("GET", "/openapi.json") @@ -55,29 +57,29 @@ class DevTokenDisabledByDefaultTest(ServerTestCase): finally: conn.close() spec = json.loads(body) - self.assertNotIn("/dev/token", spec["paths"]) + self.assertNotIn("/get-test-token", spec["paths"]) -class DevTokenDevModeButWrongBindTest(ServerTestCase): +class GetTestTokenDevModeButWrongBindTest(ServerTestCase): """dev_mode on, but not bound to loopback: still disabled.""" EXTRA_ENV = {"GENENETWORK4_DEV_MODE": "true", "GENENETWORK4_BIND": "0.0.0.0"} - def test_dev_token_returns_404(self): - response, _body = post_dev_token() + def test_get_test_token_returns_404(self): + response, _body = post_test_token() self.assertEqual(response.status, 404) -class DevTokenEnabledTest(ServerTestCase): +class GetTestTokenEnabledTest(ServerTestCase): """Both set: this is the only configuration where it works.""" EXTRA_ENV = {"GENENETWORK4_DEV_MODE": "true", "GENENETWORK4_BIND": "127.0.0.1"} - def test_dev_token_returns_a_usable_token(self): + def test_get_test_token_returns_a_usable_token(self): if not auth_server_reachable(): self.skipTest(f"no gn-auth reachable at {AUTH_SERVER_URL}") - response, body = post_dev_token() + response, body = post_test_token() self.assertEqual(response.status, 200) token = json.loads(body) diff --git a/test/python/test_group_by_name.py b/test/python/test_group_by_name.py index cc8e39b..da95cfc 100644 --- a/test/python/test_group_by_name.py +++ b/test/python/test_group_by_name.py @@ -1,6 +1,7 @@ -"""Mirrors tests/group_by_name.rs. Needs a live MariaDB (SQL_URI); skips -with a message rather than failing when none is reachable, same as -test_species_by_name.py. +"""Mirrors tests/group_by_name.rs. Needs a live MariaDB (SQL_URI) and a +valid token (every endpoint needs one now -- see docs/gn-auth.md); +skips with a message rather than failing when either is missing, same +as test_species_by_name.py. """ import http.client @@ -8,6 +9,7 @@ import json import socket import unittest +from auth_helpers import TOKEN_ENV, auth_token from server_test_case import HOST, PORT, ServerTestCase @@ -20,17 +22,25 @@ def database_reachable(): class GroupByNameTest(ServerTestCase): - def test_group_by_name_bxd_returns_group_row(self): - if not database_reachable(): - self.skipTest("no database on 127.0.0.1:3306") + EXTRA_ENV = TOKEN_ENV + def get(self, path): + token = auth_token() + if not token: + self.skipTest("no token (see docs/gn-auth.md)") conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10) try: - conn.request("GET", "/groups/BXD") + conn.request("GET", path, headers={"Authorization": f"Bearer {token}"}) response = conn.getresponse() - body = response.read() + return response, response.read() finally: conn.close() + + def test_group_by_name_bxd_returns_group_row(self): + if not database_reachable(): + self.skipTest("no database on 127.0.0.1:3306") + + response, body = self.get("/groups/BXD") self.assertEqual(response.status, 200) group = json.loads(body) @@ -45,13 +55,7 @@ class GroupByNameTest(ServerTestCase): 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", "/groups/1") - response = conn.getresponse() - body = response.read() - finally: - conn.close() + response, body = self.get("/groups/1") self.assertEqual(response.status, 200) group = json.loads(body) @@ -62,13 +66,7 @@ class GroupByNameTest(ServerTestCase): 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", "/groups/not-a-real-group") - response = conn.getresponse() - body = response.read() - finally: - conn.close() + response, body = self.get("/groups/not-a-real-group") self.assertEqual(response.status, 404) request_id = response.getheader("x-request-id") diff --git a/test/python/test_group_datasets.py b/test/python/test_group_datasets.py index d338075..f386d2c 100644 --- a/test/python/test_group_datasets.py +++ b/test/python/test_group_datasets.py @@ -1,6 +1,7 @@ -"""Mirrors tests/group_datasets.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. +"""Mirrors tests/group_datasets.rs. Needs a live MariaDB (SQL_URI) and +a valid token (every endpoint needs one now -- see docs/gn-auth.md); +skips with a message rather than failing when either is missing, same +as test_group_by_name.py. """ import http.client @@ -8,6 +9,7 @@ import json import socket import unittest +from auth_helpers import TOKEN_ENV, auth_token from server_test_case import HOST, PORT, ServerTestCase @@ -20,17 +22,25 @@ def database_reachable(): class GroupDatasetsTest(ServerTestCase): - def test_group_datasets_bxd_returns_datasets(self): - if not database_reachable(): - self.skipTest("no database on 127.0.0.1:3306") + EXTRA_ENV = TOKEN_ENV + def get(self, path): + token = auth_token() + if not token: + self.skipTest("no token (see docs/gn-auth.md)") conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10) try: - conn.request("GET", "/groups/BXD/datasets") + conn.request("GET", path, headers={"Authorization": f"Bearer {token}"}) response = conn.getresponse() - body = response.read() + return response, response.read() finally: conn.close() + + def test_group_datasets_bxd_returns_datasets(self): + if not database_reachable(): + self.skipTest("no database on 127.0.0.1:3306") + + response, body = self.get("/groups/BXD/datasets") self.assertEqual(response.status, 200) datasets = json.loads(body) @@ -42,13 +52,7 @@ class GroupDatasetsTest(ServerTestCase): 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", "/groups/not-a-real-group/datasets") - response = conn.getresponse() - body = response.read() - finally: - conn.close() + response, body = self.get("/groups/not-a-real-group/datasets") self.assertEqual(response.status, 404) error = json.loads(body) diff --git a/test/python/test_group_strains.py b/test/python/test_group_strains.py index 12b4aa4..91cc7fb 100644 --- a/test/python/test_group_strains.py +++ b/test/python/test_group_strains.py @@ -1,6 +1,7 @@ -"""Mirrors tests/group_strains.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. +"""Mirrors tests/group_strains.rs. Needs a live MariaDB (SQL_URI) and a +valid token (every endpoint needs one now -- see docs/gn-auth.md); +skips with a message rather than failing when either is missing, same +as test_group_by_name.py. """ import http.client @@ -8,6 +9,7 @@ import json import socket import unittest +from auth_helpers import TOKEN_ENV, auth_token from server_test_case import HOST, PORT, ServerTestCase @@ -20,17 +22,25 @@ def database_reachable(): class GroupStrainsTest(ServerTestCase): - def test_group_strains_bxd_returns_strains(self): - if not database_reachable(): - self.skipTest("no database on 127.0.0.1:3306") + EXTRA_ENV = TOKEN_ENV + def get(self, path): + token = auth_token() + if not token: + self.skipTest("no token (see docs/gn-auth.md)") conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10) try: - conn.request("GET", "/groups/BXD/strains") + conn.request("GET", path, headers={"Authorization": f"Bearer {token}"}) response = conn.getresponse() - body = response.read() + return response, response.read() finally: conn.close() + + def test_group_strains_bxd_returns_strains(self): + if not database_reachable(): + self.skipTest("no database on 127.0.0.1:3306") + + response, body = self.get("/groups/BXD/strains") self.assertEqual(response.status, 200) strains = json.loads(body) @@ -42,13 +52,7 @@ class GroupStrainsTest(ServerTestCase): 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", "/groups/BXD/strains?used_for_mapping=true") - response = conn.getresponse() - body = response.read() - finally: - conn.close() + response, body = self.get("/groups/BXD/strains?used_for_mapping=true") self.assertEqual(response.status, 200) strains = json.loads(body) @@ -61,13 +65,7 @@ class GroupStrainsTest(ServerTestCase): 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", "/groups/not-a-real-group/strains") - response = conn.getresponse() - body = response.read() - finally: - conn.close() + response, body = self.get("/groups/not-a-real-group/strains") self.assertEqual(response.status, 404) error = json.loads(body) diff --git a/test/python/test_group_trait_info.py b/test/python/test_group_trait_info.py index b37250e..d5a0761 100644 --- a/test/python/test_group_trait_info.py +++ b/test/python/test_group_trait_info.py @@ -1,6 +1,7 @@ -"""Mirrors tests/group_trait_info.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. +"""Mirrors tests/group_trait_info.rs. Needs a live MariaDB (SQL_URI) +and a valid token (every endpoint needs one now -- see +docs/gn-auth.md); skips with a message rather than failing when either +is missing, same as test_group_by_name.py. """ import http.client @@ -8,6 +9,7 @@ import json import socket import unittest +from auth_helpers import TOKEN_ENV, auth_token from server_test_case import HOST, PORT, ServerTestCase @@ -20,17 +22,25 @@ def database_reachable(): class GroupTraitInfoTest(ServerTestCase): - def test_group_trait_info_bxd_returns_phenotype_and_publication(self): - if not database_reachable(): - self.skipTest("no database on 127.0.0.1:3306") + EXTRA_ENV = TOKEN_ENV + def get(self, path): + token = auth_token() + if not token: + self.skipTest("no token (see docs/gn-auth.md)") conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10) try: - conn.request("GET", "/groups/BXD/traits/10001") + conn.request("GET", path, headers={"Authorization": f"Bearer {token}"}) response = conn.getresponse() - body = response.read() + return response, response.read() finally: conn.close() + + def test_group_trait_info_bxd_returns_phenotype_and_publication(self): + if not database_reachable(): + self.skipTest("no database on 127.0.0.1:3306") + + response, body = self.get("/groups/BXD/traits/10001") self.assertEqual(response.status, 200) info = json.loads(body) @@ -45,13 +55,7 @@ class GroupTraitInfoTest(ServerTestCase): 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", "/groups/not-a-real-group/traits/10001") - response = conn.getresponse() - body = response.read() - finally: - conn.close() + response, body = self.get("/groups/not-a-real-group/traits/10001") self.assertEqual(response.status, 404) error = json.loads(body) @@ -64,13 +68,7 @@ class GroupTraitInfoTest(ServerTestCase): 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", "/groups/BXD/traits/999999999") - response = conn.getresponse() - body = response.read() - finally: - conn.close() + response, body = self.get("/groups/BXD/traits/999999999") self.assertEqual(response.status, 404) error = json.loads(body) diff --git a/test/python/test_group_trait_values.py b/test/python/test_group_trait_values.py index 7501757..5b97282 100644 --- a/test/python/test_group_trait_values.py +++ b/test/python/test_group_trait_values.py @@ -1,6 +1,7 @@ -"""Mirrors tests/group_trait_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. +"""Mirrors tests/group_trait_values.rs. Needs a live MariaDB (SQL_URI) +and a valid token (every endpoint needs one now -- see +docs/gn-auth.md); skips with a message rather than failing when either +is missing, same as test_group_by_name.py. """ import http.client @@ -8,6 +9,7 @@ import json import socket import unittest +from auth_helpers import TOKEN_ENV, auth_token from server_test_case import HOST, PORT, ServerTestCase @@ -20,17 +22,25 @@ def database_reachable(): class GroupTraitValuesTest(ServerTestCase): - def test_group_trait_values_bxd_returns_strain_values(self): - if not database_reachable(): - self.skipTest("no database on 127.0.0.1:3306") + EXTRA_ENV = TOKEN_ENV + def get(self, path): + token = auth_token() + if not token: + self.skipTest("no token (see docs/gn-auth.md)") conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10) try: - conn.request("GET", "/groups/BXD/traits/10001/values") + conn.request("GET", path, headers={"Authorization": f"Bearer {token}"}) response = conn.getresponse() - body = response.read() + return response, response.read() finally: conn.close() + + def test_group_trait_values_bxd_returns_strain_values(self): + if not database_reachable(): + self.skipTest("no database on 127.0.0.1:3306") + + response, body = self.get("/groups/BXD/traits/10001/values") self.assertEqual(response.status, 200) values = json.loads(body) @@ -42,13 +52,7 @@ class GroupTraitValuesTest(ServerTestCase): 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", "/groups/not-a-real-group/traits/10001/values") - response = conn.getresponse() - body = response.read() - finally: - conn.close() + response, body = self.get("/groups/not-a-real-group/traits/10001/values") self.assertEqual(response.status, 404) error = json.loads(body) @@ -61,13 +65,7 @@ class GroupTraitValuesTest(ServerTestCase): 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", "/groups/BXD/traits/999999999/values") - response = conn.getresponse() - body = response.read() - finally: - conn.close() + response, body = self.get("/groups/BXD/traits/999999999/values") self.assertEqual(response.status, 404) error = json.loads(body) diff --git a/test/python/test_group_traits.py b/test/python/test_group_traits.py index e414e7e..d918688 100644 --- a/test/python/test_group_traits.py +++ b/test/python/test_group_traits.py @@ -1,6 +1,7 @@ -"""Mirrors tests/group_traits.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. +"""Mirrors tests/group_traits.rs. Needs a live MariaDB (SQL_URI) and a +valid token (every endpoint needs one now -- see docs/gn-auth.md); +skips with a message rather than failing when either is missing, same +as test_group_by_name.py. """ import http.client @@ -8,6 +9,7 @@ import json import socket import unittest +from auth_helpers import TOKEN_ENV, auth_token from server_test_case import HOST, PORT, ServerTestCase @@ -20,6 +22,20 @@ def database_reachable(): class GroupTraitsTest(ServerTestCase): + EXTRA_ENV = TOKEN_ENV + + def get(self, path, timeout=10): + token = auth_token() + if not token: + self.skipTest("no token (see docs/gn-auth.md)") + conn = http.client.HTTPConnection(HOST, int(PORT), timeout=timeout) + try: + conn.request("GET", path, headers={"Authorization": f"Bearer {token}"}) + response = conn.getresponse() + return response, response.read() + finally: + conn.close() + def test_group_traits_bxd_returns_traits(self): if not database_reachable(): self.skipTest("no database on 127.0.0.1:3306") @@ -27,13 +43,7 @@ class GroupTraitsTest(ServerTestCase): # BXD alone has 15000+ phenotype traits (a ~17MB response); # fetching all of them can take longer than the default 10s # used elsewhere in this test suite. - conn = http.client.HTTPConnection(HOST, int(PORT), timeout=30) - try: - conn.request("GET", "/groups/BXD/traits") - response = conn.getresponse() - body = response.read() - finally: - conn.close() + response, body = self.get("/groups/BXD/traits", timeout=30) self.assertEqual(response.status, 200) traits = json.loads(body) @@ -45,13 +55,7 @@ class GroupTraitsTest(ServerTestCase): 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", "/groups/not-a-real-group/traits") - response = conn.getresponse() - body = response.read() - finally: - conn.close() + response, body = self.get("/groups/not-a-real-group/traits") self.assertEqual(response.status, 404) error = json.loads(body) diff --git a/test/python/test_groups.py b/test/python/test_groups.py index da7b5fa..66466de 100644 --- a/test/python/test_groups.py +++ b/test/python/test_groups.py @@ -1,5 +1,7 @@ -"""Mirrors tests/groups.rs. Needs a live MariaDB (SQL_URI); skips with a -message rather than failing when none is reachable, same as test_species.py. +"""Mirrors tests/groups.rs. Needs a live MariaDB (SQL_URI) and a valid +token (every endpoint needs one now -- see docs/gn-auth.md); skips with +a message rather than failing when either is missing, same as +test_species.py. """ import http.client @@ -7,6 +9,7 @@ import json import socket import unittest +from auth_helpers import TOKEN_ENV, auth_token from server_test_case import HOST, PORT, ServerTestCase @@ -19,13 +22,18 @@ def database_reachable(): class GroupsTest(ServerTestCase): + EXTRA_ENV = TOKEN_ENV + def test_groups_returns_ok_with_bxd(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)") conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10) try: - conn.request("GET", "/groups") + conn.request("GET", "/groups", headers={"Authorization": f"Bearer {token}"}) response = conn.getresponse() body = response.read() finally: diff --git a/test/python/test_species.py b/test/python/test_species.py index f739d98..99ae794 100644 --- a/test/python/test_species.py +++ b/test/python/test_species.py @@ -1,6 +1,7 @@ -"""Mirrors tests/species.rs. Needs a live MariaDB (SQL_URI); skips with a -message rather than failing when none is reachable (e.g. inside the Guix -build sandbox, which has no network/database access). +"""Mirrors tests/species.rs. Needs a live MariaDB (SQL_URI) and a valid +token (every endpoint needs one now -- see docs/gn-auth.md); skips with +a message rather than failing when either is missing (e.g. inside the +Guix build sandbox, which has no network/database access). """ import http.client @@ -8,6 +9,7 @@ import json import socket import unittest +from auth_helpers import TOKEN_ENV, auth_token from server_test_case import HOST, PORT, ServerTestCase @@ -20,13 +22,18 @@ def database_reachable(): class SpeciesTest(ServerTestCase): + EXTRA_ENV = TOKEN_ENV + def test_species_returns_ok_with_species_list(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)") conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10) try: - conn.request("GET", "/species") + conn.request("GET", "/species", headers={"Authorization": f"Bearer {token}"}) response = conn.getresponse() body = response.read() finally: diff --git a/test/python/test_species_by_name.py b/test/python/test_species_by_name.py index 0b8dac2..f42b5fa 100644 --- a/test/python/test_species_by_name.py +++ b/test/python/test_species_by_name.py @@ -1,6 +1,7 @@ -"""Mirrors tests/species_by_name.rs. Needs a live MariaDB (SQL_URI); skips -with a message rather than failing when none is reachable, same as -test_species.py. +"""Mirrors tests/species_by_name.rs. Needs a live MariaDB (SQL_URI) and +a valid token (every endpoint needs one now -- see docs/gn-auth.md); +skips with a message rather than failing when either is missing, same +as test_species.py. """ import http.client @@ -8,6 +9,7 @@ import json import socket import unittest +from auth_helpers import TOKEN_ENV, auth_token from server_test_case import HOST, PORT, ServerTestCase @@ -20,17 +22,25 @@ def database_reachable(): class SpeciesByNameTest(ServerTestCase): - def test_species_by_name_mouse_returns_species_row(self): - if not database_reachable(): - self.skipTest("no database on 127.0.0.1:3306") + EXTRA_ENV = TOKEN_ENV + def get(self, path): + token = auth_token() + if not token: + self.skipTest("no token (see docs/gn-auth.md)") conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10) try: - conn.request("GET", "/species/mouse") + conn.request("GET", path, headers={"Authorization": f"Bearer {token}"}) response = conn.getresponse() - body = response.read() + return response, response.read() finally: conn.close() + + def test_species_by_name_mouse_returns_species_row(self): + if not database_reachable(): + self.skipTest("no database on 127.0.0.1:3306") + + response, body = self.get("/species/mouse") self.assertEqual(response.status, 200) species = json.loads(body) @@ -42,13 +52,7 @@ class SpeciesByNameTest(ServerTestCase): 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", "/species/not-a-real-species") - response = conn.getresponse() - body = response.read() - finally: - conn.close() + response, body = self.get("/species/not-a-real-species") self.assertEqual(response.status, 404) request_id = response.getheader("x-request-id") diff --git a/test/python/test_species_datasets.py b/test/python/test_species_datasets.py index 4f759b6..e74871d 100644 --- a/test/python/test_species_datasets.py +++ b/test/python/test_species_datasets.py @@ -1,6 +1,7 @@ -"""Mirrors tests/species_datasets.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. +"""Mirrors tests/species_datasets.rs. Needs a live MariaDB (SQL_URI) +and a valid token (every endpoint needs one now -- see +docs/gn-auth.md); skips with a message rather than failing when either +is missing, same as test_group_by_name.py. """ import http.client @@ -8,6 +9,7 @@ import json import socket import unittest +from auth_helpers import TOKEN_ENV, auth_token from server_test_case import HOST, PORT, ServerTestCase @@ -20,17 +22,25 @@ def database_reachable(): class SpeciesDatasetsTest(ServerTestCase): - def test_species_datasets_mouse_returns_datasets(self): - if not database_reachable(): - self.skipTest("no database on 127.0.0.1:3306") + EXTRA_ENV = TOKEN_ENV + def get(self, path): + token = auth_token() + if not token: + self.skipTest("no token (see docs/gn-auth.md)") conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10) try: - conn.request("GET", "/species/mouse/datasets") + conn.request("GET", path, headers={"Authorization": f"Bearer {token}"}) response = conn.getresponse() - body = response.read() + return response, response.read() finally: conn.close() + + def test_species_datasets_mouse_returns_datasets(self): + if not database_reachable(): + self.skipTest("no database on 127.0.0.1:3306") + + response, body = self.get("/species/mouse/datasets") self.assertEqual(response.status, 200) datasets = json.loads(body) @@ -42,13 +52,7 @@ class SpeciesDatasetsTest(ServerTestCase): 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", "/species/not-a-real-species/datasets") - response = conn.getresponse() - body = response.read() - finally: - conn.close() + response, body = self.get("/species/not-a-real-species/datasets") self.assertEqual(response.status, 404) error = json.loads(body) diff --git a/test/python/test_traits.py b/test/python/test_traits.py index 97603ba..38bf411 100644 --- a/test/python/test_traits.py +++ b/test/python/test_traits.py @@ -1,20 +1,19 @@ """Mirrors tests/traits.rs. Needs a live MariaDB (SQL_URI) and a live -gn-auth (AUTH_SERVER_URL) plus GN4_TEST_AUTH_TOKEN (see -docs/gn-auth.md); skips when any is missing, same as -test_dataset_probeset_info.py. +gn-auth (AUTH_SERVER_URL) plus a token (see docs/gn-auth.md); skips +when any is missing, same as test_dataset_probeset_info.py. +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 import json -import os import socket import unittest -from urllib.parse import urlparse +from auth_helpers import TOKEN_ENV, auth_server_reachable, auth_token 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: @@ -24,30 +23,16 @@ 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 TraitsTest(ServerTestCase): + EXTRA_ENV = TOKEN_ENV + 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)") + self.skipTest("no token (see docs/gn-auth.md)") if not auth_server_reachable(): - self.skipTest(f"no gn-auth reachable at {AUTH_SERVER_URL}") + self.skipTest("no gn-auth reachable") def get(self, path, with_token=True): conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10) |
