2020-05-19 08:42:07 +01:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package stripecoinpayments_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2022-09-06 13:43:09 +01:00
|
|
|
"storj.io/common/currency"
|
2020-05-19 08:42:07 +01:00
|
|
|
"storj.io/common/memory"
|
|
|
|
"storj.io/common/pb"
|
|
|
|
"storj.io/common/testcontext"
|
2022-05-10 20:19:53 +01:00
|
|
|
"storj.io/common/testrand"
|
|
|
|
"storj.io/storj/private/blockchain"
|
2020-05-19 08:42:07 +01:00
|
|
|
"storj.io/storj/private/testplanet"
|
|
|
|
"storj.io/storj/satellite"
|
2020-05-26 16:09:43 +01:00
|
|
|
"storj.io/storj/satellite/accounting"
|
|
|
|
"storj.io/storj/satellite/console"
|
2021-04-21 13:42:57 +01:00
|
|
|
"storj.io/storj/satellite/metabase"
|
2022-05-10 20:19:53 +01:00
|
|
|
"storj.io/storj/satellite/payments/billing"
|
2020-05-19 08:42:07 +01:00
|
|
|
"storj.io/storj/satellite/payments/stripecoinpayments"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestService_InvoiceElementsProcessing(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
2020-05-26 16:09:43 +01:00
|
|
|
SatelliteCount: 1, StorageNodeCount: 0, UplinkCount: 0,
|
2020-05-19 08:42:07 +01:00
|
|
|
Reconfigure: testplanet.Reconfigure{
|
|
|
|
Satellite: func(log *zap.Logger, index int, config *satellite.Config) {
|
|
|
|
config.Payments.StripeCoinPayments.ListingLimit = 4
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
satellite := planet.Satellites[0]
|
|
|
|
|
2020-05-30 18:38:15 +01:00
|
|
|
// pick a specific date so that it doesn't fail if it's the last day of the month
|
|
|
|
// keep month + 1 because user needs to be created before calculation
|
2021-01-04 09:47:31 +00:00
|
|
|
period := time.Date(time.Now().Year(), time.Now().Month()+1, 20, 0, 0, 0, 0, time.UTC)
|
2020-05-30 18:38:15 +01:00
|
|
|
|
2020-05-19 08:42:07 +01:00
|
|
|
numberOfProjects := 19
|
2021-08-27 01:51:26 +01:00
|
|
|
// generate test data, each user has one project and some credits
|
2020-05-19 08:42:07 +01:00
|
|
|
for i := 0; i < numberOfProjects; i++ {
|
2020-07-24 10:40:17 +01:00
|
|
|
user, err := satellite.AddUser(ctx, console.CreateUser{
|
|
|
|
FullName: "testuser" + strconv.Itoa(i),
|
|
|
|
Email: "user@test" + strconv.Itoa(i),
|
|
|
|
}, 1)
|
2020-05-19 08:42:07 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
project, err := satellite.AddProject(ctx, user.ID, "testproject-"+strconv.Itoa(i))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = satellite.DB.Orders().UpdateBucketBandwidthSettle(ctx, project.ID, []byte("testbucket"),
|
2021-05-29 23:16:12 +01:00
|
|
|
pb.PieceAction_GET, int64(i+10)*memory.GiB.Int64(), 0, period)
|
2020-05-19 08:42:07 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2022-04-28 03:54:56 +01:00
|
|
|
satellite.API.Payments.StripeService.SetNow(func() time.Time {
|
2020-05-19 08:42:07 +01:00
|
|
|
return time.Date(period.Year(), period.Month()+1, 1, 0, 0, 0, 0, time.UTC)
|
|
|
|
})
|
2022-04-28 03:54:56 +01:00
|
|
|
err := satellite.API.Payments.StripeService.PrepareInvoiceProjectRecords(ctx, period)
|
2020-05-19 08:42:07 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
start := time.Date(period.Year(), period.Month(), 1, 0, 0, 0, 0, time.UTC)
|
2021-08-25 20:35:57 +01:00
|
|
|
end := time.Date(period.Year(), period.Month()+1, 1, 0, 0, 0, 0, time.UTC)
|
2020-05-19 08:42:07 +01:00
|
|
|
|
|
|
|
// check if we have project record for each project
|
|
|
|
recordsPage, err := satellite.DB.StripeCoinPayments().ProjectRecords().ListUnapplied(ctx, 0, 40, start, end)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, numberOfProjects, len(recordsPage.Records))
|
|
|
|
|
2022-04-28 03:54:56 +01:00
|
|
|
err = satellite.API.Payments.StripeService.InvoiceApplyProjectRecords(ctx, period)
|
2020-05-19 08:42:07 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// verify that we applied all unapplied project records
|
|
|
|
recordsPage, err = satellite.DB.StripeCoinPayments().ProjectRecords().ListUnapplied(ctx, 0, 40, start, end)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, len(recordsPage.Records))
|
|
|
|
})
|
|
|
|
}
|
2020-05-26 16:09:43 +01:00
|
|
|
|
|
|
|
func TestService_InvoiceUserWithManyProjects(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 0, UplinkCount: 0,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
|
|
|
Satellite: func(log *zap.Logger, index int, config *satellite.Config) {
|
|
|
|
config.Payments.StripeCoinPayments.ListingLimit = 4
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
satellite := planet.Satellites[0]
|
|
|
|
payments := satellite.API.Payments
|
|
|
|
|
2020-05-30 18:38:15 +01:00
|
|
|
// pick a specific date so that it doesn't fail if it's the last day of the month
|
|
|
|
// keep month + 1 because user needs to be created before calculation
|
2021-01-04 09:47:31 +00:00
|
|
|
period := time.Date(time.Now().Year(), time.Now().Month()+1, 20, 0, 0, 0, 0, time.UTC)
|
2020-05-30 18:38:15 +01:00
|
|
|
|
2022-04-28 03:54:56 +01:00
|
|
|
payments.StripeService.SetNow(func() time.Time {
|
2020-05-26 16:09:43 +01:00
|
|
|
return time.Date(period.Year(), period.Month()+1, 1, 0, 0, 0, 0, time.UTC)
|
|
|
|
})
|
|
|
|
start := time.Date(period.Year(), period.Month(), 1, 0, 0, 0, 0, time.UTC)
|
2021-08-25 20:35:57 +01:00
|
|
|
end := time.Date(period.Year(), period.Month()+1, 1, 0, 0, 0, 0, time.UTC)
|
2020-05-26 16:09:43 +01:00
|
|
|
|
|
|
|
numberOfProjects := 5
|
|
|
|
storageHours := 24
|
|
|
|
|
2020-07-24 10:40:17 +01:00
|
|
|
user, err := satellite.AddUser(ctx, console.CreateUser{
|
|
|
|
FullName: "testuser",
|
|
|
|
Email: "user@test",
|
|
|
|
}, numberOfProjects)
|
2020-05-26 16:09:43 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
projects := make([]*console.Project, numberOfProjects)
|
|
|
|
projectsEgress := make([]int64, len(projects))
|
|
|
|
projectsStorage := make([]int64, len(projects))
|
|
|
|
for i := 0; i < len(projects); i++ {
|
|
|
|
projects[i], err = satellite.AddProject(ctx, user.ID, "testproject-"+strconv.Itoa(i))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// generate egress
|
|
|
|
projectsEgress[i] = int64(i+10) * memory.GiB.Int64()
|
|
|
|
err = satellite.DB.Orders().UpdateBucketBandwidthSettle(ctx, projects[i].ID, []byte("testbucket"),
|
2021-05-29 23:16:12 +01:00
|
|
|
pb.PieceAction_GET, projectsEgress[i], 0, period)
|
2020-05-26 16:09:43 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// generate storage
|
|
|
|
// we need at least two tallies across time to calculate storage
|
|
|
|
projectsStorage[i] = int64(i+1) * memory.TiB.Int64()
|
|
|
|
tally := &accounting.BucketTally{
|
2020-08-31 11:14:20 +01:00
|
|
|
BucketLocation: metabase.BucketLocation{
|
|
|
|
ProjectID: projects[i].ID,
|
|
|
|
BucketName: "testbucket",
|
|
|
|
},
|
2021-10-20 23:54:34 +01:00
|
|
|
TotalBytes: projectsStorage[i],
|
|
|
|
TotalSegments: int64(i + 1),
|
2020-05-26 16:09:43 +01:00
|
|
|
}
|
2020-08-31 11:14:20 +01:00
|
|
|
tallies := map[metabase.BucketLocation]*accounting.BucketTally{
|
|
|
|
{}: tally,
|
2020-05-26 16:09:43 +01:00
|
|
|
}
|
|
|
|
err = satellite.DB.ProjectAccounting().SaveTallies(ctx, period, tallies)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = satellite.DB.ProjectAccounting().SaveTallies(ctx, period.Add(time.Duration(storageHours)*time.Hour), tallies)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// verify that projects don't have records yet
|
|
|
|
projectRecord, err := satellite.DB.StripeCoinPayments().ProjectRecords().Get(ctx, projects[i].ID, start, end)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, projectRecord)
|
|
|
|
}
|
|
|
|
|
2022-04-28 03:54:56 +01:00
|
|
|
err = payments.StripeService.PrepareInvoiceProjectRecords(ctx, period)
|
2020-05-26 16:09:43 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
for i := 0; i < len(projects); i++ {
|
|
|
|
projectRecord, err := satellite.DB.StripeCoinPayments().ProjectRecords().Get(ctx, projects[i].ID, start, end)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, projectRecord)
|
|
|
|
require.Equal(t, projects[i].ID, projectRecord.ProjectID)
|
|
|
|
require.Equal(t, projectsEgress[i], projectRecord.Egress)
|
|
|
|
|
|
|
|
expectedStorage := float64(projectsStorage[i] * int64(storageHours))
|
|
|
|
require.Equal(t, expectedStorage, projectRecord.Storage)
|
|
|
|
|
2021-10-20 23:54:34 +01:00
|
|
|
expectedSegmentsCount := float64((i + 1) * storageHours)
|
|
|
|
require.Equal(t, expectedSegmentsCount, projectRecord.Segments)
|
2020-05-26 16:09:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// run all parts of invoice generation to see if there are no unexpected errors
|
2022-04-28 03:54:56 +01:00
|
|
|
err = payments.StripeService.InvoiceApplyProjectRecords(ctx, period)
|
2020-05-26 16:09:43 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-04-28 03:54:56 +01:00
|
|
|
err = payments.StripeService.CreateInvoices(ctx, period)
|
2020-05-26 16:09:43 +01:00
|
|
|
require.NoError(t, err)
|
2022-05-10 20:19:53 +01:00
|
|
|
|
2020-05-26 16:09:43 +01:00
|
|
|
})
|
|
|
|
}
|
2020-05-29 11:29:03 +01:00
|
|
|
|
2020-06-03 18:01:54 +01:00
|
|
|
func TestService_ProjectsWithMembers(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 0, UplinkCount: 0,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
|
|
|
Satellite: func(log *zap.Logger, index int, config *satellite.Config) {
|
|
|
|
config.Payments.StripeCoinPayments.ListingLimit = 4
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
satellite := planet.Satellites[0]
|
|
|
|
|
|
|
|
// pick a specific date so that it doesn't fail if it's the last day of the month
|
|
|
|
// keep month + 1 because user needs to be created before calculation
|
2021-01-04 09:47:31 +00:00
|
|
|
period := time.Date(time.Now().Year(), time.Now().Month()+1, 20, 0, 0, 0, 0, time.UTC)
|
2020-06-03 18:01:54 +01:00
|
|
|
|
|
|
|
numberOfUsers := 5
|
|
|
|
users := make([]*console.User, numberOfUsers)
|
|
|
|
projects := make([]*console.Project, numberOfUsers)
|
|
|
|
for i := 0; i < numberOfUsers; i++ {
|
|
|
|
var err error
|
2020-07-24 10:40:17 +01:00
|
|
|
|
|
|
|
users[i], err = satellite.AddUser(ctx, console.CreateUser{
|
|
|
|
FullName: "testuser" + strconv.Itoa(i),
|
|
|
|
Email: "user@test" + strconv.Itoa(i),
|
|
|
|
}, 1)
|
2020-06-03 18:01:54 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
projects[i], err = satellite.AddProject(ctx, users[i].ID, "testproject-"+strconv.Itoa(i))
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// all users are members in all projects
|
|
|
|
for _, project := range projects {
|
|
|
|
for _, user := range users {
|
|
|
|
if project.OwnerID != user.ID {
|
|
|
|
_, err := satellite.DB.Console().ProjectMembers().Insert(ctx, user.ID, project.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-28 03:54:56 +01:00
|
|
|
satellite.API.Payments.StripeService.SetNow(func() time.Time {
|
2020-06-03 18:01:54 +01:00
|
|
|
return time.Date(period.Year(), period.Month()+1, 1, 0, 0, 0, 0, time.UTC)
|
|
|
|
})
|
2022-04-28 03:54:56 +01:00
|
|
|
err := satellite.API.Payments.StripeService.PrepareInvoiceProjectRecords(ctx, period)
|
2020-06-03 18:01:54 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
start := time.Date(period.Year(), period.Month(), 1, 0, 0, 0, 0, time.UTC)
|
2021-08-25 20:35:57 +01:00
|
|
|
end := time.Date(period.Year(), period.Month()+1, 1, 0, 0, 0, 0, time.UTC)
|
2020-06-03 18:01:54 +01:00
|
|
|
|
|
|
|
recordsPage, err := satellite.DB.StripeCoinPayments().ProjectRecords().ListUnapplied(ctx, 0, 40, start, end)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, len(projects), len(recordsPage.Records))
|
|
|
|
})
|
|
|
|
}
|
2020-05-27 13:08:37 +01:00
|
|
|
|
|
|
|
func TestService_InvoiceItemsFromProjectRecord(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 0, UplinkCount: 0,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
satellite := planet.Satellites[0]
|
|
|
|
|
|
|
|
// these numbers are fraction of cents, not of dollars.
|
|
|
|
expectedStoragePrice := 0.001
|
|
|
|
expectedEgressPrice := 0.0045
|
2021-10-20 23:54:34 +01:00
|
|
|
expectedSegmentPrice := 0.00022
|
2020-05-27 13:08:37 +01:00
|
|
|
|
|
|
|
type TestCase struct {
|
2021-10-20 23:54:34 +01:00
|
|
|
Storage float64
|
|
|
|
Egress int64
|
|
|
|
Segments float64
|
2020-05-27 13:08:37 +01:00
|
|
|
|
2021-10-20 23:54:34 +01:00
|
|
|
StorageQuantity int64
|
|
|
|
EgressQuantity int64
|
|
|
|
SegmentsQuantity int64
|
2020-05-27 13:08:37 +01:00
|
|
|
}
|
|
|
|
|
2022-08-01 13:41:12 +01:00
|
|
|
testCases := []TestCase{
|
2020-05-27 13:08:37 +01:00
|
|
|
{}, // all zeros
|
|
|
|
{
|
|
|
|
Storage: 10000000000, // Byte-Hours
|
|
|
|
// storage quantity is calculated to Megabyte-Months
|
|
|
|
// (10000000000 / 1000000) Byte-Hours to Megabytes-Hours
|
|
|
|
// round(10000 / 720) Megabytes-Hours to Megabyte-Months, 720 - hours in month
|
|
|
|
StorageQuantity: 14, // Megabyte-Months
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Egress: 134 * memory.GB.Int64(), // Bytes
|
|
|
|
// egress quantity is calculated to Megabytes
|
|
|
|
// (134000000000 / 1000000) Bytes to Megabytes
|
|
|
|
EgressQuantity: 134000, // Megabytes
|
|
|
|
},
|
|
|
|
{
|
2021-10-20 23:54:34 +01:00
|
|
|
Segments: 400000, // Segment-Hours
|
|
|
|
// object quantity is calculated to Segment-Months
|
|
|
|
// round(400000 / 720) Segment-Hours to Segment-Months, 720 - hours in month
|
|
|
|
SegmentsQuantity: 556, // Segment-Months
|
2020-05-27 13:08:37 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
record := stripecoinpayments.ProjectRecord{
|
2021-10-20 23:54:34 +01:00
|
|
|
Storage: tc.Storage,
|
|
|
|
Egress: tc.Egress,
|
|
|
|
Segments: tc.Segments,
|
2020-05-27 13:08:37 +01:00
|
|
|
}
|
|
|
|
|
2022-04-28 03:54:56 +01:00
|
|
|
items := satellite.API.Payments.StripeService.InvoiceItemsFromProjectRecord("project name", record)
|
2020-05-27 13:08:37 +01:00
|
|
|
|
|
|
|
require.Equal(t, tc.StorageQuantity, *items[0].Quantity)
|
|
|
|
require.Equal(t, expectedStoragePrice, *items[0].UnitAmountDecimal)
|
|
|
|
|
|
|
|
require.Equal(t, tc.EgressQuantity, *items[1].Quantity)
|
|
|
|
require.Equal(t, expectedEgressPrice, *items[1].UnitAmountDecimal)
|
|
|
|
|
2021-10-20 23:54:34 +01:00
|
|
|
require.Equal(t, tc.SegmentsQuantity, *items[2].Quantity)
|
|
|
|
require.Equal(t, expectedSegmentPrice, *items[2].UnitAmountDecimal)
|
2020-05-27 13:08:37 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-05-10 20:19:53 +01:00
|
|
|
|
|
|
|
func TestService_InvoiceItemsFromZeroTokenBalance(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 0, UplinkCount: 0,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
|
|
|
Satellite: func(log *zap.Logger, index int, config *satellite.Config) {
|
|
|
|
config.Payments.StripeCoinPayments.ListingLimit = 4
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
satellite := planet.Satellites[0]
|
|
|
|
payments := satellite.API.Payments
|
|
|
|
|
|
|
|
user, err := satellite.AddUser(ctx, console.CreateUser{
|
|
|
|
FullName: "testuser",
|
|
|
|
Email: "user@test",
|
|
|
|
}, 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// setup storjscan wallet
|
|
|
|
address, err := blockchain.BytesToAddress(testrand.Bytes(20))
|
|
|
|
require.NoError(t, err)
|
|
|
|
userID := user.ID
|
|
|
|
err = satellite.DB.Wallets().Add(ctx, userID, address)
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = satellite.DB.Billing().Insert(ctx, billing.Transaction{
|
|
|
|
UserID: userID,
|
2022-09-06 13:43:09 +01:00
|
|
|
Amount: currency.AmountFromBaseUnits(1000, currency.USDollars),
|
2022-05-10 20:19:53 +01:00
|
|
|
Description: "token payment credit",
|
|
|
|
Source: "storjscan",
|
|
|
|
Status: billing.TransactionStatusCompleted,
|
|
|
|
Type: billing.TransactionTypeCredit,
|
|
|
|
Metadata: nil,
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// run apply token balance to see if there are no unexpected errors
|
|
|
|
err = payments.StripeService.InvoiceApplyTokenBalance(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
}
|