Entry offers (CRA-Check, SBOM-Erstellung, CVE-Scan) are paid at booking
outside the ERP. Staff grant them with POST /v1/admin/tenants/{id}/entry-offer
(named staff member, audited, cloud only): Prüfwerk with source "entry",
SKU CRA-CSA-CLD-EIN, the order's included targets (1..10) and an end date
that is sent again at delivery (90 days after it).
- Plan "einstieg": no monthly quota and a Prüftag cap of 0 whatever the
workspace saved, so only support scans (never counted) and change scans
of the included targets run. Any subscription plan SKU wins over it; no
plan SKU at all stays Standard, so trials and previews are unchanged.
- cap_applies is false on the entry plan, as on Enterprise.
- A trial workspace becomes active (no trial banner).
- recomputeEntitlements does not halt a workspace while an entry grant
runs; once it has ended the old rule applies.
- Refused for a workspace that booked Prüfwerk (subscription, ERP or
activation grant) or is neither trial nor active.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
125 lines
5.5 KiB
Go
125 lines
5.5 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.breakpilot.com/platform/tenant-registry/internal/store"
|
|
)
|
|
|
|
func TestAdminEntryOffer(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"}
|
|
buyer, err := f.s.Store.CreateTenant(ctx, store.TenantCreate{Slug: "k-00099", Name: "K-00099"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
path := "/v1/admin/tenants/" + buyer.ID + "/entry-offer"
|
|
at := func(d time.Duration) string { return time.Now().Add(d).UTC().Format(time.RFC3339) }
|
|
month := at(30 * 24 * time.Hour)
|
|
|
|
if code, _, _ := f.call(t, "POST", path, `{"included_targets":1,"expires_at":"`+month+`"}`, nil); code != http.StatusForbidden {
|
|
t.Fatalf("without a named staff member: %d", code)
|
|
}
|
|
for _, body := range []string{
|
|
`{"expires_at":"` + month + `"}`,
|
|
`{"included_targets":11,"expires_at":"` + month + `"}`,
|
|
`{"included_targets":1}`,
|
|
`{"included_targets":1,"expires_at":"` + at(-time.Hour) + `"}`,
|
|
`{"included_targets":1,"expires_at":"` + at(201*24*time.Hour) + `"}`,
|
|
`{"included_targets":1,"expires_at":"` + month + `","order_ref":"Max Mustermann"}`,
|
|
} {
|
|
if code, _, raw := f.call(t, "POST", path, body, staff); code != http.StatusBadRequest {
|
|
t.Errorf("%s: %d %s", body, code, raw)
|
|
}
|
|
}
|
|
|
|
// A trial workspace gets the plan and becomes active.
|
|
code, _, raw := f.call(t, "POST", path, `{"included_targets":2,"expires_at":"`+month+`","order_ref":"RE-2026-0042"}`, staff)
|
|
if code != http.StatusOK || !strings.Contains(raw, `"CRA-CSA-CLD-EIN"`) || !strings.Contains(raw, `"source":"entry"`) {
|
|
t.Fatalf("grant: %d %s", code, raw)
|
|
}
|
|
if got, _ := f.s.Store.GetTenant(ctx, buyer.ID); got.Status != store.StatusActive {
|
|
t.Fatalf("status after the grant: %s", got.Status)
|
|
}
|
|
plan, err := f.s.pruefwerkPlanOf(ctx, buyer.ID)
|
|
if err != nil || plan.Name != "einstieg" || plan.IncludedTargets != 2 {
|
|
t.Fatalf("plan: %+v %v", plan, err)
|
|
}
|
|
|
|
// Sent again at delivery: the new end replaces the old one, audited with both.
|
|
later := at(95 * 24 * time.Hour)
|
|
if code, _, raw := f.call(t, "POST", path, `{"included_targets":2,"expires_at":"`+later+`","order_ref":"RE-2026-0042"}`, staff); code != http.StatusOK || !strings.Contains(raw, later[:10]) {
|
|
t.Fatalf("reset end: %d %s", code, raw)
|
|
}
|
|
evs, _, _ := f.s.Store.ListAudit(ctx, store.AuditFilter{TenantID: buyer.ID, Action: "entitlement.entry_granted"})
|
|
if len(evs) != 2 || evs[0].ActorType != "staff" || evs[0].ActorID != "staff-1" {
|
|
t.Fatalf("audit: %+v", evs)
|
|
}
|
|
withPrevious := 0
|
|
for _, e := range evs {
|
|
if e.Metadata["order_ref"] != "RE-2026-0042" {
|
|
t.Errorf("audit without the order reference: %+v", e.Metadata)
|
|
}
|
|
if e.Metadata["previous_expires_at"] != nil {
|
|
withPrevious++
|
|
}
|
|
}
|
|
if withPrevious != 1 {
|
|
t.Errorf("previous end audited %d times, want once", withPrevious)
|
|
}
|
|
|
|
// An ERP event without subscriptions does not halt it while the grant runs.
|
|
cur, _ := f.s.Store.GetTenant(ctx, buyer.ID)
|
|
if got, err := f.s.recomputeEntitlements(ctx, nil, cur, "test"); err != nil || got.Status != store.StatusActive {
|
|
t.Fatalf("recompute with a running entry grant: %v %v", got, err)
|
|
}
|
|
// Once it has ended, the old rule applies.
|
|
tps, _ := f.s.Store.ListTenantProducts(ctx, buyer.ID)
|
|
for _, tp := range tps {
|
|
if tp.Product == pruefwerkProduct {
|
|
past := time.Now().Add(-time.Hour)
|
|
tp.ExpiresAt = &past
|
|
if _, err := f.s.Store.UpsertTenantProduct(ctx, tp); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
if got, err := f.s.recomputeEntitlements(ctx, nil, cur, "test"); err != nil || got.Status != store.StatusHalted {
|
|
t.Fatalf("recompute after the entry grant ended: %v %v", got, err)
|
|
}
|
|
// Halted is neither trial nor active.
|
|
if code, _, _ := f.call(t, "POST", path, `{"included_targets":1,"expires_at":"`+month+`"}`, staff); code != http.StatusConflict {
|
|
t.Fatalf("grant to a halted workspace: %d", code)
|
|
}
|
|
|
|
// acme has booked Prüfwerk: the booking supersedes the entry plan.
|
|
acme := "/v1/admin/tenants/" + f.tenant.ID + "/entry-offer"
|
|
if code, _, _ := f.call(t, "POST", acme, `{"included_targets":1,"expires_at":"`+month+`"}`, staff); code != http.StatusConflict {
|
|
t.Fatalf("grant over a booked grant: %d", code)
|
|
}
|
|
// So does a subscription, with or without its grant.
|
|
other, _ := f.s.Store.CreateTenant(ctx, store.TenantCreate{Slug: "k-00098", Name: "K-00098"})
|
|
if _, err := f.s.Store.UpsertSubscription(ctx, store.Subscription{TenantID: other.ID, Product: pruefwerkProduct, SKU: "CRA-CSA-CLD-STD",
|
|
Plan: "standard", Seats: 1, Status: store.SubscriptionActive, ErpSalesOrder: "SO-1"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if code, _, _ := f.call(t, "POST", "/v1/admin/tenants/"+other.ID+"/entry-offer", `{"included_targets":1,"expires_at":"`+month+`"}`, staff); code != http.StatusConflict {
|
|
t.Fatalf("grant over a subscription: %d", code)
|
|
}
|
|
// A trial grant of Prüfwerk is replaced.
|
|
trial, _ := f.s.Store.CreateTenant(ctx, store.TenantCreate{Slug: "k-00097", Name: "K-00097"})
|
|
end := time.Now().Add(14 * 24 * time.Hour)
|
|
if _, err := f.s.Store.UpsertTenantProduct(ctx, store.TenantProduct{TenantID: trial.ID, Product: pruefwerkProduct, Enabled: true,
|
|
Config: map[string]interface{}{"source": "trial"}, ExpiresAt: &end}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if code, _, raw := f.call(t, "POST", "/v1/admin/tenants/"+trial.ID+"/entry-offer", `{"included_targets":1,"expires_at":"`+month+`"}`, staff); code != http.StatusOK {
|
|
t.Fatalf("grant over a trial grant: %d %s", code, raw)
|
|
}
|
|
}
|