Files
tenant-registry/internal/server/services_scope_test.go
T
Sharang ParnerkarandClaude Opus 5.5 3ff7ce2e92
ci / shared (pull_request) Successful in 13s
ci / test (pull_request) Successful in 24m17s
ci / image (pull_request) Skipped
feat(services): managed services per tenant for the portal (#77)
Managed Git (one Gitea instance per customer, INF-GIT-{CLD,OPR}-{STD,BUS,ENT})
is provisioned by hand; the registry now keeps the record so the portal can
show it under "Verwaltete Infrastruktur" and staff can maintain it.

- 0012_managed_services: table with kind/status checks, UNIQUE (tenant_id,
  kind, url), FK cascade on tenant delete (mirrored in the memory store).
- GET /v1/tenants/{id}/managed-services: tenant view without instance_id and
  notes; user tokens need IT_ADMIN, OWNER or CXO (else FORBIDDEN_ROLE).
- GET/POST /v1/admin/tenants/{id}/managed-services and PATCH/DELETE
  .../{sid}: staff view and maintenance, validation codes invalid_kind,
  invalid_name, invalid_url, invalid_sku, invalid_status,
  invalid_instance_id, invalid_notes; 409 duplicate.
- Audit managed_service.created/updated/deleted with the staff actor;
  updated carries the changed fields and old/new status.
- OpenAPI spec and tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
2026-10-04 21:07:59 +02:00

162 lines
6.8 KiB
Go

package server
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"git.breakpilot.com/platform/tenant-registry/internal/authn"
"git.breakpilot.com/platform/tenant-registry/internal/store"
)
func seedService(t *testing.T, s *Server, tenantID string) *store.ManagedService {
t.Helper()
ms, err := s.Store.CreateManagedService(context.Background(), store.ManagedService{
TenantID: tenantID, Kind: "git", Name: "Git", URL: "https://git.k-00001.example", SKU: "INF-GIT-CLD-STD",
InstanceID: "gitea-k-00001", Notes: "staff only",
})
if err != nil {
t.Fatal(err)
}
return ms
}
func TestManagedServices_tenantRoles(t *testing.T) {
tenantID := "00000000-0000-0000-0000-000000000001"
cases := []struct {
name string
roles []string
want int
}{
{"it admin", []string{"IT_ADMIN"}, http.StatusOK},
{"owner", []string{"OWNER"}, http.StatusOK},
{"cxo", []string{"USER", "CXO"}, http.StatusOK},
{"cxo lower case", []string{"cxo"}, http.StatusOK},
{"plain user", []string{"USER"}, http.StatusForbidden},
{"finance", []string{"FINANCE"}, http.StatusForbidden},
{"legal", []string{"LEGAL"}, http.StatusForbidden},
{"no roles", nil, http.StatusForbidden},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
p := &authn.Principal{Subject: "u-1", ClientID: "portal", TenantID: tenantID, OrgRoles: c.roles}
s, h, tenant := scopedServer(t, p)
seedService(t, s, tenant.ID)
rr := httptest.NewRecorder()
h.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, "/v1/tenants/"+tenant.ID+"/managed-services", nil))
if rr.Code != c.want {
t.Fatalf("status %d, want %d: %s", rr.Code, c.want, rr.Body.String())
}
body := rr.Body.String()
if c.want == http.StatusForbidden {
if !strings.Contains(body, "FORBIDDEN_ROLE") {
t.Fatalf("error code: %s", body)
}
return
}
if !strings.Contains(body, "https://git.k-00001.example") || strings.Contains(body, "instance_id") || strings.Contains(body, "staff only") {
t.Fatalf("tenant view: %s", body)
}
})
}
// Another tenant's user, even an IT_ADMIN: 404, never a hint it exists.
other := &authn.Principal{Subject: "u-x", ClientID: "portal", TenantID: "11111111-1111-1111-1111-111111111111", OrgRoles: []string{"IT_ADMIN"}}
s, h, tenant := scopedServer(t, other)
seedService(t, s, tenant.ID)
rr := httptest.NewRecorder()
h.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, "/v1/tenants/"+tenant.ID+"/managed-services", nil))
if rr.Code != http.StatusNotFound {
t.Fatalf("cross-tenant: %d %s", rr.Code, rr.Body.String())
}
}
func TestManagedServices_clients(t *testing.T) {
const rules = "portal-svc:tenants,admin;pruefwerk-svc:tenants.read"
// The portal reads the tenant view with its service token.
portal := &authn.Principal{Subject: "svc", ClientID: "portal-svc"}
s, h, tenant := scopedServer(t, portal, rules)
ms := seedService(t, s, tenant.ID)
rr := httptest.NewRecorder()
h.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, "/v1/tenants/"+tenant.ID+"/managed-services", nil))
if rr.Code != http.StatusOK || strings.Contains(rr.Body.String(), "instance_id") {
t.Fatalf("portal tenant read: %d %s", rr.Code, rr.Body.String())
}
// A client without the admin group may neither read nor write the admin
// routes; staff write access is decided by the portal (SUPPORT_ENGINEER
// reads only), the registry only sees the admin client.
ro := &authn.Principal{Subject: "svc2", ClientID: "pruefwerk-svc"}
_, h2, _ := scopedServer(t, ro, rules)
base := "/v1/admin/tenants/" + tenant.ID + "/managed-services"
for _, c := range []struct{ method, path, body string }{
{http.MethodGet, base, ""},
{http.MethodPost, base, `{"kind":"git","name":"x","url":"https://x.k-00001.example","sku":"INF-GIT-CLD-STD"}`},
{http.MethodPatch, base + "/" + ms.ID, `{"status":"halted"}`},
{http.MethodDelete, base + "/" + ms.ID, ""},
} {
rr := httptest.NewRecorder()
h2.ServeHTTP(rr, httptest.NewRequest(c.method, c.path, strings.NewReader(c.body)))
if rr.Code != http.StatusForbidden || !strings.Contains(rr.Body.String(), "FORBIDDEN_CLIENT") {
t.Fatalf("%s %s by a non-admin client: %d %s", c.method, c.path, rr.Code, rr.Body.String())
}
}
// ...but it may read the tenant view (tenants.read).
rr = httptest.NewRecorder()
h2.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, "/v1/tenants/"+tenant.ID+"/managed-services", nil))
if rr.Code != http.StatusOK {
t.Fatalf("read client tenant view: %d %s", rr.Code, rr.Body.String())
}
}
func TestManagedServices_staffIsAuditActor(t *testing.T) {
portal := &authn.Principal{Subject: "svc", ClientID: "portal-svc"}
s, h, tenant := scopedServer(t, portal, "portal-svc:tenants,admin")
base := "/v1/admin/tenants/" + tenant.ID + "/managed-services"
send := func(method, path, body string) *httptest.ResponseRecorder {
r := httptest.NewRequest(method, path, strings.NewReader(body))
r.Header.Set("X-Staff-Subject", "staff-1")
r.Header.Set("X-Staff-Email", "ops@breakpilot.com")
rr := httptest.NewRecorder()
h.ServeHTTP(rr, r)
return rr
}
rr := send(http.MethodPost, base, `{"kind":"git","name":"Git","url":"https://git.k-00001.example","sku":"INF-GIT-OPR-BUS","instance_id":"gitea-k-00001"}`)
if rr.Code != http.StatusCreated || !strings.Contains(rr.Body.String(), `"instance_id":"gitea-k-00001"`) || !strings.Contains(rr.Body.String(), `"status":"provisioning"`) {
t.Fatalf("create: %d %s", rr.Code, rr.Body.String())
}
items, _ := s.Store.ListManagedServices(context.Background(), tenant.ID)
if len(items) != 1 {
t.Fatalf("stored: %+v", items)
}
id := items[0].ID
if rr := send(http.MethodPatch, base+"/"+id, `{"status":"restricted"}`); rr.Code != http.StatusOK {
t.Fatalf("patch: %d %s", rr.Code, rr.Body.String())
}
if rr := send(http.MethodDelete, base+"/"+id, ""); rr.Code != http.StatusNoContent {
t.Fatalf("delete: %d %s", rr.Code, rr.Body.String())
}
for _, action := range []string{"managed_service.created", "managed_service.updated", "managed_service.deleted"} {
evs, _, _ := s.Store.ListAudit(context.Background(), store.AuditFilter{TenantID: tenant.ID, Action: action})
if len(evs) != 1 || evs[0].ActorType != "staff" || evs[0].ActorID != "staff-1" || evs[0].ActorName != "ops@breakpilot.com" || evs[0].TargetID != id {
t.Fatalf("%s actor: %+v", action, evs)
}
if action == "managed_service.updated" && (evs[0].Metadata["old_status"] != "provisioning" || evs[0].Metadata["new_status"] != "restricted") {
t.Fatalf("status move: %+v", evs[0].Metadata)
}
}
}
func TestRouteGroup_managedServices(t *testing.T) {
if g := routeGroup(http.MethodGet, "/v1/tenants/x/managed-services"); g != "tenants.read" {
t.Errorf("tenant group %q", g)
}
for _, m := range []string{http.MethodGet, http.MethodPost, http.MethodPatch, http.MethodDelete} {
if g := routeGroup(m, "/v1/admin/tenants/x/managed-services/y"); g != "admin" {
t.Errorf("%s admin group %q", m, g)
}
}
}