f438e972d4
Lift duplicated test-harness code (User factories, TestConfig builder, login/CSRF flow, form/json helpers, case-directory seeding, URL path builders) into `server/tests/common/` so future API changes touch one file instead of every integration test. Migrated batches: csrf_attack, auth, login, magic_link, security_headers (28 tests); analyze, case_page, delete_recording, close_watermark, retention_sweep (67 tests). All 95 tests still green. Net line delta across these 10 files: +1113 / -1896 (~780 lines removed), plus ~500 lines of new shared infrastructure under tests/common/.
71 lines
1.7 KiB
Rust
71 lines
1.7 KiB
Rust
mod common;
|
|
|
|
use axum::body::Body;
|
|
use axum::http::{Request, StatusCode};
|
|
use tower::util::ServiceExt;
|
|
|
|
use common::{TestConfig, test_user};
|
|
|
|
#[tokio::test]
|
|
async fn valid_api_key_returns_user() {
|
|
let app = doctate_server::create_router(
|
|
TestConfig::new().with_user(test_user("dr_test")).build(),
|
|
);
|
|
|
|
let response = app
|
|
.oneshot(
|
|
Request::builder()
|
|
.uri("/api/debug/whoami")
|
|
.header("X-API-Key", "key-dr_test")
|
|
.body(Body::empty())
|
|
.unwrap(),
|
|
)
|
|
.await
|
|
.unwrap();
|
|
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
|
|
let json = common::body_json(response).await;
|
|
assert_eq!(json["slug"], "dr_test");
|
|
assert_eq!(json["role"], "doctor");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn invalid_api_key_returns_401() {
|
|
let app = doctate_server::create_router(
|
|
TestConfig::new().with_user(test_user("dr_test")).build(),
|
|
);
|
|
|
|
let response = app
|
|
.oneshot(
|
|
Request::builder()
|
|
.uri("/api/debug/whoami")
|
|
.header("X-API-Key", "wrong-key")
|
|
.body(Body::empty())
|
|
.unwrap(),
|
|
)
|
|
.await
|
|
.unwrap();
|
|
|
|
assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn missing_api_key_returns_401() {
|
|
let app = doctate_server::create_router(
|
|
TestConfig::new().with_user(test_user("dr_test")).build(),
|
|
);
|
|
|
|
let response = app
|
|
.oneshot(
|
|
Request::builder()
|
|
.uri("/api/debug/whoami")
|
|
.body(Body::empty())
|
|
.unwrap(),
|
|
)
|
|
.await
|
|
.unwrap();
|
|
|
|
assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
|
|
}
|