mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-05-09 19:32:24 +00:00
229 lines
6.9 KiB
Go
229 lines
6.9 KiB
Go
package licensing
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestServiceActivate_ExchangesLegacyJWTOutsideDevMode(t *testing.T) {
|
|
t.Setenv("PULSE_LICENSE_DEV_MODE", "false")
|
|
setupTestPublicKey(t)
|
|
|
|
tests := []struct {
|
|
name string
|
|
planKey string
|
|
}{
|
|
{name: "lifetime grandfathered", planKey: "v5_lifetime_grandfathered"},
|
|
{name: "monthly grandfathered", planKey: "v5_pro_monthly_grandfathered"},
|
|
{name: "annual grandfathered", planKey: "v5_pro_annual_grandfathered"},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
svc := NewService()
|
|
licenseKey, err := GenerateLicenseForTesting("strict-v6@example.com", TierPro, 24*time.Hour)
|
|
if err != nil {
|
|
t.Fatalf("GenerateLicenseForTesting: %v", err)
|
|
}
|
|
|
|
grantJWT := makeTestGrantJWT(t, &GrantClaims{
|
|
LicenseID: "lic_test",
|
|
Tier: "pro",
|
|
PlanKey: tt.planKey,
|
|
State: "active",
|
|
IssuedAt: time.Now().Unix(),
|
|
ExpiresAt: time.Now().Add(72 * time.Hour).Unix(),
|
|
Email: "strict-v6@example.com",
|
|
})
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/v1/licenses/exchange" {
|
|
t.Fatalf("Path = %q, want /v1/licenses/exchange", r.URL.Path)
|
|
}
|
|
|
|
var req ExchangeLegacyLicenseRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
t.Fatalf("decode request: %v", err)
|
|
}
|
|
if req.LegacyLicenseKey != licenseKey {
|
|
t.Fatalf("LegacyLicenseKey = %q, want %q", req.LegacyLicenseKey, licenseKey)
|
|
}
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
_ = json.NewEncoder(w).Encode(ActivateInstallationResponse{
|
|
License: ActivateResponseLicense{
|
|
LicenseID: "lic_test",
|
|
State: "active",
|
|
Tier: "pro",
|
|
},
|
|
Installation: ActivateResponseInstallation{
|
|
InstallationID: "inst_test",
|
|
InstallationToken: "pit_live_test",
|
|
Status: "active",
|
|
},
|
|
Grant: GrantEnvelope{
|
|
JWT: grantJWT,
|
|
JTI: "grant_test",
|
|
ExpiresAt: time.Now().Add(72 * time.Hour).UTC().Format(time.RFC3339),
|
|
},
|
|
})
|
|
}))
|
|
defer server.Close()
|
|
|
|
svc.SetLicenseServerClient(NewLicenseServerClient(server.URL))
|
|
|
|
lic, err := svc.Activate(licenseKey)
|
|
if err != nil {
|
|
t.Fatalf("expected legacy JWT exchange in strict v6 mode, got %v", err)
|
|
}
|
|
if lic == nil {
|
|
t.Fatal("expected non-nil license after exchange")
|
|
}
|
|
if !svc.IsActivated() {
|
|
t.Fatal("expected strict v6 legacy exchange to produce activation state")
|
|
}
|
|
if got := svc.Current(); got == nil || got.Claims.LicenseID != "lic_test" {
|
|
t.Fatalf("expected exchanged license to be active, got %#v", got)
|
|
} else if got.Claims.PlanVersion != tt.planKey {
|
|
t.Fatalf("expected exchanged license plan_version to be preserved, got %q", got.Claims.PlanVersion)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestServiceActivate_AllowsLegacyJWTInDevMode(t *testing.T) {
|
|
t.Setenv("PULSE_LICENSE_DEV_MODE", "true")
|
|
|
|
svc := NewService()
|
|
licenseKey, err := GenerateLicenseForTesting("dev-jwt@example.com", TierPro, 24*time.Hour)
|
|
if err != nil {
|
|
t.Fatalf("GenerateLicenseForTesting: %v", err)
|
|
}
|
|
|
|
lic, err := svc.Activate(licenseKey)
|
|
if err != nil {
|
|
t.Fatalf("Activate in dev mode: %v", err)
|
|
}
|
|
if lic == nil {
|
|
t.Fatal("expected non-nil license in dev mode")
|
|
}
|
|
if svc.IsActivated() {
|
|
t.Fatal("expected dev JWT activation to remain non-activation-key mode")
|
|
}
|
|
}
|
|
|
|
func TestServiceActivate_RejectsMalformedLegacyKeyOutsideDevMode(t *testing.T) {
|
|
t.Setenv("PULSE_LICENSE_DEV_MODE", "false")
|
|
|
|
svc := NewService()
|
|
_, err := svc.Activate("not-a-jwt-or-activation-key")
|
|
if err == nil {
|
|
t.Fatal("expected malformed legacy key rejection in strict v6 mode")
|
|
}
|
|
if !strings.Contains(err.Error(), "activation key") {
|
|
t.Fatalf("expected activation-key guidance error, got %q", err.Error())
|
|
}
|
|
}
|
|
|
|
func TestServiceStatusCanonicalizesJWTCloudPlanVersionAndLimits(t *testing.T) {
|
|
svc := NewService()
|
|
svc.license = &License{
|
|
Claims: Claims{
|
|
Tier: TierCloud,
|
|
PlanVersion: "cloud_v1",
|
|
Limits: map[string]int64{
|
|
"max_monitored_systems": 999,
|
|
},
|
|
SubState: SubStateActive,
|
|
},
|
|
}
|
|
|
|
status := svc.Status()
|
|
if status.PlanVersion != "cloud_starter" {
|
|
t.Fatalf("status.PlanVersion=%q, want %q", status.PlanVersion, "cloud_starter")
|
|
}
|
|
if status.MaxMonitoredSystems != 10 {
|
|
t.Fatalf("status.MaxMonitoredSystems=%d, want %d", status.MaxMonitoredSystems, 10)
|
|
}
|
|
}
|
|
|
|
func TestServiceStatusMissingJWTCloudPlanFailsClosed(t *testing.T) {
|
|
svc := NewService()
|
|
svc.license = &License{
|
|
Claims: Claims{
|
|
Tier: TierCloud,
|
|
PlanVersion: " ",
|
|
SubState: SubStateActive,
|
|
},
|
|
}
|
|
|
|
status := svc.Status()
|
|
if status.PlanVersion != "" {
|
|
t.Fatalf("status.PlanVersion=%q, want empty", status.PlanVersion)
|
|
}
|
|
if status.MaxMonitoredSystems != UnknownPlanDefaultMonitoredSystemLimit {
|
|
t.Fatalf("status.MaxMonitoredSystems=%d, want %d", status.MaxMonitoredSystems, UnknownPlanDefaultMonitoredSystemLimit)
|
|
}
|
|
}
|
|
|
|
func TestServiceStatus_DevModeAdvertisesOnlyRuntimeEnabledFeaturesWithoutLicense(t *testing.T) {
|
|
t.Setenv("PULSE_DEV", "true")
|
|
t.Setenv("PULSE_MULTI_TENANT_ENABLED", "")
|
|
|
|
svc := NewService()
|
|
status := svc.Status()
|
|
|
|
if status.Valid {
|
|
t.Fatalf("status.Valid=%v, want false", status.Valid)
|
|
}
|
|
if status.Tier != TierFree {
|
|
t.Fatalf("status.Tier=%q, want %q", status.Tier, TierFree)
|
|
}
|
|
|
|
for _, feature := range devModeFeatures() {
|
|
if !containsStringValue(status.Features, feature) {
|
|
t.Fatalf("status.Features missing %q in dev mode: %v", feature, status.Features)
|
|
}
|
|
}
|
|
if svc.HasFeature(FeatureMultiTenant) {
|
|
t.Fatalf("HasFeature(%q)=true, want false when runtime flag is disabled", FeatureMultiTenant)
|
|
}
|
|
if containsStringValue(status.Features, FeatureMultiTenant) {
|
|
t.Fatalf("status.Features unexpectedly includes %q when runtime flag is disabled: %v", FeatureMultiTenant, status.Features)
|
|
}
|
|
for _, feature := range []string{FeatureMultiUser, FeatureWhiteLabel, FeatureUnlimited} {
|
|
if svc.HasFeature(feature) {
|
|
t.Fatalf("HasFeature(%q)=true, want false for non-runtime dev capability", feature)
|
|
}
|
|
if containsStringValue(status.Features, feature) {
|
|
t.Fatalf("status.Features unexpectedly includes %q in dev mode: %v", feature, status.Features)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestServiceStatus_DevModeIncludesMultiTenantWhenRuntimeEnabled(t *testing.T) {
|
|
t.Setenv("PULSE_DEV", "true")
|
|
t.Setenv("PULSE_MULTI_TENANT_ENABLED", "true")
|
|
|
|
svc := NewService()
|
|
status := svc.Status()
|
|
|
|
if !containsStringValue(status.Features, FeatureMultiTenant) {
|
|
t.Fatalf("status.Features missing %q when runtime flag is enabled: %v", FeatureMultiTenant, status.Features)
|
|
}
|
|
if !svc.HasFeature(FeatureMultiTenant) {
|
|
t.Fatalf("HasFeature(%q)=false, want true when runtime flag is enabled", FeatureMultiTenant)
|
|
}
|
|
}
|
|
|
|
func containsStringValue(values []string, want string) bool {
|
|
for _, value := range values {
|
|
if value == want {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|