storj/satellite/console/consoleweb/consoleapi/payments_test.go
Cameron 8842985571 satellite/console/consoleweb: create purchase-package endpoint
Add new purchase-package endpoint to Server. The endpoint can be enabled
or disabled by a new config, --console.pricing-packages-enabled.
The purchase-package endpoint applies a coupon and adds and charges a
credit card if user's useragent is a partner with a configured package
plan.

github issue: https://github.com/storj/storj-private/issues/125

Change-Id: I0d6ccccd6874ddba360c45f338fd1c44f95e135a
2023-02-23 15:42:36 -05:00

111 lines
3.2 KiB
Go

// Copyright (C) 2023 Storj Labs, Inc.
// See LICENSE for copying information.
package consoleapi_test
import (
"fmt"
"net/http"
"strings"
"testing"
"time"
"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"
"storj.io/storj/satellite/payments/stripecoinpayments"
)
func Test_PurchasePackage(t *testing.T) {
partner := "partner1"
partner2 := "partner2"
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
config.Payments.StripeCoinPayments.StripeFreeTierCouponID = stripecoinpayments.MockCouponID1
config.Payments.PackagePlans.Packages = map[string]payments.PackagePlan{
partner: {CouponID: stripecoinpayments.MockCouponID2, Price: 1000},
partner2: {CouponID: "invalidCouponID", Price: 1000},
}
},
},
}, 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,
},
{
// partner2's coupon ID configured above in Reconfigure does not exist in underlying
// stipe mock coupons list.
"Coupon doesn't exist", validCardToken, partner2,
http.StatusInternalServerError,
},
{
"Add credit card fails", stripecoinpayments.TestPaymentMethodsNewFailure, partner,
http.StatusInternalServerError,
},
{
"Purchase fails", stripecoinpayments.MockInvoicesPayFailure, partner,
http.StatusInternalServerError,
},
{
"Success", validCardToken, partner,
http.StatusOK,
},
}
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)
userCtx, err := sat.UserContext(ctx, user.ID)
require.NoError(t, err)
tokenInfo, err := sat.API.Console.Service.Token(ctx, console.AuthUser{Email: user.Email, Password: user.FullName})
require.NoError(t, err)
req, err := http.NewRequestWithContext(userCtx, "POST", "http://"+planet.Satellites[0].API.Console.Listener.Addr().String()+"/api/v0/payments/purchase-package", strings.NewReader(tt.cardToken))
require.NoError(t, err)
expire := time.Now().AddDate(0, 0, 1)
cookie := http.Cookie{
Name: "_tokenKey",
Path: "/",
Value: tokenInfo.Token.String(),
Expires: expire,
}
req.AddCookie(&cookie)
client := http.Client{}
result, err := client.Do(req)
require.NoError(t, err)
require.Equal(t, tt.expectedStatus, result.StatusCode)
require.NoError(t, result.Body.Close())
})
}
})
}