github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/pkg/github/helpers_test.go (about)

     1  /*
     2  Copyright 2019 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 github
    18  
    19  import (
    20  	"encoding/json"
    21  	"testing"
    22  )
    23  
    24  func TestRepoPermissionLevel(t *testing.T) {
    25  	get := func(v RepoPermissionLevel) *RepoPermissionLevel {
    26  		return &v
    27  	}
    28  	cases := []struct {
    29  		input    string
    30  		expected *RepoPermissionLevel
    31  	}{
    32  		{
    33  			"admin",
    34  			get(Admin),
    35  		},
    36  		{
    37  			"maintain",
    38  			get(Maintain),
    39  		},
    40  		{
    41  			"write",
    42  			get(Write),
    43  		},
    44  		{
    45  			"triage",
    46  			get(Triage),
    47  		},
    48  		{
    49  			"read",
    50  			get(Read),
    51  		},
    52  		{
    53  			"none",
    54  			get(None),
    55  		},
    56  		{
    57  			"",
    58  			nil,
    59  		},
    60  		{
    61  			"unknown",
    62  			nil,
    63  		},
    64  	}
    65  	for _, tc := range cases {
    66  		var actual RepoPermissionLevel
    67  		err := json.Unmarshal([]byte("\""+tc.input+"\""), &actual)
    68  		switch {
    69  		case err == nil && tc.expected == nil:
    70  			t.Errorf("%s: failed to receive an error", tc.input)
    71  		case err != nil && tc.expected != nil:
    72  			t.Errorf("%s: unexpected error: %v", tc.input, err)
    73  		case err == nil && *tc.expected != actual:
    74  			t.Errorf("%s: actual %v != expected %v", tc.input, tc.expected, actual)
    75  		}
    76  	}
    77  }
    78  
    79  func TestLevelFromPermissions(t *testing.T) {
    80  	var testCases = []struct {
    81  		permissions RepoPermissions
    82  		level       RepoPermissionLevel
    83  	}{
    84  		{
    85  			permissions: RepoPermissions{},
    86  			level:       None,
    87  		},
    88  		{
    89  			permissions: RepoPermissions{Pull: true},
    90  			level:       Read,
    91  		},
    92  		{
    93  			permissions: RepoPermissions{Pull: true, Triage: true},
    94  			level:       Triage,
    95  		},
    96  		{
    97  			permissions: RepoPermissions{Pull: true, Triage: true, Push: true},
    98  			level:       Write,
    99  		},
   100  		{
   101  			permissions: RepoPermissions{Pull: true, Triage: true, Push: true, Maintain: true},
   102  			level:       Maintain,
   103  		},
   104  		{
   105  			permissions: RepoPermissions{Pull: true, Triage: true, Push: true, Maintain: true, Admin: true},
   106  			level:       Admin,
   107  		},
   108  	}
   109  
   110  	for _, testCase := range testCases {
   111  		if actual, expected := LevelFromPermissions(testCase.permissions), testCase.level; actual != expected {
   112  			t.Errorf("got incorrect level from permissions, expected %v but got %v", expected, actual)
   113  		}
   114  	}
   115  }
   116  
   117  func TestPermissionsFromTeamPermission(t *testing.T) {
   118  	var testCases = []struct {
   119  		level       TeamPermission
   120  		permissions RepoPermissions
   121  	}{
   122  		{
   123  			level:       TeamPermission("foobar"),
   124  			permissions: RepoPermissions{},
   125  		},
   126  		{
   127  			level:       RepoPull,
   128  			permissions: RepoPermissions{Pull: true},
   129  		},
   130  		{
   131  			level:       RepoTriage,
   132  			permissions: RepoPermissions{Pull: true, Triage: true},
   133  		},
   134  		{
   135  			level:       RepoPush,
   136  			permissions: RepoPermissions{Pull: true, Triage: true, Push: true},
   137  		},
   138  		{
   139  			level:       RepoMaintain,
   140  			permissions: RepoPermissions{Pull: true, Triage: true, Push: true, Maintain: true},
   141  		},
   142  		{
   143  			level:       RepoAdmin,
   144  			permissions: RepoPermissions{Pull: true, Triage: true, Push: true, Maintain: true, Admin: true},
   145  		},
   146  	}
   147  
   148  	for _, testCase := range testCases {
   149  		if actual, expected := PermissionsFromTeamPermission(testCase.level), testCase.permissions; actual != expected {
   150  			t.Errorf("got incorrect permissions from team permissions, expected %v but got %v", expected, actual)
   151  		}
   152  	}
   153  }