Files
tenant-registry/internal/server/company_profile_test.go
T
sharangandClaude Opus 5.5 174a2f1e01
ci / shared (push) Successful in 14s
ci / test (push) Successful in 25m47s
ci / image (push) Successful in 37s
feat(company-profile): Unternehmensprofil per tenant, registry as the authority (#85)
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>
2026-10-20 10:30:09 +00:00

185 lines
8.3 KiB
Go

package server
import (
"context"
"encoding/json"
"net/http"
"strings"
"testing"
"time"
"git.breakpilot.com/platform/tenant-registry/internal/authn"
"git.breakpilot.com/platform/tenant-registry/internal/store"
)
// A profile as the form writes it: values, a list, yes/no, empty, and three
// unconfirmed suggestions.
const profileDoc = `{"audience":"own_company","sections":{"company_profile":{"companyName":"Muster","legalForm":"gmbh","industry":["Maschinenbau","Robotik"],"foundedYear":2001},"compliance_scope":{"data_hr":true,"data_communication":true,"data_financial":true,"uses_tracking":false,"cloud_providers":null},"ai_act":{}},"documents":[],"suggested":["data_hr","data_communication","data_financial"],"lastStep":"company_profile"}`
func profileBody(doc string, expected int) string {
return `{"schema_version":"onboarding-product-v2","expected_revision":` + itoa(expected) + `,"data":` + doc + `}`
}
func itoa(n int) string { b, _ := json.Marshal(n); return string(b) }
func TestCompanyProfileLifecycle(t *testing.T) {
f := newTeamFixture(t, portal, "portal-svc:*")
path := "/v1/tenants/" + f.tenant.ID + "/company-profile"
user := map[string]string{"X-Portal-User-Email": "cxo@acme.test"}
if code, _, _ := f.call(t, "GET", path, "", nil); code != http.StatusNotFound {
t.Fatalf("no profile yet: %d", code)
}
if code, _, raw := f.call(t, "PUT", path, `{"schema_version":"v2","data":`+profileDoc+`}`, user); code != http.StatusBadRequest || !strings.Contains(raw, "expected_revision_required") {
t.Fatalf("without expected_revision: %d %s", code, raw)
}
code, _, raw := f.call(t, "PUT", path, profileBody(profileDoc, 0), user)
if code != http.StatusOK {
t.Fatalf("first save: %d %s", code, raw)
}
var p store.CompanyProfile
_ = json.Unmarshal([]byte(raw), &p)
if p.Revision != 1 || p.Status != store.ProfileDraft || p.UpdatedBy != "cxo@acme.test" || p.SchemaVersion != "onboarding-product-v2" {
t.Fatalf("saved: %+v", p)
}
// Read back: the same document, suggestions included.
_, _, raw = f.call(t, "GET", path, "", nil)
_ = json.Unmarshal([]byte(raw), &p)
var got, want any
_ = json.Unmarshal(p.Data, &got)
_ = json.Unmarshal([]byte(profileDoc), &want)
if !sameValue(got, want) {
t.Fatalf("stored document differs:\n got %s\nwant %s", p.Data, profileDoc)
}
// Saving the same answers again (autosave) is not a new revision.
if code, _, raw := f.call(t, "PUT", path, profileBody(profileDoc, 1), user); code != http.StatusOK || !strings.Contains(raw, `"revision":1`) {
t.Fatalf("unchanged save: %d %s", code, raw)
}
// A writer that has not seen revision 1 is refused, with the revision to reload.
changed := strings.Replace(profileDoc, `"Muster"`, `"Muster AG"`, 1)
if code, _, raw := f.call(t, "PUT", path, profileBody(changed, 0), user); code != http.StatusConflict || !strings.Contains(raw, `"current_revision":1`) {
t.Fatalf("stale save: %d %s", code, raw)
}
// Submit, then keep editing: the profile stays submitted.
if code, _, raw := f.call(t, "POST", path+"/submit", `{"expected_revision":0}`, user); code != http.StatusConflict {
t.Fatalf("stale submit: %d %s", code, raw)
}
code, _, raw = f.call(t, "POST", path+"/submit", `{"expected_revision":1}`, user)
_ = json.Unmarshal([]byte(raw), &p)
if code != http.StatusOK || p.Revision != 2 || p.Status != store.ProfileSubmitted || p.SubmittedBy != "cxo@acme.test" || p.SubmittedAt == nil {
t.Fatalf("submit: %d %s", code, raw)
}
code, _, raw = f.call(t, "PUT", path, profileBody(changed, 2), user)
_ = json.Unmarshal([]byte(raw), &p)
if code != http.StatusOK || p.Revision != 3 || p.Status != store.ProfileSubmitted {
t.Fatalf("edit after submit: %d %s", code, raw)
}
revs := f.s.Store.(*store.Memory).CompanyProfileRevisions(f.tenant.ID)
if len(revs) != 3 || revs[0].Status != store.ProfileDraft || revs[1].Status != store.ProfileSubmitted || revs[2].Revision != 3 {
t.Fatalf("history: %+v", revs)
}
saved, submitted := f.audit(t, "company_profile.saved"), f.audit(t, "company_profile.submitted")
if len(saved) != 2 || len(submitted) != 1 {
t.Fatalf("audit: %d saved, %d submitted", len(saved), len(submitted))
}
for _, e := range append(saved, submitted...) {
if b, _ := json.Marshal(e.Metadata); strings.Contains(string(b), "Muster") {
t.Fatalf("answers leaked into the audit log: %s", b)
}
}
}
func TestCompanyProfileRejectsMalformedDocuments(t *testing.T) {
f := newTeamFixture(t, portal, "portal-svc:*")
path := "/v1/tenants/" + f.tenant.ID + "/company-profile"
for name, doc := range map[string]string{
"not an object": `[1,2]`,
"unknown key": `{"sections":{},"facts":{}}`,
"client audience": `{"audience":"client","sections":{}}`,
"no sections": `{"audience":"own_company"}`,
"unknown section": `{"sections":{"pricing":{}}}`,
"nested answer": `{"sections":{"company_profile":{"address":{"street":"x"}}}}`,
"list of numbers": `{"sections":{"company_profile":{"industry":[1,2]}}}`,
"suggested object": `{"sections":{},"suggested":{"a":1}}`,
} {
if code, _, raw := f.call(t, "PUT", path, profileBody(doc, 0), nil); code != http.StatusBadRequest {
t.Errorf("%s: %d %s", name, code, raw)
}
}
big := `{"sections":{"company_profile":{"notes":"` + strings.Repeat("x", maxProfileBytes) + `"}}}`
if code, _, _ := f.call(t, "PUT", path, profileBody(big, 0), nil); code == http.StatusOK {
t.Error("an oversized document was stored")
}
}
func TestCompanyProfileImportIsStaffOnly(t *testing.T) {
f := newTeamFixture(t, portal, "portal-svc:*")
path := "/v1/admin/tenants/" + f.tenant.ID + "/company-profile"
staff := map[string]string{"X-Staff-Subject": "staff-1", "X-Staff-Email": "staff@breakpilot.test"}
if code, _, _ := f.call(t, "PUT", path, profileBody(profileDoc, 0), nil); code != http.StatusForbidden {
t.Fatalf("import without a staff member: %d", code)
}
code, _, raw := f.call(t, "PUT", path, profileBody(profileDoc, 0), staff)
if code != http.StatusOK || !strings.Contains(raw, `"status":"draft"`) {
t.Fatalf("import: %d %s", code, raw)
}
if code, _, raw := f.call(t, "GET", path, "", staff); code != http.StatusOK || !strings.Contains(raw, `"revision":1`) {
t.Fatalf("admin read: %d %s", code, raw)
}
ev := f.audit(t, "company_profile.imported")
if len(ev) != 1 || ev[0].ActorType != "staff" || ev[0].ActorID != "staff-1" {
t.Fatalf("import audit: %+v", ev)
}
if revs := f.s.Store.(*store.Memory).CompanyProfileRevisions(f.tenant.ID); len(revs) != 1 {
t.Fatalf("history after import: %d", len(revs))
}
}
func TestCompanyProfileAccess(t *testing.T) {
// A product client may read the profile but not write it.
product := &authn.Principal{Subject: "svc-pw", ClientID: "pruefwerk-svc"}
f := newTeamFixture(t, product, "pruefwerk-svc:tenants.read")
path := "/v1/tenants/" + f.tenant.ID + "/company-profile"
if code, _, _ := f.call(t, "PUT", path, profileBody(profileDoc, 0), nil); code != http.StatusForbidden {
t.Fatalf("product write: %d", code)
}
if code, _, _ := f.call(t, "GET", path, "", nil); code != http.StatusNotFound {
t.Fatalf("product read (none yet): %d", code)
}
// A signed-in user of another tenant does not see it.
other := &authn.Principal{Subject: "u-x", ClientID: "portal", TenantID: "00000000-0000-0000-0000-00000000beef"}
g := newTeamFixture(t, other)
if _, _, err := g.s.Store.SaveCompanyProfile(context.Background(), store.CompanyProfileWrite{
TenantID: g.tenant.ID, Data: json.RawMessage(profileDoc), SchemaVersion: "v2", Source: store.ProfileSourcePortal, At: time.Now(),
}); err != nil {
t.Fatal(err)
}
if code, _, _ := g.call(t, "GET", "/v1/tenants/"+g.tenant.ID+"/company-profile", "", nil); code != http.StatusNotFound {
t.Fatalf("cross-tenant read: %d", code)
}
// ... while the tenant's own user does.
own := &authn.Principal{Subject: "u-a", ClientID: "portal", TenantID: g.tenant.ID}
h := newTeamFixture(t, own)
if _, _, err := h.s.Store.SaveCompanyProfile(context.Background(), store.CompanyProfileWrite{
TenantID: h.tenant.ID, Data: json.RawMessage(profileDoc), SchemaVersion: "v2", Source: store.ProfileSourcePortal, At: time.Now(),
}); err != nil {
t.Fatal(err)
}
if code, _, raw := h.call(t, "GET", "/v1/tenants/"+h.tenant.ID+"/company-profile", "", nil); code != http.StatusOK {
t.Fatalf("own read: %d %s", code, raw)
}
}
func sameValue(a, b any) bool {
x, _ := json.Marshal(a)
y, _ := json.Marshal(b)
return string(x) == string(y)
}