github.com/fluxcd/go-git-providers@v0.19.3/gitprovider/types_validation_test.go (about) 1 /* 2 Copyright 2020 The Flux CD contributors. 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 gitprovider 18 19 import ( 20 "fmt" 21 "testing" 22 23 "github.com/fluxcd/go-git-providers/validation" 24 ) 25 26 type validateFunc func() error 27 28 func assertValidation(t *testing.T, structName string, validateFn validateFunc, expectedErrs []error) { 29 funcName := fmt.Sprintf("%s.ValidateInfo", structName) 30 wantErr := len(expectedErrs) != 0 31 // Run the validation function, and make sure we expected an error (or not) 32 err := validateFn() 33 if (err != nil) != wantErr { 34 t.Errorf("%s() error = %v, wantErr %v", funcName, err, wantErr) 35 } 36 // Make sure the error embeds the following expected errors 37 validation.TestExpectErrors(t, funcName, err, expectedErrs...) 38 } 39 40 func TestDeployKey_Validate(t *testing.T) { 41 tests := []struct { 42 name string 43 key DeployKeyInfo 44 expectedErrs []error 45 }{ 46 { 47 name: "valid create", 48 key: DeployKeyInfo{ 49 Name: "foo-deploykey", 50 Key: []byte("some-data"), 51 }, 52 }, 53 { 54 name: "valid create, with all checked fields populated", 55 key: DeployKeyInfo{ 56 Name: "foo-deploykey", 57 Key: []byte("some-data"), 58 }, 59 }, 60 { 61 name: "invalid create, missing name", 62 key: DeployKeyInfo{ 63 Key: []byte("some-data"), 64 }, 65 expectedErrs: []error{validation.ErrFieldRequired}, 66 }, 67 { 68 name: "invalid create, missing key", 69 key: DeployKeyInfo{ 70 Name: "foo-deploykey", 71 }, 72 expectedErrs: []error{validation.ErrFieldRequired}, 73 }, 74 } 75 for _, tt := range tests { 76 t.Run(tt.name, func(t *testing.T) { 77 assertValidation(t, "DeployKey", tt.key.ValidateInfo, tt.expectedErrs) 78 }) 79 } 80 } 81 82 func TestDeployToken_Validate(t *testing.T) { 83 tests := []struct { 84 name string 85 token DeployTokenInfo 86 expectedErrs []error 87 }{ 88 { 89 name: "valid create", 90 token: DeployTokenInfo{ 91 Name: "foo-deploytoken", 92 }, 93 }, 94 { 95 name: "invalid create, missing name", 96 token: DeployTokenInfo{}, 97 expectedErrs: []error{validation.ErrFieldRequired}, 98 }, 99 } 100 for _, tt := range tests { 101 t.Run(tt.name, func(t *testing.T) { 102 assertValidation(t, "DeployToken", tt.token.ValidateInfo, tt.expectedErrs) 103 }) 104 } 105 } 106 107 func TestRepository_Validate(t *testing.T) { 108 unknownRepositoryVisibility := RepositoryVisibility("unknown") 109 tests := []struct { 110 name string 111 repo RepositoryInfo 112 expectedErrs []error 113 }{ 114 { 115 name: "valid create and update, with valid enum and description", 116 repo: RepositoryInfo{ 117 Description: StringVar("foo-description"), 118 Visibility: RepositoryVisibilityVar(RepositoryVisibilityPublic), 119 }, 120 }, 121 { 122 name: "invalid create and update, invalid enum", 123 repo: RepositoryInfo{ 124 Visibility: &unknownRepositoryVisibility, 125 }, 126 expectedErrs: []error{validation.ErrFieldEnumInvalid}, 127 }, 128 } 129 for _, tt := range tests { 130 t.Run(tt.name, func(t *testing.T) { 131 assertValidation(t, "Repository", tt.repo.ValidateInfo, tt.expectedErrs) 132 }) 133 } 134 } 135 136 func TestTeamAccess_Validate(t *testing.T) { 137 invalidPermission := RepositoryPermission("unknown") 138 tests := []struct { 139 name string 140 ta TeamAccessInfo 141 expectedErrs []error 142 }{ 143 { 144 name: "valid create, required field set", 145 ta: TeamAccessInfo{ 146 Name: "foo-team", 147 }, 148 }, 149 { 150 name: "invalid create, required name", 151 ta: TeamAccessInfo{}, 152 expectedErrs: []error{validation.ErrFieldRequired}, 153 }, 154 { 155 name: "valid create, also including valid repoinfo", 156 ta: TeamAccessInfo{ 157 Name: "foo-team", 158 }, 159 }, 160 { 161 name: "valid create, with valid enum", 162 ta: TeamAccessInfo{ 163 Name: "foo-team", 164 Permission: RepositoryPermissionVar(RepositoryPermissionPull), 165 }, 166 }, 167 { 168 name: "invalid create, invalid enum", 169 ta: TeamAccessInfo{ 170 Name: "foo-team", 171 Permission: &invalidPermission, 172 }, 173 expectedErrs: []error{validation.ErrFieldEnumInvalid}, 174 }, 175 } 176 for _, tt := range tests { 177 t.Run(tt.name, func(t *testing.T) { 178 assertValidation(t, "TeamAccess", tt.ta.ValidateInfo, tt.expectedErrs) 179 }) 180 } 181 }