Lines
100 %
Functions
Branches
use axum::{
Router,
body::Body,
http::{Request, StatusCode},
routing::{get, post},
};
use sqlx::types::Uuid;
use crate::common::tenant::provision_tenant;
use crate::common::{authenticated, create_mock_user, create_test_app_state, send_json};
const UNAVAILABLE: &str = "The service is temporarily unavailable";
async fn app(user: web::model::User) -> Router {
use web::pages::{script, template};
let router = Router::new()
.route("/script/list", get(script::list::script_table))
.route("/script/edit/{id}", get(script::edit::script_edit_page))
.route("/script/delete/{id}", post(script::edit::delete_script))
.route("/template/list", get(template::list::template_table))
.route(
"/template/edit/{id}",
get(template::edit::template_edit_page),
)
"/template/delete/{id}",
post(template::edit::delete_template),
);
authenticated(router, create_test_app_state().await, user)
}
fn request(method: &str, uri: &str) -> Request<Body> {
Request::builder()
.method(method)
.uri(uri)
.body(Body::empty())
.expect("request")
async fn expect_error(app: Router, request: Request<Body>, status: StatusCode, message: &str) {
let (got, body) = send_json(app, request).await;
assert_eq!(got, status);
assert_eq!(body["status"], "error");
assert_eq!(body["message"], message);
#[supp_macro::shared_runtime_test]
async fn malformed_path_ids_are_a_400_naming_the_id() {
let user = create_mock_user();
let cases = [
("GET", "/script/edit/nope", "Invalid script ID: nope"),
("POST", "/script/delete/nope", "Invalid script ID: nope"),
("GET", "/template/edit/nope", "Invalid template ID: nope"),
("POST", "/template/delete/nope", "Invalid template ID: nope"),
];
for (method, uri, message) in cases {
expect_error(
app(user.clone()).await,
request(method, uri),
StatusCode::BAD_REQUEST,
message,
.await;
async fn missing_artifacts_are_a_404_with_a_clean_message() {
provision_tenant(user.id).await;
let id = Uuid::new_v4();
let script = send_json(
request("GET", &format!("/script/edit/{id}")),
let template = send_json(
request("GET", &format!("/template/edit/{id}")),
assert_eq!(script.0, StatusCode::NOT_FOUND);
assert_eq!(script.1["message"], "Script not found");
assert_eq!(template.0, StatusCode::NOT_FOUND);
assert_eq!(template.1["message"], "Template not found");
async fn an_unreachable_tenant_is_a_503_without_detail() {
for uri in ["/script/list", "/template/list"] {
let (status, body) = send_json(app(user.clone()).await, request("GET", uri)).await;
assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE, "{uri}");
assert_eq!(body["message"], UNAVAILABLE, "{uri}");
let (status, body) = send_json(
app(user).await,
request("POST", &format!("/script/delete/{id}")),
assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE);
assert_eq!(body["message"], UNAVAILABLE);