github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/graphql/app_validation_test.go (about) 1 package graphql_test 2 3 import ( 4 "testing" 5 6 "github.com/kyma-incubator/compass/components/director/pkg/graphql" 7 8 "github.com/kyma-incubator/compass/components/director/pkg/inputvalidation/inputvalidationtest" 9 "github.com/kyma-incubator/compass/components/director/pkg/str" 10 "github.com/stretchr/testify/require" 11 ) 12 13 func TestApplicationInput_Validate_Name(t *testing.T) { 14 testCases := []struct { 15 Name string 16 Value string 17 ExpectedValid bool 18 }{ 19 { 20 Name: "ExpectedValid", 21 Value: inputvalidationtest.ValidName, 22 ExpectedValid: true, 23 }, 24 { 25 Name: "Empty string", 26 Value: inputvalidationtest.EmptyString, 27 ExpectedValid: false, 28 }, 29 { 30 Name: "String longer than 100 chars", 31 Value: inputvalidationtest.String101Long, 32 ExpectedValid: false, 33 }, 34 } 35 36 for _, testCase := range testCases { 37 t.Run(testCase.Name, func(t *testing.T) { 38 //GIVEN 39 app1 := fixValidApplicationRegisterInput() 40 app1.Name = testCase.Value 41 app2 := fixValidApplicationJSONInput() 42 app2.Name = testCase.Value 43 // WHEN 44 err1 := app1.Validate() 45 err2 := app2.Validate() 46 // THEN 47 if testCase.ExpectedValid { 48 require.NoError(t, err1) 49 require.NoError(t, err2) 50 } else { 51 require.Error(t, err1) 52 require.Error(t, err2) 53 } 54 }) 55 } 56 } 57 58 func TestApplicationInput_Validate_ProviderName(t *testing.T) { 59 testCases := []struct { 60 Name string 61 Value *string 62 ExpectedValid bool 63 }{ 64 { 65 Name: "ExpectedValid", 66 Value: str.Ptr("provider-name"), 67 ExpectedValid: true, 68 }, 69 { 70 Name: "Nil", 71 Value: nil, 72 ExpectedValid: true, 73 }, 74 { 75 Name: "Empty string", 76 Value: str.Ptr(inputvalidationtest.EmptyString), 77 ExpectedValid: true, 78 }, 79 { 80 Name: "String longer than 256 chars", 81 Value: str.Ptr(inputvalidationtest.String257Long), 82 ExpectedValid: false, 83 }, 84 } 85 86 for _, testCase := range testCases { 87 t.Run(testCase.Name, func(t *testing.T) { 88 //GIVEN 89 app1 := fixValidApplicationRegisterInput() 90 app1.ProviderName = testCase.Value 91 app2 := fixValidApplicationJSONInput() 92 app2.ProviderName = testCase.Value 93 // WHEN 94 err1 := app1.Validate() 95 err2 := app2.Validate() 96 // THEN 97 if testCase.ExpectedValid { 98 require.NoError(t, err1) 99 require.NoError(t, err2) 100 } else { 101 require.Error(t, err1) 102 require.Error(t, err2) 103 } 104 }) 105 } 106 } 107 108 func TestApplicationInput_Validate_Description(t *testing.T) { 109 testCases := []struct { 110 Name string 111 Value *string 112 ExpectedValid bool 113 }{ 114 { 115 Name: "ExpectedValid", 116 Value: str.Ptr("this is a valid description"), 117 ExpectedValid: true, 118 }, 119 { 120 Name: "Nil pointer", 121 Value: nil, 122 ExpectedValid: true, 123 }, 124 { 125 Name: "Empty string", 126 Value: str.Ptr(inputvalidationtest.EmptyString), 127 ExpectedValid: true, 128 }, 129 { 130 Name: "String longer than 2000 chars", 131 Value: str.Ptr(inputvalidationtest.String2001Long), 132 ExpectedValid: false, 133 }, 134 } 135 136 for _, testCase := range testCases { 137 t.Run(testCase.Name, func(t *testing.T) { 138 //GIVEN 139 app1 := fixValidApplicationRegisterInput() 140 app1.Description = testCase.Value 141 app2 := fixValidApplicationJSONInput() 142 app2.Description = testCase.Value 143 // WHEN 144 err1 := app1.Validate() 145 err2 := app2.Validate() 146 // THEN 147 if testCase.ExpectedValid { 148 require.NoError(t, err1) 149 require.NoError(t, err2) 150 } else { 151 require.Error(t, err1) 152 require.Error(t, err2) 153 } 154 }) 155 } 156 } 157 158 func TestApplicationInput_Validate_Labels(t *testing.T) { 159 testCases := []struct { 160 Name string 161 Value graphql.Labels 162 ExpectedValid bool 163 }{ 164 { 165 Name: "ExpectedValid", 166 Value: graphql.Labels{"key": "value"}, 167 ExpectedValid: true, 168 }, 169 { 170 Name: "Nil pointer", 171 Value: nil, 172 ExpectedValid: true, 173 }, 174 { 175 Name: "Label with array of strings", 176 Value: graphql.Labels{"scenarios": []string{"ABC", "CBA", "TEST"}}, 177 ExpectedValid: true, 178 }, 179 { 180 Name: "Empty key", 181 Value: graphql.Labels{"": "value"}, 182 ExpectedValid: false, 183 }, 184 { 185 Name: "Invalid key", 186 Value: graphql.Labels{"not/valid": "value"}, 187 ExpectedValid: false, 188 }, 189 } 190 191 for _, testCase := range testCases { 192 t.Run(testCase.Name, func(t *testing.T) { 193 //GIVEN 194 app1 := fixValidApplicationRegisterInput() 195 app1.Labels = testCase.Value 196 app2 := fixValidApplicationJSONInput() 197 app2.Labels = testCase.Value 198 // WHEN 199 err1 := app1.Validate() 200 err2 := app2.Validate() 201 // THEN 202 if testCase.ExpectedValid { 203 require.NoError(t, err1) 204 require.NoError(t, err2) 205 } else { 206 require.Error(t, err1) 207 require.Error(t, err2) 208 } 209 }) 210 } 211 } 212 213 func TestApplicationInput_Validate_HealthCheckURL(t *testing.T) { 214 testCases := []struct { 215 Name string 216 Value *string 217 ExpectedValid bool 218 }{ 219 { 220 Name: "ExpectedValid", 221 Value: str.Ptr(inputvalidationtest.ValidURL), 222 ExpectedValid: true, 223 }, 224 { 225 Name: "ExpectedValid nil value", 226 Value: nil, 227 ExpectedValid: true, 228 }, 229 { 230 Name: "URL longer than 256", 231 Value: str.Ptr(inputvalidationtest.URL257Long), 232 ExpectedValid: false, 233 }, 234 { 235 Name: "Invalid", 236 Value: str.Ptr(inputvalidationtest.InvalidURL), 237 ExpectedValid: false, 238 }, 239 } 240 241 for _, testCase := range testCases { 242 t.Run(testCase.Name, func(t *testing.T) { 243 //GIVEN 244 app1 := fixValidApplicationRegisterInput() 245 app1.HealthCheckURL = testCase.Value 246 app2 := fixValidApplicationJSONInput() 247 app2.HealthCheckURL = testCase.Value 248 // WHEN 249 err1 := app1.Validate() 250 err2 := app2.Validate() 251 // THEN 252 if testCase.ExpectedValid { 253 require.NoError(t, err1) 254 require.NoError(t, err2) 255 } else { 256 require.Error(t, err1) 257 require.Error(t, err2) 258 } 259 }) 260 } 261 } 262 263 func TestApplicationInput_Validate_Webhooks(t *testing.T) { 264 validObj := fixValidWebhookInput(inputvalidationtest.ValidURL) 265 266 testCases := []struct { 267 Name string 268 Value []*graphql.WebhookInput 269 ExpectedValid bool 270 }{ 271 { 272 Name: "ExpectedValid array", 273 Value: []*graphql.WebhookInput{&validObj}, 274 ExpectedValid: true, 275 }, 276 { 277 Name: "Empty array", 278 Value: []*graphql.WebhookInput{}, 279 ExpectedValid: true, 280 }, 281 { 282 Name: "Array with invalid object", 283 Value: []*graphql.WebhookInput{{}}, 284 ExpectedValid: false, 285 }, 286 } 287 288 for _, testCase := range testCases { 289 t.Run(testCase.Name, func(t *testing.T) { 290 //GIVEN 291 app1 := fixValidApplicationRegisterInput() 292 app1.Webhooks = testCase.Value 293 app2 := fixValidApplicationJSONInput() 294 app2.Webhooks = testCase.Value 295 // WHEN 296 err1 := app1.Validate() 297 err2 := app2.Validate() 298 // THEN 299 if testCase.ExpectedValid { 300 require.NoError(t, err1) 301 require.NoError(t, err2) 302 } else { 303 require.Error(t, err1) 304 require.Error(t, err2) 305 } 306 }) 307 } 308 } 309 310 func TestApplicationUpdateInput_Validate_ProviderName(t *testing.T) { 311 testCases := []struct { 312 Name string 313 Value *string 314 ExpectedValid bool 315 }{ 316 { 317 Name: "ExpectedValid", 318 Value: str.Ptr("provider-name"), 319 ExpectedValid: true, 320 }, 321 { 322 Name: "Nil", 323 Value: nil, 324 ExpectedValid: true, 325 }, 326 { 327 Name: "Empty string", 328 Value: str.Ptr(inputvalidationtest.EmptyString), 329 ExpectedValid: true, 330 }, 331 { 332 Name: "String longer than 256 chars", 333 Value: str.Ptr(inputvalidationtest.String257Long), 334 ExpectedValid: false, 335 }, 336 } 337 338 for _, testCase := range testCases { 339 t.Run(testCase.Name, func(t *testing.T) { 340 //GIVEN 341 app1 := fixValidApplicationRegisterInput() 342 app1.ProviderName = testCase.Value 343 app2 := fixValidApplicationJSONInput() 344 app2.ProviderName = testCase.Value 345 // WHEN 346 err1 := app1.Validate() 347 err2 := app2.Validate() 348 // THEN 349 if testCase.ExpectedValid { 350 require.NoError(t, err1) 351 require.NoError(t, err2) 352 } else { 353 require.Error(t, err1) 354 require.Error(t, err2) 355 } 356 }) 357 } 358 } 359 360 func TestApplicationUpdateInput_Validate_Description(t *testing.T) { 361 testCases := []struct { 362 Name string 363 Value *string 364 ExpectedValid bool 365 }{ 366 { 367 Name: "ExpectedValid", 368 Value: str.Ptr(inputvalidationtest.ValidName), 369 ExpectedValid: true, 370 }, 371 { 372 Name: "Nil pointer", 373 Value: nil, 374 ExpectedValid: true, 375 }, 376 { 377 Name: "Empty string", 378 Value: str.Ptr(inputvalidationtest.EmptyString), 379 ExpectedValid: true, 380 }, 381 { 382 Name: "String longer than 2000 chars", 383 Value: str.Ptr(inputvalidationtest.String2001Long), 384 ExpectedValid: false, 385 }, 386 } 387 388 for _, testCase := range testCases { 389 t.Run(testCase.Name, func(t *testing.T) { 390 //GIVEN 391 app := fixValidApplicationUpdateInput() 392 app.Description = testCase.Value 393 // WHEN 394 err := app.Validate() 395 // THEN 396 if testCase.ExpectedValid { 397 require.NoError(t, err) 398 } else { 399 require.Error(t, err) 400 } 401 }) 402 } 403 } 404 405 func TestApplicationUpdateInput_Validate_HealthCheckURL(t *testing.T) { 406 testCases := []struct { 407 Name string 408 Value *string 409 ExpectedValid bool 410 }{ 411 { 412 Name: "ExpectedValid", 413 Value: str.Ptr(inputvalidationtest.ValidURL), 414 ExpectedValid: true, 415 }, 416 { 417 Name: "ExpectedValid nil value", 418 Value: nil, 419 ExpectedValid: true, 420 }, 421 { 422 Name: "URL longer than 256", 423 Value: str.Ptr(inputvalidationtest.URL257Long), 424 ExpectedValid: false, 425 }, 426 { 427 Name: "Invalid", 428 Value: str.Ptr(inputvalidationtest.InvalidURL), 429 ExpectedValid: false, 430 }, 431 { 432 Name: "URL without protocol", 433 Value: str.Ptr(inputvalidationtest.InvalidURL), 434 ExpectedValid: false, 435 }, 436 } 437 438 for _, testCase := range testCases { 439 t.Run(testCase.Name, func(t *testing.T) { 440 //GIVEN 441 app := fixValidApplicationUpdateInput() 442 app.HealthCheckURL = testCase.Value 443 // WHEN 444 err := app.Validate() 445 // THEN 446 if testCase.ExpectedValid { 447 require.NoError(t, err) 448 } else { 449 require.Error(t, err) 450 } 451 }) 452 } 453 } 454 455 func fixValidApplicationUpdateInput() graphql.ApplicationUpdateInput { 456 return graphql.ApplicationUpdateInput{} 457 } 458 459 func fixValidApplicationRegisterInput() graphql.ApplicationRegisterInput { 460 return graphql.ApplicationRegisterInput{ 461 Name: "application", 462 } 463 } 464 465 func fixValidApplicationJSONInput() graphql.ApplicationJSONInput { 466 return graphql.ApplicationJSONInput{ 467 Name: "application", 468 } 469 }