Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test Case(s) for creating a subscription awaiting_signup with metered components and restricted coupon #4

Merged
merged 2 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,10 @@ env:
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [ '1.18', '1.19', '1.20', '1.21.x' ]
steps:
- uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
- name: Install dependencies
run: |
go get .
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/apimatic/go-core-runtime v0.0.13 h1:KBrOoUbgdIYbjH+TrQNXkbK7QGBovdX43wr7q5Ta6yE=
github.com/apimatic/go-core-runtime v0.0.13/go.mod h1:kyqGg2v3OTV7o2fXHgbHLZPMinqZvIqw1JwdEd64OzM=
380 changes: 190 additions & 190 deletions models/coupon.go

Large diffs are not rendered by default.

102 changes: 10 additions & 92 deletions test/api_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package test

import (
"context"
"net/http"
"testing"

"github.com/caarlos0/env/v10"
"github.com/jaswdr/faker"
advancedbilling "github.com/maxio-com/ab-golang-sdk"
"github.com/maxio-com/ab-golang-sdk/models"
"github.com/stretchr/testify/suite"
)

Expand All @@ -18,14 +16,21 @@ type Config struct {
Subdomain string `env:"TEST_SUBDOMAIN,required"`
}

type APITestSuite struct {
type APISuite struct {
suite.Suite
fkr faker.Faker

client advancedbilling.ClientInterface
unauthorizedClient advancedbilling.ClientInterface
}

func (s *APITestSuite) SetupTest() {
func TestAPITestSuite(t *testing.T) {
suite.Run(t, new(APISuite))
}

func (s *APISuite) SetupTest() {
s.fkr = faker.New()

cfg := Config{}
if err := env.Parse(&cfg); err != nil {
panic(err)
Expand All @@ -46,90 +51,3 @@ func (s *APITestSuite) SetupTest() {
s.client = advancedbilling.NewClient(config)
s.unauthorizedClient = advancedbilling.NewClient(configUnauthorized)
}

func (s *APITestSuite) TestExample() {
s.T().Log(s.client.Configuration().Domain())
}

func (s *APITestSuite) TestReadSite() {
cases := []struct {
name string
client advancedbilling.ClientInterface
assert func(*testing.T, models.ApiResponse[models.SiteResponse])
expectedErr bool
}{
{
name: "success",
client: s.client,
expectedErr: false,
assert: func(t *testing.T, resp models.ApiResponse[models.SiteResponse]) {
s.Equal(http.StatusOK, resp.Response.StatusCode, "status code")

respSite := resp.Data.Site
s.Equal(4718, *respSite.Id, "ID")
s.Equal("GO SDK env", *respSite.Name, "Name")
s.Equal("go-sdk", *respSite.Subdomain, "Subdomain")
s.Equal("USD", *respSite.Currency, "Currency")
s.Equal(722159, *respSite.SellerId, "SellerID")
s.EqualValues([]string{}, respSite.NonPrimaryCurrencies, "NonPrimaryCurrencies")
s.True(*respSite.RelationshipInvoicingEnabled, "RelationshipInvoiceEnabled")
s.False(*respSite.CustomerHierarchyEnabled, "CustomerHierarchyEnabled,")
s.False(*respSite.WhopaysEnabled, "WhopaysEnabled")
s.Equal("self-ungrouped", *respSite.WhopaysDefaultPayer, "WhopaysDefaultPayer")
s.Equal(string(models.PaymentCollectionMethod_AUTOMATIC), *respSite.DefaultPaymentCollectionMethod, "DefaultPaymentCollectionMethod")

allocationSettings := respSite.AllocationSettings
s.Equal(models.CreditType_PRORATED, *allocationSettings.UpgradeCharge.Value(), "UpgradeCharge")
s.Equal(models.CreditType_NONE, *allocationSettings.DowngradeCredit.Value(), "DowngradeCredit")
s.Equal("true", *allocationSettings.AccrueCharge, "AccrueCharge")

organizationAddress := respSite.OrganizationAddress
s.Equal("Asdf Street", *organizationAddress.Street.Value(), "Street")
s.Equal("123/444", *organizationAddress.Line2.Value(), "Line2")
s.Equal("San Antonio", *organizationAddress.City.Value(), "City")
s.Equal("TX", *organizationAddress.State.Value(), "State")
s.Equal("78015", *organizationAddress.Zip.Value(), "Zip")
s.Equal("US", *organizationAddress.Country.Value(), "Country")
s.Equal("Developer Experience", *organizationAddress.Name.Value(), "AddressName")
s.Equal("555 111 222", *organizationAddress.Phone.Value(), "Phone")

taxConfiguration := respSite.TaxConfiguration
s.Equal(models.TaxConfigurationKind_CUSTOM, *taxConfiguration.Kind, "Kind")
s.Equal(models.TaxDestinationAddress_SHIPPINGTHENBILLING, *taxConfiguration.DestinationAddress, "TaxDestinationAddress")
s.False(*taxConfiguration.FullyConfigured, "FullyConfigured")

netTerms := respSite.NetTerms
s.Equal(0, *netTerms.DefaultNetTerms, "DefaultNetTerms")
s.Equal(0, *netTerms.AutomaticNetTerms, "AutomaticNetTerms")
s.Equal(0, *netTerms.RemittanceNetTerms, "RemittanceNetTerms")
s.False(*netTerms.NetTermsOnRemittanceSignupsEnabled, "NetTermsOnRemittanceSignupsEnabled")
s.False(*netTerms.CustomNetTermsEnabled, "CustomNetTermsEnabled")

s.True(*respSite.Test, "Test")
},
},
{
name: "unauthorized",
client: s.unauthorizedClient,
assert: func(t *testing.T, resp models.ApiResponse[models.SiteResponse]) {
s.Equal(http.StatusUnauthorized, resp.Response.StatusCode)
},
expectedErr: true,
},
}

for _, c := range cases {
s.T().Run(c.name, func(t *testing.T) {
resp, err := c.client.SitesController().ReadSite(context.Background())
if !c.expectedErr {
s.NoErrorf(err, "could not read site")
}
c.assert(t, resp)
})
}

}

func TestAPITestSuite(t *testing.T) {
suite.Run(t, new(APITestSuite))
}
1 change: 1 addition & 0 deletions test/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21
require (
github.com/apimatic/go-core-runtime v0.0.13 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/jaswdr/faker v1.19.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Expand Down
2 changes: 2 additions & 0 deletions test/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/caarlos0/env/v10 v10.0.0 h1:yIHUBZGsyqCnpTkbjk8asUlx6RFhhEs+h7TOBdgdz
github.com/caarlos0/env/v10 v10.0.0/go.mod h1:ZfulV76NvVPw3tm591U4SwL3Xx9ldzBP9aGxzeN7G18=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jaswdr/faker v1.19.1 h1:xBoz8/O6r0QAR8eEvKJZMdofxiRH+F0M/7MU9eNKhsM=
github.com/jaswdr/faker v1.19.1/go.mod h1:x7ZlyB1AZqwqKZgyQlnqEG8FDptmHlncA5u2zY/yi6w=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
Expand Down
163 changes: 163 additions & 0 deletions test/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package test

import (
"context"
"fmt"
"net/http"
"time"

"github.com/maxio-com/ab-golang-sdk/models"
)

func (s *APISuite) createCustomer(ctx context.Context) models.Customer {
person := s.fkr.Person()

resp, err := s.client.CustomersController().CreateCustomer(ctx, &models.CreateCustomerRequest{
Customer: models.CreateCustomer{
FirstName: person.FirstName(),
LastName: person.LastName(),
Email: person.Contact().Email,
Organization: strPtr(s.fkr.Company().Name()),
Reference: strPtr(fmt.Sprintf("%d", s.fkr.RandomNumber(10))),
Address: strPtr(person.Faker.Address().StreetAddress()),
City: strPtr(person.Faker.Address().City()),
State: strPtr(person.Faker.Address().State()),
Zip: strPtr(person.Faker.Address().PostCode()),
Country: strPtr(person.Faker.Address().Country()),
Phone: strPtr(person.Faker.Address().Country()),
},
})

s.NoErrorf(err, "create customer err")
s.Equalf(http.StatusCreated, resp.Response.StatusCode, "create customer status code")

return resp.Data.Customer
}

func (s *APISuite) createProductFamily(ctx context.Context) models.ProductFamily {
resp, err := s.client.ProductFamiliesController().CreateProductFamily(ctx, &models.CreateProductFamilyRequest{
ProductFamily: models.CreateProductFamily{
Name: strPtr(s.fkr.Company().Name()),
},
})

s.NoErrorf(err, "create product family err")
s.Equalf(http.StatusCreated, resp.Response.StatusCode, "create product family status code")

return *resp.Data.ProductFamily
}

func (s *APISuite) createProduct(ctx context.Context, productFamilyID int) models.Product {
resp, err := s.client.ProductsController().CreateProduct(ctx, productFamilyID, &models.CreateOrUpdateProductRequest{
Product: models.CreateOrUpdateProduct{
Name: s.fkr.RandomStringWithLength(30),
Description: "Testable product",
PriceInCents: 50,
Interval: 1,
IntervalUnit: models.IntervalUnit_MONTH,
},
})

s.NoErrorf(err, "create product err")
s.Equalf(http.StatusCreated, resp.Response.StatusCode, "create product status code")

return resp.Data.Product
}

func (s *APISuite) createCoupon(ctx context.Context, productFamilyID int) models.Coupon {
coupon := &models.Coupon{
Name: strPtr("100\\% off first month of usage"),
Code: strPtr("100OFF" + s.fkr.RandomStringWithLength(30)),
Description: strPtr("100\\% off one-time"),
Percentage: models.NewOptional[string](strPtr("50")),
AllowNegativeBalance: boolPtr(false),
Recurring: boolPtr(false),
EndDate: models.NewOptional[string](strPtr(newDate())),
ProductFamilyId: &productFamilyID,
Stackable: boolPtr(false),
ExcludeMidPeriodAllocations: boolPtr(true),
ApplyOnCancelAtEndOfPeriod: boolPtr(true),
}

resp, err := s.client.CouponsController().CreateCoupon(ctx, productFamilyID, &models.CreateOrUpdateCoupon{
Coupon: interfacePtr(coupon),
})

s.NoErrorf(err, "create coupon err")
s.Equalf(http.StatusCreated, resp.Response.StatusCode, "create coupon err")

return *resp.Data.Coupon
}

type MeteredComponent struct {
Name string `json:"name"`
UnitName string `json:"unit_name"`
Taxable bool `json:"taxable"`
PricingScheme string `json:"pricing_scheme"`
Prices []Price `json:"prices"`
}

type Price struct {
StartingQuantity int `json:"starting_quantity"`
UnitPrice int `json:"unit_price"`
}

func (s *APISuite) createMeteredComponent(ctx context.Context, productFamilyID int) models.Component {
component := struct {
MeteredComponent MeteredComponent `json:"metered_component"`
}{
MeteredComponent: MeteredComponent{
Name: "test 2",
UnitName: "test message",
Taxable: false,
PricingScheme: "stairstep",
Prices: []Price{{
StartingQuantity: 1,
UnitPrice: 1,
},
},
},
}

resp, err := s.client.ComponentsController().CreateComponent(
ctx,
productFamilyID,
models.ComponentKindPath_METEREDCOMPONENTS,
interfacePtr(component),
)

s.NoErrorf(err, "create component err")
s.Equalf(http.StatusCreated, resp.Response.StatusCode, "create component err")

return resp.Data.Component
}

func strPtr(v string) *string {
return &v
}

func boolPtr(v bool) *bool {
return &v
}

func intPtr(v int) *int {
return &v
}

func interfacePtr(v interface{}) *interface{} {
return &v
}

func toPtr[T any](v T) *T {
return &v
}

func newDate() string {
t := time.Now().Add(time.Hour)

return fmt.Sprintf("%d-%d-%d", t.Year(), t.Month(), t.Day())
}

func timePtr(v time.Time) *time.Time {
return &v
}
Loading