github.com/SAP/jenkins-library@v1.362.0/cmd/githubCheckBranchProtection_test.go (about) 1 //go:build unit 2 // +build unit 3 4 package cmd 5 6 import ( 7 "context" 8 "fmt" 9 "testing" 10 11 "github.com/SAP/jenkins-library/pkg/telemetry" 12 13 "github.com/google/go-github/v45/github" 14 "github.com/stretchr/testify/assert" 15 ) 16 17 type ghCheckBranchRepoService struct { 18 protection github.Protection 19 serviceError error 20 owner string 21 repo string 22 branch string 23 } 24 25 func (g *ghCheckBranchRepoService) GetBranchProtection(ctx context.Context, owner, repo, branch string) (*github.Protection, *github.Response, error) { 26 g.owner = owner 27 g.repo = repo 28 g.branch = branch 29 30 return &g.protection, nil, g.serviceError 31 } 32 33 func TestRunGithubCheckBranchProtection(t *testing.T) { 34 ctx := context.Background() 35 telemetryData := telemetry.CustomData{} 36 37 t.Run("no checks active", func(t *testing.T) { 38 config := githubCheckBranchProtectionOptions{Branch: "testBranch", Owner: "testOrg", Repository: "testRepo"} 39 ghRepo := ghCheckBranchRepoService{} 40 err := runGithubCheckBranchProtection(ctx, &config, &telemetryData, &ghRepo) 41 assert.NoError(t, err) 42 assert.Equal(t, config.Branch, ghRepo.branch) 43 assert.Equal(t, config.Owner, ghRepo.owner) 44 assert.Equal(t, config.Repository, ghRepo.repo) 45 }) 46 47 t.Run("error calling GitHub", func(t *testing.T) { 48 config := githubCheckBranchProtectionOptions{Branch: "testBranch", Owner: "testOrg", Repository: "testRepo"} 49 ghRepo := ghCheckBranchRepoService{serviceError: fmt.Errorf("gh test error")} 50 err := runGithubCheckBranchProtection(ctx, &config, &telemetryData, &ghRepo) 51 assert.EqualError(t, err, "failed to read branch protection information: gh test error") 52 }) 53 54 t.Run("all checks ok", func(t *testing.T) { 55 config := githubCheckBranchProtectionOptions{ 56 Branch: "testBranch", 57 Owner: "testOrg", 58 Repository: "testRepo", 59 RequiredChecks: []string{"check1", "check2"}, 60 RequireEnforceAdmins: true, 61 RequiredApprovingReviewCount: 1, 62 } 63 ghRepo := ghCheckBranchRepoService{protection: github.Protection{ 64 RequiredStatusChecks: &github.RequiredStatusChecks{Contexts: []string{"check0", "check1", "check2", "check3"}}, 65 EnforceAdmins: &github.AdminEnforcement{Enabled: true}, 66 RequiredPullRequestReviews: &github.PullRequestReviewsEnforcement{RequiredApprovingReviewCount: 1}, 67 }} 68 err := runGithubCheckBranchProtection(ctx, &config, &telemetryData, &ghRepo) 69 assert.NoError(t, err) 70 assert.Equal(t, config.Branch, ghRepo.branch) 71 assert.Equal(t, config.Owner, ghRepo.owner) 72 assert.Equal(t, config.Repository, ghRepo.repo) 73 }) 74 75 t.Run("no status checks", func(t *testing.T) { 76 config := githubCheckBranchProtectionOptions{ 77 RequiredChecks: []string{"check1", "check2"}, 78 } 79 ghRepo := ghCheckBranchRepoService{protection: github.Protection{}} 80 err := runGithubCheckBranchProtection(ctx, &config, &telemetryData, &ghRepo) 81 assert.Contains(t, fmt.Sprint(err), "required status check 'check1' not found") 82 }) 83 84 t.Run("status check missing", func(t *testing.T) { 85 config := githubCheckBranchProtectionOptions{ 86 RequiredChecks: []string{"check1", "check2"}, 87 } 88 ghRepo := ghCheckBranchRepoService{protection: github.Protection{ 89 RequiredStatusChecks: &github.RequiredStatusChecks{Contexts: []string{"check0", "check1"}}, 90 }} 91 err := runGithubCheckBranchProtection(ctx, &config, &telemetryData, &ghRepo) 92 assert.Contains(t, fmt.Sprint(err), "required status check 'check2' not found") 93 }) 94 95 t.Run("admin enforcement inactive", func(t *testing.T) { 96 config := githubCheckBranchProtectionOptions{ 97 RequireEnforceAdmins: true, 98 } 99 ghRepo := ghCheckBranchRepoService{protection: github.Protection{ 100 EnforceAdmins: &github.AdminEnforcement{Enabled: false}, 101 }} 102 err := runGithubCheckBranchProtection(ctx, &config, &telemetryData, &ghRepo) 103 assert.Contains(t, fmt.Sprint(err), "admins are not enforced") 104 }) 105 106 t.Run("not enough reviewers", func(t *testing.T) { 107 config := githubCheckBranchProtectionOptions{ 108 RequiredApprovingReviewCount: 2, 109 } 110 ghRepo := ghCheckBranchRepoService{protection: github.Protection{ 111 RequiredPullRequestReviews: &github.PullRequestReviewsEnforcement{RequiredApprovingReviewCount: 1}, 112 }} 113 err := runGithubCheckBranchProtection(ctx, &config, &telemetryData, &ghRepo) 114 assert.Contains(t, fmt.Sprint(err), "not enough mandatory reviewers") 115 }) 116 }