github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/graphql/bundle_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 "github.com/kyma-incubator/compass/components/director/pkg/inputvalidation/inputvalidationtest" 8 "github.com/kyma-incubator/compass/components/director/pkg/str" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestBundleCreateInput_Validate_Name(t *testing.T) { 13 testCases := []struct { 14 Name string 15 Value string 16 ExpectedValid bool 17 }{ 18 { 19 Name: "ExpectedValid", 20 Value: "name-123.com", 21 ExpectedValid: true, 22 }, 23 { 24 Name: "Valid Printable ASCII", 25 Value: "V1 +=_-)(*&^%$#@!?/>.<,|\\\"':;}{][", 26 ExpectedValid: true, 27 }, 28 { 29 Name: "Empty string", 30 Value: inputvalidationtest.EmptyString, 31 ExpectedValid: false, 32 }, 33 { 34 Name: "String longer than 100 chars", 35 Value: inputvalidationtest.String129Long, 36 ExpectedValid: false, 37 }, 38 { 39 Name: "String contains invalid ASCII", 40 Value: "ąćńłóęǖǘǚǜ", 41 ExpectedValid: false, 42 }, 43 } 44 45 for _, testCase := range testCases { 46 t.Run(testCase.Name, func(t *testing.T) { 47 //GIVEN 48 obj := fixValidBundleCreateInput() 49 obj.Name = testCase.Value 50 // WHEN 51 err := obj.Validate() 52 // THEN 53 if testCase.ExpectedValid { 54 require.NoError(t, err) 55 } else { 56 require.Error(t, err) 57 } 58 }) 59 } 60 } 61 62 func TestBundleCreateInput_Validate_Description(t *testing.T) { 63 testCases := []struct { 64 Name string 65 Value *string 66 ExpectedValid bool 67 }{ 68 { 69 Name: "ExpectedValid", 70 Value: str.Ptr("this is a valid description"), 71 ExpectedValid: true, 72 }, 73 { 74 Name: "Nil pointer", 75 Value: nil, 76 ExpectedValid: true, 77 }, 78 { 79 Name: "Empty string", 80 Value: str.Ptr(inputvalidationtest.EmptyString), 81 ExpectedValid: true, 82 }, 83 { 84 Name: "String longer than 2000 chars", 85 Value: str.Ptr(inputvalidationtest.String2001Long), 86 ExpectedValid: false, 87 }, 88 } 89 90 for _, testCase := range testCases { 91 t.Run(testCase.Name, func(t *testing.T) { 92 //GIVEN 93 obj := fixValidBundleCreateInput() 94 obj.Description = testCase.Value 95 // WHEN 96 err := obj.Validate() 97 // THEN 98 if testCase.ExpectedValid { 99 require.NoError(t, err) 100 } else { 101 require.Error(t, err) 102 } 103 }) 104 } 105 } 106 107 func TestBundleCreateInput_Validate_DefaultInstanceAuth(t *testing.T) { 108 validObj := fixValidAuthInput() 109 110 testCases := []struct { 111 Name string 112 Value *graphql.AuthInput 113 ExpectedValid bool 114 }{ 115 { 116 Name: "ExpectedValid obj", 117 Value: &validObj, 118 ExpectedValid: true, 119 }, 120 { 121 Name: "Nil object", 122 Value: nil, 123 ExpectedValid: true, 124 }, 125 { 126 Name: "Invalid - Nested validation error", 127 Value: &graphql.AuthInput{Credential: &graphql.CredentialDataInput{}}, 128 ExpectedValid: false, 129 }, 130 } 131 132 for _, testCase := range testCases { 133 t.Run(testCase.Name, func(t *testing.T) { 134 //GIVEN 135 obj := fixValidBundleCreateInput() 136 obj.DefaultInstanceAuth = testCase.Value 137 // WHEN 138 err := obj.Validate() 139 // THEN 140 if testCase.ExpectedValid { 141 require.NoError(t, err) 142 } else { 143 require.Error(t, err) 144 } 145 }) 146 } 147 } 148 149 func TestBundleCreateInput_Validate_InstanceAuthRequestInputSchema(t *testing.T) { 150 schema := graphql.JSONSchema("Test") 151 emptySchema := graphql.JSONSchema("") 152 testCases := []struct { 153 Name string 154 Value *graphql.JSONSchema 155 ExpectedValid bool 156 }{ 157 { 158 Name: "ExpectedValid", 159 Value: &schema, 160 ExpectedValid: true, 161 }, 162 { 163 Name: "Empty schema", 164 Value: &emptySchema, 165 ExpectedValid: false, 166 }, 167 { 168 Name: "Nil pointer", 169 Value: nil, 170 ExpectedValid: true, 171 }, 172 } 173 174 for _, testCase := range testCases { 175 t.Run(testCase.Name, func(t *testing.T) { 176 //GIVEN 177 obj := fixValidBundleCreateInput() 178 obj.InstanceAuthRequestInputSchema = testCase.Value 179 // WHEN 180 err := obj.Validate() 181 // THEN 182 if testCase.ExpectedValid { 183 require.NoError(t, err) 184 } else { 185 require.Error(t, err) 186 } 187 }) 188 } 189 } 190 191 func TestBundleCreateInput_Validate_APIs(t *testing.T) { 192 validObj := fixValidAPIDefinitionInput() 193 194 testCases := []struct { 195 Name string 196 Value []*graphql.APIDefinitionInput 197 ExpectedValid bool 198 }{ 199 { 200 Name: "ExpectedValid array", 201 Value: []*graphql.APIDefinitionInput{&validObj}, 202 ExpectedValid: true, 203 }, 204 { 205 Name: "Empty array", 206 Value: []*graphql.APIDefinitionInput{}, 207 ExpectedValid: true, 208 }, 209 { 210 Name: "Array with invalid object", 211 Value: []*graphql.APIDefinitionInput{{}}, 212 ExpectedValid: false, 213 }, 214 } 215 216 for _, testCase := range testCases { 217 t.Run(testCase.Name, func(t *testing.T) { 218 //GIVEN 219 app := fixValidBundleCreateInput() 220 app.APIDefinitions = testCase.Value 221 // WHEN 222 err := app.Validate() 223 // THEN 224 if testCase.ExpectedValid { 225 require.NoError(t, err) 226 } else { 227 require.Error(t, err) 228 } 229 }) 230 } 231 } 232 233 func TestBundleCreateInput_Validate_EventAPIs(t *testing.T) { 234 validObj := fixValidEventAPIDefinitionInput() 235 236 testCases := []struct { 237 Name string 238 Value []*graphql.EventDefinitionInput 239 ExpectedValid bool 240 }{ 241 { 242 Name: "ExpectedValid array", 243 Value: []*graphql.EventDefinitionInput{&validObj}, 244 ExpectedValid: true, 245 }, 246 { 247 Name: "Empty array", 248 Value: []*graphql.EventDefinitionInput{}, 249 ExpectedValid: true, 250 }, 251 { 252 Name: "Array with invalid object", 253 Value: []*graphql.EventDefinitionInput{{}}, 254 ExpectedValid: false, 255 }, 256 } 257 258 for _, testCase := range testCases { 259 t.Run(testCase.Name, func(t *testing.T) { 260 //GIVEN 261 app := fixValidBundleCreateInput() 262 app.EventDefinitions = testCase.Value 263 // WHEN 264 err := app.Validate() 265 // THEN 266 if testCase.ExpectedValid { 267 require.NoError(t, err) 268 } else { 269 require.Error(t, err) 270 } 271 }) 272 } 273 } 274 275 func TestBundleCreateInput_Validate_Documents(t *testing.T) { 276 validDoc := fixValidDocument() 277 278 testCases := []struct { 279 Name string 280 Value []*graphql.DocumentInput 281 ExpectedValid bool 282 }{ 283 { 284 Name: "ExpectedValid array", 285 Value: []*graphql.DocumentInput{&validDoc}, 286 ExpectedValid: true, 287 }, 288 { 289 Name: "Empty array", 290 Value: []*graphql.DocumentInput{}, 291 ExpectedValid: true, 292 }, 293 { 294 Name: "Array with invalid object", 295 Value: []*graphql.DocumentInput{{}}, 296 ExpectedValid: false, 297 }, 298 } 299 300 for _, testCase := range testCases { 301 t.Run(testCase.Name, func(t *testing.T) { 302 //GIVEN 303 app := fixValidBundleCreateInput() 304 app.Documents = testCase.Value 305 // WHEN 306 err := app.Validate() 307 // THEN 308 if testCase.ExpectedValid { 309 require.NoError(t, err) 310 } else { 311 require.Error(t, err) 312 } 313 }) 314 } 315 } 316 317 func TestBundleUpdateInput_Validate_Name(t *testing.T) { 318 testCases := []struct { 319 Name string 320 Value string 321 ExpectedValid bool 322 }{ 323 { 324 Name: "ExpectedValid", 325 Value: "name-123.com", 326 ExpectedValid: true, 327 }, 328 { 329 Name: "Valid Printable ASCII", 330 Value: "V1 +=_-)(*&^%$#@!?/>.<,|\\\"':;}{][", 331 ExpectedValid: true, 332 }, 333 { 334 Name: "Empty string", 335 Value: inputvalidationtest.EmptyString, 336 ExpectedValid: false, 337 }, 338 { 339 Name: "String longer than 100 chars", 340 Value: inputvalidationtest.String129Long, 341 ExpectedValid: false, 342 }, 343 { 344 Name: "String contains invalid ASCII", 345 Value: "ąćńłóęǖǘǚǜ", 346 ExpectedValid: false, 347 }, 348 } 349 350 for _, testCase := range testCases { 351 t.Run(testCase.Name, func(t *testing.T) { 352 //GIVEN 353 obj := fixValidBundleUpdateInput() 354 obj.Name = testCase.Value 355 // WHEN 356 err := obj.Validate() 357 // THEN 358 if testCase.ExpectedValid { 359 require.NoError(t, err) 360 } else { 361 require.Error(t, err) 362 } 363 }) 364 } 365 } 366 367 func TestBundleUpdateInput_Validate_Description(t *testing.T) { 368 testCases := []struct { 369 Name string 370 Value *string 371 ExpectedValid bool 372 }{ 373 { 374 Name: "ExpectedValid", 375 Value: str.Ptr("this is a valid description"), 376 ExpectedValid: true, 377 }, 378 { 379 Name: "Nil pointer", 380 Value: nil, 381 ExpectedValid: true, 382 }, 383 { 384 Name: "Empty string", 385 Value: str.Ptr(inputvalidationtest.EmptyString), 386 ExpectedValid: true, 387 }, 388 { 389 Name: "String longer than 2000 chars", 390 Value: str.Ptr(inputvalidationtest.String2001Long), 391 ExpectedValid: false, 392 }, 393 } 394 395 for _, testCase := range testCases { 396 t.Run(testCase.Name, func(t *testing.T) { 397 //GIVEN 398 obj := fixValidBundleUpdateInput() 399 obj.Description = testCase.Value 400 // WHEN 401 err := obj.Validate() 402 // THEN 403 if testCase.ExpectedValid { 404 require.NoError(t, err) 405 } else { 406 require.Error(t, err) 407 } 408 }) 409 } 410 } 411 412 func TestBundleUpdateInput_Validate_DefaultInstanceAuth(t *testing.T) { 413 validObj := fixValidAuthInput() 414 415 testCases := []struct { 416 Name string 417 Value *graphql.AuthInput 418 ExpectedValid bool 419 }{ 420 { 421 Name: "ExpectedValid obj", 422 Value: &validObj, 423 ExpectedValid: true, 424 }, 425 { 426 Name: "Nil object", 427 Value: nil, 428 ExpectedValid: true, 429 }, 430 { 431 Name: "Invalid - Nested validation error", 432 Value: &graphql.AuthInput{Credential: &graphql.CredentialDataInput{}}, 433 ExpectedValid: false, 434 }, 435 } 436 437 for _, testCase := range testCases { 438 t.Run(testCase.Name, func(t *testing.T) { 439 //GIVEN 440 obj := fixValidBundleUpdateInput() 441 obj.DefaultInstanceAuth = testCase.Value 442 // WHEN 443 err := obj.Validate() 444 // THEN 445 if testCase.ExpectedValid { 446 require.NoError(t, err) 447 } else { 448 require.Error(t, err) 449 } 450 }) 451 } 452 } 453 454 func TestBundleUpdateInput_Validate_InstanceAuthRequestInputSchema(t *testing.T) { 455 schema := graphql.JSONSchema("Test") 456 emptySchema := graphql.JSONSchema("") 457 testCases := []struct { 458 Name string 459 Value *graphql.JSONSchema 460 ExpectedValid bool 461 }{ 462 { 463 Name: "ExpectedValid", 464 Value: &schema, 465 ExpectedValid: true, 466 }, 467 { 468 Name: "Empty schema", 469 Value: &emptySchema, 470 ExpectedValid: false, 471 }, 472 { 473 Name: "Nil pointer", 474 Value: nil, 475 ExpectedValid: true, 476 }, 477 } 478 479 for _, testCase := range testCases { 480 t.Run(testCase.Name, func(t *testing.T) { 481 //GIVEN 482 obj := fixValidBundleUpdateInput() 483 obj.InstanceAuthRequestInputSchema = testCase.Value 484 // WHEN 485 err := obj.Validate() 486 // THEN 487 if testCase.ExpectedValid { 488 require.NoError(t, err) 489 } else { 490 require.Error(t, err) 491 } 492 }) 493 } 494 } 495 496 func TestBundleInstanceAuthRequestInput_Validate(t *testing.T) { 497 //GIVEN 498 val := graphql.JSON("{\"foo\": \"bar\"}") 499 testCases := []struct { 500 Name string 501 Value graphql.BundleInstanceAuthRequestInput 502 ExpectedValid bool 503 }{ 504 { 505 Name: "Empty", 506 Value: graphql.BundleInstanceAuthRequestInput{}, 507 ExpectedValid: true, 508 }, 509 { 510 Name: "InputParams and Context set", 511 Value: graphql.BundleInstanceAuthRequestInput{ 512 Context: &val, 513 InputParams: &val, 514 }, 515 ExpectedValid: true, 516 }, 517 } 518 519 for _, testCase := range testCases { 520 t.Run(testCase.Name, func(t *testing.T) { 521 // WHEN 522 err := testCase.Value.Validate() 523 // THEN 524 if testCase.ExpectedValid { 525 require.NoError(t, err) 526 } else { 527 require.Error(t, err) 528 } 529 }) 530 } 531 } 532 533 func TestBundleInstanceAuthSetInput_Validate(t *testing.T) { 534 //GIVEN 535 authInput := fixValidAuthInput() 536 str := "foo" 537 testCases := []struct { 538 Name string 539 Value graphql.BundleInstanceAuthSetInput 540 ExpectedValid bool 541 }{ 542 { 543 Name: "Auth", 544 Value: graphql.BundleInstanceAuthSetInput{ 545 Auth: &authInput, 546 }, 547 ExpectedValid: true, 548 }, 549 { 550 Name: "Failed Status", 551 Value: graphql.BundleInstanceAuthSetInput{ 552 Status: &graphql.BundleInstanceAuthStatusInput{ 553 Condition: graphql.BundleInstanceAuthSetStatusConditionInputFailed, 554 Reason: str, 555 Message: str, 556 }, 557 }, 558 ExpectedValid: true, 559 }, 560 { 561 Name: "Success Status", 562 Value: graphql.BundleInstanceAuthSetInput{ 563 Status: &graphql.BundleInstanceAuthStatusInput{ 564 Condition: graphql.BundleInstanceAuthSetStatusConditionInputSucceeded, 565 }, 566 }, 567 ExpectedValid: false, 568 }, 569 { 570 Name: "Auth and Success Status", 571 Value: graphql.BundleInstanceAuthSetInput{ 572 Auth: &authInput, 573 Status: &graphql.BundleInstanceAuthStatusInput{ 574 Condition: graphql.BundleInstanceAuthSetStatusConditionInputSucceeded, 575 Message: str, 576 Reason: str, 577 }, 578 }, 579 ExpectedValid: true, 580 }, 581 { 582 Name: "Auth and Failure Status", 583 Value: graphql.BundleInstanceAuthSetInput{ 584 Auth: &authInput, 585 Status: &graphql.BundleInstanceAuthStatusInput{ 586 Condition: graphql.BundleInstanceAuthSetStatusConditionInputFailed, 587 }, 588 }, 589 ExpectedValid: false, 590 }, 591 { 592 Name: "Empty objects", 593 Value: graphql.BundleInstanceAuthSetInput{ 594 Auth: &graphql.AuthInput{}, 595 Status: &graphql.BundleInstanceAuthStatusInput{}, 596 }, 597 ExpectedValid: false, 598 }, 599 { 600 Name: "Empty", 601 Value: graphql.BundleInstanceAuthSetInput{}, 602 ExpectedValid: false, 603 }, 604 } 605 606 for _, testCase := range testCases { 607 t.Run(testCase.Name, func(t *testing.T) { 608 // WHEN 609 err := testCase.Value.Validate() 610 // THEN 611 if testCase.ExpectedValid { 612 require.NoError(t, err) 613 } else { 614 require.Error(t, err) 615 } 616 }) 617 } 618 } 619 620 func TestBundleInstanceAuthStatusInput_Validate(t *testing.T) { 621 //GIVEN 622 str := "foo" 623 testCases := []struct { 624 Name string 625 Value graphql.BundleInstanceAuthStatusInput 626 ExpectedValid bool 627 }{ 628 { 629 Name: "Success", 630 Value: graphql.BundleInstanceAuthStatusInput{ 631 Condition: graphql.BundleInstanceAuthSetStatusConditionInputSucceeded, 632 Message: str, 633 Reason: str, 634 }, 635 ExpectedValid: true, 636 }, 637 { 638 Name: "No reason provided", 639 Value: graphql.BundleInstanceAuthStatusInput{ 640 Condition: graphql.BundleInstanceAuthSetStatusConditionInputSucceeded, 641 Message: str, 642 }, 643 ExpectedValid: false, 644 }, 645 { 646 Name: "No message provided", 647 Value: graphql.BundleInstanceAuthStatusInput{ 648 Condition: graphql.BundleInstanceAuthSetStatusConditionInputSucceeded, 649 Reason: str, 650 }, 651 ExpectedValid: false, 652 }, 653 { 654 Name: "No condition provided", 655 Value: graphql.BundleInstanceAuthStatusInput{ 656 Message: str, 657 Reason: str, 658 }, 659 ExpectedValid: false, 660 }, 661 } 662 663 for _, testCase := range testCases { 664 t.Run(testCase.Name, func(t *testing.T) { 665 // WHEN 666 err := testCase.Value.Validate() 667 // THEN 668 if testCase.ExpectedValid { 669 require.NoError(t, err) 670 } else { 671 require.Error(t, err) 672 } 673 }) 674 } 675 } 676 677 func fixValidBundleCreateInput() graphql.BundleCreateInput { 678 return graphql.BundleCreateInput{ 679 Name: inputvalidationtest.ValidName, 680 } 681 } 682 683 func fixValidBundleUpdateInput() graphql.BundleUpdateInput { 684 return graphql.BundleUpdateInput{ 685 Name: inputvalidationtest.ValidName, 686 } 687 }