diff --git a/Cargo.toml b/Cargo.toml index f8c1b278..ea9b262f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -151,6 +151,11 @@ version = "0.27.2" features = ["http1", "http2"] default-features = false +# Snapshot testing +[workspace.dependencies.insta] +version = "1.39.0" +features = ["yaml", "json"] + # Email sending [workspace.dependencies.lettre] version = "0.11.7" diff --git a/crates/handlers/Cargo.toml b/crates/handlers/Cargo.toml index 0d5b0c7f..db964e2b 100644 --- a/crates/handlers/Cargo.toml +++ b/crates/handlers/Cargo.toml @@ -99,7 +99,7 @@ oauth2-types.workspace = true zxcvbn = "3.1.0" [dev-dependencies] -insta = "1.39.0" +insta.workspace = true tracing-subscriber.workspace = true cookie_store = "0.21.0" sqlx.workspace = true diff --git a/crates/handlers/src/admin/v1/oauth2_sessions/get.rs b/crates/handlers/src/admin/v1/oauth2_sessions/get.rs index 078c92a1..9e5dc5f5 100644 --- a/crates/handlers/src/admin/v1/oauth2_sessions/get.rs +++ b/crates/handlers/src/admin/v1/oauth2_sessions/get.rs @@ -83,3 +83,77 @@ pub async fn handler( session, )))) } + +#[cfg(test)] +mod tests { + use hyper::{Request, StatusCode}; + use mas_data_model::AccessToken; + use sqlx::PgPool; + use ulid::Ulid; + + use crate::test_utils::{setup, RequestBuilderExt, ResponseExt, TestState}; + + #[sqlx::test(migrator = "mas_storage_pg::MIGRATOR")] + async fn test_get(pool: PgPool) { + setup(); + let mut state = TestState::from_pool(pool).await.unwrap(); + let token = state.token_with_scope("urn:mas:admin").await; + + // state.token_with_scope did create a session, so we can get it here + let mut repo = state.repository().await.unwrap(); + let AccessToken { session_id, .. } = repo + .oauth2_access_token() + .find_by_token(&token) + .await + .unwrap() + .unwrap(); + repo.save().await.unwrap(); + + let request = Request::get(format!("/api/admin/v1/oauth2-sessions/{session_id}")) + .bearer(&token) + .empty(); + let response = state.request(request).await; + response.assert_status(StatusCode::OK); + let body: serde_json::Value = response.json(); + assert_eq!(body["data"]["type"], "oauth2-session"); + insta::assert_json_snapshot!(body, @r###" + { + "data": { + "type": "oauth2-session", + "id": "01FSHN9AG0MKGTBNZ16RDR3PVY", + "attributes": { + "created_at": "2022-01-16T14:40:00Z", + "finished_at": null, + "user_id": null, + "user_session_id": null, + "client_id": "01FSHN9AG0FAQ50MT1E9FFRPZR", + "scope": "urn:mas:admin", + "user_agent": null, + "last_active_at": null, + "last_active_ip": null + }, + "links": { + "self": "/api/admin/v1/oauth2-sessions/01FSHN9AG0MKGTBNZ16RDR3PVY" + } + }, + "links": { + "self": "/api/admin/v1/oauth2-sessions/01FSHN9AG0MKGTBNZ16RDR3PVY" + } + } + "###); + } + + #[sqlx::test(migrator = "mas_storage_pg::MIGRATOR")] + async fn test_not_found(pool: PgPool) { + setup(); + let mut state = TestState::from_pool(pool).await.unwrap(); + let token = state.token_with_scope("urn:mas:admin").await; + + let session_id = Ulid::nil(); + let request = Request::get(format!("/api/admin/v1/oauth2-sessions/{session_id}")) + .bearer(&token) + .empty(); + let response = state.request(request).await; + response.assert_status(StatusCode::NOT_FOUND); + } +} diff --git a/crates/jose/Cargo.toml b/crates/jose/Cargo.toml index 47433d42..74b9b313 100644 --- a/crates/jose/Cargo.toml +++ b/crates/jose/Cargo.toml @@ -38,5 +38,5 @@ url.workspace = true mas-iana.workspace = true [dev-dependencies] -insta = { version = "1.39.0" } +insta.workspace = true rand_chacha = "0.3.1" diff --git a/crates/keystore/Cargo.toml b/crates/keystore/Cargo.toml index 32084977..bc6c0b8d 100644 --- a/crates/keystore/Cargo.toml +++ b/crates/keystore/Cargo.toml @@ -36,5 +36,5 @@ mas-iana.workspace = true mas-jose.workspace = true [dev-dependencies] -insta = { version = "1.39.0", features = ["yaml"] } +insta.workspace = true rand_chacha = "0.3.1"