2023-01-26 18:31:13 +00:00
|
|
|
// Copyright (C) 2023 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package consoleapi_test
|
|
|
|
|
|
|
|
import (
|
2023-03-02 15:49:45 +00:00
|
|
|
"encoding/json"
|
2023-01-26 18:31:13 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/storj/private/testplanet"
|
|
|
|
"storj.io/storj/satellite"
|
|
|
|
"storj.io/storj/satellite/console"
|
|
|
|
"storj.io/storj/satellite/payments"
|
2023-04-06 12:41:14 +01:00
|
|
|
"storj.io/storj/satellite/payments/stripe"
|
2023-01-26 18:31:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_PurchasePackage(t *testing.T) {
|
|
|
|
partner := "partner1"
|
|
|
|
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 0, UplinkCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
|
|
|
Satellite: func(log *zap.Logger, index int, config *satellite.Config) {
|
|
|
|
config.Console.OpenRegistrationEnabled = true
|
|
|
|
config.Console.RateLimit.Burst = 10
|
2023-04-06 12:41:14 +01:00
|
|
|
config.Payments.StripeCoinPayments.StripeFreeTierCouponID = stripe.MockCouponID1
|
2023-01-26 18:31:13 +00:00
|
|
|
config.Payments.PackagePlans.Packages = map[string]payments.PackagePlan{
|
2023-03-22 15:28:52 +00:00
|
|
|
partner: {Credit: 2000, Price: 1000},
|
2023-01-26 18:31:13 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
sat := planet.Satellites[0]
|
|
|
|
validCardToken := "testValidCardToken"
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name, cardToken, partner string
|
|
|
|
expectedStatus int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
// "unknownPartner" does not exist in paymentsconfig.PackagePlans
|
|
|
|
"No matching package plan for partner", validCardToken, "unknownPartner",
|
|
|
|
http.StatusNotFound,
|
|
|
|
},
|
|
|
|
{
|
2023-04-06 12:41:14 +01:00
|
|
|
"Add credit card fails", stripe.TestPaymentMethodsNewFailure, partner,
|
2023-01-26 18:31:13 +00:00
|
|
|
http.StatusInternalServerError,
|
|
|
|
},
|
|
|
|
{
|
2023-04-06 12:41:14 +01:00
|
|
|
"Purchase fails", stripe.MockInvoicesPayFailure, partner,
|
2023-01-26 18:31:13 +00:00
|
|
|
http.StatusInternalServerError,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Success", validCardToken, partner,
|
|
|
|
http.StatusOK,
|
|
|
|
},
|
2023-03-22 15:28:52 +00:00
|
|
|
{
|
|
|
|
"Subsequent request succeeds", validCardToken, partner,
|
|
|
|
http.StatusOK,
|
|
|
|
},
|
2023-01-26 18:31:13 +00:00
|
|
|
}
|
|
|
|
for i, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
user, err := sat.AddUser(ctx, console.CreateUser{
|
|
|
|
FullName: "test_name",
|
|
|
|
ShortName: "",
|
|
|
|
Email: fmt.Sprintf("test%d@storj.test", i),
|
|
|
|
UserAgent: []byte(tt.partner),
|
|
|
|
}, 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-09-01 20:17:39 +01:00
|
|
|
_, status, err := doRequestWithAuth(ctx, t, sat, user, http.MethodPost, "payments/purchase-package", strings.NewReader(tt.cardToken))
|
2023-01-26 18:31:13 +00:00
|
|
|
require.NoError(t, err)
|
2023-09-01 20:17:39 +01:00
|
|
|
require.Equal(t, tt.expectedStatus, status)
|
2023-01-26 18:31:13 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2023-03-02 15:49:45 +00:00
|
|
|
|
|
|
|
func Test_PackageAvailable(t *testing.T) {
|
|
|
|
const pkgPartner = "partner"
|
|
|
|
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
|
|
|
Satellite: func(log *zap.Logger, index int, config *satellite.Config) {
|
|
|
|
config.Payments.PackagePlans.Packages = map[string]payments.PackagePlan{
|
2023-03-22 15:28:52 +00:00
|
|
|
pkgPartner: {Credit: 2000, Price: 1000},
|
2023-03-02 15:49:45 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
sat := planet.Satellites[0]
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
partner string
|
|
|
|
shouldHavePkg bool
|
|
|
|
}{
|
|
|
|
{"Partnered", pkgPartner, true},
|
|
|
|
{"Unartnered", "", false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
user, err := sat.AddUser(ctx, console.CreateUser{
|
|
|
|
FullName: "Test User",
|
|
|
|
Email: fmt.Sprintf("test%d@storj.test", i),
|
|
|
|
UserAgent: []byte(tt.partner),
|
|
|
|
}, 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-09-01 20:17:39 +01:00
|
|
|
body, status, err := doRequestWithAuth(ctx, t, sat, user, http.MethodGet, "payments/package-available", nil)
|
2023-03-02 15:49:45 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-09-01 20:17:39 +01:00
|
|
|
require.Equal(t, http.StatusOK, status)
|
2023-03-02 15:49:45 +00:00
|
|
|
|
|
|
|
var hasPackage bool
|
2023-09-01 20:17:39 +01:00
|
|
|
err = json.Unmarshal(body, &hasPackage)
|
2023-03-02 15:49:45 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tt.shouldHavePkg, hasPackage)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|