package server_test import ( "context" "encoding/json" "errors" "strings" "testing" "git.breakpilot.com/platform/tenant-registry/internal/store" ) func createService(t *testing.T, h *testHarness, tenantID string, body map[string]any) store.ManagedService { t.Helper() resp, raw := h.do("POST", "/v1/admin/tenants/"+tenantID+"/managed-services", body) if resp.StatusCode != 201 { t.Fatalf("create managed service: %d %s", resp.StatusCode, raw) } var ms store.ManagedService _ = json.Unmarshal(raw, &ms) return ms } func TestManagedServices_crud(t *testing.T) { eachStore(t, func(t *testing.T, h *testHarness) { admin := "/v1/admin/tenants/" + h.tenant.ID + "/managed-services" git := createService(t, h, h.tenant.ID, map[string]any{ "kind": "git", "name": " Git K-00001 ", "url": "https://git.k-00001.example", "sku": "INF-GIT-CLD-STD", "instance_id": "gitea-k-00001", "notes": "provisioned by hand", }) if git.Name != "Git K-00001" || git.Status != "provisioning" || git.TenantID != h.tenant.ID || git.InstanceID != "gitea-k-00001" || git.Notes != "provisioned by hand" || git.ID == "" { t.Fatalf("created: %+v", git) } onprem := createService(t, h, h.tenant.ID, map[string]any{ "kind": "git", "name": "Git on-prem", "url": "https://git.k-00001.example/onprem", "sku": "INF-GIT-OPR-ENT", "status": "active", }) // one row per (tenant, kind, url) if resp, raw := h.do("POST", admin, map[string]any{"kind": "git", "name": "x", "url": "https://git.k-00001.example", "sku": "INF-GIT-CLD-BUS"}); resp.StatusCode != 409 || !strings.Contains(string(raw), `"duplicate"`) { t.Fatalf("duplicate: %d %s", resp.StatusCode, raw) } // validation valid := func() map[string]any { return map[string]any{"kind": "git", "name": "n", "url": "https://git2.k-00001.example", "sku": "INF-GIT-CLD-STD"} } for _, c := range []struct { field string value any code string }{ {"kind", "svn", "invalid_kind"}, {"kind", "", "invalid_kind"}, {"name", " ", "invalid_name"}, {"name", strings.Repeat("n", 101), "invalid_name"}, {"url", "http://git2.k-00001.example", "invalid_url"}, {"url", "git2.k-00001.example", "invalid_url"}, {"url", "https://", "invalid_url"}, {"url", "https:///path", "invalid_url"}, {"url", "https://git2.k-00001.example/" + strings.Repeat("p", 300), "invalid_url"}, {"sku", "INF-GIT-CLD-XXL", "invalid_sku"}, {"sku", "PW-STD", "invalid_sku"}, {"sku", "", "invalid_sku"}, {"status", "paused", "invalid_status"}, {"instance_id", "Gitea_01", "invalid_instance_id"}, {"instance_id", strings.Repeat("a", 101), "invalid_instance_id"}, {"notes", strings.Repeat("n", 2001), "invalid_notes"}, } { body := valid() body[c.field] = c.value if resp, raw := h.do("POST", admin, body); resp.StatusCode != 400 || !strings.Contains(string(raw), `"`+c.code+`"`) { t.Fatalf("%s=%v: %d %s", c.field, c.value, resp.StatusCode, raw) } } if resp, _ := h.doRaw("POST", admin, "{"); resp.StatusCode != 400 { t.Fatalf("malformed body: %d", resp.StatusCode) } if resp, _ := h.do("POST", "/v1/admin/tenants/11111111-1111-1111-1111-111111111111/managed-services", valid()); resp.StatusCode != 404 { t.Fatalf("unknown tenant: %d", resp.StatusCode) } // admin list: oldest first, with instance_id and notes resp, raw := h.do("GET", admin, nil) if resp.StatusCode != 200 { t.Fatalf("admin list: %d %s", resp.StatusCode, raw) } list := decode[struct{ Items []map[string]any }](t, raw) if len(list.Items) != 2 || list.Items[0]["id"] != git.ID || list.Items[1]["id"] != onprem.ID || list.Items[0]["instance_id"] != "gitea-k-00001" || list.Items[1]["notes"] != "" { t.Fatalf("admin list: %s", raw) } // tenant list: same order, never instance_id / notes resp, raw = h.do("GET", "/v1/tenants/"+h.tenant.ID+"/managed-services", nil) if resp.StatusCode != 200 { t.Fatalf("tenant list: %d %s", resp.StatusCode, raw) } tl := decode[struct{ Items []map[string]any }](t, raw) if len(tl.Items) != 2 || tl.Items[0]["id"] != git.ID { t.Fatalf("tenant list: %s", raw) } want := []string{"created_at", "id", "kind", "name", "sku", "status", "tenant_id", "updated_at", "url"} for _, it := range tl.Items { if len(it) != len(want) { t.Fatalf("tenant view fields: %v", it) } for _, k := range want { if _, ok := it[k]; !ok { t.Fatalf("tenant view misses %s: %v", k, it) } } } if strings.Contains(string(raw), "instance_id") || strings.Contains(string(raw), "notes") { t.Fatalf("tenant view leaks staff fields: %s", raw) } // patch changes only what is given one := admin + "/" + git.ID resp, raw = h.do("PATCH", one, map[string]any{"status": "active", "notes": "live since 10-01"}) if resp.StatusCode != 200 { t.Fatalf("patch: %d %s", resp.StatusCode, raw) } got := decode[store.ManagedService](t, raw) if got.Status != "active" || got.Notes != "live since 10-01" || got.Name != "Git K-00001" || got.SKU != "INF-GIT-CLD-STD" || got.InstanceID != "gitea-k-00001" { t.Fatalf("patched: %+v", got) } if resp, raw := h.do("PATCH", one, map[string]any{"url": onprem.URL}); resp.StatusCode != 409 || !strings.Contains(string(raw), `"duplicate"`) { t.Fatalf("patch onto another url: %d %s", resp.StatusCode, raw) } for _, bad := range []map[string]any{{"status": "gone"}, {"sku": "INF-GIT-CLD"}, {"url": "ftp://x.example"}, {"name": ""}, {"instance_id": "UPPER"}, {"notes": strings.Repeat("x", 2001)}} { if resp, raw := h.do("PATCH", one, bad); resp.StatusCode != 400 { t.Fatalf("patch %v: %d %s", bad, resp.StatusCode, raw) } } // the kind is fixed after creation resp, raw = h.do("PATCH", one, map[string]any{"kind": "svn", "name": "Git"}) if resp.StatusCode != 200 || decode[store.ManagedService](t, raw).Kind != "git" { t.Fatalf("patch kind: %d %s", resp.StatusCode, raw) } // another tenant's service is not reachable through this tenant other, _ := h.store.CreateTenant(context.Background(), store.TenantCreate{Slug: "k-00002", Name: "K-00002"}) foreign := createService(t, h, other.ID, map[string]any{"kind": "git", "name": "Git", "url": "https://git.k-00001.example", "sku": "INF-GIT-CLD-STD"}) if resp, _ := h.do("PATCH", admin+"/"+foreign.ID, map[string]any{"name": "x"}); resp.StatusCode != 404 { t.Fatalf("cross-tenant patch: %d", resp.StatusCode) } if resp, _ := h.do("DELETE", admin+"/"+foreign.ID, nil); resp.StatusCode != 404 { t.Fatalf("cross-tenant delete: %d", resp.StatusCode) } if resp, _ := h.do("PATCH", admin+"/not-a-uuid", map[string]any{"name": "x"}); resp.StatusCode != 404 { t.Fatalf("bad sid: %d", resp.StatusCode) } if resp, _ := h.do("DELETE", one, nil); resp.StatusCode != 204 { t.Fatalf("delete: %d", resp.StatusCode) } if resp, _ := h.do("DELETE", one, nil); resp.StatusCode != 404 { t.Fatalf("delete again: %d", resp.StatusCode) } // audit: created, updated (changed fields, old → new status), deleted _, raw = h.do("GET", "/v1/audit?tenant_id="+h.tenant.ID+"&action=managed_service.updated&limit=100", nil) upd := decode[struct{ Items []store.AuditEvent }](t, raw) var statusMove bool for _, ev := range upd.Items { if ev.Metadata["old_status"] == "provisioning" && ev.Metadata["new_status"] == "active" { statusMove = true if ch, _ := ev.Metadata["changed"].([]any); len(ch) != 2 { t.Fatalf("changed fields: %v", ev.Metadata) } } } if !statusMove { t.Fatalf("status move not audited: %s", raw) } _, raw = h.do("GET", "/v1/audit?tenant_id="+h.tenant.ID+"&limit=100", nil) for _, a := range []string{"managed_service.created", "managed_service.updated", "managed_service.deleted"} { if !strings.Contains(string(raw), a) { t.Fatalf("audit %s missing: %s", a, raw) } } if strings.Contains(string(raw), "live since") { t.Fatalf("notes copied into the audit trail: %s", raw) } }) } // Deleting a tenant removes its services (FK cascade; mirrored in Memory). func TestManagedServices_tenantDeleteCascades(t *testing.T) { eachStore(t, func(t *testing.T, h *testHarness) { ctx := context.Background() other, err := h.store.CreateTenant(ctx, store.TenantCreate{Slug: "k-00003", Name: "K-00003"}) if err != nil { t.Fatal(err) } gone := createService(t, h, other.ID, map[string]any{"kind": "git", "name": "Git", "url": "https://git.k-00003.example", "sku": "INF-GIT-CLD-BUS"}) kept := createService(t, h, h.tenant.ID, map[string]any{"kind": "git", "name": "Git", "url": "https://git.k-00003.example", "sku": "INF-GIT-CLD-BUS"}) if err := h.store.DeleteTenant(ctx, other.ID); err != nil { t.Fatal(err) } if _, err := h.store.GetManagedService(ctx, gone.ID); !errors.Is(err, store.ErrNotFound) { t.Fatalf("service of the deleted tenant: %v", err) } if items, _ := h.store.ListManagedServices(ctx, other.ID); len(items) != 0 { t.Fatalf("list after delete: %+v", items) } if _, err := h.store.GetManagedService(ctx, kept.ID); err != nil { t.Fatalf("other tenant's service: %v", err) } }) }