@@ -0,0 +1,159 @@ |
| 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | + |
| 3 | +package entitlements_test |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "log/slog" |
| 8 | + "net/http/httptest" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/tenseleyFlow/shithub/internal/billing" |
| 12 | + "github.com/tenseleyFlow/shithub/internal/entitlements" |
| 13 | +) |
| 14 | + |
| 15 | +func TestAppliesToOrgOnlyFeatures(t *testing.T) { |
| 16 | + t.Parallel() |
| 17 | + orgOnly := []entitlements.Feature{ |
| 18 | + entitlements.FeatureSecretTeams, |
| 19 | + entitlements.FeatureActionsOrgSecrets, |
| 20 | + entitlements.FeatureActionsOrgVariables, |
| 21 | + entitlements.FeaturePrivateCollaboration, |
| 22 | + entitlements.FeatureStorageQuota, |
| 23 | + entitlements.FeatureActionsMinutesQuota, |
| 24 | + } |
| 25 | + for _, f := range orgOnly { |
| 26 | + if !entitlements.FeatureAppliesToKind(f, billing.SubjectKindOrg) { |
| 27 | + t.Errorf("%s should apply to org kind", f) |
| 28 | + } |
| 29 | + if entitlements.FeatureAppliesToKind(f, billing.SubjectKindUser) { |
| 30 | + t.Errorf("%s should NOT apply to user kind", f) |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func TestAppliesToCrossKindFeatures(t *testing.T) { |
| 36 | + t.Parallel() |
| 37 | + crossKind := []entitlements.Feature{ |
| 38 | + entitlements.FeatureAdvancedBranchProtection, |
| 39 | + entitlements.FeatureRequiredReviewers, |
| 40 | + } |
| 41 | + for _, f := range crossKind { |
| 42 | + if !entitlements.FeatureAppliesToKind(f, billing.SubjectKindOrg) { |
| 43 | + t.Errorf("%s should apply to org kind", f) |
| 44 | + } |
| 45 | + if !entitlements.FeatureAppliesToKind(f, billing.SubjectKindUser) { |
| 46 | + t.Errorf("%s should apply to user kind", f) |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestDeprecatedAliasesPointAtCanonical(t *testing.T) { |
| 52 | + t.Parallel() |
| 53 | + // SP-era constants are aliases for the renamed ones. Code that |
| 54 | + // uses FeatureOrgSecretTeams must read the same value as |
| 55 | + // FeatureSecretTeams. |
| 56 | + cases := []struct { |
| 57 | + legacy, canonical entitlements.Feature |
| 58 | + }{ |
| 59 | + {entitlements.FeatureOrgSecretTeams, entitlements.FeatureSecretTeams}, |
| 60 | + {entitlements.FeatureOrgAdvancedBranchProtection, entitlements.FeatureAdvancedBranchProtection}, |
| 61 | + {entitlements.FeatureOrgRequiredReviewers, entitlements.FeatureRequiredReviewers}, |
| 62 | + {entitlements.FeatureOrgActionsSecrets, entitlements.FeatureActionsOrgSecrets}, |
| 63 | + {entitlements.FeatureOrgActionsVariables, entitlements.FeatureActionsOrgVariables}, |
| 64 | + {entitlements.FeatureOrgPrivateCollaboration, entitlements.FeaturePrivateCollaboration}, |
| 65 | + {entitlements.FeatureOrgStorageQuota, entitlements.FeatureStorageQuota}, |
| 66 | + {entitlements.FeatureOrgActionsMinutesQuota, entitlements.FeatureActionsMinutesQuota}, |
| 67 | + } |
| 68 | + for _, tc := range cases { |
| 69 | + if tc.legacy != tc.canonical { |
| 70 | + t.Errorf("alias %q != canonical %q", tc.legacy, tc.canonical) |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// TestRequirePrincipalFeatureUnknownKind verifies the short-circuit |
| 76 | +// when the feature doesn't apply to the principal's kind. Org-only |
| 77 | +// features on personal repos must allow without logging — they |
| 78 | +// represent "this resource type isn't gateable here." |
| 79 | +func TestRequirePrincipalFeatureUnknownKind(t *testing.T) { |
| 80 | + t.Parallel() |
| 81 | + w := httptest.NewRecorder() |
| 82 | + ok, res := entitlements.RequirePrincipalFeature( |
| 83 | + context.Background(), |
| 84 | + w, |
| 85 | + nil, // pool unused on short-circuit path |
| 86 | + billing.PrincipalForUser(42), |
| 87 | + entitlements.FeatureSecretTeams, // org-only |
| 88 | + entitlements.NewRequireOpts("label", ""), |
| 89 | + ) |
| 90 | + if !ok { |
| 91 | + t.Fatalf("inapplicable feature should short-circuit ok=true") |
| 92 | + } |
| 93 | + if !res.UnknownKindForFeature { |
| 94 | + t.Errorf("UnknownKindForFeature should be true") |
| 95 | + } |
| 96 | + if res.WouldDenyButLog { |
| 97 | + t.Errorf("WouldDenyButLog should be false for inapplicable feature") |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +// TestPrincipalUpgradeBannerForUser verifies the kind-aware banner |
| 102 | +// copy: user kind says "Pro" and points at /settings/billing; org |
| 103 | +// kind says "Team" and points at the org settings page. |
| 104 | +func TestPrincipalUpgradeBannerForUser(t *testing.T) { |
| 105 | + t.Parallel() |
| 106 | + decision := entitlements.Decision{Reason: entitlements.ReasonUpgradeRequired} |
| 107 | + user := billing.PrincipalForUser(7) |
| 108 | + banner := decision.PrincipalUpgradeBanner("Required reviewers", user, "") |
| 109 | + if banner.ActionHref != "/settings/billing" { |
| 110 | + t.Errorf("user banner href: got %q, want /settings/billing", banner.ActionHref) |
| 111 | + } |
| 112 | + if banner.Message == "" || !contains(banner.Message, "Pro") { |
| 113 | + t.Errorf("user banner message should mention Pro: %q", banner.Message) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func TestPrincipalUpgradeBannerForOrg(t *testing.T) { |
| 118 | + t.Parallel() |
| 119 | + decision := entitlements.Decision{Reason: entitlements.ReasonUpgradeRequired} |
| 120 | + org := billing.PrincipalForOrg(7) |
| 121 | + banner := decision.PrincipalUpgradeBanner("Required reviewers", org, "acme") |
| 122 | + if banner.ActionHref != "/organizations/acme/settings/billing" { |
| 123 | + t.Errorf("org banner href: got %q", banner.ActionHref) |
| 124 | + } |
| 125 | + if !contains(banner.Message, "Team") { |
| 126 | + t.Errorf("org banner message should mention Team: %q", banner.Message) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +// TestRequirePrincipalFeatureWithLogger smoke-tests that the helper |
| 131 | +// runs without panic when given a logger and a nil pool — the |
| 132 | +// short-circuit path takes effect for inapplicable features. |
| 133 | +func TestRequirePrincipalFeatureWithLogger(t *testing.T) { |
| 134 | + t.Parallel() |
| 135 | + logger := slog.New(slog.DiscardHandler) |
| 136 | + opts := entitlements.NewRequireOpts("label", "acme") |
| 137 | + opts.Logger = logger |
| 138 | + w := httptest.NewRecorder() |
| 139 | + ok, _ := entitlements.RequirePrincipalFeature( |
| 140 | + context.Background(), |
| 141 | + w, |
| 142 | + nil, |
| 143 | + billing.PrincipalForUser(1), |
| 144 | + entitlements.FeatureActionsOrgSecrets, // org-only, inapplicable to user |
| 145 | + opts, |
| 146 | + ) |
| 147 | + if !ok { |
| 148 | + t.Errorf("inapplicable feature: expected ok=true") |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +func contains(s, sub string) bool { |
| 153 | + for i := 0; i+len(sub) <= len(s); i++ { |
| 154 | + if s[i:i+len(sub)] == sub { |
| 155 | + return true |
| 156 | + } |
| 157 | + } |
| 158 | + return false |
| 159 | +} |