Files
tenant-registry/internal/server/licences_test.go
T
Sharang ParnerkarandClaude Fable 5.1 7b02cd1c21
ci / shared (pull_request) Successful in 12s
ci / test (pull_request) Failing after 14m32s
ci / image (pull_request) Skipped
feat(erp): always-on ERP link, usage events, outbox, CLI licences
M-MIG-7 (ERP/02, decisions 3, 12, 13, 18):

- migration 0004: subscriptions, usage_events, erp_outbox,
  erp_webhook_receipts, licences; tenants.status_changed_at (trigger)
- internal/erp: Frappe REST client (Customer upsert by tenant_id,
  Subscription list, Usage Record, Lead) + webhook signing/verification
- internal/erpmock + cmd/erpmock: ERPNext stand-in for staging —
  /api/resource/{doctype} with token auth, request log, /_mock/fire/*
  triggers that mutate docs and deliver signed webhooks
- POST /v1/internal/erp/webhook (HMAC, idempotent by event id):
  sales_order.submit → subscriptions + entitlements (SKU → product/plan,
  add-on SKUs → limits) + active; sales_invoice.overdue → warning /
  restricted / halted by days overdue; payment_entry.submit → active;
  subscription.cancel → halted
- outbox worker (customer.upsert on tenant create, usage.record,
  lead.create on catalog request) with backoff; reconcile job mirrors
  ERP subscriptions and applies the time-based ladder (trial expiry,
  warning→restricted 14 d, restricted→halted 30 d, halted→archived 30 d)
- POST/GET /v1/usage/events (idempotency_key, batch), GET
  /v1/subscriptions, GET /v1/internal/erp/status, POST
  /v1/internal/erp/reconcile
- CLI licences: Ed25519 signer (LICENCE_SIGNING_KEY), POST /v1/licences
  (entitlement-gated, expiry capped by the entitlement), GET
  /.well-known/licence-keys.json, POST /v1/licences/heartbeat (grace
  ladder, renewal past half-life, revoked), revoke/list
- authn Principal carries tenant-context claims (user tokens scope usage
  and licence calls to their own tenant); authz groups usage(.read)
- tests for every path (both stores), erpmock, erp client, licence pkg;
  openapi.yaml + README updated; Dockerfile ships /erpmock

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
2026-09-12 09:45:05 +02:00

126 lines
5.2 KiB
Go

package server_test
import (
"bytes"
"net/http"
"testing"
"time"
"git.breakpilot.com/platform/tenant-registry/internal/store"
)
type licenceOut struct {
ID string `json:"id"`
Licence string `json:"licence"`
Kid string `json:"kid"`
ExpiresAt time.Time `json:"expires_at"`
Status string `json:"status"`
}
type heartbeatOut struct {
Status string `json:"status"`
TenantStatus string `json:"tenant_status"`
Reason string `json:"reason"`
Renewed *licenceOut `json:"renewed"`
}
func TestLicences_issueHeartbeatRevoke(t *testing.T) {
eachStore(t, func(t *testing.T, h *testHarness) {
tn := newTenant(t, h, "lic-co")
// not entitled yet
if resp, raw := h.do("POST", "/v1/licences", map[string]any{"tenant_id": tn.ID, "product": "werkpilot", "device": "laptop-1"}); resp.StatusCode != http.StatusForbidden {
t.Fatalf("not entitled: %d %s", resp.StatusCode, raw)
}
h.do("POST", "/v1/catalog/trial-request", map[string]any{"tenant_id": tn.ID, "product": "werkpilot"})
resp, raw := h.do("POST", "/v1/licences", map[string]any{"tenant_id": tn.ID, "product": "werkpilot", "device": "laptop-1"})
if resp.StatusCode != http.StatusCreated {
t.Fatalf("issue: %d %s", resp.StatusCode, raw)
}
lic := decode[licenceOut](t, raw)
if lic.Licence == "" || lic.Kid == "" || lic.Status != "ok" {
t.Fatalf("licence: %+v", lic)
}
// trial caps expiry at trial end (14 d < 30 d default)
if lic.ExpiresAt.After(time.Now().Add(15 * 24 * time.Hour)) {
t.Fatalf("expiry not capped by trial: %v", lic.ExpiresAt)
}
// jwks published
if resp, raw := h.do("GET", "/.well-known/licence-keys.json", nil); resp.StatusCode != 200 || !bytes.Contains(raw, []byte(`"OKP"`)) || !bytes.Contains(raw, []byte(lic.Kid)) {
t.Fatalf("jwks: %d %s", resp.StatusCode, raw)
}
// heartbeat ok
resp, raw = h.do("POST", "/v1/licences/heartbeat", map[string]any{"licence": lic.Licence})
hb := decode[heartbeatOut](t, raw)
if resp.StatusCode != 200 || hb.Status != "ok" || hb.TenantStatus != "trial" {
t.Fatalf("heartbeat: %d %+v", resp.StatusCode, hb)
}
// past half-life → renewed
h.app.Now = func() time.Time { return time.Now().Add(10 * 24 * time.Hour) }
_, raw = h.do("POST", "/v1/licences/heartbeat", map[string]any{"licence": lic.Licence})
hb = decode[heartbeatOut](t, raw)
if hb.Status != "ok" || hb.Renewed == nil || hb.Renewed.Licence == lic.Licence {
t.Fatalf("renewal: %+v", hb)
}
h.app.Now = nil
// restricted tenant → degraded
h.do("POST", "/v1/tenants/"+tn.ID+"/status", map[string]any{"status": "restricted"})
_, raw = h.do("POST", "/v1/licences/heartbeat", map[string]any{"licence": lic.Licence})
if hb = decode[heartbeatOut](t, raw); hb.Status != "degraded" {
t.Fatalf("restricted: %+v", hb)
}
h.do("POST", "/v1/tenants/"+tn.ID+"/status", map[string]any{"status": "warning"})
_, raw = h.do("POST", "/v1/licences/heartbeat", map[string]any{"licence": lic.Licence})
if hb = decode[heartbeatOut](t, raw); hb.Status != "warning" {
t.Fatalf("warning: %+v", hb)
}
// halted → not entitled: grace ladder on the token's own expiry
h.do("POST", "/v1/tenants/"+tn.ID+"/status", map[string]any{"status": "halted"})
_, raw = h.do("POST", "/v1/licences/heartbeat", map[string]any{"licence": lic.Licence})
if hb = decode[heartbeatOut](t, raw); hb.Status != "warning" || hb.Reason != "tenant_halted" {
t.Fatalf("halted: %+v", hb)
}
h.app.Now = func() time.Time { return time.Now().Add(60 * 24 * time.Hour) }
_, raw = h.do("POST", "/v1/licences/heartbeat", map[string]any{"licence": lic.Licence})
if hb = decode[heartbeatOut](t, raw); hb.Status != "hard_stop" {
t.Fatalf("hard stop: %+v", hb)
}
h.app.Now = nil
// list + revoke
_, raw = h.do("GET", "/v1/licences?tenant_id="+tn.ID, nil)
list := decode[struct {
Items []store.Licence `json:"items"`
}](t, raw).Items
if len(list) != 2 {
t.Fatalf("licences = %d", len(list))
}
if resp, raw := h.do("POST", "/v1/licences/"+lic.ID+"/revoke", nil); resp.StatusCode != 200 || !bytes.Contains(raw, []byte("revoked_at")) {
t.Fatalf("revoke: %d %s", resp.StatusCode, raw)
}
_, raw = h.do("POST", "/v1/licences/heartbeat", map[string]any{"licence": lic.Licence})
if hb = decode[heartbeatOut](t, raw); hb.Status != "revoked" {
t.Fatalf("after revoke: %+v", hb)
}
// garbage + unknown
if resp, _ := h.do("POST", "/v1/licences/heartbeat", map[string]any{"licence": "not.a.jwt"}); resp.StatusCode != 401 {
t.Fatalf("garbage: %d", resp.StatusCode)
}
if resp, _ := h.do("POST", "/v1/licences/00000000-0000-0000-0000-000000000000/revoke", nil); resp.StatusCode != 404 {
t.Fatalf("revoke unknown: %d", resp.StatusCode)
}
if resp, _ := h.do("POST", "/v1/licences", map[string]any{"product": "werkpilot"}); resp.StatusCode != 400 {
t.Fatalf("missing tenant: %d", resp.StatusCode)
}
})
}
func TestLicences_disabled(t *testing.T) {
h := newMemoryHarness(t)
defer h.Close()
h.app.Licences = nil
for _, c := range []struct{ m, p string }{{"GET", "/.well-known/licence-keys.json"}, {"POST", "/v1/licences"}, {"POST", "/v1/licences/heartbeat"}} {
if resp, _ := h.do(c.m, c.p, map[string]any{}); resp.StatusCode != http.StatusServiceUnavailable {
t.Fatalf("%s %s: %d", c.m, c.p, resp.StatusCode)
}
}
}