|
| 1 | +// Copyright 2025 New Vector Ltd. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial |
| 4 | +// Please see LICENSE files in the repository root for full details. |
| 5 | + |
| 6 | +use aide::transform::TransformOperation; |
| 7 | +use axum::{Json, extract::State}; |
| 8 | +use mas_data_model::AppVersion; |
| 9 | +use schemars::JsonSchema; |
| 10 | +use serde::Serialize; |
| 11 | + |
| 12 | +use crate::admin::call_context::CallContext; |
| 13 | + |
| 14 | +#[derive(Serialize, JsonSchema)] |
| 15 | +pub struct Version { |
| 16 | + /// The semver version of the app |
| 17 | + pub version: &'static str, |
| 18 | +} |
| 19 | + |
| 20 | +pub fn doc(operation: TransformOperation) -> TransformOperation { |
| 21 | + operation |
| 22 | + .id("version") |
| 23 | + .tag("server") |
| 24 | + .summary("Get the version currently running") |
| 25 | + .response_with::<200, Json<Version>, _>(|t| t.example(Version { version: "v1.0.0" })) |
| 26 | +} |
| 27 | + |
| 28 | +#[tracing::instrument(name = "handler.admin.v1.version", skip_all)] |
| 29 | +pub async fn handler( |
| 30 | + _: CallContext, |
| 31 | + State(AppVersion(version)): State<mas_data_model::AppVersion>, |
| 32 | +) -> Json<Version> { |
| 33 | + Json(Version { version }) |
| 34 | +} |
| 35 | + |
| 36 | +#[cfg(test)] |
| 37 | +mod tests { |
| 38 | + use hyper::{Request, StatusCode}; |
| 39 | + use insta::assert_json_snapshot; |
| 40 | + use sqlx::PgPool; |
| 41 | + |
| 42 | + use crate::test_utils::{RequestBuilderExt, ResponseExt, TestState, setup}; |
| 43 | + |
| 44 | + #[sqlx::test(migrator = "mas_storage_pg::MIGRATOR")] |
| 45 | + async fn test_add_user(pool: PgPool) { |
| 46 | + setup(); |
| 47 | + let mut state = TestState::from_pool(pool).await.unwrap(); |
| 48 | + let token = state.token_with_scope("urn:mas:admin").await; |
| 49 | + |
| 50 | + let request = Request::get("/api/admin/v1/version").bearer(&token).empty(); |
| 51 | + |
| 52 | + let response = state.request(request).await; |
| 53 | + |
| 54 | + assert_eq!(response.status(), StatusCode::OK); |
| 55 | + let body: serde_json::Value = response.json(); |
| 56 | + assert_json_snapshot!(body, @r#" |
| 57 | + { |
| 58 | + "version": "v0.0.0-test" |
| 59 | + } |
| 60 | + "#); |
| 61 | + } |
| 62 | +} |
0 commit comments