mirror of
https://github.com/hanzoai/commerce.git
synced 2026-10-30 15:08:42 +00:00
Multi-tenant commerce and billing: orders, subscriptions, invoices, plans, usage metering and a credit ledger. One Go binary. MIT OR Apache-2.0. Claude-Session: https://claude.ai/code/session_01SAtaDFm8VDiWrhYHNNtxMw
62 lines
1.7 KiB
Go
62 lines
1.7 KiB
Go
package store
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/hanzoai/commerce/billing/trial"
|
|
"github.com/hanzoai/commerce/datastore"
|
|
"github.com/hanzoai/commerce/models/subscription"
|
|
"github.com/hanzoai/commerce/util/test/ae"
|
|
)
|
|
|
|
func TestHasAccessIsStoreBound(t *testing.T) {
|
|
ctx := ae.NewContext()
|
|
defer ctx.Close()
|
|
db := datastore.New(ctx)
|
|
now := time.Now()
|
|
|
|
sub := subscription.New(db)
|
|
sub.UserId = "acme"
|
|
sub.StoreId = "store-a"
|
|
sub.PlanId = trial.PlanSlug
|
|
sub.Status = subscription.Active
|
|
sub.PeriodStart = now.Add(-time.Hour)
|
|
sub.PeriodEnd = now.Add(time.Hour)
|
|
if err := sub.Create(); err != nil {
|
|
t.Fatalf("create subscription: %v", err)
|
|
}
|
|
|
|
allowed, status, err := HasAccess(db, "store-a", now)
|
|
if err != nil || !allowed || status != "active" {
|
|
t.Fatalf("store-a access = %v %q err=%v, want active", allowed, status, err)
|
|
}
|
|
allowed, status, err = HasAccess(db, "store-b", now)
|
|
if err != nil || allowed || status != "payment_required" {
|
|
t.Fatalf("store-b access = %v %q err=%v, want payment_required", allowed, status, err)
|
|
}
|
|
}
|
|
|
|
func TestHasAccessRejectsExpiredTrial(t *testing.T) {
|
|
ctx := ae.NewContext()
|
|
defer ctx.Close()
|
|
db := datastore.New(ctx)
|
|
now := time.Now()
|
|
|
|
sub := subscription.New(db)
|
|
sub.UserId = "acme"
|
|
sub.StoreId = "store-a"
|
|
sub.PlanId = trial.PlanSlug
|
|
sub.Status = subscription.Trialing
|
|
sub.TrialStart = now.Add(-8 * 24 * time.Hour)
|
|
sub.TrialEnd = now.Add(-24 * time.Hour)
|
|
if err := sub.Create(); err != nil {
|
|
t.Fatalf("create subscription: %v", err)
|
|
}
|
|
|
|
allowed, status, err := HasAccess(db, "store-a", now)
|
|
if err != nil || allowed || status != "payment_required" {
|
|
t.Fatalf("expired trial access = %v %q err=%v, want payment_required", allowed, status, err)
|
|
}
|
|
}
|