package server_test import ( "bytes" "fmt" "net/http" "testing" "git.breakpilot.com/platform/tenant-registry/internal/keycloak" "git.breakpilot.com/platform/tenant-registry/internal/store" ) type tenantPage struct { Items []store.Tenant `json:"items"` NextCursor string `json:"next_cursor"` } func TestListTenants_pagination_and_filters(t *testing.T) { eachStore(t, func(t *testing.T, h *testHarness) { for i := 0; i < 3; i++ { slug := fmt.Sprintf("list-co-%d", i) if resp, raw := h.do("POST", "/v1/tenants", map[string]any{"slug": slug, "name": slug}); resp.StatusCode != 201 { t.Fatalf("create %s: %d %s", slug, resp.StatusCode, raw) } } // walk with limit=2: acme + 3 new = 4 tenants → 2 pages seen := map[string]bool{} cursor := "" pages := 0 for { path := "/v1/tenants?limit=2" if cursor != "" { path += "&cursor=" + cursor } resp, raw := h.do("GET", path, nil) if resp.StatusCode != http.StatusOK { t.Fatalf("list: %d %s", resp.StatusCode, raw) } page := decode[tenantPage](t, raw) pages++ for _, it := range page.Items { if seen[it.ID] { t.Fatalf("tenant %s returned twice", it.Slug) } seen[it.ID] = true } if page.NextCursor == "" { break } cursor = page.NextCursor if pages > 5 { t.Fatal("cursor never terminates") } } if len(seen) != 4 || pages < 2 { t.Fatalf("seen=%d pages=%d", len(seen), pages) } // status filter: move one tenant to halted and list only those var target string for id := range seen { if id != h.tenant.ID { target = id break } } if resp, raw := h.do("POST", "/v1/tenants/"+target+"/status", map[string]any{"status": "halted", "source": "test"}); resp.StatusCode != 200 { t.Fatalf("status: %d %s", resp.StatusCode, raw) } resp, raw := h.do("GET", "/v1/tenants?status=halted", nil) page := decode[tenantPage](t, raw) if resp.StatusCode != 200 || len(page.Items) != 1 || page.Items[0].ID != target { t.Fatalf("status filter: %d %+v", resp.StatusCode, page.Items) } // kind filter (all are customer kind by default) resp, raw = h.do("GET", "/v1/tenants?kind=customer", nil) page = decode[tenantPage](t, raw) if resp.StatusCode != 200 || len(page.Items) != 4 { t.Fatalf("kind filter: %d %d", resp.StatusCode, len(page.Items)) } // validation errors for _, q := range []string{"?status=bogus", "?limit=0", "?limit=abc", "?limit=9999"} { if resp, _ := h.do("GET", "/v1/tenants"+q, nil); resp.StatusCode != http.StatusBadRequest { t.Fatalf("%s: %d, want 400", q, resp.StatusCode) } } }) } func TestGetTenant_byID(t *testing.T) { eachStore(t, func(t *testing.T, h *testHarness) { resp, raw := h.do("GET", "/v1/tenants/"+h.tenant.ID, nil) if resp.StatusCode != http.StatusOK { t.Fatalf("get: %d %s", resp.StatusCode, raw) } if got := decode[store.Tenant](t, raw); got.Slug != "acme" { t.Fatalf("slug = %q", got.Slug) } if resp, _ := h.do("GET", "/v1/tenants/00000000-0000-0000-0000-000000000000", nil); resp.StatusCode != http.StatusNotFound { t.Fatalf("unknown id: %d", resp.StatusCode) } }) } func TestSetTenantStatus_validation_and_keycloakFailure(t *testing.T) { eachStore(t, func(t *testing.T, h *testHarness) { // frozen is legacy and not settable; bogus is unknown for _, st := range []string{"frozen", "bogus", ""} { if resp, _ := h.do("POST", "/v1/tenants/"+h.tenant.ID+"/status", map[string]any{"status": st}); resp.StatusCode != http.StatusBadRequest { t.Fatalf("status %q: %d, want 400", st, resp.StatusCode) } } if resp, _ := h.do("POST", "/v1/tenants/00000000-0000-0000-0000-000000000000/status", map[string]any{"status": "warning"}); resp.StatusCode != http.StatusNotFound { t.Fatalf("unknown tenant: %d", resp.StatusCode) } // Keycloak sync failure must not fail the transition; it is audited. h.kcMock.FailNext = keycloak.ErrOrgConflict resp, raw := h.do("POST", "/v1/tenants/"+h.tenant.ID+"/status", map[string]any{"status": "warning", "source": "erp", "reason": "invoice overdue"}) if resp.StatusCode != http.StatusOK { t.Fatalf("status with kc failure: %d %s", resp.StatusCode, raw) } if got := decode[store.Tenant](t, raw); got.Status != "warning" { t.Fatalf("status = %q", got.Status) } _, raw = h.do("GET", "/v1/audit?tenant_id="+h.tenant.ID+"&limit=50", nil) if !containsAction(raw, "keycloak.status_sync_failed") || !containsAction(raw, "tenant.status_changed") { t.Fatalf("audit trail missing: %s", raw) } }) } func TestReadyz(t *testing.T) { eachStore(t, func(t *testing.T, h *testHarness) { if resp, raw := h.do("GET", "/readyz", nil); resp.StatusCode != http.StatusOK { t.Fatalf("readyz: %d %s", resp.StatusCode, raw) } }) } func containsAction(raw []byte, action string) bool { return bytes.Contains(raw, []byte(`"action":"`+action+`"`)) }