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