3b37a23d7b
An endpoint has been added that returns whether a pricing package is available for a user to purchase. This will be used to conditionally skip the pricing plan selection step of the onboarding tour. Change-Id: I8c02a4e474e5f0f80778453b7daf674c8da64306
181 lines
5.3 KiB
Go
181 lines
5.3 KiB
Go
// Copyright (C) 2023 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package consoleapi_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"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())
|
|
})
|
|
}
|
|
})
|
|
}
|
|
|
|
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{
|
|
pkgPartner: {CouponID: stripecoinpayments.MockCouponID1, Price: 1000},
|
|
}
|
|
},
|
|
},
|
|
}, 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)
|
|
|
|
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, http.MethodGet, "http://"+planet.Satellites[0].API.Console.Listener.Addr().String()+"/api/v0/payments/package-available", nil)
|
|
require.NoError(t, err)
|
|
|
|
cookie := http.Cookie{
|
|
Name: "_tokenKey",
|
|
Path: "/",
|
|
Value: tokenInfo.Token.String(),
|
|
Expires: time.Now().AddDate(0, 0, 1),
|
|
}
|
|
req.AddCookie(&cookie)
|
|
|
|
client := http.Client{}
|
|
result, err := client.Do(req)
|
|
require.NoError(t, err)
|
|
require.Equal(t, http.StatusOK, result.StatusCode)
|
|
|
|
responseBody, err := io.ReadAll(result.Body)
|
|
require.NoError(t, err)
|
|
require.NoError(t, result.Body.Close())
|
|
|
|
var hasPackage bool
|
|
err = json.Unmarshal(responseBody, &hasPackage)
|
|
require.NoError(t, err)
|
|
require.Equal(t, tt.shouldHavePkg, hasPackage)
|
|
})
|
|
}
|
|
})
|
|
}
|