about summary refs log tree commit diff
path: root/test/python/test_get_test_token.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/python/test_get_test_token.py')
-rw-r--r--test/python/test_get_test_token.py91
1 files changed, 52 insertions, 39 deletions
diff --git a/test/python/test_get_test_token.py b/test/python/test_get_test_token.py
index 6cd07c0..43bde8b 100644
--- a/test/python/test_get_test_token.py
+++ b/test/python/test_get_test_token.py
@@ -1,10 +1,11 @@
-"""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.
+"""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
@@ -30,52 +31,36 @@ def auth_server_reachable():
         conn.close()
 
 
-def post_test_token():
+def post_test_token(headers=None):
     conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10)
     try:
-        conn.request("POST", "/get-test-token")
+        conn.request("POST", "/get-test-token", headers=headers or {})
         response = conn.getresponse()
         return response, response.read()
     finally:
         conn.close()
 
 
-class GetTestTokenDisabledByDefaultTest(ServerTestCase):
-    """No EXTRA_ENV -- the out-of-the-box default (dev_mode off)."""
-
-    def test_get_test_token_returns_404_by_default(self):
-        response, _body = post_test_token()
-        self.assertEqual(response.status, 404)
-
-    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.
+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()
-        spec = json.loads(body)
-        self.assertNotIn("/get-test-token", spec["paths"])
-
-
-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_get_test_token_returns_404(self):
-        response, _body = post_test_token()
-        self.assertEqual(response.status, 404)
-
-
-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_get_test_token_returns_a_usable_token(self):
+        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}")
 
@@ -101,5 +86,33 @@ class GetTestTokenEnabledTest(ServerTestCase):
         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()