A Monitor subscriber who orders a CRA-Check, SBOM-Erstellung or CVE-Scan keeps Monitor: the entry-offer route adds the order's targets to the subscription's grant as entry_targets until entry_until, instead of refusing with already_booked. recomputeEntitlements carries a running add-on over, and pruefwerkLimits adds it to Monitor's one target while it runs (other plans ignore it). Any other Prüfwerk subscription is still refused; a Monitor subscription without its grant yet is not_provisioned. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
178 lines
7.4 KiB
Go
178 lines
7.4 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.breakpilot.com/platform/tenant-registry/internal/store"
|
|
)
|
|
|
|
func TestPruefwerkLimitsMonitor(t *testing.T) {
|
|
seven := 7
|
|
// A saved cap and stray keys in the config change nothing.
|
|
got := pruefwerkLimits(map[string]interface{}{"skus": []string{"CRA-CSA-CLD-MON"}, "included_targets": 5}, &seven, time.Now())
|
|
if got[limitIncludedTargets] != 1 || got[limitMonthlyPruefungen] != 10 || got[limitPrueftagCap] != 0 || got[limitExtraTargets] != 0 {
|
|
t.Errorf("monitor limits: %v", got)
|
|
}
|
|
if pruefwerkMonitorPlan.capApplies() {
|
|
t.Error("the saved cap applies on Monitor")
|
|
}
|
|
// The no-SKU default is still Standard, with extras billed.
|
|
for _, cfg := range []map[string]interface{}{{}, {"skus": []string{}}, {"source": "trial"}} {
|
|
got := pruefwerkLimits(cfg, nil, time.Now())
|
|
if _, extra := got[limitExtraTargets]; got[limitIncludedTargets] != 5 || got[limitMonthlyPruefungen] != 100 || extra {
|
|
t.Errorf("default %v: %v", cfg, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A Monitor order from the ERP grants the Monitor plan; Standard on top of it
|
|
// wins; an entry grant is taken over by it.
|
|
func TestMonitorFromERP(t *testing.T) {
|
|
f := newTeamFixture(t, portal, "portal-svc:*")
|
|
ctx := context.Background()
|
|
limitsOf := func(id string) map[string]interface{} {
|
|
t.Helper()
|
|
tps, err := f.s.Store.ListTenantProducts(ctx, id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, tp := range tps {
|
|
if tp.Product == pruefwerkProduct {
|
|
if !tp.Enabled || tp.Config["source"] != "erp" || tp.ExpiresAt != nil {
|
|
t.Fatalf("grant: %+v", tp)
|
|
}
|
|
return pruefwerkLimits(tp.Config, nil, time.Now())
|
|
}
|
|
}
|
|
t.Fatal("no Prüfwerk grant")
|
|
return nil
|
|
}
|
|
book := func(tenant *store.Tenant, sku, so string) {
|
|
t.Helper()
|
|
if f.s.productForSKU(ctx, sku) == nil {
|
|
t.Fatalf("%s maps to no product", sku)
|
|
}
|
|
if _, err := f.s.Store.UpsertSubscription(ctx, store.Subscription{TenantID: tenant.ID, Product: pruefwerkProduct, SKU: sku,
|
|
Plan: planForSKU(sku), Seats: 1, Status: store.SubscriptionActive, ErpSalesOrder: so}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cur, _ := f.s.Store.GetTenant(ctx, tenant.ID)
|
|
if _, err := f.s.recomputeEntitlements(ctx, nil, cur, "test"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
buyer, _ := f.s.Store.CreateTenant(ctx, store.TenantCreate{Slug: "k-00095", Name: "K-00095"})
|
|
book(buyer, monitorPlanSKU, "SO-M1")
|
|
if l := limitsOf(buyer.ID); l[limitIncludedTargets] != 1 || l[limitMonthlyPruefungen] != 10 || l[limitPrueftagCap] != 0 || l[limitExtraTargets] != 0 {
|
|
t.Fatalf("monitor from the ERP: %v", l)
|
|
}
|
|
book(buyer, "CRA-CSA-CLD-STD", "SO-M2")
|
|
if l := limitsOf(buyer.ID); l[limitIncludedTargets] != 5 || l[limitMonthlyPruefungen] != 100 {
|
|
t.Fatalf("standard on top of monitor: %v", l)
|
|
}
|
|
if _, extra := limitsOf(buyer.ID)[limitExtraTargets]; extra {
|
|
t.Fatal("standard on top of monitor refuses extras")
|
|
}
|
|
|
|
// Entry grant with three targets, then Monitor: the order's targets go.
|
|
staff := map[string]string{"X-Staff-Subject": "staff-1", "X-Staff-Email": "staff@breakpilot.test"}
|
|
upsell, _ := f.s.Store.CreateTenant(ctx, store.TenantCreate{Slug: "k-00094", Name: "K-00094"})
|
|
month := time.Now().Add(30 * 24 * time.Hour).UTC().Format(time.RFC3339)
|
|
if code, _, raw := f.call(t, "POST", "/v1/admin/tenants/"+upsell.ID+"/entry-offer", `{"included_targets":3,"expires_at":"`+month+`"}`, staff); code != 200 {
|
|
t.Fatalf("entry grant: %d %s", code, raw)
|
|
}
|
|
book(upsell, monitorPlanSKU, "SO-M3")
|
|
if l := limitsOf(upsell.ID); l[limitIncludedTargets] != 1 || l[limitMonthlyPruefungen] != 10 || l[limitExtraTargets] != 0 || l[entryOrderRefKey] != nil {
|
|
t.Fatalf("monitor after the entry plan: %v", l)
|
|
}
|
|
}
|
|
|
|
// An entry offer to a Monitor subscriber adds its targets to Monitor until
|
|
// its end; the subscription's grant keeps source, plan and end.
|
|
func TestEntryOfferWithMonitor(t *testing.T) {
|
|
f := newTeamFixture(t, portal, "portal-svc:*")
|
|
ctx := context.Background()
|
|
staff := map[string]string{"X-Staff-Subject": "staff-1", "X-Staff-Email": "staff@breakpilot.test"}
|
|
month := time.Now().Add(30 * 24 * time.Hour).UTC().Format(time.RFC3339)
|
|
subscribe := func(id, sku, so string) {
|
|
t.Helper()
|
|
if _, err := f.s.Store.UpsertSubscription(ctx, store.Subscription{TenantID: id, Product: pruefwerkProduct, SKU: sku,
|
|
Plan: planForSKU(sku), Seats: 1, Status: store.SubscriptionActive, ErpSalesOrder: so}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
recompute := func(id string) {
|
|
t.Helper()
|
|
cur, _ := f.s.Store.GetTenant(ctx, id)
|
|
if _, err := f.s.recomputeEntitlements(ctx, nil, cur, "test"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
grant := func(id string) store.TenantProduct {
|
|
t.Helper()
|
|
tps, _ := f.s.Store.ListTenantProducts(ctx, id)
|
|
for _, tp := range tps {
|
|
if tp.Product == pruefwerkProduct {
|
|
return tp
|
|
}
|
|
}
|
|
t.Fatal("no Prüfwerk grant")
|
|
return store.TenantProduct{}
|
|
}
|
|
|
|
buyer, _ := f.s.Store.CreateTenant(ctx, store.TenantCreate{Slug: "k-00093", Name: "K-00093"})
|
|
path := "/v1/admin/tenants/" + buyer.ID + "/entry-offer"
|
|
subscribe(buyer.ID, monitorPlanSKU, "SO-E1")
|
|
// Booked but not yet provisioned.
|
|
if code, _, _ := f.call(t, "POST", path, `{"included_targets":2,"expires_at":"`+month+`"}`, staff); code != 409 {
|
|
t.Fatalf("before the grant exists: %d", code)
|
|
}
|
|
recompute(buyer.ID)
|
|
if code, _, raw := f.call(t, "POST", path, `{"included_targets":2,"expires_at":"`+month+`","order_ref":"RE-2026-0050"}`, staff); code != 200 {
|
|
t.Fatalf("entry offer to a Monitor subscriber: %d %s", code, raw)
|
|
}
|
|
tp := grant(buyer.ID)
|
|
if tp.Config["source"] != "erp" || tp.ExpiresAt != nil || pruefwerkPlanOfConfig(tp.Config).Name != "monitor" {
|
|
t.Fatalf("Monitor grant changed: %+v", tp)
|
|
}
|
|
if l := pruefwerkLimits(tp.Config, nil, time.Now()); l[limitIncludedTargets] != 3 || l[limitMonthlyPruefungen] != 10 || l[limitExtraTargets] != 0 {
|
|
t.Fatalf("limits with the offer: %v", l)
|
|
}
|
|
// An ERP event keeps it; once it has ended Monitor is one target again.
|
|
recompute(buyer.ID)
|
|
tp = grant(buyer.ID)
|
|
if l := pruefwerkLimits(tp.Config, nil, time.Now()); l[limitIncludedTargets] != 3 || tp.Config[entryOrderRefKey] != "RE-2026-0050" {
|
|
t.Fatalf("after recompute: %v", tp.Config)
|
|
}
|
|
if l := pruefwerkLimits(tp.Config, nil, time.Now().Add(31*24*time.Hour)); l[limitIncludedTargets] != 1 {
|
|
t.Fatalf("after the offer's end: %v", l)
|
|
}
|
|
tp.Config[entryAddOnUntilKey] = time.Now().Add(-time.Hour).UTC().Format(time.RFC3339)
|
|
if _, err := f.s.Store.UpsertTenantProduct(ctx, tp); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
recompute(buyer.ID)
|
|
if tp = grant(buyer.ID); tp.Config[entryAddOnTargetsKey] != nil || tp.Config[entryAddOnUntilKey] != nil {
|
|
t.Fatalf("ended offer kept: %v", tp.Config)
|
|
}
|
|
// Audited with the Monitor marker.
|
|
evs, _, _ := f.s.Store.ListAudit(ctx, store.AuditFilter{TenantID: buyer.ID, Action: "entitlement.entry_granted"})
|
|
if len(evs) != 1 || evs[0].Metadata["with"] != "monitor" || evs[0].Metadata["order_ref"] != "RE-2026-0050" {
|
|
t.Fatalf("audit: %+v", evs)
|
|
}
|
|
|
|
// Standard next to Monitor: refused as before.
|
|
subscribe(buyer.ID, "CRA-CSA-CLD-STD", "SO-E2")
|
|
if code, _, _ := f.call(t, "POST", path, `{"included_targets":1,"expires_at":"`+month+`"}`, staff); code != 409 {
|
|
t.Fatalf("with Standard booked: %d", code)
|
|
}
|
|
// Other plans ignore entry_targets.
|
|
cfg := map[string]interface{}{"skus": []string{"CRA-CSA-CLD-STD"}, entryAddOnTargetsKey: 4, entryAddOnUntilKey: month}
|
|
if l := pruefwerkLimits(cfg, nil, time.Now()); l[limitIncludedTargets] != 5 {
|
|
t.Fatalf("standard with entry_targets: %v", l)
|
|
}
|
|
}
|