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) } // A BreakPilot delivery (no area) shows last; a draft not at all. 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) } if _, err := h.store.CreateDocument(ctx, store.Document{ID: "10000000-0000-0000-0000-000000000006", TenantID: tid, Title: "Entwurf", ObjectKey: "k/e", Status: store.DocumentDraft}); 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,Report" { t.Errorf("order: %v", order) } // Every document, for Werkmeister and staff. if u, _ := h.store.ListDocuments(ctx, tid, false); len(u) != 6 { t.Errorf("all documents: %d", len(u)) } if u, _ := h.store.ListDocuments(ctx, tid, true); len(u) != 5 { t.Errorf("published documents: %d", len(u)) } // Marking a delivery as the tenant's own moves it into the internal area, and back. if d, err := h.store.SetDocumentOwn(ctx, ids[4], true); err != nil || d.Area != store.AreaInternal { t.Errorf("own: %+v %v", d, err) } if d, err := h.store.SetDocumentOwn(ctx, ids[4], false); err != nil || d.Area != "" { t.Errorf("not own: %+v %v", d, err) } if d, err := h.store.SetDocumentOwn(ctx, ids[0], false); err != nil || d.Area != store.AreaProductPublic { t.Errorf("a public document keeps its area: %+v %v", d, err) } 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) } }) }