From 310c438aea2ef6ab6f2f69baccfd7fcd0e24bcee Mon Sep 17 00:00:00 2001 From: Sharang Parnerkar Date: Wed, 14 Oct 2026 11:09:29 +0000 Subject: [PATCH] fix(tenants): Setting the current status again is a no-op (#75) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On stage, staff picked "Apply" (set status) with `trial` selected on a trial tenant, instead of "Extend". The registry wrote an update, a `tenant.status_changed` audit entry (`trial` → `trial`) and a Keycloak sync. The audit log showed an entry, but nothing had changed. **Change:** `POST /v1/tenants/{id}/status` with the tenant's current status answers 200 with the unchanged tenant: no write, no audit entry, no Keycloak sync. The call stays idempotent for scripts. Other transitions are unchanged. **Test:** `TestSetStatus_unchangedIsNoOp` (memory and Postgres) checks 200 and that no audit entry is added. It fails without the fix ("audit entries: 1, want 0"). `go vet` and `go test -short ./...` pass locally; the full Postgres suite runs in CI. Companion: the portal staff page greys out "Apply" until a different status is chosen (separate portal PR). 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB Co-authored-by: Sharang Parnerkar <30073382+mighty840@users.noreply.github.com> Reviewed-on: https://git.breakpilot.com/platform/tenant-registry/pulls/75 --- internal/server/entitlements_test.go | 13 +++++++++++++ internal/server/tenants.go | 6 ++++++ 2 files changed, 19 insertions(+) diff --git a/internal/server/entitlements_test.go b/internal/server/entitlements_test.go index 5dadecf..7be3610 100644 --- a/internal/server/entitlements_test.go +++ b/internal/server/entitlements_test.go @@ -97,6 +97,19 @@ func TestSetStatus_rejectsBogus(t *testing.T) { }) } +func TestSetStatus_unchangedIsNoOp(t *testing.T) { + eachStore(t, func(t *testing.T, h *testHarness) { + before := auditCount(t, h, "action=tenant.status_changed&tenant_id="+h.tenant.ID) + resp, raw := h.do("POST", "/v1/tenants/"+h.tenant.ID+"/status", map[string]any{"status": h.tenant.Status, "source": "test"}) + if resp.StatusCode != http.StatusOK { + t.Fatalf("same status: %d %s", resp.StatusCode, raw) + } + if n := auditCount(t, h, "action=tenant.status_changed&tenant_id="+h.tenant.ID); n != before { + t.Fatalf("audit entries: %d, want %d", n, before) + } + }) +} + func TestListTenants_pagination(t *testing.T) { eachStore(t, func(t *testing.T, h *testHarness) { for _, s := range []string{"p-one", "p-two", "p-three"} { diff --git a/internal/server/tenants.go b/internal/server/tenants.go index 6bb4d58..be1e136 100644 --- a/internal/server/tenants.go +++ b/internal/server/tenants.go @@ -390,6 +390,12 @@ func (s *Server) setTenantStatus(w http.ResponseWriter, r *http.Request) { writeError(w, http.StatusInternalServerError, "internal", err.Error()) return } + // Setting the status a tenant already has changes nothing: no write, no + // audit entry, no Keycloak sync. Repeating a call stays harmless. + if before.Status == in.Status { + writeJSON(w, http.StatusOK, before) + return + } t, err := s.Store.UpdateTenant(ctx, before.ID, store.TenantUpdate{Status: ptrStr(in.Status)}) if err != nil { if mapStoreError(w, err) {