package server_test import ( "context" "encoding/json" "errors" "io" "net/http" "net/http/httptest" "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) } }) } // The purge erases the tenant's Prüfwerk data first (DELETE // …/platform/tenants/{id}/data with the service token). If Prüfwerk fails, // nothing else is removed; without Prüfwerk in the catalog it is skipped. func TestPurgeErasesPruefwerkData(t *testing.T) { eachStore(t, func(t *testing.T, h *testHarness) { var calls []string var gotAuth, gotBody string fail := true pw := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { calls = append(calls, r.Method+" "+r.URL.Path) gotAuth = r.Header.Get("Authorization") b, _ := io.ReadAll(r.Body) gotBody = string(b) if fail { w.WriteHeader(http.StatusInternalServerError) return } _, _ = io.WriteString(w, `{"status":"deleted","tenant_id":"x","files_removed":3,"files_not_removed":1}`) })) defer pw.Close() h.app.Cfg.PruefwerkPlatformURL = pw.URL + "/api/v1/platform/" tn := createWithAdmin(t, h, "pw-purge-co", "it@pw-purge.test") 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) } base := "/v1/admin/tenants/" + tn.ID + "?confirm=pw-purge-co" resp, raw := h.do("DELETE", base, nil) if resp.StatusCode != http.StatusBadGateway || !strings.Contains(string(raw), "pruefwerk_error") { t.Fatalf("pruefwerk down: %d %s", resp.StatusCode, raw) } if len(h.kcMock.Purged) != 0 { t.Fatalf("sign-in removed although Prüfwerk failed: %v", h.kcMock.Purged) } if resp, _ := h.do("GET", "/v1/tenants/"+tn.ID, nil); resp.StatusCode != 200 { t.Fatalf("tenant gone after failed erasure: %d", resp.StatusCode) } fail = false if resp, raw := h.do("DELETE", base, nil); resp.StatusCode != 200 { t.Fatalf("purge: %d %s", resp.StatusCode, raw) } want := "DELETE /api/v1/platform/tenants/" + tn.ID + "/data" if len(calls) != 2 || calls[1] != want { t.Fatalf("calls: %v, want %s", calls, want) } if gotAuth != "Bearer mock-service-token" || gotBody != `{"confirm":"`+tn.ID+`"}` { t.Fatalf("request: %q %q", gotAuth, gotBody) } _, raw = h.do("GET", "/v1/audit?action=tenant.purged", nil) if !strings.Contains(string(raw), `"pruefwerk_data":"deleted"`) || !strings.Contains(string(raw), `"pruefwerk_files_not_removed":1`) { t.Fatalf("audit: %s", raw) } h.app.Cfg.PruefwerkPlatformURL = "" other := createWithAdmin(t, h, "no-pw-co", "it@no-pw.test") if resp, _ := h.do("POST", "/v1/tenants/"+other.ID+"/status", map[string]any{"status": "archived", "source": "test"}); resp.StatusCode != 200 { t.Fatalf("archive: %d", resp.StatusCode) } // No Prüfwerk in this environment's catalog: the purge goes ahead. if resp, raw := h.do("DELETE", "/v1/admin/tenants/"+other.ID+"?confirm=no-pw-co", nil); resp.StatusCode != 200 { t.Fatalf("purge without prüfwerk: %d %s", resp.StatusCode, raw) } _, raw = h.do("GET", "/v1/audit?action=tenant.purged", nil) if !strings.Contains(string(raw), `"pruefwerk_data":"not_configured"`) { t.Fatalf("audit without prüfwerk: %s", raw) } }) }