Prevent overwriting existing documents.

This commit is contained in:
2025-05-03 08:40:51 -04:00
parent ea825c89eb
commit e7c7d9f270
4 changed files with 87 additions and 9 deletions

View File

@ -6,7 +6,7 @@ use axum::{
RequestPartsExt, Router,
};
use clap::Parser;
use morethantext::{ActionType, MoreThanText};
use morethantext::{ActionType, ErrorType, MoreThanText};
use std::{collections::HashMap, convert::Infallible};
use tokio::{spawn, sync::mpsc::channel};
use tower_cookies::{Cookie, CookieManagerLayer, Cookies};
@ -104,7 +104,10 @@ async fn mtt_conn(
});
let reply = rx.recv().await.unwrap();
let status = match reply.get_error() {
Some(_) => StatusCode::NOT_FOUND,
Some(err) => match err {
ErrorType::DocumentAlreadyExists => StatusCode::CONFLICT,
ErrorType::DocumentNotFound => StatusCode::NOT_FOUND,
},
None => StatusCode::OK,
};
(status, reply.get_document())
@ -122,7 +125,6 @@ mod servers {
};
use http_body_util::BodyExt;
use serde_json::json;
use std::time::Duration;
use tower::ServiceExt;
#[tokio::test]
@ -235,4 +237,28 @@ mod servers {
let body = response.into_body().collect().await.unwrap().to_bytes();
assert_eq!(body, content);
}
#[tokio::test]
async fn cannot_add_duplicate_document_names() {
let app = create_app(MoreThanText::new()).await;
let document = json!({
"template": "something completely different."
});
let response = app
.clone()
.oneshot(
Request::builder()
.method(Method::POST)
.uri("/api/root")
.body(document.to_string())
.unwrap(),
)
.await
.unwrap();
assert_eq!(
response.status(),
StatusCode::CONFLICT,
"do not allow post to existing documents"
);
}
}