2023-01-30 22:11:12 +00:00
|
|
|
// Copyright (C) 2023 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2023-04-06 12:41:14 +01:00
|
|
|
package stripe_test
|
2023-01-30 22:11:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/common/testcontext"
|
2023-01-25 21:38:29 +00:00
|
|
|
"storj.io/common/testrand"
|
2023-01-30 22:11:12 +00:00
|
|
|
"storj.io/storj/private/testplanet"
|
|
|
|
"storj.io/storj/satellite"
|
2023-04-06 12:41:14 +01:00
|
|
|
"storj.io/storj/satellite/payments/stripe"
|
2023-01-30 22:11:12 +00:00
|
|
|
)
|
|
|
|
|
2023-01-25 21:38:29 +00:00
|
|
|
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) {
|
2023-04-06 12:41:14 +01:00
|
|
|
config.Payments.StripeCoinPayments.StripeFreeTierCouponID = stripe.MockCouponID1
|
2023-01-25 21:38:29 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, 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) {
|
2023-04-06 12:41:14 +01:00
|
|
|
coupon, err := c.ApplyCoupon(ctx, testrand.UUID(), stripe.MockCouponID2)
|
2023-01-25 21:38:29 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, coupon)
|
|
|
|
})
|
|
|
|
t.Run("ApplyCoupon, GetByUserID succeeds", func(t *testing.T) {
|
2023-04-06 12:41:14 +01:00
|
|
|
id := stripe.MockCouponID1
|
2023-01-25 21:38:29 +00:00
|
|
|
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)
|
2023-01-30 22:11:12 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|