upload with more data

This commit is contained in:
2025-10-26 02:28:02 +02:00
parent 62cadbcfa0
commit e51a59a829
5 changed files with 364 additions and 82 deletions
+38
View File
@@ -478,6 +478,30 @@ impl TestApp {
data: &[u8],
folder_id: Option<Uuid>,
token: &str,
) -> Result<hyper::Response<Body>> {
self.upload_document_with_options(
path,
filename,
content_type,
data,
folder_id,
None,
None,
token,
)
.await
}
pub async fn upload_document_with_options(
&self,
path: &str,
filename: &str,
content_type: &str,
data: &[u8],
folder_id: Option<Uuid>,
title: Option<&str>,
metadata_json: Option<&str>,
token: &str,
) -> Result<hyper::Response<Body>> {
let boundary = format!("boundary-{}", Uuid::new_v4());
let mut body = Vec::new();
@@ -500,6 +524,20 @@ impl TestApp {
body.extend(b"\r\n");
}
if let Some(title_value) = title {
body.extend(format!("--{boundary}\r\n").as_bytes());
body.extend(b"Content-Disposition: form-data; name=\"title\"\r\n\r\n");
body.extend(title_value.as_bytes());
body.extend(b"\r\n");
}
if let Some(metadata_value) = metadata_json {
body.extend(format!("--{boundary}\r\n").as_bytes());
body.extend(b"Content-Disposition: form-data; name=\"metadata\"\r\n\r\n");
body.extend(metadata_value.as_bytes());
body.extend(b"\r\n");
}
body.extend(format!("--{boundary}--\r\n").as_bytes());
let builder = Request::builder()
+38
View File
@@ -15,6 +15,7 @@ struct DocumentDetail {
struct DocumentInfo {
id: Uuid,
title: String,
filename: String,
original_name: String,
deleted_at: Option<String>,
issued_at: Option<String>,
@@ -240,6 +241,43 @@ async fn upload_and_list_document() -> Result<()> {
Ok(())
}
#[tokio::test]
async fn upload_document_with_custom_title_sets_filename() -> Result<()> {
let _lock = acquire_db_lock().await;
let app = TestApp::new().await?;
let password = "passw0rd";
app.insert_user("nora", password, "admin").await?;
let token = app.login_token("nora", password).await?;
let file_bytes = b"example contract body".to_vec();
let title = "Vendor Contract";
let original_filename = "scan.pdf";
let upload = app
.upload_document_with_options(
"/api/documents",
original_filename,
"application/pdf",
&file_bytes,
None,
Some(title),
None,
&token,
)
.await?;
assert_eq!(upload.status(), StatusCode::CREATED);
let body = body_to_vec(upload.into_body()).await?;
let detail: DocumentDetail = serde_json::from_slice(&body)?;
assert_eq!(detail.document.title, title);
assert_eq!(detail.document.filename, format!("{title}.pdf"));
assert_eq!(detail.document.original_name, original_filename);
app.cleanup().await?;
Ok(())
}
#[tokio::test]
async fn duplicate_and_restore_document() -> Result<()> {
let _lock = acquire_db_lock().await;