github.com/xgoffin/jenkins-library@v1.154.0/cmd/githubCheckBranchProtection_test.go (about)

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