|
| 1 | +// Copyright 2025 New Vector Ltd. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: AGPL-3.0-only |
| 4 | +// Please see LICENSE in the repository root for full details. |
| 5 | + |
| 6 | +use aide::{transform::TransformOperation, OperationIo}; |
| 7 | +use axum::{response::IntoResponse, Json}; |
| 8 | +use hyper::StatusCode; |
| 9 | +use ulid::Ulid; |
| 10 | + |
| 11 | +use crate::{ |
| 12 | + admin::{ |
| 13 | + call_context::CallContext, |
| 14 | + model::UserSession, |
| 15 | + params::UlidPathParam, |
| 16 | + response::{ErrorResponse, SingleResponse}, |
| 17 | + }, |
| 18 | + impl_from_error_for_route, |
| 19 | +}; |
| 20 | + |
| 21 | +#[derive(Debug, thiserror::Error, OperationIo)] |
| 22 | +#[aide(output_with = "Json<ErrorResponse>")] |
| 23 | +pub enum RouteError { |
| 24 | + #[error(transparent)] |
| 25 | + Internal(Box<dyn std::error::Error + Send + Sync + 'static>), |
| 26 | + |
| 27 | + #[error("User session ID {0} not found")] |
| 28 | + NotFound(Ulid), |
| 29 | +} |
| 30 | + |
| 31 | +impl_from_error_for_route!(mas_storage::RepositoryError); |
| 32 | + |
| 33 | +impl IntoResponse for RouteError { |
| 34 | + fn into_response(self) -> axum::response::Response { |
| 35 | + let error = ErrorResponse::from_error(&self); |
| 36 | + let status = match self { |
| 37 | + Self::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR, |
| 38 | + Self::NotFound(_) => StatusCode::NOT_FOUND, |
| 39 | + }; |
| 40 | + (status, Json(error)).into_response() |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +pub fn doc(operation: TransformOperation) -> TransformOperation { |
| 45 | + operation |
| 46 | + .id("getUserSession") |
| 47 | + .summary("Get a user session") |
| 48 | + .tag("user-session") |
| 49 | + .response_with::<200, Json<SingleResponse<UserSession>>, _>(|t| { |
| 50 | + let [sample, ..] = UserSession::samples(); |
| 51 | + let response = SingleResponse::new_canonical(sample); |
| 52 | + t.description("User session was found").example(response) |
| 53 | + }) |
| 54 | + .response_with::<404, RouteError, _>(|t| { |
| 55 | + let response = ErrorResponse::from_error(&RouteError::NotFound(Ulid::nil())); |
| 56 | + t.description("User session was not found") |
| 57 | + .example(response) |
| 58 | + }) |
| 59 | +} |
| 60 | + |
| 61 | +#[tracing::instrument(name = "handler.admin.v1.user_sessions.get", skip_all, err)] |
| 62 | +pub async fn handler( |
| 63 | + CallContext { mut repo, .. }: CallContext, |
| 64 | + id: UlidPathParam, |
| 65 | +) -> Result<Json<SingleResponse<UserSession>>, RouteError> { |
| 66 | + let session = repo |
| 67 | + .browser_session() |
| 68 | + .lookup(*id) |
| 69 | + .await? |
| 70 | + .ok_or(RouteError::NotFound(*id))?; |
| 71 | + |
| 72 | + Ok(Json(SingleResponse::new_canonical(UserSession::from( |
| 73 | + session, |
| 74 | + )))) |
| 75 | +} |
| 76 | + |
| 77 | +#[cfg(test)] |
| 78 | +mod tests { |
| 79 | + use hyper::{Request, StatusCode}; |
| 80 | + use insta::assert_json_snapshot; |
| 81 | + use sqlx::PgPool; |
| 82 | + |
| 83 | + use crate::test_utils::{setup, RequestBuilderExt, ResponseExt, TestState}; |
| 84 | + |
| 85 | + #[sqlx::test(migrator = "mas_storage_pg::MIGRATOR")] |
| 86 | + async fn test_get(pool: PgPool) { |
| 87 | + setup(); |
| 88 | + let mut state = TestState::from_pool(pool).await.unwrap(); |
| 89 | + let token = state.token_with_scope("urn:mas:admin").await; |
| 90 | + let mut rng = state.rng(); |
| 91 | + |
| 92 | + // Provision a user and a user session |
| 93 | + let mut repo = state.repository().await.unwrap(); |
| 94 | + let user = repo |
| 95 | + .user() |
| 96 | + .add(&mut rng, &state.clock, "alice".to_owned()) |
| 97 | + .await |
| 98 | + .unwrap(); |
| 99 | + let session = repo |
| 100 | + .browser_session() |
| 101 | + .add(&mut rng, &state.clock, &user, None) |
| 102 | + .await |
| 103 | + .unwrap(); |
| 104 | + repo.save().await.unwrap(); |
| 105 | + |
| 106 | + let session_id = session.id; |
| 107 | + let request = Request::get(format!("/api/admin/v1/user-sessions/{session_id}")) |
| 108 | + .bearer(&token) |
| 109 | + .empty(); |
| 110 | + let response = state.request(request).await; |
| 111 | + response.assert_status(StatusCode::OK); |
| 112 | + let body: serde_json::Value = response.json(); |
| 113 | + assert_json_snapshot!(body, @r###" |
| 114 | + { |
| 115 | + "data": { |
| 116 | + "type": "user-session", |
| 117 | + "id": "01FSHN9AG0AJ6AC5HQ9X6H4RP4", |
| 118 | + "attributes": { |
| 119 | + "created_at": "2022-01-16T14:40:00Z", |
| 120 | + "finished_at": null, |
| 121 | + "user_id": "01FSHN9AG0MZAA6S4AF7CTV32E", |
| 122 | + "user_agent": null, |
| 123 | + "last_active_at": null, |
| 124 | + "last_active_ip": null |
| 125 | + }, |
| 126 | + "links": { |
| 127 | + "self": "/api/admin/v1/user-sessions/01FSHN9AG0AJ6AC5HQ9X6H4RP4" |
| 128 | + } |
| 129 | + }, |
| 130 | + "links": { |
| 131 | + "self": "/api/admin/v1/user-sessions/01FSHN9AG0AJ6AC5HQ9X6H4RP4" |
| 132 | + } |
| 133 | + } |
| 134 | + "###); |
| 135 | + } |
| 136 | +} |
0 commit comments