Files
tenant-registry/internal/server/openapi_test.go
T
Sharang ParnerkarandClaude Opus 5.5 b89dd2ae4c
ci / shared (pull_request) Successful in 13s
ci / test (pull_request) Successful in 25m18s
ci / image (pull_request) Skipped
feat(api-keys): CI licences by API key and integration keys
Step 1b of ADR 0001, with the user's decisions D1–D3 recorded:

- POST /v1/licences/ci (PUBLIC_EXPLICIT, the bearer is a tenant API key
  with scope <product>:licence): 24 h licence with kind "ci", no
  heartbeat, not a seat (D1; the registry records ci.licence usage once
  per key and day). Reused per key, device and UTC day; at most 60
  calls per key and hour. Unblocks firmwerk#64 step 1.
- Werkpilot CI licences carry the tenant's LiteLLM key (D2) with a
  1-day LiteLLM expiry; the secret is sealed on the licence row
  (AES-256-GCM, new SECRETS_ENCRYPTION_KEY, migration 0017) so the
  reused licence hands out the same key. At most 10 keys per API key
  and day. Without the encryption key a reuse comes without the key.
- Integration keys: a tenant admin's user token from a holder product's
  web client (werkmeister-<env>) creates, lists and revokes that
  holder's key for its own tenant only; scopes from a per-holder
  allowlist, the holder product must be booked. A user token on the key
  routes is classified as AUTHZ group api-keys.integration, so plain
  api-keys stays a service grant.
- Migration 0016: key names are unique among active keys only, so a
  revoked name can be reused (re-issue = revoke, then create).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 23:32:01 +02:00

102 lines
4.5 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", "/version"},
{"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/api-keys/scopes"},
{"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"},
{"PATCH", "/v1/tenants/00000000-0000-0000-0000-000000000001/members/u-1"},
{"DELETE", "/v1/tenants/00000000-0000-0000-0000-000000000001/members/u-1"},
{"POST", "/v1/tenants/00000000-0000-0000-0000-000000000001/members/u-1/resend"},
{"PATCH", "/v1/admin/tenants/00000000-0000-0000-0000-000000000001/members/u-1"},
{"DELETE", "/v1/admin/tenants/00000000-0000-0000-0000-000000000001/members/u-1"},
{"POST", "/v1/admin/tenants/00000000-0000-0000-0000-000000000001/members/u-1/resend"},
{"GET", "/v1/admin/tenants?q=acme"},
{"GET", "/v1/admin/tenants/00000000-0000-0000-0000-000000000001"},
{"POST", "/v1/admin/tenants/00000000-0000-0000-0000-000000000001/trial"},
{"POST", "/v1/admin/tenants/00000000-0000-0000-0000-000000000001/invites/resend"},
{"POST", "/v1/admin/tenants/00000000-0000-0000-0000-000000000001/reprovision"},
{"DELETE", "/v1/admin/tenants/00000000-0000-0000-0000-000000000001"},
{"GET", "/v1/admin/audit?limit=10"},
{"POST", "/v1/notifications"},
{"GET", "/v1/admin/tenants/00000000-0000-0000-0000-000000000001/documents"},
{"POST", "/v1/admin/documents/00000000-0000-0000-0000-000000000002/publish"},
{"DELETE", "/v1/admin/documents/00000000-0000-0000-0000-000000000002"},
{"GET", "/v1/tenants/00000000-0000-0000-0000-000000000001/documents/00000000-0000-0000-0000-000000000002/download"},
{"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/ci"},
{"POST", "/v1/licences/00000000-0000-0000-0000-000000000001/revoke"},
{"GET", "/.well-known/licence-keys.json"},
{"GET", "/v1/tenants/00000000-0000-0000-0000-000000000001/managed-services"},
{"GET", "/v1/admin/tenants/00000000-0000-0000-0000-000000000001/managed-services"},
{"POST", "/v1/admin/tenants/00000000-0000-0000-0000-000000000001/managed-services"},
{"PATCH", "/v1/admin/tenants/00000000-0000-0000-0000-000000000001/managed-services/00000000-0000-0000-0000-000000000002"},
{"DELETE", "/v1/admin/tenants/00000000-0000-0000-0000-000000000001/managed-services/00000000-0000-0000-0000-000000000002"},
}
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