-
Notifications
You must be signed in to change notification settings - Fork 9
test(authenticator): e2e 401 contract for the session-cookie surface #2131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cyberantonz
merged 3 commits into
constructorfabric:main
from
cyberantonz:test/401-coverage
Aug 3, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
86ab785
test(authenticator): e2e 401 contract for the session-cookie surface
cyberantonz 75d65d9
test(analytics): 401 is real — cover it on every operation, drop it f…
cyberantonz b261958
test: address review — assert 401 body for unknown tokens, type the a…
cyberantonz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
src/backend/services/authenticator/tests/e2e_unauthorized.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| //! End-to-end 401 contract for the session-cookie surface: every `.public()` | ||
| //! route that requires a session answers 401 both without a cookie and with a | ||
| //! cookie that resolves to no session. | ||
| //! | ||
| //! `#[ignore]` by default (needs the stack up; `run-e2e.sh` drives it): | ||
| //! | ||
| //! ```text | ||
| //! AUTH_BASE=http://localhost:8083 \ | ||
| //! cargo test -p authenticator --test e2e_unauthorized -- --ignored --nocapture | ||
| //! ``` | ||
| //! | ||
| //! Covers the 401 responses declared in the OpenAPI spec for /auth/me, | ||
| //! /auth/csrf, and the /auth/sessions family; /auth/refresh and | ||
| //! /internal/authz 401s are exercised by `e2e_refresh` and `e2e_login_loop`. | ||
|
|
||
| #![allow(clippy::unwrap_used, clippy::expect_used)] | ||
|
|
||
| mod common; | ||
|
|
||
| use reqwest::Method; | ||
|
|
||
| const COOKIE: &str = "__Host-sid"; | ||
|
|
||
| fn env(key: &str, default: &str) -> String { | ||
| std::env::var(key).unwrap_or_else(|_| default.to_owned()) | ||
| } | ||
|
|
||
| const SESSION_ROUTES: &[(Method, &str)] = &[ | ||
| (Method::GET, "/auth/me"), | ||
| (Method::GET, "/auth/csrf"), | ||
| (Method::GET, "/auth/sessions"), | ||
| (Method::DELETE, "/auth/sessions"), | ||
| ( | ||
| Method::DELETE, | ||
| "/auth/sessions/00000000-0000-7000-8000-000000000000", | ||
| ), | ||
| ]; | ||
|
|
||
| #[tokio::test] | ||
| #[ignore = "requires a running authenticator + fakeidp + Redis stack"] | ||
| async fn session_routes_reject_missing_cookie_with_401() { | ||
| let auth_base = env("AUTH_BASE", "http://localhost:8083"); | ||
| let http = common::client(); | ||
|
|
||
| for (method, path) in SESSION_ROUTES { | ||
| let resp = http | ||
| .request(method.clone(), format!("{auth_base}{path}")) | ||
| .send() | ||
| .await | ||
| .unwrap(); | ||
| assert_eq!( | ||
| resp.status(), | ||
| 401, | ||
| "{method} {path} without a cookie must be 401" | ||
| ); | ||
| let body = resp.text().await.unwrap(); | ||
| assert_eq!( | ||
| body, r#"{"error":"unauthenticated"}"#, | ||
| "{method} {path} must return the unauthenticated body" | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| #[ignore = "requires a running authenticator + fakeidp + Redis stack"] | ||
| async fn session_routes_reject_unknown_session_token_with_401() { | ||
| let auth_base = env("AUTH_BASE", "http://localhost:8083"); | ||
| let http = common::client(); | ||
|
|
||
| for (method, path) in SESSION_ROUTES { | ||
| let resp = http | ||
| .request(method.clone(), format!("{auth_base}{path}")) | ||
| .header( | ||
| reqwest::header::COOKIE.as_str(), | ||
| format!("{COOKIE}=e2e-unauthorized-not-a-session"), | ||
| ) | ||
| .send() | ||
| .await | ||
| .unwrap(); | ||
| assert_eq!( | ||
| resp.status(), | ||
| 401, | ||
| "{method} {path} with an unknown session token must be 401" | ||
| ); | ||
| let body = resp.text().await.unwrap(); | ||
| assert_eq!( | ||
| body, r#"{"error":"unauthenticated"}"#, | ||
| "{method} {path} must return the unauthenticated body" | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| """Contract: every documented operation answers 401 to an anonymous call. | ||
|
|
||
| The rig runs auth-ENABLED (the gears host's oidc-authn-plugin verifies the | ||
| gateway JWT), so authentication is rejected before any handler runs: no body, | ||
| no path lookup, no tenant resolution. One case per spec operation keeps the | ||
| coverage ledger honest — 401 left UNIVERSAL_BOILERPLATE when the auth-disabled | ||
| rig did (lib/api_coverage.py), so each declared 401 must now be observed. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
|
|
||
| pytestmark = pytest.mark.api | ||
|
|
||
| PLACEHOLDER_ID = "00000000-0000-7000-8000-000000000000" | ||
|
|
||
| OPERATIONS = [ | ||
| ("GET", "/v1/admin/metric-thresholds"), | ||
| ("POST", "/v1/admin/metric-thresholds"), | ||
| ("GET", f"/v1/admin/metric-thresholds/{PLACEHOLDER_ID}"), | ||
| ("PUT", f"/v1/admin/metric-thresholds/{PLACEHOLDER_ID}"), | ||
| ("DELETE", f"/v1/admin/metric-thresholds/{PLACEHOLDER_ID}"), | ||
| ("POST", "/v1/catalog/get_metrics"), | ||
| ("GET", "/v1/columns"), | ||
| ("GET", "/v1/columns/anon_probe_table"), | ||
| ("GET", "/v1/metric-definitions"), | ||
| ("POST", "/v1/metric-drilldown"), | ||
| ("POST", "/v1/metric-results"), | ||
| ("GET", "/v1/metrics"), | ||
| ("POST", "/v1/metrics"), | ||
| ("POST", "/v1/metrics/queries"), | ||
| ("GET", f"/v1/metrics/{PLACEHOLDER_ID}"), | ||
| ("PUT", f"/v1/metrics/{PLACEHOLDER_ID}"), | ||
| ("DELETE", f"/v1/metrics/{PLACEHOLDER_ID}"), | ||
| ("POST", f"/v1/metrics/{PLACEHOLDER_ID}/query"), | ||
| ("GET", f"/v1/metrics/{PLACEHOLDER_ID}/thresholds"), | ||
| ("POST", f"/v1/metrics/{PLACEHOLDER_ID}/thresholds"), | ||
| ("PUT", f"/v1/metrics/{PLACEHOLDER_ID}/thresholds/{PLACEHOLDER_ID}"), | ||
| ("DELETE", f"/v1/metrics/{PLACEHOLDER_ID}/thresholds/{PLACEHOLDER_ID}"), | ||
| ("GET", "/v1/persons/anon@example.com"), | ||
| ("GET", "/v1/queries"), | ||
| ("POST", "/v1/queries"), | ||
| ("GET", f"/v1/queries/{PLACEHOLDER_ID}"), | ||
| ("PUT", f"/v1/queries/{PLACEHOLDER_ID}"), | ||
| ("DELETE", f"/v1/queries/{PLACEHOLDER_ID}"), | ||
| ("POST", f"/v1/queries/{PLACEHOLDER_ID}/run"), | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize(("method", "path"), OPERATIONS, ids=lambda v: v if isinstance(v, str) else None) | ||
| def test_anonymous_call_is_rejected_401(anon_api, method: str, path: str) -> None: | ||
| r = anon_api.request(method, path) | ||
| assert r.status_code == 401, f"{method} {path}: status={r.status_code} body={r.text}" | ||
|
|
||
|
|
||
| def test_garbage_bearer_is_rejected_401(anon_api) -> None: | ||
| r = anon_api.get("/v1/metrics", headers={"Authorization": "Bearer not-a-jwt"}) | ||
| assert r.status_code == 401, f"status={r.status_code} body={r.text}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.