2021-10-26 14:30:19 +01:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2023-04-06 12:41:14 +01:00
|
|
|
package stripe_test
|
2021-10-26 14:30:19 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap/zaptest"
|
|
|
|
|
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/storj/private/testplanet"
|
|
|
|
"storj.io/storj/private/testredis"
|
|
|
|
"storj.io/storj/satellite/accounting"
|
|
|
|
"storj.io/storj/satellite/accounting/live"
|
|
|
|
"storj.io/storj/satellite/analytics"
|
|
|
|
"storj.io/storj/satellite/console"
|
|
|
|
"storj.io/storj/satellite/console/consoleauth"
|
2022-04-12 17:59:07 +01:00
|
|
|
"storj.io/storj/satellite/console/restkeys"
|
2021-10-26 14:30:19 +01:00
|
|
|
"storj.io/storj/satellite/payments"
|
|
|
|
"storj.io/storj/satellite/payments/paymentsconfig"
|
2023-04-06 12:41:14 +01:00
|
|
|
"storj.io/storj/satellite/payments/stripe"
|
2021-10-26 14:30:19 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSignupCouponCodes(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{SatelliteCount: 1}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
sat := planet.Satellites[0]
|
|
|
|
db := sat.DB
|
|
|
|
log := zaptest.NewLogger(t)
|
|
|
|
|
|
|
|
analyticsService := analytics.NewService(log, analytics.Config{}, "test-satellite")
|
|
|
|
|
|
|
|
redis, err := testredis.Mini(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(redis.Close)
|
|
|
|
|
|
|
|
cache, err := live.OpenCache(ctx, log.Named("cache"), live.Config{StorageBackend: "redis://" + redis.Addr() + "?db=0"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
projectLimitCache := accounting.NewProjectLimitCache(db.ProjectAccounting(), 0, 0, 0, accounting.ProjectLimitConfig{CacheCapacity: 100})
|
|
|
|
|
|
|
|
projectUsage := accounting.NewService(db.ProjectAccounting(), cache, projectLimitCache, *sat.API.Metainfo.Metabase, 5*time.Minute, -10*time.Second)
|
|
|
|
|
|
|
|
pc := paymentsconfig.Config{
|
2022-12-01 07:40:52 +00:00
|
|
|
UsagePrice: paymentsconfig.ProjectUsagePrice{
|
|
|
|
StorageTB: "10",
|
|
|
|
EgressTB: "45",
|
|
|
|
Segment: "0.0000022",
|
|
|
|
},
|
2021-10-26 14:30:19 +01:00
|
|
|
}
|
|
|
|
|
2022-12-01 07:40:52 +00:00
|
|
|
prices, err := pc.UsagePrice.ToModel()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
priceOverrides, err := pc.UsagePriceOverrides.ToModels()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-04-06 12:41:14 +01:00
|
|
|
paymentsService, err := stripe.NewService(
|
2021-10-26 14:30:19 +01:00
|
|
|
log.Named("payments.stripe:service"),
|
2023-04-06 12:41:14 +01:00
|
|
|
stripe.NewStripeMock(
|
2021-10-26 14:30:19 +01:00
|
|
|
db.StripeCoinPayments().Customers(),
|
|
|
|
db.Console().Users(),
|
|
|
|
),
|
|
|
|
pc.StripeCoinPayments,
|
|
|
|
db.StripeCoinPayments(),
|
2022-05-10 20:19:53 +01:00
|
|
|
db.Wallets(),
|
|
|
|
db.Billing(),
|
2021-10-26 14:30:19 +01:00
|
|
|
db.Console().Projects(),
|
2023-01-30 22:11:12 +00:00
|
|
|
db.Console().Users(),
|
2021-10-26 14:30:19 +01:00
|
|
|
db.ProjectAccounting(),
|
2022-12-01 07:40:52 +00:00
|
|
|
prices,
|
|
|
|
priceOverrides,
|
2023-01-30 22:11:12 +00:00
|
|
|
pc.PackagePlans.Packages,
|
2023-04-19 20:48:36 +01:00
|
|
|
pc.BonusRate,
|
|
|
|
nil,
|
|
|
|
)
|
2021-10-26 14:30:19 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
service, err := console.NewService(
|
|
|
|
log.Named("console"),
|
|
|
|
db.Console(),
|
2022-04-12 17:59:07 +01:00
|
|
|
restkeys.NewService(db.OIDC().OAuthTokens(), planet.Satellites[0].Config.RESTKeys),
|
2021-10-26 14:30:19 +01:00
|
|
|
db.ProjectAccounting(),
|
|
|
|
projectUsage,
|
|
|
|
sat.API.Buckets.Service,
|
|
|
|
paymentsService.Accounts(),
|
2022-04-28 03:54:56 +01:00
|
|
|
// TODO: do we need a payment deposit wallet here?
|
|
|
|
nil,
|
2022-08-15 15:41:19 +01:00
|
|
|
db.Billing(),
|
2021-10-26 14:30:19 +01:00
|
|
|
analyticsService,
|
2022-04-19 21:50:15 +01:00
|
|
|
consoleauth.NewService(consoleauth.Config{
|
|
|
|
TokenExpirationTime: 24 * time.Hour,
|
|
|
|
}, &consoleauth.Hmac{Secret: []byte("my-suppa-secret-key")}),
|
2022-07-14 14:44:06 +01:00
|
|
|
nil,
|
|
|
|
"",
|
2023-06-07 22:13:39 +01:00
|
|
|
"",
|
2021-10-26 14:30:19 +01:00
|
|
|
console.Config{PasswordCost: console.TestPasswordCost, DefaultProjectLimit: 5},
|
|
|
|
)
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
email string
|
|
|
|
signupPromoCode string
|
|
|
|
expectedCouponType payments.CouponType
|
|
|
|
}{
|
|
|
|
{"good signup promo code", "test1@mail.test", "promo1", payments.SignupCoupon},
|
|
|
|
{"bad signup promo code", "test2@mail.test", "badpromo", payments.NoCoupon},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range testCases {
|
|
|
|
tt := tt
|
|
|
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
createUser := console.CreateUser{
|
|
|
|
FullName: "Fullname",
|
|
|
|
ShortName: "Shortname",
|
|
|
|
Email: tt.email,
|
|
|
|
Password: "123a123",
|
|
|
|
SignupPromoCode: tt.signupPromoCode,
|
|
|
|
}
|
|
|
|
|
|
|
|
regToken, err := service.CreateRegToken(ctx, 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rootUser, err := service.CreateUser(ctx, createUser, regToken.Secret)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
couponType, err := paymentsService.Accounts().Setup(ctx, rootUser.ID, rootUser.Email, rootUser.SignupPromoCode)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, tt.expectedCouponType, couponType)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2023-03-22 20:23:44 +00:00
|
|
|
|
|
|
|
func TestUpdateGetPackage(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 0, UplinkCount: 1,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
accounts := planet.Satellites[0].API.Payments.Accounts
|
|
|
|
userID := planet.Uplinks[0].Projects[0].Owner.ID
|
|
|
|
|
|
|
|
var packagePlan string
|
|
|
|
var purchaseTime time.Time
|
|
|
|
packagePlan = "package-plan-1"
|
|
|
|
purchaseTime = time.Now()
|
|
|
|
|
|
|
|
require.NoError(t, accounts.UpdatePackage(ctx, userID, &packagePlan, &purchaseTime))
|
|
|
|
dbPackagePlan, dbPurchaseTime, err := accounts.GetPackageInfo(ctx, userID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, dbPackagePlan)
|
|
|
|
require.NotNil(t, dbPurchaseTime)
|
|
|
|
require.Equal(t, packagePlan, *dbPackagePlan)
|
|
|
|
require.Equal(t, purchaseTime.Truncate(time.Millisecond), dbPurchaseTime.Truncate(time.Millisecond))
|
|
|
|
|
|
|
|
require.NoError(t, accounts.UpdatePackage(ctx, userID, nil, nil))
|
|
|
|
dbPackagePlan, dbPurchaseTime, err = accounts.GetPackageInfo(ctx, userID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, dbPackagePlan)
|
|
|
|
require.Nil(t, dbPurchaseTime)
|
|
|
|
})
|
|
|
|
}
|