mas_handlers/admin/v1/upstream_oauth_links/
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::UpstreamOAuthLink,
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("Upstream OAuth 2.0 Link 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_entry_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_entry_id, Json(error)).into_response()
43 }
44}
45
46pub fn doc(operation: TransformOperation) -> TransformOperation {
47 operation
48 .id("getUpstreamOAuthLink")
49 .summary("Get an upstream OAuth 2.0 link")
50 .tag("upstream-oauth-link")
51 .response_with::<200, Json<SingleResponse<UpstreamOAuthLink>>, _>(|t| {
52 let [sample, ..] = UpstreamOAuthLink::samples();
53 let response = SingleResponse::new_canonical(sample);
54 t.description("Upstream OAuth 2.0 link was found")
55 .example(response)
56 })
57 .response_with::<404, RouteError, _>(|t| {
58 let response = ErrorResponse::from_error(&RouteError::NotFound(Ulid::nil()));
59 t.description("Upstream OAuth 2.0 link was not found")
60 .example(response)
61 })
62}
63
64#[tracing::instrument(name = "handler.admin.v1.upstream_oauth_links.get", skip_all)]
65pub async fn handler(
66 CallContext { mut repo, .. }: CallContext,
67 id: UlidPathParam,
68) -> Result<Json<SingleResponse<UpstreamOAuthLink>>, RouteError> {
69 let link = repo
70 .upstream_oauth_link()
71 .lookup(*id)
72 .await?
73 .ok_or(RouteError::NotFound(*id))?;
74
75 Ok(Json(SingleResponse::new_canonical(
76 UpstreamOAuthLink::from(link),
77 )))
78}
79
80#[cfg(test)]
81mod tests {
82 use hyper::{Request, StatusCode};
83 use insta::assert_json_snapshot;
84 use sqlx::PgPool;
85 use ulid::Ulid;
86
87 use super::super::test_utils;
88 use crate::test_utils::{RequestBuilderExt, ResponseExt, TestState, setup};
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 let mut repo = state.repository().await.unwrap();
99 let provider = repo
100 .upstream_oauth_provider()
101 .add(
102 &mut rng,
103 &state.clock,
104 test_utils::oidc_provider_params("provider1"),
105 )
106 .await
107 .unwrap();
108 let user = repo
109 .user()
110 .add(&mut rng, &state.clock, "alice".to_owned())
111 .await
112 .unwrap();
113 let link = repo
114 .upstream_oauth_link()
115 .add(
116 &mut rng,
117 &state.clock,
118 &provider,
119 "subject1".to_owned(),
120 None,
121 )
122 .await
123 .unwrap();
124 repo.upstream_oauth_link()
125 .associate_to_user(&link, &user)
126 .await
127 .unwrap();
128 repo.save().await.unwrap();
129
130 let link_id = link.id;
131 let request = Request::get(format!("/api/admin/v1/upstream-oauth-links/{link_id}"))
132 .bearer(&token)
133 .empty();
134 let response = state.request(request).await;
135 response.assert_status(StatusCode::OK);
136 let body: serde_json::Value = response.json();
137 assert_json_snapshot!(body, @r###"
138 {
139 "data": {
140 "type": "upstream-oauth-link",
141 "id": "01FSHN9AG09NMZYX8MFYH578R9",
142 "attributes": {
143 "created_at": "2022-01-16T14:40:00Z",
144 "provider_id": "01FSHN9AG0MZAA6S4AF7CTV32E",
145 "subject": "subject1",
146 "user_id": "01FSHN9AG0AJ6AC5HQ9X6H4RP4",
147 "human_account_name": null
148 },
149 "links": {
150 "self": "/api/admin/v1/upstream-oauth-links/01FSHN9AG09NMZYX8MFYH578R9"
151 }
152 },
153 "links": {
154 "self": "/api/admin/v1/upstream-oauth-links/01FSHN9AG09NMZYX8MFYH578R9"
155 }
156 }
157 "###);
158 }
159
160 #[sqlx::test(migrator = "mas_storage_pg::MIGRATOR")]
161 async fn test_not_found(pool: PgPool) {
162 setup();
163 let mut state = TestState::from_pool(pool).await.unwrap();
164 let token = state.token_with_scope("urn:mas:admin").await;
165
166 let link_id = Ulid::nil();
167 let request = Request::get(format!("/api/admin/v1/upstream-oauth-links/{link_id}"))
168 .bearer(&token)
169 .empty();
170 let response = state.request(request).await;
171 response.assert_status(StatusCode::NOT_FOUND);
172 }
173}