Files
sharangandClaude Opus 5.5 17bffab165
ci / shared (push) Successful in 12s
ci / test (push) Successful in 7m56s
ci / image (push) Successful in 53s
feat(pruefwerk): entry plan for one-off orders, granted by staff (#150)
Plan "einstieg" (SKU CRA-CSA-CLD-EIN): no monthly quota, Prüftag cap always 0,
included targets 1..10 per order. Staff grant it with
POST /v1/admin/tenants/{id}/entry-offer (named staff, audited, cloud only);
a trial workspace becomes active, a Prüfwerk booking is refused, and an ERP
event does not halt the workspace while the grant runs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 11:43:57 +00:00

168 lines
7.4 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)
}
}
// An entry workspace keeps its trial end on record; once that passes it is
// not restricted (only status trial expires), and a later subscription takes
// Prüfwerk over: plan from the ERP, no end date.
func TestEntryOfferLater(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, _ := f.s.Store.CreateTenant(ctx, store.TenantCreate{Slug: "k-00096", Name: "K-00096"})
month := time.Now().Add(30 * 24 * time.Hour).UTC().Format(time.RFC3339)
if code, _, raw := f.call(t, "POST", "/v1/admin/tenants/"+buyer.ID+"/entry-offer", `{"included_targets":1,"expires_at":"`+month+`"}`, staff); code != http.StatusOK {
t.Fatalf("grant: %d %s", code, raw)
}
cur, _ := f.s.Store.GetTenant(ctx, buyer.ID)
if cur.TrialEndsAt == nil {
t.Fatal("expected the trial end to stay on record")
}
if next, why := f.s.escalation(cur, cur.TrialEndsAt.Add(24*time.Hour)); next != "" {
t.Fatalf("an active entry workspace escalated after its trial end: %s (%s)", next, why)
}
if _, err := f.s.Store.UpsertSubscription(ctx, store.Subscription{TenantID: buyer.ID, Product: pruefwerkProduct, SKU: "CRA-CSA-CLD-STD",
Plan: "standard", Seats: 1, Status: store.SubscriptionActive, ErpSalesOrder: "SO-2"}); err != nil {
t.Fatal(err)
}
if _, err := f.s.recomputeEntitlements(ctx, nil, cur, "test"); err != nil {
t.Fatal(err)
}
tps, _ := f.s.Store.ListTenantProducts(ctx, buyer.ID)
for _, tp := range tps {
if tp.Product != pruefwerkProduct {
continue
}
if tp.Config["source"] != "erp" || tp.ExpiresAt != nil || !tp.Enabled {
t.Fatalf("subscription did not take over: %+v", tp)
}
if p := pruefwerkPlanOfConfig(tp.Config); p.Name != "standard" {
t.Fatalf("plan after the subscription: %s", p.Name)
}
return
}
t.Fatal("no Prüfwerk entitlement after the subscription")
}