satellite/console/consoleweb: add pricing package availability endpoint
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
This commit is contained in:
parent
fb471d7d8d
commit
3b37a23d7b
@ -517,6 +517,26 @@ func (p *Payments) PurchasePackage(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// PackageAvailable returns whether a package plan is configured for the user's partner.
|
||||
func (p *Payments) PackageAvailable(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
var err error
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
u, err := console.GetUser(ctx)
|
||||
if err != nil {
|
||||
p.serveJSONError(w, http.StatusUnauthorized, err)
|
||||
return
|
||||
}
|
||||
|
||||
pkg, err := p.packagePlans.Get(u.UserAgent)
|
||||
hasPkg := err == nil && pkg != payments.PackagePlan{}
|
||||
|
||||
if err = json.NewEncoder(w).Encode(hasPkg); err != nil {
|
||||
p.log.Error("failed to encode package plan checking response", zap.Error(ErrPaymentsAPI.Wrap(err)))
|
||||
}
|
||||
}
|
||||
|
||||
// serveJSONError writes JSON error to response output stream.
|
||||
func (p *Payments) serveJSONError(w http.ResponseWriter, status int, err error) {
|
||||
web.ServeJSONError(p.log, w, status, err)
|
||||
|
@ -4,7 +4,9 @@
|
||||
package consoleapi_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
@ -108,3 +110,71 @@ func Test_PurchasePackage(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
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)
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -323,6 +323,7 @@ func NewServer(logger *zap.Logger, config Config, service *console.Service, oidc
|
||||
paymentsRouter.HandleFunc("/pricing", paymentController.GetProjectUsagePriceModel).Methods(http.MethodGet)
|
||||
if config.PricingPackagesEnabled {
|
||||
paymentsRouter.HandleFunc("/purchase-package", paymentController.PurchasePackage).Methods(http.MethodPost)
|
||||
paymentsRouter.HandleFunc("/package-available", paymentController.PackageAvailable).Methods(http.MethodGet)
|
||||
}
|
||||
|
||||
bucketsController := consoleapi.NewBuckets(logger, service)
|
||||
|
Loading…
Reference in New Issue
Block a user