This commit is contained in:
2025-10-31 02:19:38 +01:00
parent f796932cd8
commit eacab2ced6
7 changed files with 462 additions and 17 deletions
+145 -7
View File
@@ -2,7 +2,7 @@ mod common;
use anyhow::Result;
use axum::http::StatusCode;
use common::{acquire_db_lock, body_to_vec, ApiErrorResponse, TestApp, UploadExtras};
use common::{acquire_db_lock, body_to_vec, TestApp, UploadExtras};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use uuid::Uuid;
@@ -12,6 +12,13 @@ struct DocumentDetail {
document: DocumentInfo,
}
#[derive(Deserialize)]
struct ApiErrorResponse {
error: String,
#[serde(default)]
code: Option<String>,
}
#[derive(Deserialize)]
struct DocumentInfo {
id: Uuid,
@@ -25,11 +32,19 @@ struct DocumentInfo {
metadata: Value,
#[serde(default)]
document_type_id: Option<Uuid>,
#[serde(default)]
document_type: Option<DocumentTypeInfo>,
tags: Vec<TagSummary>,
#[serde(default)]
current_version: Option<DocumentVersionPayload>,
}
#[derive(Deserialize)]
struct DocumentTypeInfo {
id: Uuid,
name: String,
}
#[derive(Deserialize)]
struct DocumentVersionPayload {
id: Uuid,
@@ -83,12 +98,6 @@ struct TagSummary {
label: String,
}
#[derive(Deserialize)]
struct DocumentTypeInfo {
id: Uuid,
name: String,
}
#[derive(Deserialize)]
struct AnalyzeJobPayload {
document_id: Uuid,
@@ -411,6 +420,7 @@ async fn upload_skips_existing_when_requested() -> Result<()> {
correspondents_json: None,
issued_at: None,
skip_existing: false,
document_type_id: None,
};
let first_upload = app
@@ -461,6 +471,7 @@ async fn upload_skips_existing_when_requested() -> Result<()> {
correspondents_json: None,
issued_at: None,
skip_existing: true,
document_type_id: None,
};
let skip_resp = app
@@ -1442,6 +1453,133 @@ async fn list_documents_by_status_filter() -> Result<()> {
Ok(())
}
#[tokio::test]
async fn document_upload_and_patch_document_type() -> Result<()> {
let _lock = acquire_db_lock().await;
let app = TestApp::new().await?;
let password = "doctypeupload";
let username = "doctype_user";
app.insert_user(username, password, "admin").await?;
let token = app.login_token(username, password).await?;
let invoices_resp = app
.post_json(
"/api/document-types",
&json!({"name": "Invoices"}),
Some(&token),
)
.await?;
assert_eq!(invoices_resp.status(), StatusCode::CREATED);
let invoices_body = body_to_vec(invoices_resp.into_body()).await?;
let invoices: DocumentTypeInfo = serde_json::from_slice(&invoices_body)?;
let receipts_resp = app
.post_json(
"/api/document-types",
&json!({"name": "Receipts"}),
Some(&token),
)
.await?;
assert_eq!(receipts_resp.status(), StatusCode::CREATED);
let receipts_body = body_to_vec(receipts_resp.into_body()).await?;
let receipts: DocumentTypeInfo = serde_json::from_slice(&receipts_body)?;
let upload_extras = UploadExtras {
title: Some("Invoice #1"),
metadata_json: None,
tag_ids_json: None,
correspondents_json: None,
issued_at: None,
skip_existing: false,
document_type_id: Some(invoices.id),
};
let upload_resp = app
.upload_document_with_extras(
"/api/documents",
"invoice.pdf",
"application/pdf",
b"invoice-bytes",
None,
upload_extras,
&token,
)
.await?;
assert!(upload_resp.status().is_success());
let upload_body = body_to_vec(upload_resp.into_body()).await?;
let mut detail: DocumentDetail = serde_json::from_slice(&upload_body)?;
assert_eq!(detail.document.document_type_id, Some(invoices.id));
let doc_type_info = detail
.document
.document_type
.as_ref()
.expect("document type present");
assert_eq!(doc_type_info.name, "Invoices");
let document_id = detail.document.id;
let update_resp = app
.patch_json(
&format!("/api/documents/{}", document_id),
&json!({"document_type_id": receipts.id}),
Some(&token),
)
.await?;
assert!(update_resp.status().is_success());
let update_body = body_to_vec(update_resp.into_body()).await?;
detail = serde_json::from_slice(&update_body)?;
assert_eq!(detail.document.document_type_id, Some(receipts.id));
assert_eq!(
detail
.document
.document_type
.as_ref()
.map(|info| info.name.as_str()),
Some("Receipts")
);
let clear_resp = app
.patch_json(
&format!("/api/documents/{}", document_id),
&json!({"document_type_id": null}),
Some(&token),
)
.await?;
assert!(clear_resp.status().is_success());
let clear_body = body_to_vec(clear_resp.into_body()).await?;
detail = serde_json::from_slice(&clear_body)?;
assert!(detail.document.document_type_id.is_none());
assert!(detail.document.document_type.is_none());
let invalid_id = Uuid::new_v4();
let invalid_resp = app
.patch_json(
&format!("/api/documents/{}", document_id),
&json!({"document_type_id": invalid_id}),
Some(&token),
)
.await?;
assert_eq!(invalid_resp.status(), StatusCode::BAD_REQUEST);
let invalid_body = body_to_vec(invalid_resp.into_body()).await?;
let invalid_error: ApiErrorResponse = serde_json::from_slice(&invalid_body)?;
assert_eq!(
invalid_error.error,
"document_type_id does not exist for this tenant"
);
let final_detail_resp = app
.get(&format!("/api/documents/{}", document_id), Some(&token))
.await?;
let final_detail_body = body_to_vec(final_detail_resp.into_body()).await?;
let final_detail: DocumentDetail = serde_json::from_slice(&final_detail_body)?;
assert!(final_detail.document.document_type_id.is_none());
assert!(final_detail.document.document_type.is_none());
app.cleanup().await?;
Ok(())
}
#[tokio::test]
async fn list_documents_filtered_by_document_type() -> Result<()> {
let _lock = acquire_db_lock().await;