storj/satellite/payments/stripe/balances_test.go
Cameron 09ec5f107d satellite/payments: Remove expired package credits
During billing, before invoice creation, check if users are part of a
package plan. If so, and if the package plan is expired, remove unused
credit from the user's balance. If the user has credit in addition to
the package credit, send an analytics event to notify someone to handle
the credit removal manually.

Change-Id: Iad71d791f67c9733f9d9e42f962c64b2780264cc
2023-05-22 11:58:37 +00:00

77 lines
2.3 KiB
Go

// Copyright (C) 2023 Storj Labs, Inc.
// See LICENSE for copying information.
package stripe_test
import (
"testing"
"github.com/shopspring/decimal"
"github.com/stretchr/testify/require"
"storj.io/common/testcontext"
"storj.io/storj/private/testplanet"
"storj.io/storj/satellite/payments/stripe"
)
func TestBalances(t *testing.T) {
testplanet.Run(t, testplanet.Config{
SatelliteCount: 1, UplinkCount: 1,
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
sat := planet.Satellites[0]
userID := planet.Uplinks[0].Projects[0].Owner.ID
balances := sat.API.Payments.Accounts.Balances()
tx1Amount := int64(1000)
tx1Desc := "test description 1"
b, err := balances.ApplyCredit(ctx, userID, tx1Amount, tx1Desc)
require.NoError(t, err)
require.Equal(t, decimal.NewFromInt(tx1Amount), b.Credits)
bal, err := balances.Get(ctx, userID)
require.NoError(t, err)
require.Equal(t, decimal.NewFromInt(tx1Amount), bal.Credits)
tx2Amount := int64(-1000)
tx2Desc := "test description 2"
endingBalance := tx1Amount + tx2Amount
b, err = balances.ApplyCredit(ctx, userID, tx2Amount, tx2Desc)
require.NoError(t, err)
require.Equal(t, decimal.NewFromInt(endingBalance), b.Credits)
bal, err = balances.Get(ctx, userID)
require.NoError(t, err)
require.Equal(t, decimal.NewFromInt(endingBalance), bal.Credits)
tx3Amount := int64(-1000)
tx3Desc := "test description 3"
endingBalance += tx3Amount
b, err = balances.ApplyCredit(ctx, userID, tx3Amount, tx3Desc)
require.NoError(t, err)
require.Equal(t, decimal.NewFromInt(endingBalance), b.Credits)
bal, err = balances.Get(ctx, userID)
require.NoError(t, err)
require.Equal(t, decimal.NewFromInt(endingBalance), bal.Credits)
list, err := balances.ListTransactions(ctx, userID)
require.NoError(t, err)
require.Len(t, list, 3)
require.Equal(t, tx3Amount, list[0].Amount)
require.Equal(t, tx3Desc, list[0].Description)
require.Equal(t, tx2Amount, list[1].Amount)
require.Equal(t, tx2Desc, list[1].Description)
require.Equal(t, tx1Amount, list[2].Amount)
require.Equal(t, tx1Desc, list[2].Description)
b, err = balances.ApplyCredit(ctx, userID, tx2Amount, stripe.MockCBTXsNewFailure)
require.Error(t, err)
require.Nil(t, b)
list, err = balances.ListTransactions(ctx, userID)
require.NoError(t, err)
require.Equal(t, 3, len(list))
})
}