Files
tenant-registry/internal/server/admin_preview_test.go
T
Sharang ParnerkarandClaude Opus 5.5 c99aa6908d
ci / shared (pull_request) Successful in 12s
ci / test (pull_request) Successful in 25m20s
ci / image (pull_request) Skipped
feat(entitlements): Staff preview grants for products not for sale yet
Lets BreakPilot staff open a product that is still disabled in the
catalogue (Werkmeister) for one tenant for a limited time, e.g. a seeded
staging tenant for portal#86, without a stage/prod divergence:

- Migration 0018: tenant_products.preview and preview_expires_at, kept
  apart from a booking's expires_at.
- Entitlement check: a disabled product is allowed only under an active
  preview (answer carries "preview": true); expiry is enforced at check
  time (reason preview_expired). A leftover plain row of a product off
  sale stays unknown_product.
- A real booking (ERP, trial, catalogue upsert) clears the preview and
  its expiry, so it never cuts off a paid entitlement; a preview over a
  booking is refused (409).
- Usage under a preview is accepted, tagged properties.preview=true and
  kept out of the ERP outbox; claims list a preview product only while
  it is active.
- Staff only, audited: POST /v1/admin/tenants/{id}/preview-grants
  {product, expires_at ≤ 180 d}, DELETE …/preview-grants/{product}; a
  named staff member (X-Staff-Subject) is required.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 01:25:04 +02:00

56 lines
2.2 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)
}
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 := false
for _, e := range evs {
if e.Action == "entitlement.preview_granted" && e.ActorType == "staff" && e.ActorID == "staff-1" {
granted = true
}
}
if !granted {
t.Fatal("grant not audited under the staff member")
}
}