Files
tenant-registry/internal/server/document_store_db_test.go
T
Sharang ParnerkarandClaude Opus 5.5 94da0e0013
ci / shared (pull_request) Successful in 12s
ci / test (pull_request) Successful in 25m54s
ci / image (pull_request) Skipped
feat(store): Dokumentenablage with areas, checked serving, import
Workspace ticket #171 (decided 2026-10-20): the tenant's own documents
live next to BreakPilot's deliveries ("Unterlagen") in the same table and
bucket, in three areas, with the original and the prepared version the
Factory delivers.

- migration 0022: documents get area (legal_web / product_public /
  internal; NULL = Unterlagen), legal_kind (the 8 folders as the workspace
  and Factory name them), processing_status (none/pending/ready/failed),
  external_id (the Factory's tenant_document_id, unique per tenant);
  document_preparations (chunk file and/or cookie-banner capture with
  digest, format, revision, pin, image digests); store_assets (images per
  tenant, content-addressed)
- GET /v1/tenants/{id}/store; POST …/store/documents (customer upload:
  legal folder, product of an internal document; legal and product
  documents wait for the Factory, an internal one only on request);
  GET …/original, …/prepared, …/consent-banner and …/store/assets/{sha}:
  every byte is hashed and compared before anything is sent (fail-closed,
  502 digest_mismatch), HTML originals sandboxed
- POST /v1/admin/tenants/{id}/store/import (named staff only): a .tar.gz in
  the Factory's run layout; unsafe paths and links refused, every file
  checked against its digest, keyed on tenant_document_id so it can run
  again; per-document outcome, never content
- Unterlagen lists stay as they were (area NULL only); deleting a store
  document removes its prepared files; storage gains Get
- OpenAPI for the seven routes; tests on memory and Postgres with
  synthetic data only

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 15:11:52 +02:00

135 lines
5.6 KiB
Go

package server_test
import (
"context"
"errors"
"strings"
"testing"
"git.breakpilot.com/platform/tenant-registry/internal/store"
)
// The document store contract on both stores (synthetic data).
func TestDocumentStoreContract(t *testing.T) {
eachStore(t, func(t *testing.T, h *testHarness) {
ctx := context.Background()
tid := h.tenant.ID
mk := func(id, title, area, kind, ext string) (*store.Document, error) {
return h.store.CreateDocument(ctx, store.Document{
ID: id, TenantID: tid, Title: title, ObjectKey: "k/" + id, Status: store.DocumentPublished,
Area: area, LegalKind: kind, ExternalID: ext, ProcessingStatus: store.ProcessingPending,
})
}
ids := []string{
"10000000-0000-0000-0000-000000000001", "10000000-0000-0000-0000-000000000002",
"10000000-0000-0000-0000-000000000003", "10000000-0000-0000-0000-000000000004",
"10000000-0000-0000-0000-000000000005",
}
if _, err := mk(ids[0], "Datenblatt", store.AreaProductPublic, "", "wsdoc-a"); err != nil {
t.Fatal(err)
}
if _, err := mk(ids[1], "Widerruf", store.AreaLegalWeb, "widerruf", "wsdoc-b"); err != nil {
t.Fatal(err)
}
if _, err := mk(ids[2], "Impressum", store.AreaLegalWeb, "impressum", ""); err != nil {
t.Fatal(err)
}
if _, err := mk(ids[3], "Richtlinie", store.AreaInternal, "", ""); err != nil {
t.Fatal(err)
}
// Unterlagen (no area) stay apart.
if _, err := h.store.CreateDocument(ctx, store.Document{ID: ids[4], TenantID: tid, Title: "Report", ObjectKey: "k/r", Status: store.DocumentPublished}); err != nil {
t.Fatal(err)
}
for name, bad := range map[string]store.Document{
"legal without folder": {Area: store.AreaLegalWeb},
"folder outside legal": {Area: store.AreaInternal, LegalKind: "agb"},
"unknown folder": {Area: store.AreaLegalWeb, LegalKind: "faq"},
"unknown area": {Area: "secret"},
"external id twice": {Area: store.AreaInternal, ExternalID: "wsdoc-a"},
} {
bad.ID, bad.TenantID, bad.Title, bad.ObjectKey = "20000000-0000-0000-0000-00000000000"+string(rune('1'+len(name)%9)), tid, "x", "k/bad-"+name
if _, err := h.store.CreateDocument(ctx, bad); err == nil {
t.Errorf("%s: accepted", name)
}
}
docs, err := h.store.ListStoreDocuments(ctx, tid)
if err != nil {
t.Fatal(err)
}
var order []string
for _, d := range docs {
order = append(order, d.Title)
}
if strings.Join(order, ",") != "Impressum,Widerruf,Datenblatt,Richtlinie" {
t.Errorf("order: %v", order)
}
if u, _ := h.store.ListDocuments(ctx, tid, false); len(u) != 1 || u[0].Title != "Report" {
t.Errorf("Unterlagen: %+v", u)
}
if d, err := h.store.GetDocumentByExternalID(ctx, tid, "wsdoc-b"); err != nil || d.ID != ids[1] {
t.Errorf("by external id: %v %v", d, err)
}
if _, err := h.store.GetDocumentByExternalID(ctx, tid, ""); !errors.Is(err, store.ErrNotFound) {
t.Errorf("empty external id: %v", err)
}
// A chunk part, then a consent part: the second keeps the first.
a, b := strings.Repeat("a", 64), strings.Repeat("b", 64)
p, err := h.store.SaveDocumentPreparation(ctx, store.Preparation{
DocumentID: ids[0], Format: "LANE_B_UNIFIED_V5", Revision: 8, ChunkObjectKey: "k/c", ChunkSHA256: a, ChunkCount: 12, AssetDigests: []string{b},
})
if err != nil || !p.HasChunks() || p.ChunkCount != 12 {
t.Fatalf("chunks: %+v %v", p, err)
}
p, err = h.store.SaveDocumentPreparation(ctx, store.Preparation{DocumentID: ids[0], ConsentObjectKey: "k/x", ConsentSHA256: b})
if err != nil || p.ChunkSHA256 != a || p.ConsentSHA256 != b || p.Revision != 8 || len(p.AssetDigests) != 1 {
t.Fatalf("merge: %+v %v", p, err)
}
if d, _ := h.store.GetDocument(ctx, ids[0]); d.ProcessingStatus != store.ProcessingReady {
t.Errorf("status after chunks: %q", d.ProcessingStatus)
}
if _, err := h.store.SaveDocumentPreparation(ctx, store.Preparation{DocumentID: ids[1]}); !errors.Is(err, store.ErrInvalidInput) {
t.Errorf("empty preparation: %v", err)
}
if m, _ := h.store.ListDocumentPreparations(ctx, tid); len(m) != 1 || m[ids[0]].ChunkCount != 12 {
t.Errorf("list preparations: %+v", m)
}
if _, err := h.store.GetDocumentPreparation(ctx, ids[1]); !errors.Is(err, store.ErrNotFound) {
t.Errorf("no preparation: %v", err)
}
if d, err := h.store.SetDocumentProcessing(ctx, ids[1], store.ProcessingFailed, "unreadable scan"); err != nil || d.ProcessingError != "unreadable scan" {
t.Errorf("failed: %+v %v", d, err)
}
if _, err := h.store.SetDocumentProcessing(ctx, ids[1], "done", ""); err == nil {
t.Error("unknown status accepted")
}
// Images per tenant, once.
for i := 0; i < 2; i++ {
if _, err := h.store.PutStoreAsset(ctx, store.StoreAsset{TenantID: tid, SHA256: b, ObjectKey: "k/a", ContentType: "image/png", SizeBytes: 3}); err != nil {
t.Fatal(err)
}
}
if got, err := h.store.GetStoreAsset(ctx, tid, b); err != nil || got.ContentType != "image/png" {
t.Errorf("asset: %+v %v", got, err)
}
other, _ := h.store.CreateTenant(ctx, store.TenantCreate{Slug: "andere", Name: "Andere GmbH"})
if _, err := h.store.GetStoreAsset(ctx, other.ID, b); !errors.Is(err, store.ErrNotFound) {
t.Errorf("another tenant's asset: %v", err)
}
if _, err := h.store.PutStoreAsset(ctx, store.StoreAsset{TenantID: tid, SHA256: "nope", ObjectKey: "k"}); !errors.Is(err, store.ErrInvalidInput) {
t.Errorf("bad digest: %v", err)
}
// Deleting a document takes its preparation along.
if err := h.store.DeleteDocument(ctx, ids[0]); err != nil {
t.Fatal(err)
}
if _, err := h.store.GetDocumentPreparation(ctx, ids[0]); !errors.Is(err, store.ErrNotFound) {
t.Errorf("preparation after delete: %v", err)
}
})
}