Merged in feature/download-pdf (pull request #216)
retrieve document by id and tests * working * missing files
This commit is contained in:
@@ -759,6 +759,123 @@ func TestGetDocument_NilFileSize(t *testing.T) {
|
||||
assert.Nil(t, result.FileSizeBytes, "legacy document should have nil file size")
|
||||
}
|
||||
|
||||
func TestDownloadDocument_Handler(t *testing.T) {
|
||||
cfg := &ControllerConfig{}
|
||||
test.CreateDB(t, cfg)
|
||||
initializeTestConfig(t, cfg)
|
||||
test.CreateAWSResources(t, cfg)
|
||||
|
||||
svc := createControllerServices(cfg)
|
||||
cons := queryapi.NewControllers(svc, cfg)
|
||||
|
||||
// Create test client with root folder
|
||||
test.CreateTestClient(t, cfg, "download_client", "Download Client")
|
||||
|
||||
// Create document with filename
|
||||
filename := "test-report.pdf"
|
||||
fileContent := []byte("test file content for download")
|
||||
docId, err := cfg.GetDBQueries().CreateDocument(t.Context(), &repository.CreateDocumentParams{
|
||||
Clientid: "download_client",
|
||||
Hash: "download_hash",
|
||||
Filename: &filename,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// Upload content to S3
|
||||
bucket := cfg.GetBucket()
|
||||
s3Key := "test/download/" + docId.String()
|
||||
_, err = cfg.GetStoreClient().PutObject(t.Context(), &s3.PutObjectInput{
|
||||
Bucket: &bucket,
|
||||
Key: &s3Key,
|
||||
Body: bytes.NewReader(fileContent),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// Create document entry linking document to S3 object
|
||||
err = cfg.GetDBQueries().AddDocumentEntry(t.Context(), &repository.AddDocumentEntryParams{
|
||||
Documentid: docId,
|
||||
Bucket: bucket,
|
||||
Key: s3Key,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// Call handler
|
||||
ctx, rec := createContext(t)
|
||||
err = cons.DownloadDocument(ctx, docId)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, http.StatusOK, rec.Code)
|
||||
assert.Equal(t, "application/pdf", rec.Header().Get("Content-Type"))
|
||||
assert.Contains(t, rec.Header().Get("Content-Disposition"), "test-report.pdf")
|
||||
assert.Equal(t, fileContent, rec.Body.Bytes())
|
||||
}
|
||||
|
||||
func TestDownloadDocument_Handler_NotFound(t *testing.T) {
|
||||
cfg := &ControllerConfig{}
|
||||
test.CreateDB(t, cfg)
|
||||
initializeTestConfig(t, cfg)
|
||||
test.CreateAWSResources(t, cfg)
|
||||
|
||||
svc := createControllerServices(cfg)
|
||||
cons := queryapi.NewControllers(svc, cfg)
|
||||
|
||||
// Call handler with non-existent document ID
|
||||
ctx, _ := createContext(t)
|
||||
err := cons.DownloadDocument(ctx, uuid.New())
|
||||
require.Error(t, err)
|
||||
httpErr, ok := err.(*echo.HTTPError)
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, http.StatusNotFound, httpErr.Code)
|
||||
}
|
||||
|
||||
func TestDownloadDocument_Handler_ContentDisposition(t *testing.T) {
|
||||
cfg := &ControllerConfig{}
|
||||
test.CreateDB(t, cfg)
|
||||
initializeTestConfig(t, cfg)
|
||||
test.CreateAWSResources(t, cfg)
|
||||
|
||||
svc := createControllerServices(cfg)
|
||||
cons := queryapi.NewControllers(svc, cfg)
|
||||
|
||||
// Create client
|
||||
test.CreateTestClient(t, cfg, "dl_disposition_client", "Disposition Client")
|
||||
|
||||
// Create document with specific filename
|
||||
filename := "quarterly-financial-report.pdf"
|
||||
fileContent := []byte("financial report content")
|
||||
docId, err := cfg.GetDBQueries().CreateDocument(t.Context(), &repository.CreateDocumentParams{
|
||||
Clientid: "dl_disposition_client",
|
||||
Hash: "disposition_hash",
|
||||
Filename: &filename,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
bucket := cfg.GetBucket()
|
||||
s3Key := "test/download/" + docId.String()
|
||||
_, err = cfg.GetStoreClient().PutObject(t.Context(), &s3.PutObjectInput{
|
||||
Bucket: &bucket,
|
||||
Key: &s3Key,
|
||||
Body: bytes.NewReader(fileContent),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
err = cfg.GetDBQueries().AddDocumentEntry(t.Context(), &repository.AddDocumentEntryParams{
|
||||
Documentid: docId,
|
||||
Bucket: bucket,
|
||||
Key: s3Key,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx, rec := createContext(t)
|
||||
err = cons.DownloadDocument(ctx, docId)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, http.StatusOK, rec.Code)
|
||||
|
||||
// Verify Content-Disposition header contains the original filename
|
||||
contentDisposition := rec.Header().Get("Content-Disposition")
|
||||
assert.Contains(t, contentDisposition, "attachment")
|
||||
assert.Contains(t, contentDisposition, "quarterly-financial-report.pdf")
|
||||
}
|
||||
|
||||
// Helper function to create test ZIP with multiple PDFs
|
||||
func createTestZIPFile(t *testing.T, pdfCount int) *bytes.Reader {
|
||||
var buf bytes.Buffer
|
||||
|
||||
Reference in New Issue
Block a user