Files
tenant-registry/internal/server/entitlements_test.go
T
sharang 2ea49be29d
ci / shared (push) Successful in 12s
ci / test (push) Successful in 21m58s
ci / image (push) Successful in 56s
feat(m-mig-5): products table, entitlement check, tenant ladder, per-client authz (#23)
2026-09-12 07:38:47 +00:00

184 lines
6.3 KiB
Go

package server_test
import (
"net/http"
"testing"
"git.breakpilot.com/platform/tenant-registry/internal/server"
"git.breakpilot.com/platform/tenant-registry/internal/store"
)
func check(t *testing.T, h *testHarness, body map[string]any) server.EntitlementCheck {
t.Helper()
resp, raw := h.do("POST", "/v1/internal/entitlements/check", body)
if resp.StatusCode != http.StatusOK {
t.Fatalf("status = %d body=%s", resp.StatusCode, raw)
}
return decode[server.EntitlementCheck](t, raw)
}
func TestEntitlementCheck_ladder(t *testing.T) {
eachStore(t, func(t *testing.T, h *testHarness) {
// fresh tenant, trial of pruefwerk
_, raw := h.do("POST", "/v1/tenants", map[string]any{"slug": "ent-co", "name": "Ent Co"})
tn := decode[struct {
Tenant *store.Tenant `json:"tenant"`
}](t, raw).Tenant
got := check(t, h, map[string]any{"tenant_id": tn.ID, "product": "pruefwerk"})
if got.Allowed || got.Reason != "not_entitled" {
t.Fatalf("before trial: %+v", got)
}
if resp, _ := h.do("POST", "/v1/catalog/trial-request", map[string]any{"tenant_id": tn.ID, "product": "pruefwerk"}); resp.StatusCode != 201 {
t.Fatalf("trial: %d", resp.StatusCode)
}
got = check(t, h, map[string]any{"tenant_id": tn.ID, "product": "pruefwerk"})
if !got.Allowed || got.ReadOnly || got.Status != "trial" || got.ExpiresAt == nil {
t.Fatalf("trial: %+v", got)
}
// add-on not purchased
got = check(t, h, map[string]any{"tenant_id": tn.ID, "product": "pruefwerk", "addon": "werkbank"})
if got.Allowed || got.Reason != "addon_not_purchased" {
t.Fatalf("addon: %+v", got)
}
// activation grants the add-on limit
if resp, raw := h.do("POST", "/v1/tenants/"+tn.ID+"/activate", map[string]any{
"plan": "business", "products": []map[string]any{{"key": "pruefwerk", "config": map[string]any{"runners": 2, "targets": 25}}},
}); resp.StatusCode != 200 {
t.Fatalf("activate: %d %s", resp.StatusCode, raw)
}
got = check(t, h, map[string]any{"tenant_id": tn.ID, "product": "pruefwerk", "addon": "werkbank"})
if !got.Allowed || got.Status != "active" || toF(got.Limits["runners"]) != 2 {
t.Fatalf("after activate: %+v", got)
}
if h.kcMock.Status["ent-co"] != "active" {
t.Fatalf("keycloak status not synced: %q", h.kcMock.Status["ent-co"])
}
// ladder: restricted → read-only; halted → denied; archived → denied
for _, tc := range []struct {
status string
allowed, ro bool
reason string
}{
{"warning", true, false, ""},
{"restricted", true, true, ""},
{"halted", false, false, "tenant_halted"},
{"archived", false, false, "tenant_archived"},
} {
if resp, raw := h.do("POST", "/v1/tenants/"+tn.ID+"/status", map[string]any{"status": tc.status, "source": "test"}); resp.StatusCode != 200 {
t.Fatalf("status %s: %d %s", tc.status, resp.StatusCode, raw)
}
got = check(t, h, map[string]any{"tenant_id": tn.ID, "product": "pruefwerk"})
if got.Allowed != tc.allowed || got.ReadOnly != tc.ro || got.Reason != tc.reason || got.Status != tc.status {
t.Fatalf("status %s: %+v", tc.status, got)
}
if h.kcMock.Status["ent-co"] != tc.status {
t.Fatalf("status %s not synced to keycloak", tc.status)
}
}
// unknown product / tenant
if got = check(t, h, map[string]any{"tenant_id": tn.ID, "product": "nope"}); got.Allowed || got.Reason != "unknown_product" {
t.Fatalf("unknown product: %+v", got)
}
if got = check(t, h, map[string]any{"tenant_id": "00000000-0000-0000-0000-00000000dead", "product": "pruefwerk"}); got.Allowed || got.Reason != "tenant_not_found" {
t.Fatalf("unknown tenant: %+v", got)
}
})
}
func TestSetStatus_rejectsBogus(t *testing.T) {
eachStore(t, func(t *testing.T, h *testHarness) {
for _, bad := range []string{"frozen", "paid", ""} {
resp, _ := h.do("POST", "/v1/tenants/"+h.tenant.ID+"/status", map[string]any{"status": bad})
if resp.StatusCode != http.StatusBadRequest {
t.Errorf("status %q: got %d", bad, resp.StatusCode)
}
}
})
}
func TestListTenants_pagination(t *testing.T) {
eachStore(t, func(t *testing.T, h *testHarness) {
for _, s := range []string{"p-one", "p-two", "p-three"} {
if resp, _ := h.do("POST", "/v1/tenants", map[string]any{"slug": s, "name": s}); resp.StatusCode != 201 {
t.Fatalf("create %s: %d", s, resp.StatusCode)
}
}
seen := map[string]bool{}
cursor := ""
for pages := 0; pages < 10; pages++ {
path := "/v1/tenants?limit=2"
if cursor != "" {
path += "&cursor=" + cursor
}
resp, raw := h.do("GET", path, nil)
if resp.StatusCode != 200 {
t.Fatalf("list: %d %s", resp.StatusCode, raw)
}
page := decode[struct {
Items []store.Tenant `json:"items"`
NextCursor string `json:"next_cursor"`
}](t, raw)
for _, it := range page.Items {
if seen[it.Slug] {
t.Fatalf("duplicate %s across pages", it.Slug)
}
seen[it.Slug] = true
}
if page.NextCursor == "" {
break
}
cursor = page.NextCursor
}
for _, s := range []string{"acme", "p-one", "p-two", "p-three"} {
if !seen[s] {
t.Errorf("missing %s", s)
}
}
// the three new tenants are trial; the seeded acme is active on the
// memory store and trial on the postgres harness.
want := 3
if h.tenant.Status == "trial" {
want = 4
}
resp, raw := h.do("GET", "/v1/tenants?status=trial", nil)
got := decode[struct {
Items []store.Tenant `json:"items"`
}](t, raw)
if resp.StatusCode != 200 || len(got.Items) != want {
t.Fatalf("status filter: %d got %d want %d", resp.StatusCode, len(got.Items), want)
}
if resp, _ := h.do("GET", "/v1/tenants?status=bogus", nil); resp.StatusCode != 400 {
t.Errorf("bogus status: %d", resp.StatusCode)
}
})
}
func TestCatalog_isStoreBacked(t *testing.T) {
eachStore(t, func(t *testing.T, h *testHarness) {
_, raw := h.do("GET", "/v1/catalog", nil)
out := decode[struct {
Items []store.CatalogEntry `json:"items"`
}](t, raw)
keys := map[string]store.CatalogEntry{}
for _, it := range out.Items {
keys[it.Key] = it
}
if _, ok := keys["compliance"]; ok {
t.Errorf("disabled product listed")
}
pw, ok := keys["pruefwerk"]
if !ok || pw.Type != "standalone" || pw.LaunchURL == "" || len(pw.Addons) != 1 || pw.Addons[0].ID != "werkbank" {
t.Fatalf("pruefwerk entry: %+v", pw)
}
if wp := keys["werkpilot"]; wp.Type != "cli" {
t.Errorf("werkpilot type: %+v", wp)
}
})
}
func toF(v interface{}) float64 {
f, _ := v.(float64)
return f
}