Files
tenant-registry/internal/server/openapi_test.go
T
Sharang ParnerkarandClaude Opus 5 9f393b728f
ci / shared (pull_request) Successful in 13s
ci / test (pull_request) Successful in 22m19s
ci / image (pull_request) Skipped
feat(members): tenant organization members, invites, admin email
The portal's Settings/Users page showed design fixtures because the
registry had no membership surface. Two endpoints close that:

- GET /v1/tenants/{id}/members lists the Keycloak organization members
  (user id, email, name, org_roles, enabled). A tenant without an
  organization answers an empty list, not an error.
- POST /v1/tenants/{id}/invites creates the user if needed, adds the org
  member (KC 26: bare user id body), writes the tenant-context
  attributes, sends the execute-actions mail and audits user.invited.
  Idempotent: an existing member answers 200 with status=member.

User tokens are confined to their own tenant (cross-tenant is a 404) and
need IT_ADMIN or OWNER to invite; service clients are gated per route
group as before. Principal gains the org_roles claim for that check.

Tenants carry admin_email (migration 0006, backfilled from the invite
audit trail) so Settings can show a real contact instead of an em dash.

The members route is registered as GET /v1/tenants/{id}/{sub}: ServeMux
rejects /v1/tenants/{id}/members as conflicting with the by-slug lookup,
because both match /v1/tenants/by-slug/members and neither pattern is
more specific.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
2026-09-12 20:59:32 +02:00

76 lines
2.7 KiB
Go

package server_test
import (
"context"
"path/filepath"
"testing"
"github.com/getkin/kin-openapi/openapi3"
"github.com/getkin/kin-openapi/openapi3filter"
"github.com/getkin/kin-openapi/routers/gorillamux"
)
// TestOpenAPISpec_loads_and_validates is the contract gate: the committed
// openapi.yaml must parse, every $ref must resolve, and every documented
// operation must be reachable from the router. If a handler is missing
// from the spec or vice-versa, this fails.
func TestOpenAPISpec_loadsAndIsConsistent(t *testing.T) {
loader := &openapi3.Loader{Context: context.Background(), IsExternalRefsAllowed: false}
specPath, _ := filepath.Abs("../../openapi.yaml")
doc, err := loader.LoadFromFile(specPath)
if err != nil {
t.Fatalf("load spec: %v", err)
}
if err := doc.Validate(loader.Context); err != nil {
t.Fatalf("validate spec: %v", err)
}
// Replace the servers block so the validator matches any host.
doc.Servers = openapi3.Servers{{URL: "/"}}
router, err := gorillamux.NewRouter(doc)
if err != nil {
t.Fatalf("build router: %v", err)
}
// Run a few sample requests through the validator. Each one must be
// matched to an operation in the spec.
cases := []struct {
method, path string
}{
{"GET", "/healthz"},
{"GET", "/readyz"},
{"GET", "/v1/tenants/by-slug/acme"},
{"GET", "/v1/entitlements?tenant_id=00000000-0000-0000-0000-000000000001"},
{"GET", "/v1/api-keys?tenant_id=00000000-0000-0000-0000-000000000001"},
{"GET", "/v1/catalog"},
{"GET", "/v1/audit?limit=10"},
{"POST", "/v1/internal/keycloak/claims"},
{"GET", "/v1/tenants?limit=10"},
{"GET", "/v1/tenants/00000000-0000-0000-0000-000000000001/members"},
{"POST", "/v1/tenants/00000000-0000-0000-0000-000000000001/invites"},
{"POST", "/v1/tenants/00000000-0000-0000-0000-000000000001/status"},
{"POST", "/v1/internal/entitlements/check"},
{"POST", "/v1/internal/erp/webhook"},
{"POST", "/v1/internal/erp/reconcile"},
{"GET", "/v1/internal/erp/status"},
{"GET", "/v1/subscriptions?tenant_id=00000000-0000-0000-0000-000000000001"},
{"POST", "/v1/usage/events"},
{"GET", "/v1/usage/events?tenant_id=00000000-0000-0000-0000-000000000001"},
{"POST", "/v1/licences"},
{"POST", "/v1/licences/heartbeat"},
{"POST", "/v1/licences/00000000-0000-0000-0000-000000000001/revoke"},
{"GET", "/.well-known/licence-keys.json"},
}
for _, c := range cases {
req := newRequest(t, c.method, c.path)
_, _, err := router.FindRoute(req)
if err != nil {
t.Errorf("%s %s: not in spec — %v", c.method, c.path, err)
}
}
}
// Reference the openapi3filter package so its symbol survives if the
// per-request validation block grows back later.
var _ = openapi3filter.ValidateRequest