Files
tenant-registry/internal/server/admin_lifecycle_test.go
sharangandClaude Opus 5.5 e8dcd1d259
ci / shared (push) Successful in 15s
ci / test (push) Successful in 23m50s
ci / image (push) Successful in 45s
feat(admin): repair a tenant's sign-in, delete an archived tenant (#42)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
2026-09-26 11:00:14 +00:00

135 lines
5.8 KiB
Go

package server_test
import (
"net/http"
"strings"
"testing"
"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)
}
})
}