Files
commerce/test/api/library/library_test.go
zeekay 1943f1c093 Hanzo Commerce
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
2026-09-23 21:52:16 -07:00

231 lines
5.7 KiB
Go

package test
import (
"net/http"
"testing"
"time"
"github.com/hanzoai/commerce/datastore"
"github.com/hanzoai/commerce/log"
"github.com/hanzoai/commerce/middleware"
"github.com/hanzoai/commerce/models/fixtures"
"github.com/hanzoai/commerce/models/organization"
"github.com/hanzoai/commerce/models/shippingrates"
"github.com/hanzoai/commerce/models/store"
"github.com/hanzoai/commerce/models/taxrates"
"github.com/hanzoai/commerce/models/types/country"
"github.com/hanzoai/commerce/models/types/currency"
"github.com/hanzoai/commerce/models/types/georate"
"github.com/hanzoai/commerce/util/permission"
"github.com/hanzoai/commerce/util/test/ae"
"github.com/hanzoai/commerce/util/test/zipclient"
"github.com/hanzoai/commerce/util/zipctx"
. "github.com/hanzoai/commerce/util/test/ginkgo"
libraryApi "github.com/hanzoai/commerce/api/library"
)
func Test(t *testing.T) {
Setup("api/account", t)
}
var (
ctx ae.Context
cl *zipclient.Client
accessToken string
db *datastore.Datastore
org *organization.Organization
stor *store.Store
)
// Setup test context
var _ = BeforeSuite(func() {
var err error
publishedRequired := middleware.TokenRequired(permission.Published)
// Create a new app engine context
ctx = ae.NewContext()
// Create mock gin context that we can use with fixtures
c := zipctx.New(ctx)
// Run fixtures
org = fixtures.Organization(c).(*organization.Organization)
// Setup client and add routes for account API tests.
cl = zipclient.New(ctx)
libraryApi.Route(cl.Router, publishedRequired)
// Create organization for tests, accessToken
accessToken, _ := org.GetTokenByName("test-published-key")
err = org.Put()
Expect(err).NotTo(HaveOccurred())
// Set authorization header for subsequent requests
cl.Defaults(func(r *http.Request) {
r.Header.Set("Authorization", accessToken.String)
})
// Save namespaced db
db = datastore.New(org.Namespaced(ctx))
// Add some basic rate date
stor, err = org.GetDefaultStore()
Expect(err).NotTo(HaveOccurred())
sr, err := stor.GetShippingRates()
Expect(err).NotTo(HaveOccurred())
sr.GeoRates = append(sr.GeoRates, shippingrates.GeoRate{
GeoRate: georate.New(
"",
"",
"",
"",
currency.Cents(0),
currency.Cents(499),
0,
currency.Cents(0),
),
ShippingName: "SHIPPING",
})
sr.MustUpdate()
tr, err := stor.GetTaxRates()
Expect(err).NotTo(HaveOccurred())
tr.GeoRates = append(tr.GeoRates, taxrates.GeoRate{
GeoRate: georate.New(
"US",
"KS",
"",
"66212",
currency.Cents(0),
currency.Cents(0),
0.0885,
currency.Cents(0),
),
TaxShipping: false,
TaxName: "TEST TAX",
})
tr.GeoRates = append(tr.GeoRates, taxrates.GeoRate{
GeoRate: georate.New(
"US",
"KS",
"",
"",
currency.Cents(0),
currency.Cents(0),
0.065,
currency.Cents(0),
),
TaxShipping: false,
TaxName: "TEST TAX",
})
tr.GeoRates = append(tr.GeoRates, taxrates.GeoRate{
GeoRate: georate.New(
"GB",
"",
"",
"",
currency.Cents(0),
currency.Cents(0),
0.2,
currency.Cents(0),
),
TaxShipping: false,
TaxName: "VAT",
})
tr.MustUpdate()
})
// Tear-down test context
var _ = AfterSuite(func() {
ctx.Close()
})
var _ = Describe("library", func() {
Context("shopjs", func() {
It("Should get everything", func() {
req := libraryApi.LoadShopJSReq{}
res := libraryApi.LoadShopJSRes{}
log.Debug("Response %s", cl.Post("/library/shopjs", req, &res))
Expect(len(res.Countries)).To(Equal(len(country.All())))
Expect(res.ShippingRates.StoreId).To(Equal(org.DefaultStore))
Expect(len(res.ShippingRates.GeoRates)).To(Equal(3))
Expect(res.TaxRates.StoreId).To(Equal(org.DefaultStore))
Expect(len(res.TaxRates.GeoRates)).To(Equal(5))
Expect(res.Currency).ToNot(Equal(""))
})
It("Should get nothing", func() {
req := libraryApi.LoadShopJSReq{
HasCountries: true,
HasTaxRates: true,
HasShippingRates: true,
LastChecked: time.Now().Add(2 * time.Hour),
}
res := libraryApi.LoadShopJSRes{}
log.Debug("Response %s", cl.Post("/library/shopjs", req, &res))
Expect(res.Countries).To(BeNil())
Expect(res.ShippingRates).To(BeNil())
Expect(res.TaxRates).To(BeNil())
})
It("Should get out of date", func() {
req := libraryApi.LoadShopJSReq{
HasCountries: true,
HasTaxRates: true,
HasShippingRates: true,
LastChecked: time.Now().Add(-2 * time.Hour),
}
res := libraryApi.LoadShopJSRes{}
log.Debug("Response %s", cl.Post("/library/shopjs", req, &res))
Expect(len(res.Countries)).To(Equal(len(country.All())))
Expect(res.ShippingRates.StoreId).To(Equal(org.DefaultStore))
Expect(len(res.ShippingRates.GeoRates)).To(Equal(3))
Expect(res.TaxRates.StoreId).To(Equal(org.DefaultStore))
Expect(len(res.TaxRates.GeoRates)).To(Equal(5))
})
It("Should fail for missing store", func() {
req := libraryApi.LoadShopJSReq{
StoreId: "123",
}
cl.Post("/library/shopjs", req, nil, 404)
})
It("Should return the currency right", func() {
req := libraryApi.LoadShopJSReq{}
res := libraryApi.LoadShopJSRes{}
// Default to USD
org.Currency = ""
org.MustUpdate()
stor.Currency = ""
stor.MustUpdate()
log.Debug("Response %s", cl.Post("/library/shopjs", req, &res))
Expect(string(res.Currency)).To(Equal(string(currency.USD)))
// Use Order if Store is Blank
org.Currency = currency.ZMW
org.MustUpdate()
log.Debug("Response %s", cl.Post("/library/shopjs", req, &res))
Expect(string(res.Currency)).To(Equal(string(currency.ZMW)))
// Use Store is Available
stor.Currency = currency.AUD
stor.MustUpdate()
log.Debug("Response %s", cl.Post("/library/shopjs", req, &res))
Expect(string(res.Currency)).To(Equal(string(currency.AUD)))
})
})
})