storj/satellite/payments/stripecoinpayments/coupons_test.go
Cameron c2cd213c4f satellite/console: purchase package grants credit
Instead of granting a coupon when purchasing a package, grant credit.
This changes paymentsconfig.PackagePlan to use credit amount rather than
coupon ID. Add additional check to see if a paid invoice with the
description exists. If so, don't create and pay another invoice.

Change-Id: I81df24984c519c773db5fc8e9070bd7797070ec2
2023-03-31 22:29:54 +00:00

71 lines
2.3 KiB
Go

// Copyright (C) 2023 Storj Labs, Inc.
// See LICENSE for copying information.
package stripecoinpayments_test
import (
"testing"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"storj.io/common/testcontext"
"storj.io/common/testrand"
"storj.io/storj/private/testplanet"
"storj.io/storj/satellite"
"storj.io/storj/satellite/payments/stripecoinpayments"
)
func TestCoupons(t *testing.T) {
testplanet.Run(t, testplanet.Config{
SatelliteCount: 1, StorageNodeCount: 0, UplinkCount: 1,
Reconfigure: testplanet.Reconfigure{
Satellite: func(log *zap.Logger, index int, config *satellite.Config) {
config.Payments.StripeCoinPayments.StripeFreeTierCouponID = stripecoinpayments.MockCouponID1
},
},
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
satellite := planet.Satellites[0]
c := satellite.API.Payments.Accounts.Coupons()
userID := planet.Uplinks[0].Projects[0].Owner.ID
t.Run("ApplyCoupon fails with no matching coupon", func(t *testing.T) {
coupon, err := c.ApplyCoupon(ctx, userID, "unknown_coupon_id")
require.Error(t, err)
require.Nil(t, coupon)
})
t.Run("ApplyCoupon fails with no matching customer", func(t *testing.T) {
coupon, err := c.ApplyCoupon(ctx, testrand.UUID(), stripecoinpayments.MockCouponID2)
require.Error(t, err)
require.Nil(t, coupon)
})
t.Run("ApplyCoupon, GetByUserID succeeds", func(t *testing.T) {
id := stripecoinpayments.MockCouponID1
coupon, err := c.ApplyCoupon(ctx, userID, id)
require.NoError(t, err)
require.NotNil(t, coupon)
require.Equal(t, id, coupon.ID)
coupon, err = c.GetByUserID(ctx, userID)
require.NoError(t, err)
require.Equal(t, id, coupon.ID)
})
t.Run("ApplyFreeTierCoupon succeeds", func(t *testing.T) {
id := satellite.API.Payments.StripeService.StripeFreeTierCouponID
coupon, err := c.ApplyFreeTierCoupon(ctx, userID)
require.NoError(t, err)
require.NotNil(t, coupon)
require.Equal(t, id, coupon.ID)
coupon, err = c.GetByUserID(ctx, userID)
require.NoError(t, err)
require.Equal(t, id, coupon.ID)
})
t.Run("ApplyFreeTierCoupon fails with unknown user", func(t *testing.T) {
coupon, err := c.ApplyFreeTierCoupon(ctx, testrand.UUID())
require.Error(t, err)
require.Nil(t, coupon)
})
})
}