mas_handlers/admin/v1/policy_data/
get.rs

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