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>
128 lines
5.0 KiB
Go
128 lines
5.0 KiB
Go
package server_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.breakpilot.com/platform/tenant-registry/internal/store"
|
|
)
|
|
|
|
type checkResult struct {
|
|
Allowed bool `json:"allowed"`
|
|
Reason string `json:"reason"`
|
|
Preview bool `json:"preview"`
|
|
}
|
|
|
|
func entCheck(t *testing.T, h *testHarness, tenantID, product string) checkResult {
|
|
t.Helper()
|
|
resp, body := h.do("POST", "/v1/internal/entitlements/check", map[string]any{"tenant_id": tenantID, "product": product})
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Fatalf("check: %d %s", resp.StatusCode, body)
|
|
}
|
|
return decode[checkResult](t, body)
|
|
}
|
|
|
|
// Werkmeister is in the catalogue but not for sale (enabled = false).
|
|
func TestPreviewGrant_opensADisabledProductUntilItExpires(t *testing.T) {
|
|
eachStore(t, func(t *testing.T, h *testHarness) {
|
|
ctx := context.Background()
|
|
tn := newTenant(t, h, "preview-co")
|
|
if r := entCheck(t, h, tn.ID, "werkmeister"); r.Allowed || r.Reason != "unknown_product" {
|
|
t.Fatalf("before the grant: %+v", r)
|
|
}
|
|
if _, err := h.store.SetPreviewGrant(ctx, tn.ID, "werkmeister", time.Now().Add(24*time.Hour)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if r := entCheck(t, h, tn.ID, "werkmeister"); !r.Allowed || !r.Preview {
|
|
t.Fatalf("with the grant: %+v", r)
|
|
}
|
|
// The expiry is enforced when checking, not only when granting.
|
|
if _, err := h.store.SetPreviewGrant(ctx, tn.ID, "werkmeister", time.Now().Add(-time.Minute)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if r := entCheck(t, h, tn.ID, "werkmeister"); r.Allowed || r.Reason != "preview_expired" {
|
|
t.Fatalf("expired grant: %+v", r)
|
|
}
|
|
if err := h.store.RevokePreviewGrant(ctx, tn.ID, "werkmeister"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := h.store.RevokePreviewGrant(ctx, tn.ID, "werkmeister"); !errors.Is(err, store.ErrNotFound) {
|
|
t.Fatalf("second revoke: %v", err)
|
|
}
|
|
if r := entCheck(t, h, tn.ID, "werkmeister"); r.Allowed || r.Reason != "unknown_product" {
|
|
t.Fatalf("after revoke: %+v", r)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestPreviewGrant_realBookingWins(t *testing.T) {
|
|
eachStore(t, func(t *testing.T, h *testHarness) {
|
|
ctx := context.Background()
|
|
tn := newTenant(t, h, "booking-co")
|
|
if _, err := h.store.SetPreviewGrant(ctx, tn.ID, "firmwerk", time.Now().Add(time.Hour)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// A real booking (ERP, trial, catalogue) clears the preview and its
|
|
// expiry, so the preview can never cut off a paid entitlement.
|
|
if _, err := h.store.UpsertTenantProduct(ctx, store.TenantProduct{TenantID: tn.ID, Product: "firmwerk", Enabled: true, Config: map[string]interface{}{"source": "erp"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tps, _ := h.store.ListTenantProducts(ctx, tn.ID)
|
|
for _, tp := range tps {
|
|
if tp.Product == "firmwerk" && (tp.Preview || tp.PreviewExpiresAt != nil) {
|
|
t.Fatalf("booking kept the preview: %+v", tp)
|
|
}
|
|
}
|
|
if r := entCheck(t, h, tn.ID, "firmwerk"); !r.Allowed || r.Preview {
|
|
t.Fatalf("booked: %+v", r)
|
|
}
|
|
if _, err := h.store.SetPreviewGrant(ctx, tn.ID, "firmwerk", time.Now().Add(time.Hour)); !errors.Is(err, store.ErrConflict) {
|
|
t.Fatalf("preview over a booking: %v", err)
|
|
}
|
|
if err := h.store.RevokePreviewGrant(ctx, tn.ID, "firmwerk"); !errors.Is(err, store.ErrNotFound) {
|
|
t.Fatalf("revoking a booking through preview: %v", err)
|
|
}
|
|
})
|
|
}
|
|
|
|
// A plain leftover row of a product taken off sale does not open it.
|
|
func TestDisabledProductWithoutPreviewStaysClosed(t *testing.T) {
|
|
eachStore(t, func(t *testing.T, h *testHarness) {
|
|
tn := newTenant(t, h, "leftover-co")
|
|
if _, err := h.store.UpsertTenantProduct(context.Background(), store.TenantProduct{TenantID: tn.ID, Product: "compliance", Enabled: true, Config: map[string]interface{}{}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if r := entCheck(t, h, tn.ID, "compliance"); r.Allowed || r.Reason != "unknown_product" {
|
|
t.Fatalf("leftover row: %+v", r)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestPreviewUsageIsTaggedAndNotInvoiced(t *testing.T) {
|
|
eachStore(t, func(t *testing.T, h *testHarness) {
|
|
ctx := context.Background()
|
|
tn := newTenant(t, h, "usage-preview-co")
|
|
pendingBefore, _ := h.store.PendingOutbox(ctx)
|
|
ev := map[string]any{"idempotency_key": "wm-1", "tenant_id": tn.ID, "product": "werkmeister", "metric": "run.completed"}
|
|
if resp, body := h.do("POST", "/v1/usage/events", ev); resp.StatusCode != http.StatusBadRequest {
|
|
t.Fatalf("usage for a product not for sale without a grant: %d %s", resp.StatusCode, body)
|
|
}
|
|
if _, err := h.store.SetPreviewGrant(ctx, tn.ID, "werkmeister", time.Now().Add(time.Hour)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if resp, body := h.do("POST", "/v1/usage/events", ev); resp.StatusCode != http.StatusAccepted {
|
|
t.Fatalf("usage under preview: %d %s", resp.StatusCode, body)
|
|
}
|
|
evs, _ := h.store.ListUsageEvents(ctx, store.UsageFilter{TenantID: tn.ID})
|
|
if len(evs) != 1 || evs[0].Properties["preview"] != true {
|
|
t.Fatalf("usage not tagged: %+v", evs)
|
|
}
|
|
if pending, _ := h.store.PendingOutbox(ctx); pending != pendingBefore {
|
|
t.Fatalf("preview usage reached the ERP outbox: %d → %d", pendingBefore, pending)
|
|
}
|
|
})
|
|
}
|