|
| 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 | + |
| 6 | +use aide::{OperationIo, transform::TransformOperation}; |
| 7 | +use axum::{Json, response::IntoResponse}; |
| 8 | +use hyper::StatusCode; |
| 9 | +use mas_axum_utils::record_error; |
| 10 | +use ulid::Ulid; |
| 11 | + |
| 12 | +use 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>")] |
| 24 | +pub 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 | + |
| 32 | +impl_from_error_for_route!(mas_storage::RepositoryError); |
| 33 | + |
| 34 | +impl 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 | + |
| 46 | +pub 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)] |
| 65 | +pub async fn handler( |
| 66 | + CallContext { mut repo, .. }: CallContext, |
| 67 | + id: UlidPathParam, |
| 68 | +) -> Result<Json<SingleResponse<UserRegistrationToken>>, RouteError> { |
| 69 | + let token = repo |
| 70 | + .user_registration_token() |
| 71 | + .lookup(*id) |
| 72 | + .await? |
| 73 | + .ok_or(RouteError::NotFound(*id))?; |
| 74 | + |
| 75 | + Ok(Json(SingleResponse::new_canonical( |
| 76 | + UserRegistrationToken::from(token), |
| 77 | + ))) |
| 78 | +} |
| 79 | + |
| 80 | +#[cfg(test)] |
| 81 | +mod tests { |
| 82 | + use hyper::{Request, StatusCode}; |
| 83 | + use insta::assert_json_snapshot; |
| 84 | + use sqlx::PgPool; |
| 85 | + use ulid::Ulid; |
| 86 | + |
| 87 | + use crate::test_utils::{RequestBuilderExt, ResponseExt, TestState, setup}; |
| 88 | + |
| 89 | + #[sqlx::test(migrator = "mas_storage_pg::MIGRATOR")] |
| 90 | + async fn test_get_token(pool: PgPool) { |
| 91 | + setup(); |
| 92 | + let mut state = TestState::from_pool(pool).await.unwrap(); |
| 93 | + let token = state.token_with_scope("urn:mas:admin").await; |
| 94 | + |
| 95 | + let mut repo = state.repository().await.unwrap(); |
| 96 | + let registration_token = repo |
| 97 | + .user_registration_token() |
| 98 | + .add( |
| 99 | + &mut state.rng(), |
| 100 | + &state.clock, |
| 101 | + "test_token_123".to_owned(), |
| 102 | + Some(5), |
| 103 | + None, |
| 104 | + ) |
| 105 | + .await |
| 106 | + .unwrap(); |
| 107 | + repo.save().await.unwrap(); |
| 108 | + |
| 109 | + let request = Request::get(format!( |
| 110 | + "/api/admin/v1/user-registration-tokens/{}", |
| 111 | + registration_token.id |
| 112 | + )) |
| 113 | + .bearer(&token) |
| 114 | + .empty(); |
| 115 | + let response = state.request(request).await; |
| 116 | + response.assert_status(StatusCode::OK); |
| 117 | + let body: serde_json::Value = response.json(); |
| 118 | + |
| 119 | + assert_json_snapshot!(body, @r###" |
| 120 | + { |
| 121 | + "data": { |
| 122 | + "type": "user-registration_token", |
| 123 | + "id": "01FSHN9AG0MZAA6S4AF7CTV32E", |
| 124 | + "attributes": { |
| 125 | + "token": "test_token_123", |
| 126 | + "usage_limit": 5, |
| 127 | + "times_used": 0, |
| 128 | + "created_at": "2022-01-16T14:40:00Z", |
| 129 | + "last_used_at": null, |
| 130 | + "expires_at": null, |
| 131 | + "revoked_at": null |
| 132 | + }, |
| 133 | + "links": { |
| 134 | + "self": "/api/admin/v1/user-registration-tokens/01FSHN9AG0MZAA6S4AF7CTV32E" |
| 135 | + } |
| 136 | + }, |
| 137 | + "links": { |
| 138 | + "self": "/api/admin/v1/user-registration-tokens/01FSHN9AG0MZAA6S4AF7CTV32E" |
| 139 | + } |
| 140 | + } |
| 141 | + "###); |
| 142 | + } |
| 143 | + |
| 144 | + #[sqlx::test(migrator = "mas_storage_pg::MIGRATOR")] |
| 145 | + async fn test_get_nonexistent_token(pool: PgPool) { |
| 146 | + setup(); |
| 147 | + let mut state = TestState::from_pool(pool).await.unwrap(); |
| 148 | + let token = state.token_with_scope("urn:mas:admin").await; |
| 149 | + |
| 150 | + // Use a fixed ID for the test to ensure consistent snapshots |
| 151 | + let nonexistent_id = Ulid::from_string("00000000000000000000000000").unwrap(); |
| 152 | + let request = Request::get(format!( |
| 153 | + "/api/admin/v1/user-registration-tokens/{nonexistent_id}" |
| 154 | + )) |
| 155 | + .bearer(&token) |
| 156 | + .empty(); |
| 157 | + let response = state.request(request).await; |
| 158 | + response.assert_status(StatusCode::NOT_FOUND); |
| 159 | + let body: serde_json::Value = response.json(); |
| 160 | + |
| 161 | + assert_json_snapshot!(body, @r###" |
| 162 | + { |
| 163 | + "errors": [ |
| 164 | + { |
| 165 | + "title": "Registration token with ID 00000000000000000000000000 not found" |
| 166 | + } |
| 167 | + ] |
| 168 | + } |
| 169 | + "###); |
| 170 | + } |
| 171 | +} |
0 commit comments