@@ -0,0 +1,192 @@ |
| | 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| | 2 | + |
| | 3 | +package repo |
| | 4 | + |
| | 5 | +import ( |
| | 6 | + "context" |
| | 7 | + "net/http/httptest" |
| | 8 | + "net/url" |
| | 9 | + "strconv" |
| | 10 | + "testing" |
| | 11 | + "time" |
| | 12 | + |
| | 13 | + "github.com/jackc/pgx/v5/pgtype" |
| | 14 | + |
| | 15 | + billingdb "github.com/tenseleyFlow/shithub/internal/billing/sqlc" |
| | 16 | + "github.com/tenseleyFlow/shithub/internal/infra/config" |
| | 17 | + reposdb "github.com/tenseleyFlow/shithub/internal/repos/sqlc" |
| | 18 | +) |
| | 19 | + |
| | 20 | +// PRO07 — user-kind branch protection enforce flip. |
| | 21 | +// |
| | 22 | +// PRO05 plumbed user-kind report-only logging through |
| | 23 | +// evaluateBranchProtectionFeature. PRO07 wires |
| | 24 | +// EnforceConfig.User{RequiredReviewers,AdvancedBranchProtection} into |
| | 25 | +// the same site so operators can flip per-feature enforcement after |
| | 26 | +// the telemetry soak. These tests pin both modes. |
| | 27 | + |
| | 28 | +func TestSettingsBranches_UserKindReportOnlyRequiredReviewersAllowsSave(t *testing.T) { |
| | 29 | + t.Parallel() |
| | 30 | + f := newRepoFixture(t) // zero-value enforce ⇒ report-only |
| | 31 | + mux := f.branchesSettingsMux(f.owner.ID, f.owner.Username) |
| | 32 | + |
| | 33 | + resp := httptest.NewRecorder() |
| | 34 | + req := newFormRequest("POST", "/alice/private-repo/settings/branches", url.Values{ |
| | 35 | + "pattern": {"trunk"}, |
| | 36 | + "required_review_count": {"2"}, |
| | 37 | + }) |
| | 38 | + mux.ServeHTTP(resp, req) |
| | 39 | + if resp.Code != 303 { |
| | 40 | + t.Fatalf("status=%d body=%s", resp.Code, resp.Body.String()) |
| | 41 | + } |
| | 42 | + // Report-only ⇒ no notice code; redirect lands on the saved |
| | 43 | + // confirmation page. |
| | 44 | + if got := resp.Header().Get("Location"); got != "/alice/private-repo/settings/branches?notice=saved" { |
| | 45 | + t.Fatalf("expected report-only save, got redirect=%q", got) |
| | 46 | + } |
| | 47 | + assertBranchProtectionRuleCount(t, f, f.privateRepo.ID, 1) |
| | 48 | +} |
| | 49 | + |
| | 50 | +func TestSettingsBranches_UserKindEnforceFlipRequiredReviewersBlocks(t *testing.T) { |
| | 51 | + t.Parallel() |
| | 52 | + f := newRepoFixtureWithEnforce(t, config.EnforceConfig{ |
| | 53 | + UserRequiredReviewers: true, |
| | 54 | + }) |
| | 55 | + mux := f.branchesSettingsMux(f.owner.ID, f.owner.Username) |
| | 56 | + |
| | 57 | + resp := httptest.NewRecorder() |
| | 58 | + req := newFormRequest("POST", "/alice/private-repo/settings/branches", url.Values{ |
| | 59 | + "pattern": {"trunk"}, |
| | 60 | + "required_review_count": {"2"}, |
| | 61 | + }) |
| | 62 | + mux.ServeHTTP(resp, req) |
| | 63 | + if resp.Code != 303 { |
| | 64 | + t.Fatalf("status=%d body=%s", resp.Code, resp.Body.String()) |
| | 65 | + } |
| | 66 | + if got := resp.Header().Get("Location"); got != "/alice/private-repo/settings/branches?notice=required-reviewers-upgrade" { |
| | 67 | + t.Fatalf("expected upgrade notice, got %q", got) |
| | 68 | + } |
| | 69 | + assertBranchProtectionRuleCount(t, f, f.privateRepo.ID, 0) |
| | 70 | +} |
| | 71 | + |
| | 72 | +func TestSettingsBranches_UserKindEnforceFlipAdvancedChecksBlocks(t *testing.T) { |
| | 73 | + t.Parallel() |
| | 74 | + f := newRepoFixtureWithEnforce(t, config.EnforceConfig{ |
| | 75 | + UserAdvancedBranchProtection: true, |
| | 76 | + }) |
| | 77 | + mux := f.branchesSettingsMux(f.owner.ID, f.owner.Username) |
| | 78 | + |
| | 79 | + resp := httptest.NewRecorder() |
| | 80 | + req := newFormRequest("POST", "/alice/private-repo/settings/branches", url.Values{ |
| | 81 | + "pattern": {"trunk"}, |
| | 82 | + "required_status_check_names": {"ci"}, |
| | 83 | + }) |
| | 84 | + mux.ServeHTTP(resp, req) |
| | 85 | + if resp.Code != 303 { |
| | 86 | + t.Fatalf("status=%d body=%s", resp.Code, resp.Body.String()) |
| | 87 | + } |
| | 88 | + if got := resp.Header().Get("Location"); got != "/alice/private-repo/settings/branches?notice=branch-protection-upgrade" { |
| | 89 | + t.Fatalf("expected upgrade notice, got %q", got) |
| | 90 | + } |
| | 91 | + assertBranchProtectionRuleCount(t, f, f.privateRepo.ID, 0) |
| | 92 | +} |
| | 93 | + |
| | 94 | +// TestSettingsBranches_UserKindEnforceFlipDoesNotAffectPublicRepos |
| | 95 | +// locks the visibility short-circuit: enforce flags fire only on |
| | 96 | +// private personal repos. Public personal repos stay ungated. |
| | 97 | +func TestSettingsBranches_UserKindEnforceFlipDoesNotAffectPublicRepos(t *testing.T) { |
| | 98 | + t.Parallel() |
| | 99 | + f := newRepoFixtureWithEnforce(t, config.EnforceConfig{ |
| | 100 | + UserRequiredReviewers: true, |
| | 101 | + UserAdvancedBranchProtection: true, |
| | 102 | + }) |
| | 103 | + mux := f.branchesSettingsMux(f.owner.ID, f.owner.Username) |
| | 104 | + |
| | 105 | + resp := httptest.NewRecorder() |
| | 106 | + req := newFormRequest("POST", "/alice/public-repo/settings/branches", url.Values{ |
| | 107 | + "pattern": {"trunk"}, |
| | 108 | + "required_review_count": {"2"}, |
| | 109 | + }) |
| | 110 | + mux.ServeHTTP(resp, req) |
| | 111 | + if resp.Code != 303 { |
| | 112 | + t.Fatalf("status=%d body=%s", resp.Code, resp.Body.String()) |
| | 113 | + } |
| | 114 | + if got := resp.Header().Get("Location"); got != "/alice/public-repo/settings/branches?notice=saved" { |
| | 115 | + t.Fatalf("public repo should bypass gate, got %q", got) |
| | 116 | + } |
| | 117 | +} |
| | 118 | + |
| | 119 | +// TestSettingsBranches_UserKindEnforceFlipAllowsProUser verifies the |
| | 120 | +// gate is keyed on Plan, not just kind: a Pro personal account |
| | 121 | +// completes the save even with the enforce flag flipped. |
| | 122 | +func TestSettingsBranches_UserKindEnforceFlipAllowsProUser(t *testing.T) { |
| | 123 | + t.Parallel() |
| | 124 | + f := newRepoFixtureWithEnforce(t, config.EnforceConfig{ |
| | 125 | + UserRequiredReviewers: true, |
| | 126 | + }) |
| | 127 | + upgradeRepoFixtureOwnerToPro(t, f) |
| | 128 | + mux := f.branchesSettingsMux(f.owner.ID, f.owner.Username) |
| | 129 | + |
| | 130 | + resp := httptest.NewRecorder() |
| | 131 | + req := newFormRequest("POST", "/alice/private-repo/settings/branches", url.Values{ |
| | 132 | + "pattern": {"trunk"}, |
| | 133 | + "required_review_count": {"2"}, |
| | 134 | + }) |
| | 135 | + mux.ServeHTTP(resp, req) |
| | 136 | + if resp.Code != 303 { |
| | 137 | + t.Fatalf("status=%d body=%s", resp.Code, resp.Body.String()) |
| | 138 | + } |
| | 139 | + if got := resp.Header().Get("Location"); got != "/alice/private-repo/settings/branches?notice=saved" { |
| | 140 | + t.Fatalf("Pro user with enforce should save, got %q", got) |
| | 141 | + } |
| | 142 | + assertBranchProtectionRuleCount(t, f, f.privateRepo.ID, 1) |
| | 143 | +} |
| | 144 | + |
| | 145 | +// TestSettingsBranches_OrgKindUnchangedByPRO07Flags is the regression |
| | 146 | +// guard: PRO07's per-feature flags target user kind only. Org-kind |
| | 147 | +// gating has been on since SP05 and must not change shape when the |
| | 148 | +// user-kind flags flip. |
| | 149 | +func TestSettingsBranches_OrgKindUnchangedByPRO07Flags(t *testing.T) { |
| | 150 | + t.Parallel() |
| | 151 | + for _, enforce := range []config.EnforceConfig{ |
| | 152 | + {}, |
| | 153 | + {UserRequiredReviewers: true, UserAdvancedBranchProtection: true}, |
| | 154 | + } { |
| | 155 | + f := newRepoFixtureWithEnforce(t, enforce) |
| | 156 | + orgID := f.insertOwnedOrg(t, "acme") |
| | 157 | + repo := f.insertOrgRepo(t, orgID, "private-org-repo", reposdb.RepoVisibilityPrivate) |
| | 158 | + mux := f.branchesSettingsMux(f.owner.ID, f.owner.Username) |
| | 159 | + |
| | 160 | + resp := httptest.NewRecorder() |
| | 161 | + req := newFormRequest("POST", "/acme/private-org-repo/settings/branches", url.Values{ |
| | 162 | + "pattern": {"trunk"}, |
| | 163 | + "required_review_count": {"2"}, |
| | 164 | + }) |
| | 165 | + mux.ServeHTTP(resp, req) |
| | 166 | + if resp.Code != 303 { |
| | 167 | + t.Fatalf("enforce=%+v status=%d body=%s", enforce, resp.Code, resp.Body.String()) |
| | 168 | + } |
| | 169 | + if got := resp.Header().Get("Location"); got != "/acme/private-org-repo/settings/branches?notice=required-reviewers-upgrade" { |
| | 170 | + t.Errorf("enforce=%+v: org redirect=%q, want upgrade notice", enforce, got) |
| | 171 | + } |
| | 172 | + assertBranchProtectionRuleCount(t, f, repo.ID, 0) |
| | 173 | + } |
| | 174 | +} |
| | 175 | + |
| | 176 | +func upgradeRepoFixtureOwnerToPro(t *testing.T, f *repoFixture) { |
| | 177 | + t.Helper() |
| | 178 | + now := time.Now().UTC() |
| | 179 | + suffix := strconv.FormatInt(f.owner.ID, 10) |
| | 180 | + _, err := billingdb.New().ApplyUserSubscriptionSnapshot(context.Background(), f.pool, billingdb.ApplyUserSubscriptionSnapshotParams{ |
| | 181 | + UserID: f.owner.ID, |
| | 182 | + Plan: billingdb.UserPlanPro, |
| | 183 | + SubscriptionStatus: billingdb.BillingSubscriptionStatusActive, |
| | 184 | + StripeSubscriptionID: pgtype.Text{String: "sub_bp_pro_" + suffix, Valid: true}, |
| | 185 | + CurrentPeriodStart: pgtype.Timestamptz{Time: now.Add(-time.Hour), Valid: true}, |
| | 186 | + CurrentPeriodEnd: pgtype.Timestamptz{Time: now.Add(30 * 24 * time.Hour), Valid: true}, |
| | 187 | + LastWebhookEventID: "evt_bp_pro_" + suffix, |
| | 188 | + }) |
| | 189 | + if err != nil { |
| | 190 | + t.Fatalf("upgrade owner to Pro: %v", err) |
| | 191 | + } |
| | 192 | +} |