diff --git a/internal/server/admin_entry_test.go b/internal/server/admin_entry_test.go index 0f71db2..a0b0a61 100644 --- a/internal/server/admin_entry_test.go +++ b/internal/server/admin_entry_test.go @@ -122,3 +122,46 @@ func TestAdminEntryOffer(t *testing.T) { 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") +}