fix(trial): a product trial ends with the workspace's trial #77
No files matched your search
@@ -175,3 +175,21 @@ func TestAdminResendInvite(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestProductTrialEndsWithTheWorkspaceTrial(t *testing.T) {
|
||||
eachStore(t, func(t *testing.T, h *testHarness) {
|
||||
ctx := context.Background()
|
||||
tn := newTenant(t, h, "trial-end-co")
|
||||
end := time.Now().UTC().Add(28 * 24 * time.Hour).Truncate(time.Second)
|
||||
if _, err := h.store.UpdateTenant(ctx, tn.ID, store.TenantUpdate{Status: ptr("trial"), TrialEndsAt: &end}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp, raw := h.do("POST", "/v1/catalog/trial-request", map[string]any{"tenant_id": tn.ID, "product": "pruefwerk"}); resp.StatusCode != 201 {
|
||||
t.Fatalf("trial: %d %s", resp.StatusCode, raw)
|
||||
}
|
||||
tps, _ := h.store.ListTenantProducts(ctx, tn.ID)
|
||||
if len(tps) != 1 || tps[0].ExpiresAt == nil || tps[0].ExpiresAt.Sub(end).Abs() > time.Second {
|
||||
t.Fatalf("product trial does not end with the workspace trial: %+v", tps)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -144,7 +144,7 @@ func (s *Server) catalogTrialRequest(w http.ResponseWriter, r *http.Request) {
|
||||
if days <= 0 {
|
||||
days = 14
|
||||
}
|
||||
expiresAt := time.Now().UTC().Add(time.Duration(days) * 24 * time.Hour)
|
||||
expiresAt := productTrialEnd(t, time.Now().UTC(), days)
|
||||
tp, err := s.Store.UpsertTenantProduct(ctx, store.TenantProduct{
|
||||
TenantID: in.TenantID, Product: in.Product, Enabled: true,
|
||||
Config: map[string]interface{}{"source": "trial"}, ExpiresAt: &expiresAt,
|
||||
@@ -164,3 +164,15 @@ func (s *Server) catalogTrialRequest(w http.ResponseWriter, r *http.Request) {
|
||||
s.provisionProduct(ctx, r, t, entry, tp)
|
||||
writeJSON(w, http.StatusCreated, tp)
|
||||
}
|
||||
|
||||
// productTrialEnd is when a product trial started now ends. In a workspace on
|
||||
// trial it ends with the workspace's trial, which staff set or extended for
|
||||
// the customer; a product of its own default length would end before or
|
||||
// after what was agreed. Otherwise (a paying workspace trying another
|
||||
// product) it runs `days` days.
|
||||
func productTrialEnd(t *store.Tenant, now time.Time, days int) time.Time {
|
||||
if t != nil && t.Status == store.StatusTrial && t.TrialEndsAt != nil && t.TrialEndsAt.After(now) {
|
||||
return t.TrialEndsAt.UTC()
|
||||
}
|
||||
return now.Add(time.Duration(days) * 24 * time.Hour)
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.breakpilot.com/platform/tenant-registry/internal/store"
|
||||
)
|
||||
|
||||
func TestProductTrialEnd(t *testing.T) {
|
||||
now := time.Date(2026, 10, 15, 9, 0, 0, 0, time.UTC)
|
||||
later := now.Add(14 * 24 * time.Hour)
|
||||
past := now.Add(-time.Hour)
|
||||
def := now.Add(7 * 24 * time.Hour)
|
||||
for _, c := range []struct {
|
||||
name string
|
||||
t *store.Tenant
|
||||
want time.Time
|
||||
}{
|
||||
{"no tenant", nil, def},
|
||||
{"trial with a later end", &store.Tenant{Status: store.StatusTrial, TrialEndsAt: &later}, later},
|
||||
{"trial without an end", &store.Tenant{Status: store.StatusTrial}, def},
|
||||
{"trial already over", &store.Tenant{Status: store.StatusTrial, TrialEndsAt: &past}, def},
|
||||
{"paying workspace", &store.Tenant{Status: "active", TrialEndsAt: &later}, def},
|
||||
} {
|
||||
if got := productTrialEnd(c.t, now, 7); !got.Equal(c.want) {
|
||||
t.Errorf("%s: %v, want %v", c.name, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
-- Nothing to undo: the aligned end dates are what the trials should have had.
|
||||
SELECT 1;
|
||||
@@ -0,0 +1,25 @@
|
||||
-- A product trial started from the catalogue ran its own 14 days instead of
|
||||
-- ending with the workspace's trial, so a workspace whose trial staff had set
|
||||
-- to a later day lost the product before its trial ended. Product trials of
|
||||
-- workspaces still on trial end with that trial again; each change is in the
|
||||
-- audit log. Rows that end later, preview grants and booked products are not
|
||||
-- touched.
|
||||
WITH aligned AS (
|
||||
UPDATE tenant_products tp
|
||||
SET expires_at = t.trial_ends_at,
|
||||
enabled = TRUE,
|
||||
updated_at = NOW()
|
||||
FROM tenants t
|
||||
WHERE tp.tenant_id = t.id
|
||||
AND t.status = 'trial'
|
||||
AND t.trial_ends_at > NOW()
|
||||
AND tp.config ->> 'source' = 'trial'
|
||||
AND NOT tp.preview
|
||||
AND tp.expires_at IS NOT NULL
|
||||
AND tp.expires_at < t.trial_ends_at
|
||||
RETURNING tp.tenant_id, tp.product, t.trial_ends_at
|
||||
)
|
||||
INSERT INTO audit_log (tenant_id, actor_name, actor_type, action, target_id, target_type, target_name, metadata)
|
||||
SELECT tenant_id, 'migration 0019', 'system', 'entitlement.trial_aligned', tenant_id::text, 'tenant', product,
|
||||
jsonb_build_object('product', product, 'expires_at', trial_ends_at)
|
||||
FROM aligned;
|
||||
@@ -362,3 +362,72 @@ func TestSlugConstraint(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 0019 ends the product trials of workspaces on trial with that trial, and
|
||||
// leaves everything else alone.
|
||||
func TestMigrate_0019AlignsTrialEntitlements(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping integration test under -short")
|
||||
}
|
||||
dsn, stop := startPostgres(t)
|
||||
defer stop()
|
||||
m := newMigrator(t, dsn)
|
||||
if err := m.Migrate(18); err != nil {
|
||||
t.Fatalf("up to 18: %v", err)
|
||||
}
|
||||
db, err := sql.Open("pgx", dsn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() { _ = db.Close() }()
|
||||
|
||||
tenant := func(slug, status, trialEnd string) string {
|
||||
var id string
|
||||
if err := db.QueryRow(`INSERT INTO tenants (slug, name, status, trial_ends_at)
|
||||
VALUES ($1, 'X', $2, NOW() + $3::interval) RETURNING id`, slug, status, trialEnd).Scan(&id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
product := func(tenantID, key, source, expires string) {
|
||||
if _, err := db.Exec(`INSERT INTO tenant_products (tenant_id, product, enabled, config, expires_at)
|
||||
VALUES ($1, $2, FALSE, jsonb_build_object('source', $3::text), NOW() + $4::interval)`, tenantID, key, source, expires); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
short := tenant("trial-short", "trial", "14 days")
|
||||
product(short, "pruefwerk", "trial", "-1 hour") // expired before the trial: aligned
|
||||
product(short, "firmwerk", "erp", "-1 hour") // booked: untouched
|
||||
longer := tenant("trial-longer", "trial", "3 days")
|
||||
product(longer, "pruefwerk", "trial", "10 days") // ends later: untouched
|
||||
paying := tenant("paying", "active", "14 days")
|
||||
product(paying, "pruefwerk", "trial", "-1 hour") // not on trial: untouched
|
||||
|
||||
if err := m.Up(); err != nil && !errors.Is(err, migrate.ErrNoChange) {
|
||||
t.Fatalf("up: %v", err)
|
||||
}
|
||||
aligned := func(tenantID, key string) bool {
|
||||
var ok bool
|
||||
if err := db.QueryRow(`SELECT tp.enabled AND tp.expires_at = t.trial_ends_at
|
||||
FROM tenant_products tp JOIN tenants t ON t.id = tp.tenant_id
|
||||
WHERE tp.tenant_id = $1 AND tp.product = $2`, tenantID, key).Scan(&ok); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return ok
|
||||
}
|
||||
if !aligned(short, "pruefwerk") {
|
||||
t.Error("the trial product of a workspace on trial was not aligned")
|
||||
}
|
||||
for _, c := range []struct{ id, key string }{{short, "firmwerk"}, {longer, "pruefwerk"}, {paying, "pruefwerk"}} {
|
||||
if aligned(c.id, c.key) {
|
||||
t.Errorf("%s/%s changed", c.id, c.key)
|
||||
}
|
||||
}
|
||||
var audited int
|
||||
if err := db.QueryRow(`SELECT COUNT(*) FROM audit_log WHERE action = 'entitlement.trial_aligned' AND tenant_id = $1`, short).Scan(&audited); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if audited != 1 {
|
||||
t.Errorf("audit rows: %d", audited)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user