"""Mirrors tests/get_test_token.rs. POST /get-test-token is not tied to a mode or a bind address: it is registered and documented by default (GENENETWORK4_TEST_TOKEN switches it off), and answers only a caller on this machine (a local TCP peer, no proxy header; see src/routes/dev.rs). This client is local, so it is answered; a request claiming to come through a proxy is refused. (A request from another machine cannot be made from here; the Rust test and the unit tests cover it.) """ import http.client import json import os import unittest from urllib.parse import urlparse from server_test_case import HOST, PORT, ServerTestCase, envelope_data AUTH_SERVER_URL = os.environ.get("AUTH_SERVER_URL", "http://127.0.0.1:8081/") 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 post_test_token(headers=None): conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10) try: conn.request("POST", "/get-test-token", headers=headers or {}) response = conn.getresponse() return response, response.read() finally: conn.close() class GetTestTokenTest(ServerTestCase): def test_get_test_token_is_documented_without_any_configuration(self): conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10) try: conn.request("GET", "/openapi.json") body = conn.getresponse().read() finally: conn.close() operation = json.loads(body)["paths"]["/get-test-token"]["post"] self.assertIn("403", operation["responses"]) def test_get_test_token_refuses_a_request_that_came_through_a_proxy(self): for header in ("X-Forwarded-For", "Forwarded", "X-Real-IP"): response, body = post_test_token({header: "203.0.113.9"}) self.assertEqual(response.status, 403, header) error = json.loads(body) self.assertEqual(error["code"], "forbidden") self.assertEqual(error["operation_id"], "get_test_token") def test_get_test_token_returns_a_usable_token_to_a_local_caller(self): if not auth_server_reachable(): self.skipTest(f"no gn-auth reachable at {AUTH_SERVER_URL}") response, body = post_test_token() self.assertEqual(response.status, 200) token = envelope_data(body) self.assertTrue(token["access_token"]) self.assertEqual(token["token_type"], "Bearer") # Exercise the issued token against a real gated endpoint -- it # should be genuinely usable, not just a well-formed response. conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10) try: conn.request( "GET", "/traits/BXD/10001", headers={"Authorization": f"Bearer {token['access_token']}"}, ) response = conn.getresponse() finally: conn.close() self.assertEqual(response.status, 200) class GetTestTokenSwitchedOffTest(ServerTestCase): """GENENETWORK4_TEST_TOKEN not clearly on: the route is not registered.""" EXTRA_ENV = {"GENENETWORK4_TEST_TOKEN": "false"} def test_get_test_token_is_a_404_and_not_documented(self): response, _body = post_test_token() self.assertEqual(response.status, 404) conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10) try: conn.request("GET", "/openapi.json") body = conn.getresponse().read() finally: conn.close() self.assertNotIn("/get-test-token", json.loads(body)["paths"]) class GetTestTokenTypoFailsClosedTest(ServerTestCase): """A misspelt "off" must not leave the endpoint running.""" EXTRA_ENV = {"GENENETWORK4_TEST_TOKEN": "flase"} def test_get_test_token_is_a_404(self): response, _body = post_test_token() self.assertEqual(response.status, 404) if __name__ == "__main__": unittest.main()