Files
tenant-registry/internal/server/admin_preview_test.go
T
Sharang ParnerkarandClaude Opus 5.5 a4136df554
ci / shared (pull_request) Successful in 14s
ci / test (pull_request) Successful in 25m25s
ci / image (pull_request) Skipped
feat(entitlements): audit a preview overriding a disabled booking
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 10:16:15 +02:00

70 lines
3.0 KiB
Go

package server
import (
"context"
"net/http"
"strings"
"testing"
"time"
"git.breakpilot.com/platform/tenant-registry/internal/store"
)
func TestAdminPreviewGrantRoutes(t *testing.T) {
f := newTeamFixture(t, portal, "portal-svc:*")
staff := map[string]string{"X-Staff-Subject": "staff-1", "X-Staff-Email": "staff@breakpilot.test"}
path := "/v1/admin/tenants/" + f.tenant.ID + "/preview-grants"
soon := time.Now().Add(30 * 24 * time.Hour).UTC().Format(time.RFC3339)
if code, _, _ := f.call(t, "POST", path, `{"product":"werkmeister","expires_at":"`+soon+`"}`, nil); code != http.StatusForbidden {
t.Fatalf("without a named staff member: %d", code)
}
for _, body := range []string{
`{"product":"werkmeister"}`,
`{"product":"werkmeister","expires_at":"` + time.Now().Add(-time.Hour).UTC().Format(time.RFC3339) + `"}`,
`{"product":"werkmeister","expires_at":"` + time.Now().Add(200*24*time.Hour).UTC().Format(time.RFC3339) + `"}`,
`{"product":"nope","expires_at":"` + soon + `"}`,
} {
if code, _, raw := f.call(t, "POST", path, body, staff); code != http.StatusBadRequest {
t.Errorf("%s: %d %s", body, code, raw)
}
}
if code, _, raw := f.call(t, "POST", path, `{"product":"werkmeister","expires_at":"`+soon+`"}`, staff); code != http.StatusCreated || !strings.Contains(raw, `"preview":true`) {
t.Fatalf("grant: %d %s", code, raw)
}
// acme has really booked Prüfwerk: a preview would change nothing.
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)
}
if code, _, _ := f.call(t, "DELETE", path+"/werkmeister", "", staff); code != http.StatusNotFound {
t.Fatalf("revoke again: %d", code)
}
evs, _, _ := f.s.Store.ListAudit(context.Background(), store.AuditFilter{TenantID: f.tenant.ID})
granted, overrode := false, false
for _, e := range evs {
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 || !overrode {
t.Fatalf("grant audit: granted=%v overrides_disabled_booking=%v", granted, overrode)
}
}