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) } } }