mas_handlers/admin/v1/user_registration_tokens/
get.rs

1// Copyright 2025 The Matrix.org Foundation C.I.C.
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 mas_axum_utils::record_error;
10use ulid::Ulid;
11
12use crate::{
13    admin::{
14        call_context::CallContext,
15        model::UserRegistrationToken,
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("Registration token 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("getUserRegistrationToken")
49        .summary("Get a user registration token")
50        .tag("user-registration-token")
51        .response_with::<200, Json<SingleResponse<UserRegistrationToken>>, _>(|t| {
52            let [sample, ..] = UserRegistrationToken::samples();
53            let response = SingleResponse::new_canonical(sample);
54            t.description("Registration token 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("Registration token was not found")
60                .example(response)
61        })
62}
63
64#[tracing::instrument(name = "handler.admin.v1.user_registration_tokens.get", skip_all)]
65pub async fn handler(
66    CallContext {
67        mut repo, clock, ..
68    }: CallContext,
69    id: UlidPathParam,
70) -> Result<Json<SingleResponse<UserRegistrationToken>>, RouteError> {
71    let token = repo
72        .user_registration_token()
73        .lookup(*id)
74        .await?
75        .ok_or(RouteError::NotFound(*id))?;
76
77    Ok(Json(SingleResponse::new_canonical(
78        UserRegistrationToken::new(token, clock.now()),
79    )))
80}
81
82#[cfg(test)]
83mod tests {
84    use hyper::{Request, StatusCode};
85    use insta::assert_json_snapshot;
86    use sqlx::PgPool;
87    use ulid::Ulid;
88
89    use crate::test_utils::{RequestBuilderExt, ResponseExt, TestState, setup};
90
91    #[sqlx::test(migrator = "mas_storage_pg::MIGRATOR")]
92    async fn test_get_token(pool: PgPool) {
93        setup();
94        let mut state = TestState::from_pool(pool).await.unwrap();
95        let token = state.token_with_scope("urn:mas:admin").await;
96
97        let mut repo = state.repository().await.unwrap();
98        let registration_token = repo
99            .user_registration_token()
100            .add(
101                &mut state.rng(),
102                &state.clock,
103                "test_token_123".to_owned(),
104                Some(5),
105                None,
106            )
107            .await
108            .unwrap();
109        repo.save().await.unwrap();
110
111        let request = Request::get(format!(
112            "/api/admin/v1/user-registration-tokens/{}",
113            registration_token.id
114        ))
115        .bearer(&token)
116        .empty();
117        let response = state.request(request).await;
118        response.assert_status(StatusCode::OK);
119        let body: serde_json::Value = response.json();
120
121        assert_json_snapshot!(body, @r#"
122        {
123          "data": {
124            "type": "user-registration_token",
125            "id": "01FSHN9AG0MZAA6S4AF7CTV32E",
126            "attributes": {
127              "token": "test_token_123",
128              "valid": true,
129              "usage_limit": 5,
130              "times_used": 0,
131              "created_at": "2022-01-16T14:40:00Z",
132              "last_used_at": null,
133              "expires_at": null,
134              "revoked_at": null
135            },
136            "links": {
137              "self": "/api/admin/v1/user-registration-tokens/01FSHN9AG0MZAA6S4AF7CTV32E"
138            }
139          },
140          "links": {
141            "self": "/api/admin/v1/user-registration-tokens/01FSHN9AG0MZAA6S4AF7CTV32E"
142          }
143        }
144        "#);
145    }
146
147    #[sqlx::test(migrator = "mas_storage_pg::MIGRATOR")]
148    async fn test_get_nonexistent_token(pool: PgPool) {
149        setup();
150        let mut state = TestState::from_pool(pool).await.unwrap();
151        let token = state.token_with_scope("urn:mas:admin").await;
152
153        // Use a fixed ID for the test to ensure consistent snapshots
154        let nonexistent_id = Ulid::from_string("00000000000000000000000000").unwrap();
155        let request = Request::get(format!(
156            "/api/admin/v1/user-registration-tokens/{nonexistent_id}"
157        ))
158        .bearer(&token)
159        .empty();
160        let response = state.request(request).await;
161        response.assert_status(StatusCode::NOT_FOUND);
162        let body: serde_json::Value = response.json();
163
164        assert_json_snapshot!(body, @r###"
165        {
166          "errors": [
167            {
168              "title": "Registration token with ID 00000000000000000000000000 not found"
169            }
170          ]
171        }
172        "###);
173    }
174}