CI / Web-Barrierefreiheitspruefung (axe, Tastatur, Reflow, Textabstaende) (push) Canceled after 0s
CI / Web-Barrierefreiheitspruefung (axe, Tastatur, Reflow, Textabstaende) (pull_request) Canceled after 0s
CI / Core-Modul (Build/Vet/Test) (push) Failing after 1m2s
CI / DMS Security-Test-Gate (AC5, Merge-Gate) (push) Successful in 5s
CI / Connect Security-Test-Gate (QA-01 AC4, Merge-Gate) (push) Successful in 5s
CI / DMS-Modul Supply-Chain-Scan (govulncheck) (push) Failing after 22s
CI / Web (dms-browser) (push) Failing after 1m22s
CI / Web (pdf-viewer) (push) Failing after 53s
CI / Web (shl) (push) Failing after 11s
Supply-Chain-Scan (Go) / govulncheck (push) Failing after 1m6s
CI / Core-Modul (Build/Vet/Test) (pull_request) Failing after 1m0s
CI / DMS Security-Test-Gate (AC5, Merge-Gate) (pull_request) Successful in 4s
CI / Connect Security-Test-Gate (QA-01 AC4, Merge-Gate) (pull_request) Successful in 3s
CI / DMS-Modul Supply-Chain-Scan (govulncheck) (pull_request) Failing after 21s
CI / Web (dms-browser) (pull_request) Failing after 1m22s
CI / Web (pdf-viewer) (pull_request) Failing after 57s
CI / Web (shl) (pull_request) Failing after 12s
Core-Schnittstellen-Vertragstests / contract-tests (pull_request) Successful in 10m56s
Supply-Chain-Scan (Go) / govulncheck (pull_request) Failing after 3m16s
Supply-Chain-Scan (npm) / npm-audit (pull_request) Failing after 1m46s
Core-Pflichttest-Gate / pflichttest-gate (pull_request) Successful in 11m24s
RBAC-Rollenhierarchie-Pflichtgate / rbac-policy-invariant (pull_request) Successful in 11m44s
CI / DMS-Modul (Lint/Build/Test/Coverage/Artefakt) (push) Failing after 2s
CI / DMS-Modul (Lint/Build/Test/Coverage/Artefakt) (pull_request) Failing after 3s
DB-Integrationstests / db-tests (pull_request) Canceled after 0s
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
143 lines
5.3 KiB
Go
143 lines
5.3 KiB
Go
package metadataapi
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"gitea.perlbach24.de/scripte/nexarch/common/testdb"
|
|
"gitea.perlbach24.de/scripte/nexarch/dms/internal/testaudit"
|
|
)
|
|
|
|
func setupTest(t *testing.T) (*pgxpool.Pool, *httptest.Server, string) {
|
|
t.Helper()
|
|
dsn := os.Getenv("TEST_TENANT_DSN")
|
|
if dsn == "" {
|
|
t.Skip("TEST_TENANT_DSN nicht gesetzt, Integrationstest uebersprungen")
|
|
}
|
|
ctx := context.Background()
|
|
pool := testdb.Pool(t, dsn)
|
|
t.Cleanup(func() { pool.Close() })
|
|
testaudit.Ensure(t, pool)
|
|
|
|
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 documents (
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(), folder_id UUID,
|
|
title TEXT NOT NULL, current_revision_id UUID, created_by UUID NOT NULL REFERENCES users(id),
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(), updated_at TIMESTAMPTZ NOT NULL DEFAULT now(), deleted_at TIMESTAMPTZ
|
|
);
|
|
CREATE TABLE IF NOT EXISTS metadata_fields (
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(), field_key TEXT NOT NULL UNIQUE, label TEXT NOT NULL,
|
|
field_type TEXT NOT NULL CHECK (field_type IN ('text', 'number', 'date', 'bool', 'select')),
|
|
required BOOLEAN NOT NULL DEFAULT false, default_value TEXT, validation_rule JSONB NOT NULL DEFAULT '{}'::jsonb,
|
|
version INT NOT NULL DEFAULT 1, created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
);
|
|
CREATE TABLE IF NOT EXISTS document_metadata_values (
|
|
document_id UUID NOT NULL REFERENCES documents(id) ON DELETE CASCADE,
|
|
field_id UUID NOT NULL REFERENCES metadata_fields(id) ON DELETE CASCADE,
|
|
value TEXT NOT NULL, PRIMARY KEY (document_id, field_id)
|
|
);
|
|
`); err != nil {
|
|
t.Fatalf("schema: %v", err)
|
|
}
|
|
t.Cleanup(func() {
|
|
_, _ = pool.Exec(context.Background(), `TRUNCATE document_metadata_values, metadata_fields, documents, users CASCADE`)
|
|
})
|
|
|
|
var userID string
|
|
if err := pool.QueryRow(ctx, `INSERT INTO users (email, name) VALUES ($1, 'A') RETURNING id`, fmt.Sprintf("metaapi-%d@example.test", time.Now().UnixNano())).Scan(&userID); err != nil {
|
|
t.Fatalf("user: %v", err)
|
|
}
|
|
|
|
server := NewServer(pool)
|
|
mux := http.NewServeMux()
|
|
server.RegisterRoutes(mux)
|
|
httpServer := httptest.NewServer(mux)
|
|
t.Cleanup(httpServer.Close)
|
|
|
|
return pool, httpServer, userID
|
|
}
|
|
|
|
// Akzeptanzkriterium 1: Formular passt sich der Felddefinition automatisch
|
|
// an — Grundlage ist die vollstaendige, korrekte Feldliste.
|
|
func TestHandleListFields_ReturnsAllFieldDefinitions(t *testing.T) {
|
|
pool, server, _ := setupTest(t)
|
|
ctx := context.Background()
|
|
if _, err := pool.Exec(ctx, `INSERT INTO metadata_fields (field_key, label, field_type, required) VALUES ('betrag', 'Betrag', 'number', true)`); err != nil {
|
|
t.Fatalf("feld anlegen: %v", err)
|
|
}
|
|
|
|
resp, err := http.Get(server.URL + "/metadata-fields")
|
|
if err != nil {
|
|
t.Fatalf("get: %v", err)
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
var fields []fieldDTO
|
|
if err := json.NewDecoder(resp.Body).Decode(&fields); err != nil {
|
|
t.Fatalf("decode: %v", err)
|
|
}
|
|
if len(fields) != 1 || fields[0].Key != "betrag" || fields[0].Type != "number" || !fields[0].Required {
|
|
t.Fatalf("felder falsch: %+v", fields)
|
|
}
|
|
}
|
|
|
|
func TestHandleSetValues_ValidatesBeforeSaving(t *testing.T) {
|
|
pool, server, userID := setupTest(t)
|
|
ctx := context.Background()
|
|
var fieldID, docID string
|
|
if err := pool.QueryRow(ctx, `INSERT INTO metadata_fields (field_key, label, field_type) VALUES ('betrag', 'Betrag', 'number') RETURNING id`).Scan(&fieldID); err != nil {
|
|
t.Fatalf("feld anlegen: %v", err)
|
|
}
|
|
if err := pool.QueryRow(ctx, `INSERT INTO documents (title, created_by) VALUES ('x.pdf', $1) RETURNING id`, userID).Scan(&docID); err != nil {
|
|
t.Fatalf("dokument anlegen: %v", err)
|
|
}
|
|
|
|
body, _ := json.Marshal(map[string]string{fieldID: "keine-zahl"})
|
|
req, _ := http.NewRequest(http.MethodPut, server.URL+"/documents/"+docID+"/metadata?caller=tester", bytes.NewReader(body))
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
t.Fatalf("put: %v", err)
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
if resp.StatusCode != http.StatusUnprocessableEntity {
|
|
t.Fatalf("status = %d, want 422 fuer ungueltigen wert", resp.StatusCode)
|
|
}
|
|
|
|
body2, _ := json.Marshal(map[string]string{fieldID: "42"})
|
|
req2, _ := http.NewRequest(http.MethodPut, server.URL+"/documents/"+docID+"/metadata?caller=tester", bytes.NewReader(body2))
|
|
resp2, err := http.DefaultClient.Do(req2)
|
|
if err != nil {
|
|
t.Fatalf("put2: %v", err)
|
|
}
|
|
defer func() { _ = resp2.Body.Close() }()
|
|
if resp2.StatusCode != http.StatusNoContent {
|
|
t.Fatalf("status = %d, want 204 fuer gueltigen wert", resp2.StatusCode)
|
|
}
|
|
|
|
getResp, err := http.Get(server.URL + "/documents/" + docID + "/metadata")
|
|
if err != nil {
|
|
t.Fatalf("get: %v", err)
|
|
}
|
|
defer func() { _ = getResp.Body.Close() }()
|
|
var values map[string]string
|
|
if err := json.NewDecoder(getResp.Body).Decode(&values); err != nil {
|
|
t.Fatalf("decode: %v", err)
|
|
}
|
|
if values[fieldID] != "42" {
|
|
t.Fatalf("gespeicherter wert = %q, want 42", values[fieldID])
|
|
}
|
|
}
|