mas_handlers/admin/v1/user_emails/
get.rs

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