2022-04-26 21:23:27 +01:00
|
|
|
// Copyright (C) 2022 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package billing_test
|
|
|
|
|
|
|
|
import (
|
2022-06-16 17:48:07 +01:00
|
|
|
"encoding/json"
|
2022-04-26 21:23:27 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/common/testrand"
|
2022-06-16 17:48:07 +01:00
|
|
|
"storj.io/storj/private/blockchain"
|
2022-04-26 21:23:27 +01:00
|
|
|
"storj.io/storj/satellite"
|
|
|
|
"storj.io/storj/satellite/payments/billing"
|
|
|
|
"storj.io/storj/satellite/payments/monetary"
|
|
|
|
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTransactionsDBList(t *testing.T) {
|
|
|
|
const (
|
|
|
|
limit = 3
|
|
|
|
transactionCount = limit * 4
|
|
|
|
)
|
|
|
|
|
|
|
|
// create transactions
|
|
|
|
userID := testrand.UUID()
|
|
|
|
|
|
|
|
var txs []billing.Transaction
|
2022-06-16 17:48:07 +01:00
|
|
|
var txStatus billing.TransactionStatus
|
|
|
|
var txType billing.TransactionType
|
2022-04-26 21:23:27 +01:00
|
|
|
for i := 0; i < transactionCount; i++ {
|
2022-06-16 17:48:07 +01:00
|
|
|
txSource := "storjscan"
|
2022-05-10 20:19:53 +01:00
|
|
|
txStatus = billing.TransactionStatusCompleted
|
2022-06-16 17:48:07 +01:00
|
|
|
txType = billing.TransactionTypeCredit
|
2022-04-26 21:23:27 +01:00
|
|
|
if i%2 == 0 {
|
2022-06-16 17:48:07 +01:00
|
|
|
txSource = "stripe"
|
2022-04-26 21:23:27 +01:00
|
|
|
}
|
|
|
|
if i%3 == 0 {
|
2022-06-16 17:48:07 +01:00
|
|
|
txSource = "coinpayments"
|
2022-04-26 21:23:27 +01:00
|
|
|
}
|
|
|
|
|
2022-06-16 17:48:07 +01:00
|
|
|
address, err := blockchain.BytesToAddress(testrand.Bytes(20))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
metadata, err := json.Marshal(map[string]interface{}{
|
|
|
|
"ReferenceID": "some stripe invoice ID",
|
|
|
|
"Wallet": address.Hex(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-04-26 21:23:27 +01:00
|
|
|
createTX := billing.Transaction{
|
2022-06-16 17:48:07 +01:00
|
|
|
UserID: userID,
|
|
|
|
Amount: monetary.AmountFromBaseUnits(4, monetary.USDollars),
|
2022-04-26 21:23:27 +01:00
|
|
|
Description: "credit from storjscan payment",
|
2022-06-16 17:48:07 +01:00
|
|
|
Source: txSource,
|
|
|
|
Status: txStatus,
|
|
|
|
Type: txType,
|
|
|
|
Metadata: metadata,
|
2022-04-26 21:23:27 +01:00
|
|
|
Timestamp: time.Now(),
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
txs = append(txs, createTX)
|
|
|
|
}
|
|
|
|
|
2022-06-16 17:48:07 +01:00
|
|
|
t.Run("insert and list transactions", func(t *testing.T) {
|
2022-04-26 21:23:27 +01:00
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
|
|
|
for _, tx := range txs {
|
2022-06-16 17:48:07 +01:00
|
|
|
_, err := db.Billing().Insert(ctx, tx)
|
2022-04-26 21:23:27 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2022-06-16 17:48:07 +01:00
|
|
|
storjscanTXs, err := db.Billing().List(ctx, userID)
|
2022-04-26 21:23:27 +01:00
|
|
|
require.NoError(t, err)
|
2022-06-16 17:48:07 +01:00
|
|
|
require.Equal(t, 12, len(storjscanTXs))
|
2022-04-26 21:23:27 +01:00
|
|
|
for _, act := range storjscanTXs {
|
|
|
|
for _, exp := range txs {
|
2022-06-16 17:48:07 +01:00
|
|
|
if act.ID == exp.ID {
|
2022-04-26 21:23:27 +01:00
|
|
|
compareTransactions(t, exp, act)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTransactionsDBBalance(t *testing.T) {
|
2022-06-16 17:48:07 +01:00
|
|
|
tenUSD := monetary.AmountFromBaseUnits(10, monetary.USDollars)
|
|
|
|
twentyUSD := monetary.AmountFromBaseUnits(20, monetary.USDollars)
|
|
|
|
thirtyUSD := monetary.AmountFromBaseUnits(30, monetary.USDollars)
|
|
|
|
fortyUSD := monetary.AmountFromBaseUnits(40, monetary.USDollars)
|
|
|
|
negativeTwentyUSD := monetary.AmountFromBaseUnits(-20, monetary.USDollars)
|
|
|
|
|
|
|
|
userID := testrand.UUID()
|
|
|
|
|
|
|
|
address, err := blockchain.BytesToAddress(testrand.Bytes(20))
|
2022-04-26 21:23:27 +01:00
|
|
|
require.NoError(t, err)
|
2022-06-16 17:48:07 +01:00
|
|
|
|
|
|
|
metadata, err := json.Marshal(map[string]interface{}{
|
|
|
|
"ReferenceID": "some stripe invoice ID",
|
|
|
|
"Wallet": address.Hex(),
|
|
|
|
})
|
2022-04-26 21:23:27 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
credit10TX := billing.Transaction{
|
2022-06-16 17:48:07 +01:00
|
|
|
UserID: userID,
|
2022-04-26 21:23:27 +01:00
|
|
|
Amount: tenUSD,
|
|
|
|
Description: "credit from storjscan payment",
|
2022-06-16 17:48:07 +01:00
|
|
|
Source: "storjscan",
|
2022-05-10 20:19:53 +01:00
|
|
|
Status: billing.TransactionStatusCompleted,
|
2022-06-16 17:48:07 +01:00
|
|
|
Type: billing.TransactionTypeCredit,
|
|
|
|
Metadata: metadata,
|
2022-04-26 21:23:27 +01:00
|
|
|
Timestamp: time.Now().Add(time.Second),
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
credit30TX := billing.Transaction{
|
2022-06-16 17:48:07 +01:00
|
|
|
UserID: userID,
|
2022-04-26 21:23:27 +01:00
|
|
|
Amount: thirtyUSD,
|
|
|
|
Description: "credit from storjscan payment",
|
2022-06-16 17:48:07 +01:00
|
|
|
Source: "storjscan",
|
2022-05-10 20:19:53 +01:00
|
|
|
Status: billing.TransactionStatusCompleted,
|
2022-06-16 17:48:07 +01:00
|
|
|
Type: billing.TransactionTypeCredit,
|
|
|
|
Metadata: metadata,
|
2022-04-26 21:23:27 +01:00
|
|
|
Timestamp: time.Now().Add(time.Second * 2),
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
charge20TX := billing.Transaction{
|
2022-06-16 17:48:07 +01:00
|
|
|
UserID: userID,
|
2022-04-26 21:23:27 +01:00
|
|
|
Amount: negativeTwentyUSD,
|
|
|
|
Description: "charge for storage and bandwidth",
|
2022-06-16 17:48:07 +01:00
|
|
|
Source: "storjscan",
|
2022-05-10 20:19:53 +01:00
|
|
|
Status: billing.TransactionStatusCompleted,
|
2022-06-16 17:48:07 +01:00
|
|
|
Type: billing.TransactionTypeDebit,
|
|
|
|
Metadata: metadata,
|
2022-04-26 21:23:27 +01:00
|
|
|
Timestamp: time.Now().Add(time.Second * 3),
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("add 10 USD to account", func(t *testing.T) {
|
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
2022-06-16 17:48:07 +01:00
|
|
|
_, err := db.Billing().Insert(ctx, credit10TX)
|
2022-04-26 21:23:27 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
txs, err := db.Billing().List(ctx, userID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, txs, 1)
|
|
|
|
compareTransactions(t, credit10TX, txs[0])
|
2022-06-16 17:48:07 +01:00
|
|
|
balance, err := db.Billing().GetBalance(ctx, userID)
|
2022-04-26 21:23:27 +01:00
|
|
|
require.NoError(t, err)
|
2022-06-16 17:48:07 +01:00
|
|
|
require.Equal(t, tenUSD.BaseUnits(), balance)
|
2022-04-26 21:23:27 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("add 10 and 30 USD to account", func(t *testing.T) {
|
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
2022-06-16 17:48:07 +01:00
|
|
|
_, err := db.Billing().Insert(ctx, credit10TX)
|
2022-04-26 21:23:27 +01:00
|
|
|
require.NoError(t, err)
|
2022-06-16 17:48:07 +01:00
|
|
|
_, err = db.Billing().Insert(ctx, credit30TX)
|
2022-04-26 21:23:27 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
txs, err := db.Billing().List(ctx, userID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, txs, 2)
|
|
|
|
compareTransactions(t, credit30TX, txs[0])
|
|
|
|
compareTransactions(t, credit10TX, txs[1])
|
2022-06-16 17:48:07 +01:00
|
|
|
balance, err := db.Billing().GetBalance(ctx, userID)
|
2022-04-26 21:23:27 +01:00
|
|
|
require.NoError(t, err)
|
2022-06-16 17:48:07 +01:00
|
|
|
require.Equal(t, fortyUSD.BaseUnits(), balance)
|
2022-04-26 21:23:27 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("add 10 USD, add 30 USD, subtract 20 USD", func(t *testing.T) {
|
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
2022-06-16 17:48:07 +01:00
|
|
|
_, err := db.Billing().Insert(ctx, credit10TX)
|
2022-04-26 21:23:27 +01:00
|
|
|
require.NoError(t, err)
|
2022-06-16 17:48:07 +01:00
|
|
|
_, err = db.Billing().Insert(ctx, credit30TX)
|
2022-04-26 21:23:27 +01:00
|
|
|
require.NoError(t, err)
|
2022-06-16 17:48:07 +01:00
|
|
|
_, err = db.Billing().Insert(ctx, charge20TX)
|
2022-04-26 21:23:27 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
txs, err := db.Billing().List(ctx, userID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, txs, 3)
|
|
|
|
compareTransactions(t, charge20TX, txs[0])
|
|
|
|
compareTransactions(t, credit30TX, txs[1])
|
|
|
|
compareTransactions(t, credit10TX, txs[2])
|
2022-06-16 17:48:07 +01:00
|
|
|
balance, err := db.Billing().GetBalance(ctx, userID)
|
2022-04-26 21:23:27 +01:00
|
|
|
require.NoError(t, err)
|
2022-06-16 17:48:07 +01:00
|
|
|
require.Equal(t, twentyUSD.BaseUnits(), balance)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("insert batch", func(t *testing.T) {
|
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
|
|
|
err = db.Billing().InsertBatch(ctx, []billing.Transaction{credit10TX, credit30TX, charge20TX})
|
|
|
|
require.NoError(t, err)
|
|
|
|
txs, err := db.Billing().List(ctx, userID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, txs, 3)
|
|
|
|
compareTransactions(t, charge20TX, txs[0])
|
|
|
|
compareTransactions(t, credit30TX, txs[1])
|
|
|
|
compareTransactions(t, credit10TX, txs[2])
|
|
|
|
balance, err := db.Billing().GetBalance(ctx, userID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, twentyUSD.BaseUnits(), balance)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateTransactions(t *testing.T) {
|
|
|
|
tenUSD := monetary.AmountFromBaseUnits(10, monetary.USDollars)
|
|
|
|
userID := testrand.UUID()
|
|
|
|
address, err := blockchain.BytesToAddress(testrand.Bytes(20))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
metadata, err := json.Marshal(map[string]interface{}{
|
|
|
|
"ReferenceID": "some stripe invoice ID",
|
|
|
|
"Wallet": address.Hex(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
credit10TX := billing.Transaction{
|
|
|
|
UserID: userID,
|
|
|
|
Amount: tenUSD,
|
|
|
|
Description: "credit from storjscan payment",
|
|
|
|
Source: "storjscan",
|
2022-05-10 20:19:53 +01:00
|
|
|
Status: billing.TransactionStatusCompleted,
|
2022-06-16 17:48:07 +01:00
|
|
|
Type: billing.TransactionTypeCredit,
|
|
|
|
Metadata: metadata,
|
|
|
|
Timestamp: time.Now().Add(time.Second),
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("update metadata", func(t *testing.T) {
|
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
|
|
|
txID, err := db.Billing().Insert(ctx, credit10TX)
|
|
|
|
require.NoError(t, err)
|
|
|
|
newAddress, err := blockchain.BytesToAddress(testrand.Bytes(20))
|
|
|
|
require.NoError(t, err)
|
|
|
|
metadata, err := json.Marshal(map[string]interface{}{
|
|
|
|
"Wallet": newAddress.Hex(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = db.Billing().UpdateMetadata(ctx, txID, metadata)
|
|
|
|
require.NoError(t, err)
|
|
|
|
expMetadata, err := json.Marshal(map[string]interface{}{
|
|
|
|
"ReferenceID": "some stripe invoice ID",
|
|
|
|
"Wallet": newAddress.Hex(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
credit10TX.Metadata = expMetadata
|
|
|
|
tx, err := db.Billing().List(ctx, userID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
compareTransactions(t, credit10TX, tx[0])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("update status", func(t *testing.T) {
|
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
|
|
|
txID, err := db.Billing().Insert(ctx, credit10TX)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = db.Billing().UpdateStatus(ctx, txID, billing.TransactionStatusCancelled)
|
|
|
|
require.NoError(t, err)
|
|
|
|
credit10TX.Status = billing.TransactionStatusCancelled
|
|
|
|
tx, err := db.Billing().List(ctx, userID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
compareTransactions(t, credit10TX, tx[0])
|
2022-04-26 21:23:27 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-06-16 17:48:07 +01:00
|
|
|
func TestUpdateMetadata(t *testing.T) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-04-26 21:23:27 +01:00
|
|
|
// compareTransactions is a helper method to compare tx used to create db entry,
|
|
|
|
// with the tx returned from the db. Method doesn't compare created at field, but
|
|
|
|
// ensures that is not empty.
|
|
|
|
func compareTransactions(t *testing.T, exp, act billing.Transaction) {
|
2022-06-16 17:48:07 +01:00
|
|
|
assert.Equal(t, exp.UserID, act.UserID)
|
2022-04-26 21:23:27 +01:00
|
|
|
assert.Equal(t, exp.Amount, act.Amount)
|
|
|
|
assert.Equal(t, exp.Description, act.Description)
|
2022-06-16 17:48:07 +01:00
|
|
|
assert.Equal(t, exp.Status, act.Status)
|
|
|
|
assert.Equal(t, exp.Source, act.Source)
|
|
|
|
assert.Equal(t, exp.Type, act.Type)
|
|
|
|
var expUpdatedMetadata map[string]string
|
|
|
|
var actUpdatedMetadata map[string]string
|
|
|
|
err := json.Unmarshal(exp.Metadata, &expUpdatedMetadata)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = json.Unmarshal(act.Metadata, &actUpdatedMetadata)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, expUpdatedMetadata["ReferenceID"], actUpdatedMetadata["ReferenceID"])
|
|
|
|
assert.Equal(t, expUpdatedMetadata["Wallet"], actUpdatedMetadata["Wallet"])
|
2022-04-26 21:23:27 +01:00
|
|
|
assert.WithinDuration(t, exp.Timestamp, act.Timestamp, time.Microsecond) // database timestamps use microsecond precision
|
|
|
|
assert.False(t, act.CreatedAt.IsZero())
|
|
|
|
}
|