feat(entitlements): audit a preview overriding a disabled booking
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c99aa6908d
commit
a4136df554
2 files changed
+30
-6
No files matched your search
@@ -52,6 +52,16 @@ func (s *Server) adminGrantPreview(w http.ResponseWriter, r *http.Request) {
|
||||
writeError(w, http.StatusBadRequest, "unknown_product", "product is not in the catalogue")
|
||||
return
|
||||
}
|
||||
meta := map[string]interface{}{"preview_expires_at": in.ExpiresAt.UTC().Format(time.RFC3339)}
|
||||
// A booking that was later disabled (e.g. a cancelled subscription) is
|
||||
// overridden by the grant; the audit trail says so.
|
||||
if tps, err := s.Store.ListTenantProducts(ctx, t.ID); err == nil {
|
||||
for _, old := range tps {
|
||||
if old.Product == in.Product && !old.Preview && !old.Enabled {
|
||||
meta["overrides_disabled_booking"] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
tp, err := s.Store.SetPreviewGrant(ctx, t.ID, in.Product, in.ExpiresAt.UTC())
|
||||
if errors.Is(err, store.ErrConflict) {
|
||||
writeError(w, http.StatusConflict, "already_booked", "the tenant has booked this product; a preview grant would not change anything")
|
||||
@@ -66,7 +76,7 @@ func (s *Server) adminGrantPreview(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
s.emitAudit(ctx, r, store.AuditEvent{
|
||||
TenantID: t.ID, Action: "entitlement.preview_granted", TargetID: in.Product, TargetType: "product", TargetName: t.Slug,
|
||||
Product: in.Product, Metadata: map[string]interface{}{"preview_expires_at": in.ExpiresAt.UTC().Format(time.RFC3339)},
|
||||
Product: in.Product, Metadata: meta,
|
||||
})
|
||||
writeJSON(w, http.StatusCreated, tp)
|
||||
}
|
||||
|
||||
@@ -36,6 +36,13 @@ func TestAdminPreviewGrantRoutes(t *testing.T) {
|
||||
if code, _, _ := f.call(t, "POST", path, `{"product":"pruefwerk","expires_at":"`+soon+`"}`, staff); code != http.StatusConflict {
|
||||
t.Fatalf("preview over a booking: %d", code)
|
||||
}
|
||||
// A booking that was later disabled may be overridden, and the audit says so.
|
||||
if _, err := f.s.Store.UpsertTenantProduct(context.Background(), store.TenantProduct{TenantID: f.tenant.ID, Product: "firmwerk", Enabled: false}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if code, _, raw := f.call(t, "POST", path, `{"product":"firmwerk","expires_at":"`+soon+`"}`, staff); code != http.StatusCreated {
|
||||
t.Fatalf("preview over a disabled booking: %d %s", code, raw)
|
||||
}
|
||||
if code, _, _ := f.call(t, "DELETE", path+"/werkmeister", "", staff); code != http.StatusNoContent {
|
||||
t.Fatalf("revoke: %d", code)
|
||||
}
|
||||
@@ -43,13 +50,20 @@ func TestAdminPreviewGrantRoutes(t *testing.T) {
|
||||
t.Fatalf("revoke again: %d", code)
|
||||
}
|
||||
evs, _, _ := f.s.Store.ListAudit(context.Background(), store.AuditFilter{TenantID: f.tenant.ID})
|
||||
granted := false
|
||||
granted, overrode := false, false
|
||||
for _, e := range evs {
|
||||
if e.Action == "entitlement.preview_granted" && e.ActorType == "staff" && e.ActorID == "staff-1" {
|
||||
granted = true
|
||||
if e.Action != "entitlement.preview_granted" || e.ActorType != "staff" || e.ActorID != "staff-1" {
|
||||
continue
|
||||
}
|
||||
granted = true
|
||||
if e.Product == "firmwerk" && e.Metadata["overrides_disabled_booking"] == true {
|
||||
overrode = true
|
||||
}
|
||||
if e.Product == "werkmeister" && e.Metadata["overrides_disabled_booking"] != nil {
|
||||
t.Error("a plain grant was audited as overriding a booking")
|
||||
}
|
||||
}
|
||||
if !granted {
|
||||
t.Fatal("grant not audited under the staff member")
|
||||
if !granted || !overrode {
|
||||
t.Fatalf("grant audit: granted=%v overrides_disabled_booking=%v", granted, overrode)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user