mas_handlers/admin/v1/policy_data/
get_latest.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 mas_axum_utils::record_error;
9
10use crate::{
11    admin::{
12        call_context::CallContext,
13        model::PolicyData,
14        response::{ErrorResponse, SingleResponse},
15    },
16    impl_from_error_for_route,
17};
18
19#[derive(Debug, thiserror::Error, OperationIo)]
20#[aide(output_with = "Json<ErrorResponse>")]
21pub enum RouteError {
22    #[error(transparent)]
23    Internal(Box<dyn std::error::Error + Send + Sync + 'static>),
24
25    #[error("No policy data found")]
26    NotFound,
27}
28
29impl_from_error_for_route!(mas_storage::RepositoryError);
30
31impl IntoResponse for RouteError {
32    fn into_response(self) -> axum::response::Response {
33        let error = ErrorResponse::from_error(&self);
34        let sentry_event_id = record_error!(self, Self::Internal(_));
35        let status = match self {
36            Self::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR,
37            Self::NotFound => StatusCode::NOT_FOUND,
38        };
39        (status, sentry_event_id, Json(error)).into_response()
40    }
41}
42
43pub fn doc(operation: TransformOperation) -> TransformOperation {
44    operation
45        .id("getLatestPolicyData")
46        .summary("Get the latest policy data")
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("Latest policy data was found")
52                .example(response)
53        })
54        .response_with::<404, RouteError, _>(|t| {
55            let response = ErrorResponse::from_error(&RouteError::NotFound);
56            t.description("No policy data was found").example(response)
57        })
58}
59
60#[tracing::instrument(name = "handler.admin.v1.policy_data.get_latest", skip_all)]
61pub async fn handler(
62    CallContext { mut repo, .. }: CallContext,
63) -> Result<Json<SingleResponse<PolicyData>>, RouteError> {
64    let policy_data = repo
65        .policy_data()
66        .get()
67        .await?
68        .ok_or(RouteError::NotFound)?;
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
79    use crate::test_utils::{RequestBuilderExt, ResponseExt, TestState, setup};
80
81    #[sqlx::test(migrator = "mas_storage_pg::MIGRATOR")]
82    async fn test_get_latest(pool: PgPool) {
83        setup();
84        let mut state = TestState::from_pool(pool).await.unwrap();
85        let token = state.token_with_scope("urn:mas:admin").await;
86
87        let mut rng = state.rng();
88        let mut repo = state.repository().await.unwrap();
89
90        repo.policy_data()
91            .set(
92                &mut rng,
93                &state.clock,
94                serde_json::json!({"hello": "world"}),
95            )
96            .await
97            .unwrap();
98
99        repo.save().await.unwrap();
100
101        let request = Request::get("/api/admin/v1/policy-data/latest")
102            .bearer(&token)
103            .empty();
104        let response = state.request(request).await;
105        response.assert_status(StatusCode::OK);
106        let body: serde_json::Value = response.json();
107        assert_json_snapshot!(body, @r###"
108        {
109          "data": {
110            "type": "policy-data",
111            "id": "01FSHN9AG0MZAA6S4AF7CTV32E",
112            "attributes": {
113              "created_at": "2022-01-16T14:40:00Z",
114              "data": {
115                "hello": "world"
116              }
117            },
118            "links": {
119              "self": "/api/admin/v1/policy-data/01FSHN9AG0MZAA6S4AF7CTV32E"
120            }
121          },
122          "links": {
123            "self": "/api/admin/v1/policy-data/01FSHN9AG0MZAA6S4AF7CTV32E"
124          }
125        }
126        "###);
127    }
128
129    #[sqlx::test(migrator = "mas_storage_pg::MIGRATOR")]
130    async fn test_get_no_latest(pool: PgPool) {
131        setup();
132        let mut state = TestState::from_pool(pool).await.unwrap();
133        let token = state.token_with_scope("urn:mas:admin").await;
134
135        let request = Request::get("/api/admin/v1/policy-data/latest")
136            .bearer(&token)
137            .empty();
138        let response = state.request(request).await;
139        response.assert_status(StatusCode::NOT_FOUND);
140        let body: serde_json::Value = response.json();
141        assert_json_snapshot!(body, @r###"
142        {
143          "errors": [
144            {
145              "title": "No policy data found"
146            }
147          ]
148        }
149        "###);
150    }
151}