mas_handlers/admin/v1/policy_data/
get_latest.rs1use aide::{OperationIo, transform::TransformOperation};
7use axum::{Json, response::IntoResponse};
8use hyper::StatusCode;
9use mas_axum_utils::record_error;
10
11use crate::{
12 admin::{
13 call_context::CallContext,
14 model::PolicyData,
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("No policy data found")]
27 NotFound,
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 sentry_event_id = record_error!(self, Self::Internal(_));
36 let status = match self {
37 Self::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR,
38 Self::NotFound => StatusCode::NOT_FOUND,
39 };
40 (status, sentry_event_id, Json(error)).into_response()
41 }
42}
43
44pub fn doc(operation: TransformOperation) -> TransformOperation {
45 operation
46 .id("getLatestPolicyData")
47 .summary("Get the latest policy data")
48 .tag("policy-data")
49 .response_with::<200, Json<SingleResponse<PolicyData>>, _>(|t| {
50 let [sample, ..] = PolicyData::samples();
51 let response = SingleResponse::new_canonical(sample);
52 t.description("Latest policy data was found")
53 .example(response)
54 })
55 .response_with::<404, RouteError, _>(|t| {
56 let response = ErrorResponse::from_error(&RouteError::NotFound);
57 t.description("No policy data was found").example(response)
58 })
59}
60
61#[tracing::instrument(name = "handler.admin.v1.policy_data.get_latest", skip_all)]
62pub async fn handler(
63 CallContext { mut repo, .. }: CallContext,
64) -> Result<Json<SingleResponse<PolicyData>>, RouteError> {
65 let policy_data = repo
66 .policy_data()
67 .get()
68 .await?
69 .ok_or(RouteError::NotFound)?;
70
71 Ok(Json(SingleResponse::new_canonical(policy_data.into())))
72}
73
74#[cfg(test)]
75mod tests {
76 use hyper::{Request, StatusCode};
77 use insta::assert_json_snapshot;
78 use sqlx::PgPool;
79
80 use crate::test_utils::{RequestBuilderExt, ResponseExt, TestState, setup};
81
82 #[sqlx::test(migrator = "mas_storage_pg::MIGRATOR")]
83 async fn test_get_latest(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 repo.policy_data()
92 .set(
93 &mut rng,
94 &state.clock,
95 serde_json::json!({"hello": "world"}),
96 )
97 .await
98 .unwrap();
99
100 repo.save().await.unwrap();
101
102 let request = Request::get("/api/admin/v1/policy-data/latest")
103 .bearer(&token)
104 .empty();
105 let response = state.request(request).await;
106 response.assert_status(StatusCode::OK);
107 let body: serde_json::Value = response.json();
108 assert_json_snapshot!(body, @r###"
109 {
110 "data": {
111 "type": "policy-data",
112 "id": "01FSHN9AG0MZAA6S4AF7CTV32E",
113 "attributes": {
114 "created_at": "2022-01-16T14:40:00Z",
115 "data": {
116 "hello": "world"
117 }
118 },
119 "links": {
120 "self": "/api/admin/v1/policy-data/01FSHN9AG0MZAA6S4AF7CTV32E"
121 }
122 },
123 "links": {
124 "self": "/api/admin/v1/policy-data/01FSHN9AG0MZAA6S4AF7CTV32E"
125 }
126 }
127 "###);
128 }
129
130 #[sqlx::test(migrator = "mas_storage_pg::MIGRATOR")]
131 async fn test_get_no_latest(pool: PgPool) {
132 setup();
133 let mut state = TestState::from_pool(pool).await.unwrap();
134 let token = state.token_with_scope("urn:mas:admin").await;
135
136 let request = Request::get("/api/admin/v1/policy-data/latest")
137 .bearer(&token)
138 .empty();
139 let response = state.request(request).await;
140 response.assert_status(StatusCode::NOT_FOUND);
141 let body: serde_json::Value = response.json();
142 assert_json_snapshot!(body, @r###"
143 {
144 "errors": [
145 {
146 "title": "No policy data found"
147 }
148 ]
149 }
150 "###);
151 }
152}