Skip to content
This repository was archived by the owner on Sep 10, 2024. It is now read-only.

Commit 9c18ffa

Browse files
committed
admin: add tests for the get OAuth session API
1 parent 77b5500 commit 9c18ffa

File tree

5 files changed

+82
-3
lines changed

5 files changed

+82
-3
lines changed

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ version = "0.27.2"
151151
features = ["http1", "http2"]
152152
default-features = false
153153

154+
# Snapshot testing
155+
[workspace.dependencies.insta]
156+
version = "1.39.0"
157+
features = ["yaml", "json"]
158+
154159
# Email sending
155160
[workspace.dependencies.lettre]
156161
version = "0.11.7"

crates/handlers/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ oauth2-types.workspace = true
9999
zxcvbn = "3.1.0"
100100

101101
[dev-dependencies]
102-
insta = "1.39.0"
102+
insta.workspace = true
103103
tracing-subscriber.workspace = true
104104
cookie_store = "0.21.0"
105105
sqlx.workspace = true

crates/handlers/src/admin/v1/oauth2_sessions/get.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,77 @@ pub async fn handler(
8383
session,
8484
))))
8585
}
86+
87+
#[cfg(test)]
88+
mod tests {
89+
use hyper::{Request, StatusCode};
90+
use mas_data_model::AccessToken;
91+
use sqlx::PgPool;
92+
use ulid::Ulid;
93+
94+
use crate::test_utils::{setup, RequestBuilderExt, ResponseExt, TestState};
95+
96+
#[sqlx::test(migrator = "mas_storage_pg::MIGRATOR")]
97+
async fn test_get(pool: PgPool) {
98+
setup();
99+
let mut state = TestState::from_pool(pool).await.unwrap();
100+
let token = state.token_with_scope("urn:mas:admin").await;
101+
102+
// state.token_with_scope did create a session, so we can get it here
103+
let mut repo = state.repository().await.unwrap();
104+
let AccessToken { session_id, .. } = repo
105+
.oauth2_access_token()
106+
.find_by_token(&token)
107+
.await
108+
.unwrap()
109+
.unwrap();
110+
repo.save().await.unwrap();
111+
112+
let request = Request::get(format!("/api/admin/v1/oauth2-sessions/{session_id}"))
113+
.bearer(&token)
114+
.empty();
115+
let response = state.request(request).await;
116+
response.assert_status(StatusCode::OK);
117+
let body: serde_json::Value = response.json();
118+
assert_eq!(body["data"]["type"], "oauth2-session");
119+
insta::assert_json_snapshot!(body, @r###"
120+
{
121+
"data": {
122+
"type": "oauth2-session",
123+
"id": "01FSHN9AG0MKGTBNZ16RDR3PVY",
124+
"attributes": {
125+
"created_at": "2022-01-16T14:40:00Z",
126+
"finished_at": null,
127+
"user_id": null,
128+
"user_session_id": null,
129+
"client_id": "01FSHN9AG0FAQ50MT1E9FFRPZR",
130+
"scope": "urn:mas:admin",
131+
"user_agent": null,
132+
"last_active_at": null,
133+
"last_active_ip": null
134+
},
135+
"links": {
136+
"self": "/api/admin/v1/oauth2-sessions/01FSHN9AG0MKGTBNZ16RDR3PVY"
137+
}
138+
},
139+
"links": {
140+
"self": "/api/admin/v1/oauth2-sessions/01FSHN9AG0MKGTBNZ16RDR3PVY"
141+
}
142+
}
143+
"###);
144+
}
145+
146+
#[sqlx::test(migrator = "mas_storage_pg::MIGRATOR")]
147+
async fn test_not_found(pool: PgPool) {
148+
setup();
149+
let mut state = TestState::from_pool(pool).await.unwrap();
150+
let token = state.token_with_scope("urn:mas:admin").await;
151+
152+
let session_id = Ulid::nil();
153+
let request = Request::get(format!("/api/admin/v1/oauth2-sessions/{session_id}"))
154+
.bearer(&token)
155+
.empty();
156+
let response = state.request(request).await;
157+
response.assert_status(StatusCode::NOT_FOUND);
158+
}
159+
}

crates/jose/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ url.workspace = true
3838
mas-iana.workspace = true
3939

4040
[dev-dependencies]
41-
insta = { version = "1.39.0" }
41+
insta.workspace = true
4242
rand_chacha = "0.3.1"

crates/keystore/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ mas-iana.workspace = true
3636
mas-jose.workspace = true
3737

3838
[dev-dependencies]
39-
insta = { version = "1.39.0", features = ["yaml"] }
39+
insta.workspace = true
4040
rand_chacha = "0.3.1"

0 commit comments

Comments
 (0)