A workspace can leave Werkpilot's self-update on ("auto", the default),
turn it off, or pin a release version.
- GET/PUT /v1/tenants/{id}/products/{product}/updates with
{"policy": "auto" | "off" | "<semver>"}: the "updates" key of the
product's settings document (0025, no migration); IT_ADMIN/OWNER or
services; audited as product_settings.changed; werkpilot only
- "updates" in licence grants (POST /v1/licences, /v1/licences/ci,
heartbeat renewals) and at the top of the heartbeat answer, read fresh
each time, "auto" when unset
Refs platform/werkpilot#89
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q9inRayW7ztyk1uAtwMJPR
181 lines
7.2 KiB
Go
181 lines
7.2 KiB
Go
package server_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.breakpilot.com/platform/tenant-registry/internal/store"
|
|
)
|
|
|
|
type grantUpdates struct {
|
|
Licence string `json:"licence"`
|
|
Updates *string `json:"updates"`
|
|
}
|
|
|
|
type heartbeatUpdates struct {
|
|
Status string `json:"status"`
|
|
Updates *string `json:"updates"`
|
|
Renewed *grantUpdates `json:"renewed"`
|
|
}
|
|
|
|
func updatesPath(h *testHarness, product string) string {
|
|
return "/v1/tenants/" + h.tenant.ID + "/products/" + product + "/updates"
|
|
}
|
|
|
|
func putPolicy(t *testing.T, h *testHarness, policy string) {
|
|
t.Helper()
|
|
resp, raw := h.do("PUT", updatesPath(h, "werkpilot"), map[string]any{"policy": policy})
|
|
if resp.StatusCode != http.StatusOK || !bytes.Contains(raw, []byte(`"policy":"`+policy+`"`)) {
|
|
t.Fatalf("put %s: %d %s", policy, resp.StatusCode, raw)
|
|
}
|
|
}
|
|
|
|
// Grants and heartbeats carry the workspace's current update policy for the
|
|
// licence's product: "auto" until an administrator changes it.
|
|
func TestLicences_updatePolicyFollowsTheSetting(t *testing.T) {
|
|
eachStore(t, func(t *testing.T, h *testHarness) {
|
|
fake := newFakeLiteLLM(t)
|
|
h.app.LLMKeys = fake.client()
|
|
entitle(t, h, "werkpilot", true)
|
|
entitle(t, h, "firmwerk", true)
|
|
issue := func() string {
|
|
t.Helper()
|
|
resp, raw := h.do("POST", "/v1/licences", map[string]any{"tenant_id": h.tenant.ID, "product": "werkpilot", "device": "laptop"})
|
|
g := decode[grantUpdates](t, raw)
|
|
if resp.StatusCode != http.StatusCreated || g.Updates == nil {
|
|
t.Fatalf("issue: %d %s", resp.StatusCode, raw)
|
|
}
|
|
return *g.Updates
|
|
}
|
|
resp, raw := h.do("POST", "/v1/licences", map[string]any{"tenant_id": h.tenant.ID, "product": "werkpilot", "device": "laptop"})
|
|
first := decode[grantUpdates](t, raw)
|
|
if resp.StatusCode != http.StatusCreated || first.Updates == nil || *first.Updates != "auto" {
|
|
t.Fatalf("default grant: %d %s", resp.StatusCode, raw)
|
|
}
|
|
beat := func() heartbeatUpdates {
|
|
t.Helper()
|
|
_, raw := h.do("POST", "/v1/licences/heartbeat", map[string]any{"licence": first.Licence})
|
|
hb := decode[heartbeatUpdates](t, raw)
|
|
if hb.Updates == nil {
|
|
t.Fatalf("heartbeat without updates: %s", raw)
|
|
}
|
|
return hb
|
|
}
|
|
key := ciKey(t, h, "ci", "werkpilot:licence", "firmwerk:licence")
|
|
ci := func(product, device string) string {
|
|
t.Helper()
|
|
code, _, body := postCI(t, h, key, product, device)
|
|
g := decode[grantUpdates](t, []byte(body))
|
|
if code != http.StatusCreated && code != http.StatusOK || g.Updates == nil {
|
|
t.Fatalf("ci %s: %d %s", product, code, body)
|
|
}
|
|
return *g.Updates
|
|
}
|
|
if hb := beat(); *hb.Updates != "auto" {
|
|
t.Fatalf("default heartbeat: %+v", hb)
|
|
}
|
|
if got := ci("werkpilot", "r-1"); got != "auto" {
|
|
t.Fatalf("default CI grant: %q", got)
|
|
}
|
|
resp, raw = h.do("GET", updatesPath(h, "werkpilot"), nil)
|
|
if resp.StatusCode != http.StatusOK || !bytes.Contains(raw, []byte(`"policy":"auto"`)) {
|
|
t.Fatalf("default setting: %d %s", resp.StatusCode, raw)
|
|
}
|
|
|
|
putPolicy(t, h, "0.4.0-rc2")
|
|
putPolicy(t, h, "0.4.0-rc2") // unchanged: no write, no audit
|
|
if hb := beat(); *hb.Updates != "0.4.0-rc2" {
|
|
t.Fatalf("heartbeat after pinning: %+v", hb)
|
|
}
|
|
if got := issue(); got != "0.4.0-rc2" {
|
|
t.Fatalf("grant after pinning: %q", got)
|
|
}
|
|
if got, other := ci("werkpilot", "r-2"), ci("firmwerk", "r-1"); got != "0.4.0-rc2" || other != "auto" {
|
|
t.Fatalf("CI grants: werkpilot %q, firmwerk (no setting) %q", got, other)
|
|
}
|
|
|
|
putPolicy(t, h, "off")
|
|
// Past half-life of the 30-day licence: renewed, with the policy.
|
|
h.app.Now = func() time.Time { return time.Now().Add(20 * 24 * time.Hour) }
|
|
hb := beat()
|
|
h.app.Now = nil
|
|
if hb.Renewed == nil || hb.Renewed.Updates == nil || *hb.Renewed.Updates != "off" || *hb.Updates != "off" {
|
|
t.Fatalf("renewal: %+v", hb)
|
|
}
|
|
|
|
putPolicy(t, h, "auto")
|
|
if got := issue(); got != "auto" {
|
|
t.Fatalf("grant after auto: %q", got)
|
|
}
|
|
events, _, err := h.store.ListAudit(context.Background(), store.AuditFilter{TenantID: h.tenant.ID, Action: "product_settings.changed"})
|
|
if err != nil || len(events) != 3 {
|
|
t.Fatalf("audit: %v %+v", err, events)
|
|
}
|
|
want := [][2]string{{"auto", "0.4.0-rc2"}, {"0.4.0-rc2", "off"}, {"off", "auto"}}
|
|
for _, ev := range events {
|
|
found := false
|
|
for _, w := range want {
|
|
if ev.Metadata["old"] == w[0] && ev.Metadata["new"] == w[1] {
|
|
found = true
|
|
}
|
|
}
|
|
if !found || ev.TargetID != "werkpilot" || ev.TargetType != "product_settings" || ev.Product != "werkpilot" || ev.Metadata["setting"] != "updates" {
|
|
t.Errorf("audit event: %+v", ev)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestUpdatePolicy_isChecked(t *testing.T) {
|
|
eachStore(t, func(t *testing.T, h *testHarness) {
|
|
for _, body := range []string{
|
|
`{}`, `{"policy":""}`, `{"policy":null}`, `{"policy":1}`, `{"policy":true}`, `{"policy":"auto","other":1}`, `[]`, `nope`,
|
|
`{"policy":"on"}`, `{"policy":"Auto"}`, `{"policy":"latest"}`, `{"policy":"stable"}`,
|
|
`{"policy":"v0.3.1"}`, `{"policy":"0.3"}`, `{"policy":"0.3.1.2"}`, `{"policy":"01.3.1"}`, `{"policy":"0.3.01"}`,
|
|
`{"policy":"0.3.1-"}`, `{"policy":"0.3.1-01"}`, `{"policy":"0.3.1+"}`, `{"policy":" 0.3.1"}`, `{"policy":"0.3.1\n"}`,
|
|
`{"policy":"^0.3.1"}`, `{"policy":"~0.3"}`, `{"policy":">=0.3.1"}`, `{"policy":"0.3.x"}`, `{"policy":"0.3.*"}`,
|
|
`{"policy":"0.3.1 - 0.4.0"}`, `{"policy":"0.3.1 || 0.4.0"}`,
|
|
`{"policy":"0.3.1-` + string(bytes.Repeat([]byte("a"), 60)) + `"}`,
|
|
} {
|
|
if resp, raw := h.doRaw("PUT", updatesPath(h, "werkpilot"), body); resp.StatusCode != http.StatusBadRequest {
|
|
t.Errorf("%s: %d %s", body, resp.StatusCode, raw)
|
|
}
|
|
}
|
|
for _, policy := range []string{"0.3.1", "0.4.0-rc2", "1.0.0-alpha.1", "1.0.0+build.5", "1.0.0-rc.1+g1a2b3c", "10.20.30"} {
|
|
putPolicy(t, h, policy)
|
|
}
|
|
// Only products that update themselves take the policy.
|
|
for _, product := range []string{"pruefwerk", "firmwerk", "nope"} {
|
|
if resp, _ := h.do("PUT", updatesPath(h, product), map[string]any{"policy": "off"}); resp.StatusCode != http.StatusNotFound {
|
|
t.Errorf("put %s: %d", product, resp.StatusCode)
|
|
}
|
|
if resp, _ := h.do("GET", updatesPath(h, product), nil); resp.StatusCode != http.StatusNotFound {
|
|
t.Errorf("get %s: %d", product, resp.StatusCode)
|
|
}
|
|
}
|
|
if resp, _ := h.do("GET", "/v1/tenants/00000000-0000-0000-0000-00000000dead/products/werkpilot/updates", nil); resp.StatusCode != http.StatusNotFound {
|
|
t.Errorf("unknown tenant: %d", resp.StatusCode)
|
|
}
|
|
// Other keys of the product's settings document survive.
|
|
if _, err := h.store.PutTenantProductSettings(context.Background(), store.TenantProductSettings{
|
|
TenantID: h.tenant.ID, Product: "werkpilot", Settings: json.RawMessage(`{"keep":42,"telemetry":true}`), UpdatedAt: time.Now(),
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
resp, raw := h.do("GET", updatesPath(h, "werkpilot"), nil)
|
|
if resp.StatusCode != http.StatusOK || !bytes.Contains(raw, []byte(`"policy":"auto"`)) {
|
|
t.Fatalf("document without the key: %d %s", resp.StatusCode, raw)
|
|
}
|
|
putPolicy(t, h, "off")
|
|
row, err := h.store.GetTenantProductSettings(context.Background(), h.tenant.ID, "werkpilot")
|
|
var doc map[string]any
|
|
if err != nil || json.Unmarshal(row.Settings, &doc) != nil || doc["keep"] != float64(42) || doc["telemetry"] != true || doc["updates"] != "off" {
|
|
t.Fatalf("settings document: %v %s", err, row.Settings)
|
|
}
|
|
})
|
|
}
|