package accounts import ( "context" "os" "testing" "time" ) func TestStoreIntegration(t *testing.T) { databaseURL := os.Getenv("SSATLAS_TEST_DATABASE_URL") if databaseURL == "" { t.Skip("set SSATLAS_TEST_DATABASE_URL to run Postgres integration test") } ctx := context.Background() store, err := New(ctx, databaseURL) if err != nil { t.Fatalf("New() error = %v", err) } t.Cleanup(func() { _, _ = store.db.ExecContext(ctx, "TRUNCATE billing_events, account_identities, instances, accounts RESTART IDENTITY CASCADE") _ = store.Close() }) email := "buyer-" + time.Now().UTC().Format("20060102150405") + "@example.com" acct, inst, err := store.UpsertCheckout(ctx, CheckoutInput{ Email: email, DisplayName: "Buyer Example", Phone: "+15551234567", CustomerDomain: "example.com", StripeCustomerID: "cus_test_store", StripeSessionID: "cs_test_store", StripeEventID: "evt_test_store", }) if err != nil { t.Fatalf("UpsertCheckout() error = %v", err) } if acct.ID == 0 || inst.ID == 0 { t.Fatalf("expected persisted account and instance, got account=%+v instance=%+v", acct, inst) } if inst.Slug == "" || inst.StackName == "" { t.Fatalf("expected instance slug and stack name, got %+v", inst) } count, err := store.CountCustomers(ctx) if err != nil { t.Fatalf("CountCustomers() error = %v", err) } if count != 1 { t.Fatalf("CountCustomers() = %d, want 1", count) } linked, err := store.UpsertFromIdentity(ctx, Identity{ Provider: "authentik", Subject: "ak-user-1", Username: "buyer", Email: email, Name: "Buyer Example", }) if err != nil { t.Fatalf("UpsertFromIdentity() error = %v", err) } if linked.ID != acct.ID { t.Fatalf("identity linked account %d, want %d", linked.ID, acct.ID) } owned, err := store.InstanceBySlug(ctx, inst.Slug) if err != nil { t.Fatalf("InstanceBySlug() error = %v", err) } if owned.AccountID != acct.ID { t.Fatalf("InstanceBySlug().AccountID = %d, want %d", owned.AccountID, acct.ID) } if err := store.MarkSubscriptionStatus(ctx, "cus_test_store", "cancelled"); err != nil { t.Fatalf("MarkSubscriptionStatus() error = %v", err) } }