|
| 1 | + |
| 2 | +import pytest |
| 3 | +import requests |
| 4 | + |
| 5 | +def _get(url, headers=None, timeout=10): |
| 6 | + return requests.get(url, headers=headers or {}, timeout=timeout) |
| 7 | + |
| 8 | +@pytest.mark.smoketest |
| 9 | +def test_ping(nhsd_apim_proxy_url): |
| 10 | + resp = requests.get(nhsd_apim_proxy_url + "/_ping") |
| 11 | + assert resp.status_code == 200 |
| 12 | + print("Ping Response Body:", resp.text) |
| 13 | + |
| 14 | +@pytest.mark.smoketest |
| 15 | +def test_401_status_without_api_key(nhsd_apim_proxy_url): |
| 16 | + resp = requests.get( |
| 17 | + f"{nhsd_apim_proxy_url}/_status" |
| 18 | + ) |
| 19 | + assert resp.status_code == 401 |
| 20 | + |
| 21 | +@pytest.mark.smoketest |
| 22 | +@pytest.mark.nhsd_apim_authorization(access="application", level="level3") |
| 23 | +def test_invalid_jwt_rejected(nhsd_apim_proxy_url, nhsd_apim_auth_headers): |
| 24 | + """ |
| 25 | + Best-effort: if gateway validates JWTs, an invalid token should be rejected. |
| 26 | + If JWT not used in this env, test is skipped. |
| 27 | + """ |
| 28 | + headers = { |
| 29 | + **nhsd_apim_auth_headers, |
| 30 | + "headerauth1": "headervalue1", |
| 31 | + "x-request-id": "123456" |
| 32 | + } |
| 33 | + print(headers) |
| 34 | + # If no Authorization configured in project headers, skip |
| 35 | + if "Authorization" not in headers: |
| 36 | + pytest.skip("JWT auth not configured for this environment") |
| 37 | + |
| 38 | + bad_headers = dict(headers) |
| 39 | + bad_headers["Authorization"] = "Bearer invalid.invalid.invalid" |
| 40 | + status = _get(f"{nhsd_apim_proxy_url}/_status", headers=bad_headers).status_code |
| 41 | + assert status in (401, 403), "Expected gateway to reject invalid JWT" |
0 commit comments