package server_test import ( "context" "encoding/json" "errors" "net/http" "strings" "testing" "time" "git.breakpilot.com/platform/tenant-registry/internal/keycloak" "git.breakpilot.com/platform/tenant-registry/internal/store" ) func createWithAdmin(t *testing.T, h *testHarness, slug, email string) *store.Tenant { t.Helper() resp, raw := h.do("POST", "/v1/tenants", map[string]any{"slug": slug, "name": slug + " GmbH", "admin_email": email}) if resp.StatusCode != http.StatusCreated { t.Fatalf("create %s: %d %s", slug, resp.StatusCode, raw) } return decode[struct { Tenant *store.Tenant `json:"tenant"` }](t, raw).Tenant } func auditCount(t *testing.T, h *testHarness, q string) int { t.Helper() _, raw := h.do("GET", "/v1/audit?"+q, nil) return len(decode[struct { Items []store.AuditEvent `json:"items"` }](t, raw).Items) } func TestAdminReprovision(t *testing.T) { eachStore(t, func(t *testing.T, h *testHarness) { tn := createWithAdmin(t, h, "repro-co", "it@repro.test") // Someone removed the org and the account in Keycloak by hand. delete(h.kcMock.Orgs, tn.ID) delete(h.kcMock.Users, "it@repro.test") resp, raw := h.do("POST", "/v1/admin/tenants/"+tn.ID+"/reprovision", nil) if resp.StatusCode != http.StatusOK || !strings.Contains(string(raw), `"status":"invited"`) || !strings.Contains(string(raw), "it@repro.test") { t.Fatalf("reprovision: %d %s", resp.StatusCode, raw) } if _, ok := h.kcMock.Orgs[tn.ID]; !ok { t.Fatal("org not recreated") } if n := auditCount(t, h, "action=tenant.signin_reprovisioned&tenant_id="+tn.ID); n != 1 { t.Fatalf("audit events: %d", n) } // With the org present and the admin a member, the invitation goes out again. h.kcMock.Members["repro-co"] = []keycloak.OrgMember{{UserID: "u1", Email: "it@repro.test"}} if resp, raw := h.do("POST", "/v1/admin/tenants/"+tn.ID+"/reprovision", map[string]any{"email": "IT@repro.test"}); resp.StatusCode != 200 || !strings.Contains(string(raw), `"status":"resent"`) { t.Fatalf("resend: %d %s", resp.StatusCode, raw) } // An existing org and a new address: the person is invited into it. if resp, raw := h.do("POST", "/v1/admin/tenants/"+tn.ID+"/reprovision", map[string]any{"email": "second@repro.test"}); resp.StatusCode != 200 || !strings.Contains(string(raw), `"status":"invited"`) { t.Fatalf("invite into org: %d %s", resp.StatusCode, raw) } }) } func TestAdminReprovisionRejects(t *testing.T) { eachStore(t, func(t *testing.T, h *testHarness) { noAdmin := newTenant(t, h, "no-admin-co") if resp, _ := h.do("POST", "/v1/admin/tenants/"+noAdmin.ID+"/reprovision", nil); resp.StatusCode != 400 { t.Fatalf("no admin on record: %d", resp.StatusCode) } if resp, _ := h.do("POST", "/v1/admin/tenants/"+noAdmin.ID+"/reprovision", map[string]any{"email": "not an email"}); resp.StatusCode != 400 { t.Fatalf("bad email: %d", resp.StatusCode) } // The address already belongs to another workspace's account. h.kcMock.Users["taken@x.test"] = "u-other" if resp, _ := h.do("POST", "/v1/admin/tenants/"+noAdmin.ID+"/reprovision", map[string]any{"email": "taken@x.test"}); resp.StatusCode != 409 { t.Fatalf("taken: %d", resp.StatusCode) } if resp, _ := h.do("POST", "/v1/tenants/"+noAdmin.ID+"/status", map[string]any{"status": "archived", "source": "test"}); resp.StatusCode != 200 { t.Fatalf("archive: %d", resp.StatusCode) } if resp, _ := h.do("POST", "/v1/admin/tenants/"+noAdmin.ID+"/reprovision", map[string]any{"email": "a@b.test"}); resp.StatusCode != 409 { t.Fatalf("archived: %d", resp.StatusCode) } if resp, _ := h.do("POST", "/v1/admin/tenants/00000000-0000-0000-0000-00000000dead/reprovision", nil); resp.StatusCode != 404 { t.Fatalf("unknown: %d", resp.StatusCode) } h.kcMock.FailNext = keycloak.ErrUnavailable other := createWithAdmin(t, h, "kc-down-co", "it@down.test") h.kcMock.FailNext = keycloak.ErrUnavailable if resp, _ := h.do("POST", "/v1/admin/tenants/"+other.ID+"/reprovision", nil); resp.StatusCode != 503 { t.Fatalf("keycloak down: %d", resp.StatusCode) } }) } func TestAdminPurgeTenant(t *testing.T) { eachStore(t, func(t *testing.T, h *testHarness) { tn := createWithAdmin(t, h, "purge-co", "it@purge.test") base := "/v1/admin/tenants/" + tn.ID if resp, _ := h.do("DELETE", base+"?confirm=purge-co", nil); resp.StatusCode != 409 { t.Fatalf("not archived: %d", resp.StatusCode) } if resp, _ := h.do("POST", "/v1/tenants/"+tn.ID+"/status", map[string]any{"status": "archived", "source": "test"}); resp.StatusCode != 200 { t.Fatalf("archive: %d", resp.StatusCode) } if resp, _ := h.do("DELETE", base+"?confirm=wrong", nil); resp.StatusCode != 400 { t.Fatalf("confirm mismatch: %d", resp.StatusCode) } h.kcMock.FailNext = keycloak.ErrUnavailable if resp, _ := h.do("DELETE", base+"?confirm=purge-co", nil); resp.StatusCode != 503 { t.Fatalf("keycloak down keeps the tenant: %d", resp.StatusCode) } if resp, _ := h.do("GET", "/v1/tenants/"+tn.ID, nil); resp.StatusCode != 200 { t.Fatalf("tenant gone after failed purge: %d", resp.StatusCode) } resp, raw := h.do("DELETE", base+"?confirm=purge-co", nil) if resp.StatusCode != 200 || !strings.Contains(string(raw), `"status":"purged"`) || !strings.Contains(string(raw), `"accounts_removed":1`) { t.Fatalf("purge: %d %s", resp.StatusCode, raw) } if resp, _ := h.do("GET", "/v1/tenants/"+tn.ID, nil); resp.StatusCode != 404 { t.Fatalf("tenant still there: %d", resp.StatusCode) } if len(h.kcMock.Purged) != 1 || h.kcMock.Purged[0] != "purge-co" { t.Fatalf("keycloak purge: %v", h.kcMock.Purged) } if n := auditCount(t, h, "action=tenant.purged"); n != 1 { t.Fatalf("purge audit: %d", n) } // The name and the address are free again. createWithAdmin(t, h, "purge-co", "it@purge.test") if resp, _ := h.do("DELETE", "/v1/admin/tenants/00000000-0000-0000-0000-00000000dead?confirm=x", nil); resp.StatusCode != 404 { t.Fatalf("unknown: %d", resp.StatusCode) } }) } // Security reports outlive their workspace (docgen#6 decision Nr. 17, // V-g): an archived workspace with reports is not purged, and nothing of it // is removed. func TestPurgeKeepsIntakeReports(t *testing.T) { eachStore(t, func(t *testing.T, h *testHarness) { tn := createWithAdmin(t, h, "reports-co", "it@reports.test") if _, _, err := h.store.CreateIntakeReport(context.Background(), store.IntakeCapture{TenantID: tn.ID, Channel: store.IntakeChannelPortal, ReporterSub: "u-1", About: store.IntakeAboutProduct, Danger: "no", Original: json.RawMessage(`{"what":"x"}`), At: time.Now().UTC()}); err != nil { t.Fatal(err) } if resp, _ := h.do("POST", "/v1/tenants/"+tn.ID+"/status", map[string]any{"status": "archived", "source": "test"}); resp.StatusCode != 200 { t.Fatalf("archive: %d", resp.StatusCode) } resp, raw := h.do("DELETE", "/v1/admin/tenants/"+tn.ID+"?confirm=reports-co", nil) if resp.StatusCode != 409 || !strings.Contains(string(raw), "intake_retained") { t.Fatalf("purge with reports: %d %s", resp.StatusCode, raw) } if len(h.kcMock.Purged) != 0 { t.Fatalf("sign-in removed although the purge was refused: %v", h.kcMock.Purged) } if resp, _ := h.do("GET", "/v1/tenants/"+tn.ID, nil); resp.StatusCode != 200 { t.Fatalf("tenant gone: %d", resp.StatusCode) } if n, err := h.store.CountIntakeReports(context.Background(), tn.ID); err != nil || n != 1 { t.Fatalf("reports kept: %d %v", n, err) } // The store refuses on its own too. if err := h.store.DeleteTenant(context.Background(), tn.ID); !errors.Is(err, store.ErrConflict) { t.Fatalf("store delete with reports: %v", err) } }) }