Migration 0021 (company_profiles + company_profile_revisions), GET/PUT /v1/tenants/{id}/company-profile, POST …/submit with optimistic revisions (409 + current revision; unchanged saves are no new revision), staff import via /v1/admin/tenants/{id}/company-profile (named staff member only), outline validation of the form document, audit without answers, OpenAPI.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
74 lines
3.3 KiB
Go
74 lines
3.3 KiB
Go
package server_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.breakpilot.com/platform/tenant-registry/internal/store"
|
|
)
|
|
|
|
// The store contract on both stores: revisions, the unchanged save, conflicts
|
|
// with the current revision, submit, and a document that survives JSONB.
|
|
func TestCompanyProfileStore(t *testing.T) {
|
|
eachStore(t, func(t *testing.T, h *testHarness) {
|
|
ctx := context.Background()
|
|
doc := json.RawMessage(`{"audience":"own_company","sections":{"company_profile":{"companyName":"Muster","industry":["Robotik","Maschinenbau"],"foundedYear":2001,"legalNote":null},"compliance_scope":{"data_hr":true},"ai_act":{}},"documents":[],"suggested":["data_hr"]}`)
|
|
write := func(data json.RawMessage, expected int) (*store.CompanyProfile, bool, error) {
|
|
return h.store.SaveCompanyProfile(ctx, store.CompanyProfileWrite{
|
|
TenantID: h.tenant.ID, Data: data, SchemaVersion: "v2", ExpectedRevision: expected,
|
|
Source: store.ProfileSourcePortal, By: "a@acme.test", At: time.Now(),
|
|
})
|
|
}
|
|
|
|
if _, err := h.store.GetCompanyProfile(ctx, h.tenant.ID); !errors.Is(err, store.ErrNotFound) {
|
|
t.Fatalf("none yet: %v", err)
|
|
}
|
|
p, changed, err := write(doc, 0)
|
|
if err != nil || !changed || p.Revision != 1 || p.Status != store.ProfileDraft || p.UpdatedBy != "a@acme.test" {
|
|
t.Fatalf("first save: %+v %v %v", p, changed, err)
|
|
}
|
|
if mustCanon(t, p.Data) != mustCanon(t, doc) {
|
|
t.Fatalf("document changed on the way:\n%s\n%s", p.Data, doc)
|
|
}
|
|
|
|
// Same answers in another key order: no new revision.
|
|
reordered := json.RawMessage(`{"suggested":["data_hr"],"documents":[],"sections":{"ai_act":{},"compliance_scope":{"data_hr":true},"company_profile":{"legalNote":null,"foundedYear":2001,"industry":["Robotik","Maschinenbau"],"companyName":"Muster"}},"audience":"own_company"}`)
|
|
if p, changed, err := write(reordered, 1); err != nil || changed || p.Revision != 1 {
|
|
t.Fatalf("unchanged save: %+v %v %v", p, changed, err)
|
|
}
|
|
var rc *store.RevisionConflict
|
|
if _, _, err := write(json.RawMessage(`{"sections":{}}`), 0); !errors.As(err, &rc) || rc.Current != 1 || !errors.Is(err, store.ErrConflict) {
|
|
t.Fatalf("stale save: %v", err)
|
|
}
|
|
if _, err := h.store.SubmitCompanyProfile(ctx, h.tenant.ID, 5, "a@acme.test", time.Now()); !errors.As(err, &rc) || rc.Current != 1 {
|
|
t.Fatalf("stale submit: %v", err)
|
|
}
|
|
p, err = h.store.SubmitCompanyProfile(ctx, h.tenant.ID, 1, "b@acme.test", time.Now())
|
|
if err != nil || p.Revision != 2 || p.Status != store.ProfileSubmitted || p.SubmittedBy != "b@acme.test" || p.SubmittedAt == nil {
|
|
t.Fatalf("submit: %+v %v", p, err)
|
|
}
|
|
p, changed, err = write(json.RawMessage(`{"sections":{"company_profile":{"companyName":"Muster AG"}}}`), 2)
|
|
if err != nil || !changed || p.Revision != 3 || p.Status != store.ProfileSubmitted {
|
|
t.Fatalf("edit after submit: %+v %v %v", p, changed, err)
|
|
}
|
|
if _, _, err := h.store.SaveCompanyProfile(ctx, store.CompanyProfileWrite{
|
|
TenantID: "00000000-0000-0000-0000-00000000beef", Data: doc, SchemaVersion: "v2", Source: store.ProfileSourcePortal, At: time.Now(),
|
|
}); !errors.Is(err, store.ErrNotFound) {
|
|
t.Fatalf("unknown tenant: %v", err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func mustCanon(t *testing.T, raw json.RawMessage) string {
|
|
t.Helper()
|
|
var v any
|
|
if err := json.Unmarshal(raw, &v); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b, _ := json.Marshal(v)
|
|
return string(b)
|
|
}
|