storj/satellite/payments/stripecoinpayments/tokens_test.go
Kaloyan Raev 4bcf308a04 satellite/payments: fetch old deposit bonuses from Stripe metadata
Jira: https://storjlabs.atlassian.net/browse/USR-822

The balance history in Satellite GUI display the deposit bonuses as
separate rows. These bonuses used to be stored in the satellite DB. We
recently started depositing the bonus directly to the Stripe balance and
migrated old bonuses to Stripe metadata.

This change displays all billing history entirely from Stripe, so we can
remove the `credits` and `credits_spendings` DB tables in a next step.

Change-Id: I14c304c66ec47c6a51f5b8508f11470cf36c4e24
2020-07-23 12:11:17 +00:00

92 lines
3.4 KiB
Go

// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package stripecoinpayments_test
import (
"encoding/base64"
"encoding/json"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/stripe/stripe-go"
"storj.io/common/memory"
"storj.io/common/testcontext"
"storj.io/common/testrand"
"storj.io/storj/private/testplanet"
"storj.io/storj/satellite/payments"
"storj.io/storj/satellite/payments/coinpayments"
"storj.io/storj/satellite/payments/stripecoinpayments"
)
func TestTokens_ListDepositBonuses(t *testing.T) {
testplanet.Run(t, testplanet.Config{
SatelliteCount: 1, StorageNodeCount: 0, UplinkCount: 1,
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
satellite := planet.Satellites[0]
userID := planet.Uplinks[0].Projects[0].Owner.ID
// No deposit bonuses expected at this point
bonuses, err := satellite.API.Payments.Accounts.StorjTokens().ListDepositBonuses(ctx, userID)
require.NoError(t, err)
require.Len(t, bonuses, 0)
customerID, err := satellite.DB.StripeCoinPayments().Customers().GetCustomerID(ctx, userID)
require.NoError(t, err)
txID := base64.StdEncoding.EncodeToString(testrand.Bytes(4 * memory.B))
// Credit the Stripe balance with a STORJ deposit of $10
txParams := stripe.CustomerBalanceTransactionParams{
Customer: stripe.String(customerID),
Amount: stripe.Int64(-1000),
Description: stripe.String(stripecoinpayments.StripeDepositTransactionDescription),
}
txParams.AddMetadata("txID", txID)
_, err = satellite.API.Payments.Stripe.CustomerBalanceTransactions().New(&txParams)
require.NoError(t, err)
// Credit the Stripe balance with a 13% bonus - $1.30
txParams = stripe.CustomerBalanceTransactionParams{
Customer: stripe.String(customerID),
Amount: stripe.Int64(-130),
Description: stripe.String(stripecoinpayments.StripeDepositBonusTransactionDescription),
}
txParams.AddMetadata("txID", txID)
txParams.AddMetadata("percentage", "13")
bonusTx, err := satellite.API.Payments.Stripe.CustomerBalanceTransactions().New(&txParams)
require.NoError(t, err)
// Add migrated deposit bonus to Stripe metadata
credit := payments.Credit{
UserID: userID,
Amount: 100,
TransactionID: coinpayments.TransactionID(base64.StdEncoding.EncodeToString(testrand.Bytes(4 * memory.B))),
Created: time.Now(),
}
b, err := json.Marshal(credit)
require.NoError(t, err)
customerParams := stripe.CustomerParams{}
customerParams.Metadata = map[string]string{
"credit_" + credit.TransactionID.String(): string(b),
}
_, err = satellite.API.Payments.Stripe.Customers().Update(customerID, &customerParams)
require.NoError(t, err)
// Expect the list to contain the 10% bonus from Stripe metadata and 13% bonus from Stripe balance
bonuses, err = satellite.API.Payments.Accounts.StorjTokens().ListDepositBonuses(ctx, userID)
require.NoError(t, err)
require.Len(t, bonuses, 2)
require.EqualValues(t, credit.TransactionID, bonuses[0].TransactionID)
require.EqualValues(t, 100, bonuses[0].AmountCents)
require.EqualValues(t, 10, bonuses[0].Percentage)
require.Equal(t, bonusTx.Created, bonuses[0].CreatedAt.Unix())
require.EqualValues(t, txID, bonuses[1].TransactionID)
require.EqualValues(t, 130, bonuses[1].AmountCents)
require.EqualValues(t, 13, bonuses[1].Percentage)
require.Equal(t, bonusTx.Created, bonuses[1].CreatedAt.Unix())
})
}