| 
 | 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::CompatSession,  | 
 | 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("Compatibility 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("getCompatSession")  | 
 | 47 | +        .summary("Get a compatibility session")  | 
 | 48 | +        .tag("compat-session")  | 
 | 49 | +        .response_with::<200, Json<SingleResponse<CompatSession>>, _>(|t| {  | 
 | 50 | +            let [sample, ..] = CompatSession::samples();  | 
 | 51 | +            let response = SingleResponse::new_canonical(sample);  | 
 | 52 | +            t.description("Compatibility session was found")  | 
 | 53 | +                .example(response)  | 
 | 54 | +        })  | 
 | 55 | +        .response_with::<404, RouteError, _>(|t| {  | 
 | 56 | +            let response = ErrorResponse::from_error(&RouteError::NotFound(Ulid::nil()));  | 
 | 57 | +            t.description("Compatibility session was not found")  | 
 | 58 | +                .example(response)  | 
 | 59 | +        })  | 
 | 60 | +}  | 
 | 61 | + | 
 | 62 | +#[tracing::instrument(name = "handler.admin.v1.compat_sessions.get", skip_all, err)]  | 
 | 63 | +pub async fn handler(  | 
 | 64 | +    CallContext { mut repo, .. }: CallContext,  | 
 | 65 | +    id: UlidPathParam,  | 
 | 66 | +) -> Result<Json<SingleResponse<CompatSession>>, RouteError> {  | 
 | 67 | +    let session = repo  | 
 | 68 | +        .compat_session()  | 
 | 69 | +        .lookup(*id)  | 
 | 70 | +        .await?  | 
 | 71 | +        .ok_or(RouteError::NotFound(*id))?;  | 
 | 72 | + | 
 | 73 | +    let sso_login = repo.compat_sso_login().find_for_session(&session).await?;  | 
 | 74 | + | 
 | 75 | +    Ok(Json(SingleResponse::new_canonical(CompatSession::from((  | 
 | 76 | +        session, sso_login,  | 
 | 77 | +    )))))  | 
 | 78 | +}  | 
 | 79 | + | 
 | 80 | +#[cfg(test)]  | 
 | 81 | +mod tests {  | 
 | 82 | +    use hyper::{Request, StatusCode};  | 
 | 83 | +    use insta::assert_json_snapshot;  | 
 | 84 | +    use mas_data_model::Device;  | 
 | 85 | +    use sqlx::PgPool;  | 
 | 86 | +    use ulid::Ulid;  | 
 | 87 | + | 
 | 88 | +    use crate::test_utils::{setup, RequestBuilderExt, ResponseExt, TestState};  | 
 | 89 | + | 
 | 90 | +    #[sqlx::test(migrator = "mas_storage_pg::MIGRATOR")]  | 
 | 91 | +    async fn test_get(pool: PgPool) {  | 
 | 92 | +        setup();  | 
 | 93 | +        let mut state = TestState::from_pool(pool).await.unwrap();  | 
 | 94 | +        let token = state.token_with_scope("urn:mas:admin").await;  | 
 | 95 | +        let mut rng = state.rng();  | 
 | 96 | + | 
 | 97 | +        // Provision a user and a compat session  | 
 | 98 | +        let mut repo = state.repository().await.unwrap();  | 
 | 99 | +        let user = repo  | 
 | 100 | +            .user()  | 
 | 101 | +            .add(&mut rng, &state.clock, "alice".to_owned())  | 
 | 102 | +            .await  | 
 | 103 | +            .unwrap();  | 
 | 104 | +        let device = Device::generate(&mut rng);  | 
 | 105 | +        let session = repo  | 
 | 106 | +            .compat_session()  | 
 | 107 | +            .add(&mut rng, &state.clock, &user, device, None, false)  | 
 | 108 | +            .await  | 
 | 109 | +            .unwrap();  | 
 | 110 | +        repo.save().await.unwrap();  | 
 | 111 | + | 
 | 112 | +        let session_id = session.id;  | 
 | 113 | +        let request = Request::get(format!("/api/admin/v1/compat-sessions/{session_id}"))  | 
 | 114 | +            .bearer(&token)  | 
 | 115 | +            .empty();  | 
 | 116 | +        let response = state.request(request).await;  | 
 | 117 | +        response.assert_status(StatusCode::OK);  | 
 | 118 | +        let body: serde_json::Value = response.json();  | 
 | 119 | +        assert_json_snapshot!(body, @r###"  | 
 | 120 | +        {  | 
 | 121 | +          "data": {  | 
 | 122 | +            "type": "compat-session",  | 
 | 123 | +            "id": "01FSHN9AG0QHEHKX2JNQ2A2D07",  | 
 | 124 | +            "attributes": {  | 
 | 125 | +              "user_id": "01FSHN9AG0MZAA6S4AF7CTV32E",  | 
 | 126 | +              "device_id": "TpLoieH5Ie",  | 
 | 127 | +              "user_session_id": null,  | 
 | 128 | +              "redirect_uri": null,  | 
 | 129 | +              "created_at": "2022-01-16T14:40:00Z",  | 
 | 130 | +              "user_agent": null,  | 
 | 131 | +              "last_active_at": null,  | 
 | 132 | +              "last_active_ip": null,  | 
 | 133 | +              "finished_at": null  | 
 | 134 | +            },  | 
 | 135 | +            "links": {  | 
 | 136 | +              "self": "/api/admin/v1/compat-sessions/01FSHN9AG0QHEHKX2JNQ2A2D07"  | 
 | 137 | +            }  | 
 | 138 | +          },  | 
 | 139 | +          "links": {  | 
 | 140 | +            "self": "/api/admin/v1/compat-sessions/01FSHN9AG0QHEHKX2JNQ2A2D07"  | 
 | 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/compat-sessions/{session_id}"))  | 
 | 154 | +            .bearer(&token)  | 
 | 155 | +            .empty();  | 
 | 156 | +        let response = state.request(request).await;  | 
 | 157 | +        response.assert_status(StatusCode::NOT_FOUND);  | 
 | 158 | +    }  | 
 | 159 | +}  | 
0 commit comments