ClamAV-Anbindung (clamd-INSTREAM-Protokoll) als verpflichtender Scan-Schritt vor Ablage. Fund fuehrt zu vollstaendiger Entfernung von Dateiobjekt und Dokument plus Audit-Event statt stiller Ablage; Sichtbarkeit in Listen/Direktzugriff gesperrt bis scan_status=clean. EICAR-Erkennung real gegen installierten clamd verifiziert (dabei Null-Byte-Antwort-Bug gefunden und behoben). Nebenbei: fehlende trashed_at-Spalte in drei Test-Schemata (DOC-11-Luecke) nachgetragen. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
206 lines
7.5 KiB
Go
206 lines
7.5 KiB
Go
package folderapi
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"gitea.perlbach24.de/scripte/nexarch/dms/internal/folder"
|
|
)
|
|
|
|
func requireTestPool(t *testing.T) (*pgxpool.Pool, string) {
|
|
t.Helper()
|
|
dsn := os.Getenv("TEST_TENANT_DSN")
|
|
if dsn == "" {
|
|
t.Skip("TEST_TENANT_DSN nicht gesetzt, Integrationstest uebersprungen")
|
|
}
|
|
ctx := context.Background()
|
|
pool, err := pgxpool.New(ctx, dsn)
|
|
if err != nil {
|
|
t.Fatalf("pool: %v", err)
|
|
}
|
|
t.Cleanup(func() { pool.Close() })
|
|
|
|
if _, err := pool.Exec(ctx, `
|
|
CREATE EXTENSION IF NOT EXISTS pgcrypto;
|
|
CREATE TABLE IF NOT EXISTS users (
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(), email TEXT NOT NULL UNIQUE, name TEXT NOT NULL,
|
|
status TEXT NOT NULL DEFAULT 'active', created_at TIMESTAMPTZ NOT NULL DEFAULT now(), updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
);
|
|
CREATE TABLE IF NOT EXISTS folders (
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(), parent_folder_id UUID REFERENCES folders(id) ON DELETE CASCADE,
|
|
name TEXT NOT NULL, path TEXT NOT NULL, created_by UUID NOT NULL REFERENCES users(id),
|
|
trashed_at TIMESTAMPTZ, trashed_via_folder_id UUID,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(), updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_folders_path ON folders (path text_pattern_ops);
|
|
CREATE TABLE IF NOT EXISTS documents (
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(), folder_id UUID REFERENCES folders(id) ON DELETE SET NULL,
|
|
title TEXT NOT NULL, current_revision_id UUID, created_by UUID NOT NULL REFERENCES users(id),
|
|
trashed_at TIMESTAMPTZ, trashed_via_folder_id UUID,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(), updated_at TIMESTAMPTZ NOT NULL DEFAULT now(), deleted_at TIMESTAMPTZ
|
|
);
|
|
CREATE TABLE IF NOT EXISTS file_revisions (
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(), document_id UUID NOT NULL REFERENCES documents(id) ON DELETE CASCADE,
|
|
revision_number INT NOT NULL, storage_key TEXT NOT NULL, checksum_sha256 TEXT NOT NULL,
|
|
size_bytes BIGINT NOT NULL, mime_type TEXT NOT NULL, created_by UUID NOT NULL REFERENCES users(id),
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(), wrapped_dek BYTEA,
|
|
UNIQUE (document_id, revision_number)
|
|
);
|
|
CREATE TABLE IF NOT EXISTS revision_scans (
|
|
revision_id UUID PRIMARY KEY REFERENCES file_revisions(id) ON DELETE CASCADE,
|
|
status TEXT NOT NULL CHECK (status IN ('pending', 'processing', 'clean', 'infected')),
|
|
finding TEXT, created_at TIMESTAMPTZ NOT NULL DEFAULT now(), updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
);
|
|
`); err != nil {
|
|
t.Fatalf("schema: %v", err)
|
|
}
|
|
t.Cleanup(func() {
|
|
_, _ = pool.Exec(context.Background(), `TRUNCATE revision_scans, file_revisions, documents, folders, users CASCADE`)
|
|
})
|
|
|
|
var userID string
|
|
if err := pool.QueryRow(ctx, `
|
|
INSERT INTO users (email, name) VALUES ($1, 'Test-Benutzer') RETURNING id
|
|
`, fmt.Sprintf("folderapi-test-%d@example.test", time.Now().UnixNano())).Scan(&userID); err != nil {
|
|
t.Fatalf("testbenutzer anlegen: %v", err)
|
|
}
|
|
return pool, userID
|
|
}
|
|
|
|
func createDocument(t *testing.T, pool *pgxpool.Pool, userID string, folderID *string, title string, sizeBytes int64) string {
|
|
t.Helper()
|
|
ctx := context.Background()
|
|
var documentID string
|
|
if err := pool.QueryRow(ctx, `
|
|
INSERT INTO documents (folder_id, title, created_by) VALUES ($1, $2, $3) RETURNING id
|
|
`, folderID, title, userID).Scan(&documentID); err != nil {
|
|
t.Fatalf("dokument anlegen: %v", err)
|
|
}
|
|
var revisionID string
|
|
if err := pool.QueryRow(ctx, `
|
|
INSERT INTO file_revisions (document_id, revision_number, storage_key, checksum_sha256, size_bytes, mime_type, created_by)
|
|
VALUES ($1, 1, 'k', 'x', $2, 'application/pdf', $3) RETURNING id
|
|
`, documentID, sizeBytes, userID).Scan(&revisionID); err != nil {
|
|
t.Fatalf("revision anlegen: %v", err)
|
|
}
|
|
if _, err := pool.Exec(ctx, `UPDATE documents SET current_revision_id = $2 WHERE id = $1`, documentID, revisionID); err != nil {
|
|
t.Fatalf("aktuelle revision setzen: %v", err)
|
|
}
|
|
return documentID
|
|
}
|
|
|
|
// Akzeptanzkriterium 1: Baum und Liste bleiben synchron - beide fragen
|
|
// denselben Ordner-/Dokumentenbestand ab.
|
|
func TestHandleChildrenAndDocuments_ReflectSameFolderState(t *testing.T) {
|
|
pool, userID := requireTestPool(t)
|
|
root, err := folder.Create(context.Background(), pool, "wurzel", nil, userID)
|
|
if err != nil {
|
|
t.Fatalf("root: %v", err)
|
|
}
|
|
child, err := folder.Create(context.Background(), pool, "kind", &root.ID, userID)
|
|
if err != nil {
|
|
t.Fatalf("child: %v", err)
|
|
}
|
|
createDocument(t, pool, userID, &root.ID, "dok-a.pdf", 1000)
|
|
createDocument(t, pool, userID, &child.ID, "dok-b.pdf", 2000)
|
|
|
|
mux := http.NewServeMux()
|
|
NewServer(pool).RegisterRoutes(mux)
|
|
server := httptest.NewServer(mux)
|
|
defer server.Close()
|
|
|
|
resp, err := http.Get(server.URL + "/folders/" + root.ID + "/children")
|
|
if err != nil {
|
|
t.Fatalf("get children: %v", err)
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
var children []folderDTO
|
|
_ = json.NewDecoder(resp.Body).Decode(&children)
|
|
if len(children) != 1 || children[0].ID != child.ID {
|
|
t.Fatalf("erwartet genau 1 kind (%q), habe: %+v", child.ID, children)
|
|
}
|
|
|
|
respDocs, err := http.Get(server.URL + "/folders/" + root.ID + "/documents")
|
|
if err != nil {
|
|
t.Fatalf("get documents: %v", err)
|
|
}
|
|
defer func() { _ = respDocs.Body.Close() }()
|
|
var docs []documentDTO
|
|
_ = json.NewDecoder(respDocs.Body).Decode(&docs)
|
|
if len(docs) != 1 || docs[0].Title != "dok-a.pdf" {
|
|
t.Fatalf("erwartet genau 1 dokument in root (dok-a.pdf), habe: %+v", docs)
|
|
}
|
|
}
|
|
|
|
// Brotkrumenpfad liefert die Kette von der Wurzel bis zum Ordner.
|
|
func TestHandleBreadcrumb_ReturnsFullTrail(t *testing.T) {
|
|
pool, userID := requireTestPool(t)
|
|
ctx := context.Background()
|
|
root, err := folder.Create(ctx, pool, "wurzel", nil, userID)
|
|
if err != nil {
|
|
t.Fatalf("root: %v", err)
|
|
}
|
|
child, err := folder.Create(ctx, pool, "kind", &root.ID, userID)
|
|
if err != nil {
|
|
t.Fatalf("child: %v", err)
|
|
}
|
|
grandchild, err := folder.Create(ctx, pool, "enkel", &child.ID, userID)
|
|
if err != nil {
|
|
t.Fatalf("grandchild: %v", err)
|
|
}
|
|
|
|
mux := http.NewServeMux()
|
|
NewServer(pool).RegisterRoutes(mux)
|
|
server := httptest.NewServer(mux)
|
|
defer server.Close()
|
|
|
|
resp, err := http.Get(server.URL + "/folders/" + grandchild.ID + "/breadcrumb")
|
|
if err != nil {
|
|
t.Fatalf("get: %v", err)
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
var trail []folderDTO
|
|
_ = json.NewDecoder(resp.Body).Decode(&trail)
|
|
if len(trail) != 3 {
|
|
t.Fatalf("erwartet 3 brotkrumen-eintraege, habe %d: %+v", len(trail), trail)
|
|
}
|
|
if trail[0].Name != "wurzel" || trail[1].Name != "kind" || trail[2].Name != "enkel" {
|
|
t.Fatalf("brotkrumen-reihenfolge falsch: %+v", trail)
|
|
}
|
|
}
|
|
|
|
// Dokumentenliste unterstuetzt Sortierung.
|
|
func TestHandleDocuments_SortsByRequestedColumn(t *testing.T) {
|
|
pool, userID := requireTestPool(t)
|
|
root, err := folder.Create(context.Background(), pool, "wurzel", nil, userID)
|
|
if err != nil {
|
|
t.Fatalf("root: %v", err)
|
|
}
|
|
createDocument(t, pool, userID, &root.ID, "b.pdf", 500)
|
|
createDocument(t, pool, userID, &root.ID, "a.pdf", 9000)
|
|
|
|
mux := http.NewServeMux()
|
|
NewServer(pool).RegisterRoutes(mux)
|
|
server := httptest.NewServer(mux)
|
|
defer server.Close()
|
|
|
|
resp, err := http.Get(server.URL + "/folders/" + root.ID + "/documents?sort=title&order=asc")
|
|
if err != nil {
|
|
t.Fatalf("get: %v", err)
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
var docs []documentDTO
|
|
_ = json.NewDecoder(resp.Body).Decode(&docs)
|
|
if len(docs) != 2 || docs[0].Title != "a.pdf" || docs[1].Title != "b.pdf" {
|
|
t.Fatalf("erwartet nach titel aufsteigend sortiert, habe: %+v", docs)
|
|
}
|
|
}
|