sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/cmd/branchprotector/request_test.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	branchprotection "sigs.k8s.io/prow/pkg/config"
    24  	"sigs.k8s.io/prow/pkg/github"
    25  )
    26  
    27  func TestMakeBool(t *testing.T) {
    28  	yes := true
    29  	no := false
    30  	cases := []struct {
    31  		input    *bool
    32  		expected bool
    33  	}{
    34  		{
    35  			input:    nil,
    36  			expected: false,
    37  		},
    38  		{
    39  			input:    &no,
    40  			expected: false,
    41  		},
    42  		{
    43  			input:    &yes,
    44  			expected: true,
    45  		},
    46  	}
    47  	for _, tc := range cases {
    48  		if actual := makeBool(tc.input); actual != tc.expected {
    49  			t.Errorf("%v: actual %v != expected %t", tc.input, actual, tc.expected)
    50  		}
    51  	}
    52  }
    53  
    54  func TestMakeReviews(t *testing.T) {
    55  	zero := 0
    56  	three := 3
    57  	one := 1
    58  	yes := true
    59  	cases := []struct {
    60  		name     string
    61  		input    *branchprotection.ReviewPolicy
    62  		expected *github.RequiredPullRequestReviewsRequest
    63  	}{
    64  		{
    65  			name: "nil returns nil",
    66  		},
    67  		{
    68  			name: "nil apporvals returns nil",
    69  			input: &branchprotection.ReviewPolicy{
    70  				Approvals: nil,
    71  			},
    72  		},
    73  		{
    74  			name: "0 approvals set",
    75  			input: &branchprotection.ReviewPolicy{
    76  				Approvals: &zero,
    77  			},
    78  			expected: &github.RequiredPullRequestReviewsRequest{
    79  				RequiredApprovingReviewCount: 0,
    80  			},
    81  		},
    82  		{
    83  			name: "approvals set",
    84  			input: &branchprotection.ReviewPolicy{
    85  				Approvals: &three,
    86  			},
    87  			expected: &github.RequiredPullRequestReviewsRequest{
    88  				RequiredApprovingReviewCount: 3,
    89  			},
    90  		},
    91  		{
    92  			name: "set all",
    93  			input: &branchprotection.ReviewPolicy{
    94  				Approvals:     &one,
    95  				RequireOwners: &yes,
    96  				DismissStale:  &yes,
    97  				DismissalRestrictions: &branchprotection.DismissalRestrictions{
    98  					Users: []string{"fred", "jane"},
    99  					Teams: []string{"megacorp", "startup"},
   100  				},
   101  				BypassRestrictions: &branchprotection.BypassRestrictions{
   102  					Users: []string{"fred", "jane"},
   103  					Teams: []string{"megacorp", "startup"},
   104  				},
   105  			},
   106  			expected: &github.RequiredPullRequestReviewsRequest{
   107  				RequiredApprovingReviewCount: 1,
   108  				RequireCodeOwnerReviews:      true,
   109  				DismissStaleReviews:          true,
   110  				DismissalRestrictions: github.DismissalRestrictionsRequest{
   111  					Teams: &[]string{"megacorp", "startup"},
   112  					Users: &[]string{"fred", "jane"},
   113  				},
   114  				BypassRestrictions: github.BypassRestrictionsRequest{
   115  					Teams: &[]string{"megacorp", "startup"},
   116  					Users: &[]string{"fred", "jane"},
   117  				},
   118  			},
   119  		},
   120  	}
   121  
   122  	for _, tc := range cases {
   123  		actual := makeReviews(tc.input)
   124  		if !reflect.DeepEqual(actual, tc.expected) {
   125  			t.Errorf("%s: actual %v != expected %v", tc.name, actual, tc.expected)
   126  		}
   127  	}
   128  }
   129  
   130  func TestMakeRequest(t *testing.T) {
   131  	yes := true
   132  	no := false
   133  	cases := []struct {
   134  		name                    string
   135  		disableAppsRestrictions bool
   136  		policy                  branchprotection.Policy
   137  		expected                github.BranchProtectionRequest
   138  	}{
   139  		{
   140  			name: "Empty works",
   141  			expected: github.BranchProtectionRequest{
   142  				EnforceAdmins: &no,
   143  			},
   144  		},
   145  		{
   146  			name: "teams != nil => apps != nil, users != nil",
   147  			policy: branchprotection.Policy{
   148  				Restrictions: &branchprotection.Restrictions{
   149  					Teams: []string{"hello"},
   150  				},
   151  			},
   152  			expected: github.BranchProtectionRequest{
   153  				EnforceAdmins: &no,
   154  				Restrictions: &github.RestrictionsRequest{
   155  					Apps:  &[]string{},
   156  					Teams: &[]string{"hello"},
   157  					Users: &[]string{},
   158  				},
   159  			},
   160  		},
   161  		{
   162  			name: "users != nil => apps != nil, teams != nil",
   163  			policy: branchprotection.Policy{
   164  				Restrictions: &branchprotection.Restrictions{
   165  					Users: []string{"there"},
   166  				},
   167  			},
   168  			expected: github.BranchProtectionRequest{
   169  				EnforceAdmins: &no,
   170  				Restrictions: &github.RestrictionsRequest{
   171  					Apps:  &[]string{},
   172  					Users: &[]string{"there"},
   173  					Teams: &[]string{},
   174  				},
   175  			},
   176  		},
   177  		{
   178  			name: "apps != nil => users != nil, teams != nil",
   179  			policy: branchprotection.Policy{
   180  				Restrictions: &branchprotection.Restrictions{
   181  					Apps: []string{"friends"},
   182  				},
   183  			},
   184  			expected: github.BranchProtectionRequest{
   185  				EnforceAdmins: &no,
   186  				Restrictions: &github.RestrictionsRequest{
   187  					Apps:  &[]string{"friends"},
   188  					Users: &[]string{},
   189  					Teams: &[]string{},
   190  				},
   191  			},
   192  		},
   193  		{
   194  			name:                    "apps restrictions disabled works",
   195  			disableAppsRestrictions: true,
   196  			policy: branchprotection.Policy{
   197  				Restrictions: &branchprotection.Restrictions{
   198  					Teams: []string{"hello"},
   199  					Users: []string{"there"},
   200  				},
   201  			},
   202  			expected: github.BranchProtectionRequest{
   203  				EnforceAdmins: &no,
   204  				Restrictions: &github.RestrictionsRequest{
   205  					Apps:  nil,
   206  					Teams: &[]string{"hello"},
   207  					Users: &[]string{"there"},
   208  				},
   209  			},
   210  		},
   211  		{
   212  			name: "Strict => Contexts != nil",
   213  			policy: branchprotection.Policy{
   214  				RequiredStatusChecks: &branchprotection.ContextPolicy{
   215  					Strict: &yes,
   216  				},
   217  			},
   218  			expected: github.BranchProtectionRequest{
   219  				EnforceAdmins: &no,
   220  				RequiredStatusChecks: &github.RequiredStatusChecks{
   221  					Strict:   true,
   222  					Contexts: []string{},
   223  				},
   224  			},
   225  		},
   226  	}
   227  	for _, tc := range cases {
   228  		t.Run(tc.name, func(t *testing.T) {
   229  			actual := makeRequest(tc.policy, !tc.disableAppsRestrictions)
   230  			expected := tc.expected
   231  			if !reflect.DeepEqual(actual, expected) {
   232  				t.Errorf("actual %+v != expected %+v", actual, expected)
   233  			}
   234  		})
   235  	}
   236  }