metadataapi/tagapi/bulkeditapi als Backend-Voraussetzung. MetadataForm passt sich der Felddefinition an und warnt vor Verwerfen ungespeicherter Aenderungen, TagInput schlaegt Tags vor und ist per Pfeiltasten/Enter bedienbar, Massenbearbeitung wendet Tags auf beliebig grosse Auswahlen in einem Aufruf an. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
135 lines
5.1 KiB
Go
135 lines
5.1 KiB
Go
package bulkeditapi
|
|
|
|
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/dms/internal/bulkedit"
|
|
)
|
|
|
|
type alwaysUnlocked struct{}
|
|
|
|
func (alwaysUnlocked) IsLocked(ctx context.Context, documentID string) (bool, string, error) {
|
|
return false, "", nil
|
|
}
|
|
|
|
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, 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 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 tags (
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(), name TEXT NOT NULL UNIQUE, color TEXT NOT NULL DEFAULT '#808080',
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
);
|
|
CREATE TABLE IF NOT EXISTS document_tags (
|
|
document_id UUID NOT NULL REFERENCES documents(id) ON DELETE CASCADE,
|
|
tag_id UUID NOT NULL REFERENCES tags(id) ON DELETE CASCADE,
|
|
PRIMARY KEY (document_id, tag_id)
|
|
);
|
|
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)
|
|
);
|
|
CREATE TABLE IF NOT EXISTS audit_log (
|
|
id BIGSERIAL PRIMARY KEY, occurred_at TIMESTAMPTZ NOT NULL DEFAULT now(), actor TEXT NOT NULL,
|
|
action TEXT NOT NULL, object_type TEXT NOT NULL, object_reference TEXT NOT NULL,
|
|
metadata JSONB NOT NULL DEFAULT '{}'::jsonb, prev_hash TEXT NOT NULL, entry_hash TEXT NOT NULL
|
|
);
|
|
`); err != nil {
|
|
t.Fatalf("schema: %v", err)
|
|
}
|
|
t.Cleanup(func() {
|
|
_, _ = pool.Exec(context.Background(), `TRUNCATE audit_log, document_metadata_values, metadata_fields, document_tags, tags, documents, users CASCADE`)
|
|
})
|
|
|
|
var userID string
|
|
if err := pool.QueryRow(ctx, `INSERT INTO users (email, name) VALUES ($1, 'A') RETURNING id`, fmt.Sprintf("bulkeditapi-%d@example.test", time.Now().UnixNano())).Scan(&userID); err != nil {
|
|
t.Fatalf("user: %v", err)
|
|
}
|
|
|
|
server := NewServer(pool, alwaysUnlocked{})
|
|
mux := http.NewServeMux()
|
|
server.RegisterRoutes(mux)
|
|
httpServer := httptest.NewServer(mux)
|
|
t.Cleanup(httpServer.Close)
|
|
|
|
return pool, httpServer, userID
|
|
}
|
|
|
|
// Akzeptanzkriterium 3 (MET-03): Massenbearbeitung fuer ausgewaehlte
|
|
// Dokumente ueber HTTP moeglich.
|
|
func TestHandleApply_AppliesTagToSelection(t *testing.T) {
|
|
pool, server, userID := setupTest(t)
|
|
ctx := context.Background()
|
|
var tagID, docA, docB string
|
|
if err := pool.QueryRow(ctx, `INSERT INTO tags (name) VALUES ('review') RETURNING id`).Scan(&tagID); err != nil {
|
|
t.Fatalf("tag anlegen: %v", err)
|
|
}
|
|
if err := pool.QueryRow(ctx, `INSERT INTO documents (title, created_by) VALUES ('a.pdf', $1) RETURNING id`, userID).Scan(&docA); err != nil {
|
|
t.Fatalf("dokument a anlegen: %v", err)
|
|
}
|
|
if err := pool.QueryRow(ctx, `INSERT INTO documents (title, created_by) VALUES ('b.pdf', $1) RETURNING id`, userID).Scan(&docB); err != nil {
|
|
t.Fatalf("dokument b anlegen: %v", err)
|
|
}
|
|
|
|
body, _ := json.Marshal(applyRequest{Actor: userID, DocumentIDs: []string{docA, docB}, AddTagIDs: []string{tagID}})
|
|
resp, err := http.Post(server.URL+"/bulk-edit", "application/json", bytes.NewReader(body))
|
|
if err != nil {
|
|
t.Fatalf("post: %v", err)
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
var result applyResult
|
|
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
|
t.Fatalf("decode: %v", err)
|
|
}
|
|
if len(result.Applied) != 2 || len(result.Skipped) != 0 {
|
|
t.Fatalf("erwartet 2 angewendet, 0 uebersprungen, habe: %+v", result)
|
|
}
|
|
|
|
var count int
|
|
if err := pool.QueryRow(ctx, `SELECT count(*) FROM document_tags WHERE tag_id = $1`, tagID).Scan(&count); err != nil {
|
|
t.Fatalf("zaehlen: %v", err)
|
|
}
|
|
if count != 2 {
|
|
t.Fatalf("erwartet 2 zuordnungen, habe %d", count)
|
|
}
|
|
}
|
|
|
|
var _ = bulkedit.LockChecker(alwaysUnlocked{})
|