Merged in feature/download-pdf (pull request #216)
retrieve document by id and tests * working * missing files
This commit is contained in:
@@ -27,6 +27,8 @@
|
||||
# - GET /client/{CLIENT_ID}/document/batch - List all batches for client with pagination
|
||||
# - POST /client/{CLIENT_ID}/document/batch (invalid) - Test error handling with non-ZIP file
|
||||
# - DELETE /client/{CLIENT_ID}/document/batch/{BATCH_ID} - Cancel/delete batch processing
|
||||
# - GET /document/{DOCUMENT_ID}/download - Download a processed document
|
||||
# - GET /document/{FAKE_ID}/download - Verify 404 for non-existent document
|
||||
|
||||
set -e # Exit on any error
|
||||
|
||||
@@ -257,6 +259,124 @@ test_invalid_file() {
|
||||
fi
|
||||
}
|
||||
|
||||
# Wait for at least one document in the batch to have a document_id assigned.
|
||||
# Polls the batch status endpoint until a document_id appears or timeout is reached.
|
||||
wait_for_document_id() {
|
||||
log_info "Waiting for documents to be processed (up to 60s)..."
|
||||
|
||||
local max_wait=60
|
||||
local elapsed=0
|
||||
local interval=3
|
||||
|
||||
while [ $elapsed -lt $max_wait ]; do
|
||||
response=$(curl -s "$BASE_URL/client/$CLIENT_ID/document/batch/$BATCH_ID" 2>/dev/null)
|
||||
|
||||
# Extract first non-null document_id from document_outcomes array
|
||||
DOCUMENT_ID=$(echo "$response" | python3 -c "
|
||||
import sys, json
|
||||
data = json.load(sys.stdin)
|
||||
for outcome in data.get('document_outcomes', []):
|
||||
doc_id = outcome.get('document_id')
|
||||
if doc_id:
|
||||
print(doc_id)
|
||||
sys.exit(0)
|
||||
sys.exit(1)
|
||||
" 2>/dev/null)
|
||||
|
||||
if [ $? -eq 0 ] && [ -n "$DOCUMENT_ID" ]; then
|
||||
log_info "Document ready for download: $DOCUMENT_ID"
|
||||
return 0
|
||||
fi
|
||||
|
||||
sleep $interval
|
||||
elapsed=$((elapsed + interval))
|
||||
log_info "Still waiting... ($elapsed/${max_wait}s)"
|
||||
done
|
||||
|
||||
log_error "Timed out waiting for documents to be processed"
|
||||
return 1
|
||||
}
|
||||
|
||||
# Test downloading a document via GET /document/{id}/download.
|
||||
# Verifies HTTP 200, Content-Disposition header, Content-Type, non-empty body, and PDF magic bytes.
|
||||
test_document_download() {
|
||||
log_info "Testing document download: GET /document/$DOCUMENT_ID/download"
|
||||
|
||||
local output_file="downloaded_document.pdf"
|
||||
|
||||
# Download with headers captured
|
||||
curl -s -D headers.txt -o "$output_file" \
|
||||
"$BASE_URL/document/$DOCUMENT_ID/download" 2>/dev/null
|
||||
|
||||
http_code=$(grep -m1 "^HTTP/" headers.txt | awk '{print $2}')
|
||||
|
||||
if [ "$http_code" != "200" ]; then
|
||||
log_error "Download failed (HTTP $http_code)"
|
||||
cat headers.txt 2>/dev/null
|
||||
rm -f "$output_file" headers.txt
|
||||
return 1
|
||||
fi
|
||||
|
||||
log_info "Download returned HTTP 200"
|
||||
|
||||
# Verify Content-Disposition header is present
|
||||
if grep -qi "Content-Disposition" headers.txt; then
|
||||
disposition=$(grep -i "Content-Disposition" headers.txt | tr -d '\r')
|
||||
log_info "$disposition"
|
||||
else
|
||||
log_error "Missing Content-Disposition header"
|
||||
rm -f "$output_file" headers.txt
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Log Content-Type
|
||||
content_type=$(grep -i "Content-Type" headers.txt | head -1 | tr -d '\r')
|
||||
log_info "$content_type"
|
||||
|
||||
# Verify file is non-empty
|
||||
file_size=$(stat -f%z "$output_file" 2>/dev/null || stat -c%s "$output_file" 2>/dev/null || echo "0")
|
||||
if [ "$file_size" -eq 0 ]; then
|
||||
log_error "Downloaded file is empty"
|
||||
rm -f "$output_file" headers.txt
|
||||
return 1
|
||||
fi
|
||||
log_info "Downloaded file size: $file_size bytes"
|
||||
|
||||
# Verify file starts with PDF magic bytes (%PDF)
|
||||
file_header=$(head -c 4 "$output_file")
|
||||
if [ "$file_header" = "%PDF" ]; then
|
||||
log_info "File is a valid PDF (starts with %%PDF header)"
|
||||
else
|
||||
log_warn "File does not start with %%PDF header (got: $file_header)"
|
||||
fi
|
||||
|
||||
# Cleanup
|
||||
rm -f "$output_file" headers.txt
|
||||
|
||||
log_info "Document download test PASSED"
|
||||
return 0
|
||||
}
|
||||
|
||||
# Test that downloading a non-existent document returns 404.
|
||||
test_download_not_found() {
|
||||
log_info "Testing download of non-existent document (should return 404)..."
|
||||
|
||||
local fake_id="00000000-0000-0000-0000-000000000000"
|
||||
|
||||
response=$(curl -s -w "\n%{http_code}" \
|
||||
"$BASE_URL/document/$fake_id/download" 2>/dev/null)
|
||||
|
||||
http_code=$(echo "$response" | tail -n1)
|
||||
|
||||
if [ "$http_code" = "404" ]; then
|
||||
log_info "Non-existent document correctly returned 404"
|
||||
return 0
|
||||
else
|
||||
log_warn "Expected HTTP 404 for non-existent document, got HTTP $http_code"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Cancel batch
|
||||
cancel_batch() {
|
||||
log_info "Canceling batch..."
|
||||
@@ -280,7 +400,7 @@ cancel_batch() {
|
||||
# Cleanup function
|
||||
cleanup() {
|
||||
log_info "Cleaning up..."
|
||||
rm -f "$ZIP_FILE" invalid.txt
|
||||
rm -f "$ZIP_FILE" invalid.txt downloaded_document.pdf headers.txt
|
||||
rm -rf temp_zip_structure
|
||||
log_info "Cleanup complete"
|
||||
}
|
||||
@@ -303,6 +423,15 @@ main() {
|
||||
sleep 1 # Brief pause to let the system process
|
||||
get_batch_status
|
||||
list_batches
|
||||
|
||||
# Document download tests -- must run before cancel_batch
|
||||
if wait_for_document_id; then
|
||||
test_document_download
|
||||
test_download_not_found
|
||||
else
|
||||
log_warn "Skipping download tests - no documents were processed in time"
|
||||
fi
|
||||
|
||||
test_invalid_file
|
||||
cancel_batch
|
||||
|
||||
|
||||
Reference in New Issue
Block a user