Files
tenant-registry/internal/server/update_policy_scope_test.go
Sharang ParnerkarandClaude Opus 5.5 361e748f06
ci / shared (pull_request) Successful in 11s
ci / test (pull_request) Successful in 6m39s
ci / image (pull_request) Skipped
feat(updates): per-tenant self-update policy for Werkpilot
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
2026-11-23 16:38:36 +01:00

61 lines
2.6 KiB
Go

package server
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"git.breakpilot.com/platform/tenant-registry/internal/authn"
"git.breakpilot.com/platform/tenant-registry/internal/store"
)
// Only a workspace administrator reads or sets the update policy, and only
// in its own workspace; another workspace's administrator gets a 404.
func TestUpdatePolicy_adminsOnly(t *testing.T) {
acme := "00000000-0000-0000-0000-000000000001"
for _, c := range []struct {
name string
tenant string
roles []string
want int
}{
{"plain user", acme, []string{"USER"}, http.StatusForbidden},
{"cxo", acme, []string{"CXO"}, http.StatusForbidden},
{"finance", acme, []string{"FINANCE"}, http.StatusForbidden},
{"another workspace's admin", "11111111-1111-1111-1111-111111111111", []string{"IT_ADMIN"}, http.StatusNotFound},
{"it admin", acme, []string{"IT_ADMIN"}, http.StatusOK},
{"owner", acme, []string{"OWNER"}, http.StatusOK},
} {
for _, rt := range []struct{ method, body string }{{http.MethodGet, ""}, {http.MethodPut, `{"policy":"off"}`}} {
p := &authn.Principal{Subject: "u-1", ClientID: "portal", TenantID: c.tenant, Email: "ada@acme.test", OrgRoles: c.roles}
s, h, tenant := scopedServer(t, p)
rr := httptest.NewRecorder()
h.ServeHTTP(rr, httptest.NewRequest(rt.method, "/v1/tenants/"+tenant.ID+"/products/werkpilot/updates", strings.NewReader(rt.body)))
if rr.Code != c.want {
t.Errorf("%s %s: %d %s", c.name, rt.method, rr.Code, rr.Body.String())
}
want := "auto"
if rt.method == http.MethodPut && c.want == http.StatusOK {
want = "off"
}
if got := s.updatePolicy(context.Background(), tenant.ID, "werkpilot"); got != want {
t.Errorf("%s %s: policy now %q", c.name, rt.method, got)
}
}
}
// The change is attributed to the administrator who made it.
p := &authn.Principal{Subject: "u-1", ClientID: "portal", TenantID: acme, Email: "ada@acme.test", OrgRoles: []string{"IT_ADMIN"}}
s, h, tenant := scopedServer(t, p)
rr := httptest.NewRecorder()
h.ServeHTTP(rr, httptest.NewRequest(http.MethodPut, "/v1/tenants/"+tenant.ID+"/products/werkpilot/updates", strings.NewReader(`{"policy":"0.3.1"}`)))
if rr.Code != http.StatusOK || !strings.Contains(rr.Body.String(), `"updated_by":"ada@acme.test"`) {
t.Fatalf("put: %d %s", rr.Code, rr.Body.String())
}
events, _, _ := s.Store.ListAudit(context.Background(), store.AuditFilter{TenantID: tenant.ID, Action: "product_settings.changed"})
if len(events) != 1 || events[0].ActorID != "u-1" || events[0].ActorType != "user" || events[0].Metadata["new"] != "0.3.1" {
t.Fatalf("audit: %+v", events)
}
}