github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/integrationsystem/resolver_test.go (about) 1 package integrationsystem_test 2 3 import ( 4 "context" 5 "testing" 6 7 pkgmodel "github.com/kyma-incubator/compass/components/director/pkg/model" 8 9 "github.com/kyma-incubator/compass/components/director/pkg/resource" 10 11 "github.com/kyma-incubator/compass/components/director/pkg/apperrors" 12 13 "github.com/kyma-incubator/compass/components/director/internal/model" 14 15 "github.com/kyma-incubator/compass/components/director/internal/domain/integrationsystem" 16 "github.com/kyma-incubator/compass/components/director/internal/domain/integrationsystem/automock" 17 "github.com/kyma-incubator/compass/components/director/internal/domain/tenant" 18 "github.com/kyma-incubator/compass/components/director/pkg/graphql" 19 persistenceautomock "github.com/kyma-incubator/compass/components/director/pkg/persistence/automock" 20 "github.com/kyma-incubator/compass/components/director/pkg/persistence/txtest" 21 22 "github.com/stretchr/testify/assert" 23 "github.com/stretchr/testify/require" 24 ) 25 26 func TestResolver_IntegrationSystem(t *testing.T) { 27 // GIVEN 28 ctx := tenant.SaveToContext(context.TODO(), testTenant, testExternalTenant) 29 30 txGen := txtest.NewTransactionContextGenerator(testError) 31 32 modelIntSys := fixModelIntegrationSystem(testID, testName) 33 gqlIntSys := fixGQLIntegrationSystem(testID, testName) 34 35 testCases := []struct { 36 Name string 37 TxFn func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) 38 IntSysSvcFn func() *automock.IntegrationSystemService 39 IntSysConvFn func() *automock.IntegrationSystemConverter 40 ExpectedOutput *graphql.IntegrationSystem 41 ExpectedError error 42 }{ 43 { 44 Name: "Success", 45 TxFn: txGen.ThatSucceeds, 46 IntSysSvcFn: func() *automock.IntegrationSystemService { 47 intSysSvc := &automock.IntegrationSystemService{} 48 intSysSvc.On("Get", txtest.CtxWithDBMatcher(), testID).Return(modelIntSys, nil).Once() 49 return intSysSvc 50 }, 51 IntSysConvFn: func() *automock.IntegrationSystemConverter { 52 intSysConv := &automock.IntegrationSystemConverter{} 53 intSysConv.On("ToGraphQL", modelIntSys).Return(gqlIntSys).Once() 54 return intSysConv 55 }, 56 ExpectedOutput: gqlIntSys, 57 }, 58 { 59 Name: "Returns nil when integration system not found", 60 TxFn: txGen.ThatSucceeds, 61 IntSysSvcFn: func() *automock.IntegrationSystemService { 62 intSysSvc := &automock.IntegrationSystemService{} 63 intSysSvc.On("Get", txtest.CtxWithDBMatcher(), testID).Return(nil, apperrors.NewNotFoundError(resource.IntegrationSystem, "")).Once() 64 return intSysSvc 65 }, 66 IntSysConvFn: func() *automock.IntegrationSystemConverter { 67 intSysConv := &automock.IntegrationSystemConverter{} 68 return intSysConv 69 }, 70 ExpectedOutput: nil, 71 }, 72 { 73 Name: "Returns error when getting integration system failed", 74 TxFn: txGen.ThatDoesntExpectCommit, 75 IntSysSvcFn: func() *automock.IntegrationSystemService { 76 intSysSvc := &automock.IntegrationSystemService{} 77 intSysSvc.On("Get", txtest.CtxWithDBMatcher(), testID).Return(nil, testError).Once() 78 return intSysSvc 79 }, 80 IntSysConvFn: func() *automock.IntegrationSystemConverter { 81 intSysConv := &automock.IntegrationSystemConverter{} 82 return intSysConv 83 }, 84 ExpectedError: testError, 85 }, 86 { 87 Name: "Returns error when beginning transaction", 88 TxFn: txGen.ThatFailsOnBegin, 89 IntSysSvcFn: func() *automock.IntegrationSystemService { 90 intSysSvc := &automock.IntegrationSystemService{} 91 return intSysSvc 92 }, 93 IntSysConvFn: func() *automock.IntegrationSystemConverter { 94 intSysConv := &automock.IntegrationSystemConverter{} 95 return intSysConv 96 }, 97 ExpectedError: testError, 98 }, 99 { 100 Name: "Returns error when committing transaction", 101 TxFn: txGen.ThatFailsOnCommit, 102 IntSysSvcFn: func() *automock.IntegrationSystemService { 103 intSysSvc := &automock.IntegrationSystemService{} 104 intSysSvc.On("Get", txtest.CtxWithDBMatcher(), testID).Return(modelIntSys, nil).Once() 105 return intSysSvc 106 }, 107 IntSysConvFn: func() *automock.IntegrationSystemConverter { 108 intSysConv := &automock.IntegrationSystemConverter{} 109 return intSysConv 110 }, 111 ExpectedError: testError, 112 }, 113 } 114 115 for _, testCase := range testCases { 116 t.Run(testCase.Name, func(t *testing.T) { 117 persist, transact := testCase.TxFn() 118 intSysSvc := testCase.IntSysSvcFn() 119 intSysConv := testCase.IntSysConvFn() 120 121 resolver := integrationsystem.NewResolver(transact, intSysSvc, nil, nil, intSysConv, nil) 122 123 // WHEN 124 result, err := resolver.IntegrationSystem(ctx, testID) 125 126 // THEN 127 if testCase.ExpectedError != nil { 128 require.Error(t, err) 129 assert.Contains(t, err.Error(), testCase.ExpectedError.Error()) 130 } else { 131 assert.NoError(t, err) 132 } 133 assert.Equal(t, testCase.ExpectedOutput, result) 134 135 persist.AssertExpectations(t) 136 transact.AssertExpectations(t) 137 intSysSvc.AssertExpectations(t) 138 intSysConv.AssertExpectations(t) 139 }) 140 } 141 } 142 143 func TestResolver_IntegrationSystems(t *testing.T) { 144 // GIVEN 145 ctx := tenant.SaveToContext(context.TODO(), testTenant, testExternalTenant) 146 txGen := txtest.NewTransactionContextGenerator(testError) 147 modelIntSys := []*model.IntegrationSystem{ 148 fixModelIntegrationSystem("i1", "n1"), 149 fixModelIntegrationSystem("i2", "n2"), 150 } 151 modelPage := fixModelIntegrationSystemPage(modelIntSys) 152 gqlIntSys := []*graphql.IntegrationSystem{ 153 fixGQLIntegrationSystem("i1", "n1"), 154 fixGQLIntegrationSystem("i2", "n2"), 155 } 156 gqlPage := fixGQLIntegrationSystemPage(gqlIntSys) 157 first := 2 158 after := "test" 159 gqlAfter := graphql.PageCursor(after) 160 161 testCases := []struct { 162 Name string 163 TxFn func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) 164 IntSysSvcFn func() *automock.IntegrationSystemService 165 IntSysConvFn func() *automock.IntegrationSystemConverter 166 ExpectedOutput *graphql.IntegrationSystemPage 167 ExpectedError error 168 }{ 169 { 170 Name: "Success", 171 TxFn: txGen.ThatSucceeds, 172 IntSysSvcFn: func() *automock.IntegrationSystemService { 173 intSysSvc := &automock.IntegrationSystemService{} 174 intSysSvc.On("List", txtest.CtxWithDBMatcher(), first, after).Return(modelPage, nil).Once() 175 return intSysSvc 176 }, 177 IntSysConvFn: func() *automock.IntegrationSystemConverter { 178 intSysConv := &automock.IntegrationSystemConverter{} 179 intSysConv.On("MultipleToGraphQL", modelIntSys).Return(gqlIntSys).Once() 180 return intSysConv 181 }, 182 ExpectedOutput: &gqlPage, 183 }, 184 { 185 Name: "Returns error when getting integration system failed", 186 TxFn: txGen.ThatDoesntExpectCommit, 187 IntSysSvcFn: func() *automock.IntegrationSystemService { 188 intSysSvc := &automock.IntegrationSystemService{} 189 intSysSvc.On("List", txtest.CtxWithDBMatcher(), first, after).Return(model.IntegrationSystemPage{}, testError).Once() 190 return intSysSvc 191 }, 192 IntSysConvFn: func() *automock.IntegrationSystemConverter { 193 intSysConv := &automock.IntegrationSystemConverter{} 194 return intSysConv 195 }, 196 ExpectedError: testError, 197 }, 198 { 199 Name: "Returns error when beginning transaction", 200 TxFn: txGen.ThatFailsOnBegin, 201 IntSysSvcFn: func() *automock.IntegrationSystemService { 202 intSysSvc := &automock.IntegrationSystemService{} 203 return intSysSvc 204 }, 205 IntSysConvFn: func() *automock.IntegrationSystemConverter { 206 intSysConv := &automock.IntegrationSystemConverter{} 207 return intSysConv 208 }, 209 ExpectedError: testError, 210 }, 211 { 212 Name: "Returns error when committing transaction", 213 TxFn: txGen.ThatFailsOnCommit, 214 IntSysSvcFn: func() *automock.IntegrationSystemService { 215 intSysSvc := &automock.IntegrationSystemService{} 216 intSysSvc.On("List", txtest.CtxWithDBMatcher(), first, after).Return(modelPage, nil).Once() 217 return intSysSvc 218 }, 219 IntSysConvFn: func() *automock.IntegrationSystemConverter { 220 intSysConv := &automock.IntegrationSystemConverter{} 221 return intSysConv 222 }, 223 ExpectedError: testError, 224 }, 225 } 226 227 for _, testCase := range testCases { 228 t.Run(testCase.Name, func(t *testing.T) { 229 persist, transact := testCase.TxFn() 230 intSysSvc := testCase.IntSysSvcFn() 231 intSysConv := testCase.IntSysConvFn() 232 233 resolver := integrationsystem.NewResolver(transact, intSysSvc, nil, nil, intSysConv, nil) 234 235 // WHEN 236 result, err := resolver.IntegrationSystems(ctx, &first, &gqlAfter) 237 238 // THEN 239 if testCase.ExpectedError != nil { 240 require.Error(t, err) 241 assert.Contains(t, err.Error(), testCase.ExpectedError.Error()) 242 } else { 243 assert.NoError(t, err) 244 } 245 assert.Equal(t, testCase.ExpectedOutput, result) 246 247 persist.AssertExpectations(t) 248 transact.AssertExpectations(t) 249 intSysSvc.AssertExpectations(t) 250 intSysConv.AssertExpectations(t) 251 }) 252 } 253 } 254 255 func TestResolver_CreateIntegrationSystem(t *testing.T) { 256 // GIVEN 257 ctx := tenant.SaveToContext(context.TODO(), testTenant, testExternalTenant) 258 259 txGen := txtest.NewTransactionContextGenerator(testError) 260 261 modelIntSys := fixModelIntegrationSystem(testID, testName) 262 modelIntSysInput := fixModelIntegrationSystemInput(testName) 263 gqlIntSys := fixGQLIntegrationSystem(testID, testName) 264 gqlIntSysInput := fixGQLIntegrationSystemInput(testName) 265 266 testCases := []struct { 267 Name string 268 TxFn func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) 269 IntSysSvcFn func() *automock.IntegrationSystemService 270 IntSysConvFn func() *automock.IntegrationSystemConverter 271 ExpectedOutput *graphql.IntegrationSystem 272 ExpectedError error 273 }{ 274 { 275 Name: "Success", 276 TxFn: txGen.ThatSucceeds, 277 IntSysSvcFn: func() *automock.IntegrationSystemService { 278 intSysSvc := &automock.IntegrationSystemService{} 279 intSysSvc.On("Create", txtest.CtxWithDBMatcher(), modelIntSysInput).Return(modelIntSys.ID, nil).Once() 280 intSysSvc.On("Get", txtest.CtxWithDBMatcher(), testID).Return(modelIntSys, nil).Once() 281 return intSysSvc 282 }, 283 IntSysConvFn: func() *automock.IntegrationSystemConverter { 284 intSysConv := &automock.IntegrationSystemConverter{} 285 intSysConv.On("InputFromGraphQL", gqlIntSysInput).Return(modelIntSysInput).Once() 286 intSysConv.On("ToGraphQL", modelIntSys).Return(gqlIntSys).Once() 287 return intSysConv 288 }, 289 ExpectedOutput: gqlIntSys, 290 }, 291 { 292 Name: "Returns error when creating integration system failed", 293 TxFn: txGen.ThatDoesntExpectCommit, 294 IntSysSvcFn: func() *automock.IntegrationSystemService { 295 intSysSvc := &automock.IntegrationSystemService{} 296 intSysSvc.On("Create", txtest.CtxWithDBMatcher(), modelIntSysInput).Return("", testError).Once() 297 return intSysSvc 298 }, 299 IntSysConvFn: func() *automock.IntegrationSystemConverter { 300 intSysConv := &automock.IntegrationSystemConverter{} 301 intSysConv.On("InputFromGraphQL", gqlIntSysInput).Return(modelIntSysInput).Once() 302 return intSysConv 303 }, 304 ExpectedError: testError, 305 }, 306 { 307 Name: "Returns error when getting integration system failed", 308 TxFn: txGen.ThatDoesntExpectCommit, 309 IntSysSvcFn: func() *automock.IntegrationSystemService { 310 intSysSvc := &automock.IntegrationSystemService{} 311 intSysSvc.On("Create", txtest.CtxWithDBMatcher(), modelIntSysInput).Return(modelIntSys.ID, nil).Once() 312 intSysSvc.On("Get", txtest.CtxWithDBMatcher(), testID).Return(nil, testError).Once() 313 return intSysSvc 314 }, 315 IntSysConvFn: func() *automock.IntegrationSystemConverter { 316 intSysConv := &automock.IntegrationSystemConverter{} 317 intSysConv.On("InputFromGraphQL", gqlIntSysInput).Return(modelIntSysInput).Once() 318 return intSysConv 319 }, 320 ExpectedError: testError, 321 }, 322 { 323 Name: "Returns error when beginning transaction", 324 TxFn: txGen.ThatFailsOnBegin, 325 IntSysSvcFn: func() *automock.IntegrationSystemService { 326 intSysSvc := &automock.IntegrationSystemService{} 327 return intSysSvc 328 }, 329 IntSysConvFn: func() *automock.IntegrationSystemConverter { 330 intSysConv := &automock.IntegrationSystemConverter{} 331 intSysConv.On("InputFromGraphQL", gqlIntSysInput).Return(modelIntSysInput).Once() 332 return intSysConv 333 }, 334 ExpectedError: testError, 335 }, 336 { 337 Name: "Returns error when committing transaction", 338 TxFn: txGen.ThatFailsOnCommit, 339 IntSysSvcFn: func() *automock.IntegrationSystemService { 340 intSysSvc := &automock.IntegrationSystemService{} 341 intSysSvc.On("Create", txtest.CtxWithDBMatcher(), modelIntSysInput).Return(modelIntSys.ID, nil).Once() 342 intSysSvc.On("Get", txtest.CtxWithDBMatcher(), testID).Return(modelIntSys, nil).Once() 343 return intSysSvc 344 }, 345 IntSysConvFn: func() *automock.IntegrationSystemConverter { 346 intSysConv := &automock.IntegrationSystemConverter{} 347 intSysConv.On("InputFromGraphQL", gqlIntSysInput).Return(modelIntSysInput).Once() 348 return intSysConv 349 }, 350 ExpectedError: testError, 351 }, 352 } 353 354 for _, testCase := range testCases { 355 t.Run(testCase.Name, func(t *testing.T) { 356 persist, transact := testCase.TxFn() 357 intSysSvc := testCase.IntSysSvcFn() 358 intSysConv := testCase.IntSysConvFn() 359 360 resolver := integrationsystem.NewResolver(transact, intSysSvc, nil, nil, intSysConv, nil) 361 362 // WHEN 363 result, err := resolver.RegisterIntegrationSystem(ctx, gqlIntSysInput) 364 365 // THEN 366 if testCase.ExpectedError != nil { 367 require.Error(t, err) 368 assert.Contains(t, err.Error(), testCase.ExpectedError.Error()) 369 } else { 370 assert.NoError(t, err) 371 } 372 assert.Equal(t, testCase.ExpectedOutput, result) 373 374 persist.AssertExpectations(t) 375 transact.AssertExpectations(t) 376 intSysSvc.AssertExpectations(t) 377 intSysConv.AssertExpectations(t) 378 }) 379 } 380 } 381 382 func TestResolver_UpdateIntegrationSystem(t *testing.T) { 383 // GIVEN 384 ctx := tenant.SaveToContext(context.TODO(), testTenant, testExternalTenant) 385 386 txGen := txtest.NewTransactionContextGenerator(testError) 387 388 modelIntSys := fixModelIntegrationSystem(testID, testName) 389 modelIntSysInput := fixModelIntegrationSystemInput(testName) 390 gqlIntSys := fixGQLIntegrationSystem(testID, testName) 391 gqlIntSysInput := fixGQLIntegrationSystemInput(testName) 392 393 testCases := []struct { 394 Name string 395 TxFn func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) 396 IntSysSvcFn func() *automock.IntegrationSystemService 397 IntSysConvFn func() *automock.IntegrationSystemConverter 398 ExpectedOutput *graphql.IntegrationSystem 399 ExpectedError error 400 }{ 401 { 402 Name: "Success", 403 TxFn: txGen.ThatSucceeds, 404 IntSysSvcFn: func() *automock.IntegrationSystemService { 405 intSysSvc := &automock.IntegrationSystemService{} 406 intSysSvc.On("Update", txtest.CtxWithDBMatcher(), testID, modelIntSysInput).Return(nil).Once() 407 intSysSvc.On("Get", txtest.CtxWithDBMatcher(), testID).Return(modelIntSys, nil).Once() 408 return intSysSvc 409 }, 410 IntSysConvFn: func() *automock.IntegrationSystemConverter { 411 intSysConv := &automock.IntegrationSystemConverter{} 412 intSysConv.On("InputFromGraphQL", gqlIntSysInput).Return(modelIntSysInput).Once() 413 intSysConv.On("ToGraphQL", modelIntSys).Return(gqlIntSys).Once() 414 return intSysConv 415 }, 416 ExpectedOutput: gqlIntSys, 417 }, 418 { 419 Name: "Returns error when updating integration system failed", 420 TxFn: txGen.ThatDoesntExpectCommit, 421 IntSysSvcFn: func() *automock.IntegrationSystemService { 422 intSysSvc := &automock.IntegrationSystemService{} 423 intSysSvc.On("Update", txtest.CtxWithDBMatcher(), testID, modelIntSysInput).Return(testError).Once() 424 return intSysSvc 425 }, 426 IntSysConvFn: func() *automock.IntegrationSystemConverter { 427 intSysConv := &automock.IntegrationSystemConverter{} 428 intSysConv.On("InputFromGraphQL", gqlIntSysInput).Return(modelIntSysInput).Once() 429 return intSysConv 430 }, 431 ExpectedError: testError, 432 }, 433 { 434 Name: "Returns error when getting integration system failed", 435 TxFn: txGen.ThatDoesntExpectCommit, 436 IntSysSvcFn: func() *automock.IntegrationSystemService { 437 intSysSvc := &automock.IntegrationSystemService{} 438 intSysSvc.On("Update", txtest.CtxWithDBMatcher(), testID, modelIntSysInput).Return(nil).Once() 439 intSysSvc.On("Get", txtest.CtxWithDBMatcher(), testID).Return(nil, testError).Once() 440 return intSysSvc 441 }, 442 IntSysConvFn: func() *automock.IntegrationSystemConverter { 443 intSysConv := &automock.IntegrationSystemConverter{} 444 intSysConv.On("InputFromGraphQL", gqlIntSysInput).Return(modelIntSysInput).Once() 445 return intSysConv 446 }, 447 ExpectedError: testError, 448 }, 449 { 450 Name: "Returns error when beginning transaction", 451 TxFn: txGen.ThatFailsOnBegin, 452 IntSysSvcFn: func() *automock.IntegrationSystemService { 453 intSysSvc := &automock.IntegrationSystemService{} 454 return intSysSvc 455 }, 456 IntSysConvFn: func() *automock.IntegrationSystemConverter { 457 intSysConv := &automock.IntegrationSystemConverter{} 458 return intSysConv 459 }, 460 ExpectedError: testError, 461 }, 462 { 463 Name: "Returns error when committing transaction", 464 TxFn: txGen.ThatFailsOnCommit, 465 IntSysSvcFn: func() *automock.IntegrationSystemService { 466 intSysSvc := &automock.IntegrationSystemService{} 467 intSysSvc.On("Update", txtest.CtxWithDBMatcher(), testID, modelIntSysInput).Return(nil).Once() 468 intSysSvc.On("Get", txtest.CtxWithDBMatcher(), testID).Return(modelIntSys, nil).Once() 469 return intSysSvc 470 }, 471 IntSysConvFn: func() *automock.IntegrationSystemConverter { 472 intSysConv := &automock.IntegrationSystemConverter{} 473 intSysConv.On("InputFromGraphQL", gqlIntSysInput).Return(modelIntSysInput).Once() 474 return intSysConv 475 }, 476 ExpectedError: testError, 477 }, 478 } 479 480 for _, testCase := range testCases { 481 t.Run(testCase.Name, func(t *testing.T) { 482 persist, transact := testCase.TxFn() 483 intSysSvc := testCase.IntSysSvcFn() 484 intSysConv := testCase.IntSysConvFn() 485 486 resolver := integrationsystem.NewResolver(transact, intSysSvc, nil, nil, intSysConv, nil) 487 488 // WHEN 489 result, err := resolver.UpdateIntegrationSystem(ctx, testID, gqlIntSysInput) 490 491 // THEN 492 if testCase.ExpectedError != nil { 493 require.Error(t, err) 494 assert.Contains(t, err.Error(), testCase.ExpectedError.Error()) 495 } else { 496 assert.NoError(t, err) 497 } 498 assert.Equal(t, testCase.ExpectedOutput, result) 499 500 persist.AssertExpectations(t) 501 transact.AssertExpectations(t) 502 intSysSvc.AssertExpectations(t) 503 intSysConv.AssertExpectations(t) 504 }) 505 } 506 } 507 508 func TestResolver_UnregisterIntegrationSystem(t *testing.T) { 509 // GIVEN 510 ctx := tenant.SaveToContext(context.TODO(), testTenant, testExternalTenant) 511 512 txGen := txtest.NewTransactionContextGenerator(testError) 513 514 modelIntSys := fixModelIntegrationSystem(testID, testName) 515 gqlIntSys := fixGQLIntegrationSystem(testID, testName) 516 testAuths := fixOAuths() 517 518 testCases := []struct { 519 Name string 520 TxFn func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) 521 IntSysSvcFn func() *automock.IntegrationSystemService 522 IntSysConvFn func() *automock.IntegrationSystemConverter 523 SysAuthSvcFn func() *automock.SystemAuthService 524 OAuth20SvcFn func() *automock.OAuth20Service 525 ExpectedOutput *graphql.IntegrationSystem 526 ExpectedError error 527 }{ 528 { 529 Name: "Success", 530 TxFn: txGen.ThatSucceeds, 531 IntSysSvcFn: func() *automock.IntegrationSystemService { 532 intSysSvc := &automock.IntegrationSystemService{} 533 intSysSvc.On("Get", txtest.CtxWithDBMatcher(), testID).Return(modelIntSys, nil).Once() 534 intSysSvc.On("Delete", txtest.CtxWithDBMatcher(), testID).Return(nil).Once() 535 return intSysSvc 536 }, 537 IntSysConvFn: func() *automock.IntegrationSystemConverter { 538 intSysConv := &automock.IntegrationSystemConverter{} 539 intSysConv.On("ToGraphQL", modelIntSys).Return(gqlIntSys).Once() 540 return intSysConv 541 }, 542 SysAuthSvcFn: func() *automock.SystemAuthService { 543 svc := &automock.SystemAuthService{} 544 svc.On("ListForObject", txtest.CtxWithDBMatcher(), pkgmodel.IntegrationSystemReference, modelIntSys.ID).Return(testAuths, nil) 545 return svc 546 }, 547 OAuth20SvcFn: func() *automock.OAuth20Service { 548 svc := &automock.OAuth20Service{} 549 svc.On("DeleteMultipleClientCredentials", txtest.CtxWithDBMatcher(), testAuths).Return(nil) 550 return svc 551 }, 552 553 ExpectedOutput: gqlIntSys, 554 }, 555 { 556 Name: "Returns error when getting integration system failed", 557 TxFn: txGen.ThatDoesntExpectCommit, 558 IntSysSvcFn: func() *automock.IntegrationSystemService { 559 intSysSvc := &automock.IntegrationSystemService{} 560 intSysSvc.On("Get", txtest.CtxWithDBMatcher(), testID).Return(nil, testError).Once() 561 return intSysSvc 562 }, 563 IntSysConvFn: func() *automock.IntegrationSystemConverter { 564 intSysConv := &automock.IntegrationSystemConverter{} 565 return intSysConv 566 }, 567 SysAuthSvcFn: func() *automock.SystemAuthService { 568 svc := &automock.SystemAuthService{} 569 return svc 570 }, 571 OAuth20SvcFn: func() *automock.OAuth20Service { 572 svc := &automock.OAuth20Service{} 573 return svc 574 }, 575 ExpectedError: testError, 576 }, 577 { 578 Name: "Returns error when deleting integration system failed", 579 TxFn: txGen.ThatDoesntExpectCommit, 580 IntSysSvcFn: func() *automock.IntegrationSystemService { 581 intSysSvc := &automock.IntegrationSystemService{} 582 intSysSvc.On("Get", txtest.CtxWithDBMatcher(), testID).Return(modelIntSys, nil).Once() 583 intSysSvc.On("Delete", txtest.CtxWithDBMatcher(), testID).Return(testError).Once() 584 return intSysSvc 585 }, 586 IntSysConvFn: func() *automock.IntegrationSystemConverter { 587 intSysConv := &automock.IntegrationSystemConverter{} 588 return intSysConv 589 }, 590 SysAuthSvcFn: func() *automock.SystemAuthService { 591 svc := &automock.SystemAuthService{} 592 svc.On("ListForObject", txtest.CtxWithDBMatcher(), pkgmodel.IntegrationSystemReference, modelIntSys.ID).Return(testAuths, nil) 593 return svc 594 }, 595 OAuth20SvcFn: func() *automock.OAuth20Service { 596 svc := &automock.OAuth20Service{} 597 return svc 598 }, 599 ExpectedError: testError, 600 }, 601 { 602 Name: "Returns error when beginning transaction", 603 TxFn: txGen.ThatFailsOnBegin, 604 IntSysSvcFn: func() *automock.IntegrationSystemService { 605 intSysSvc := &automock.IntegrationSystemService{} 606 return intSysSvc 607 }, 608 IntSysConvFn: func() *automock.IntegrationSystemConverter { 609 intSysConv := &automock.IntegrationSystemConverter{} 610 return intSysConv 611 }, 612 SysAuthSvcFn: func() *automock.SystemAuthService { 613 svc := &automock.SystemAuthService{} 614 return svc 615 }, 616 OAuth20SvcFn: func() *automock.OAuth20Service { 617 svc := &automock.OAuth20Service{} 618 return svc 619 }, 620 ExpectedError: testError, 621 }, 622 { 623 Name: "Returns error when committing transaction", 624 TxFn: txGen.ThatFailsOnCommit, 625 IntSysSvcFn: func() *automock.IntegrationSystemService { 626 intSysSvc := &automock.IntegrationSystemService{} 627 intSysSvc.On("Get", txtest.CtxWithDBMatcher(), testID).Return(modelIntSys, nil).Once() 628 intSysSvc.On("Delete", txtest.CtxWithDBMatcher(), testID).Return(nil).Once() 629 return intSysSvc 630 }, 631 IntSysConvFn: func() *automock.IntegrationSystemConverter { 632 intSysConv := &automock.IntegrationSystemConverter{} 633 return intSysConv 634 }, 635 SysAuthSvcFn: func() *automock.SystemAuthService { 636 svc := &automock.SystemAuthService{} 637 svc.On("ListForObject", txtest.CtxWithDBMatcher(), pkgmodel.IntegrationSystemReference, modelIntSys.ID).Return(testAuths, nil) 638 return svc 639 }, 640 OAuth20SvcFn: func() *automock.OAuth20Service { 641 svc := &automock.OAuth20Service{} 642 return svc 643 }, 644 645 ExpectedError: testError, 646 }, 647 { 648 Name: "Return error when listing all auths failed", 649 TxFn: txGen.ThatDoesntExpectCommit, 650 IntSysSvcFn: func() *automock.IntegrationSystemService { 651 svc := &automock.IntegrationSystemService{} 652 svc.On("Get", txtest.CtxWithDBMatcher(), "foo").Return(modelIntSys, nil).Once() 653 return svc 654 }, 655 IntSysConvFn: func() *automock.IntegrationSystemConverter { 656 conv := &automock.IntegrationSystemConverter{} 657 return conv 658 }, 659 SysAuthSvcFn: func() *automock.SystemAuthService { 660 svc := &automock.SystemAuthService{} 661 svc.On("ListForObject", txtest.CtxWithDBMatcher(), pkgmodel.IntegrationSystemReference, modelIntSys.ID).Return(nil, testError) 662 return svc 663 }, 664 OAuth20SvcFn: func() *automock.OAuth20Service { 665 svc := &automock.OAuth20Service{} 666 return svc 667 }, 668 669 ExpectedError: testError, 670 }, 671 { 672 Name: "Return error when deleting oauth credential failed ", 673 TxFn: txGen.ThatSucceeds, 674 IntSysSvcFn: func() *automock.IntegrationSystemService { 675 svc := &automock.IntegrationSystemService{} 676 svc.On("Get", txtest.CtxWithDBMatcher(), "foo").Return(modelIntSys, nil).Once() 677 svc.On("Delete", txtest.CtxWithDBMatcher(), testID).Return(nil).Once() 678 return svc 679 }, 680 IntSysConvFn: func() *automock.IntegrationSystemConverter { 681 conv := &automock.IntegrationSystemConverter{} 682 return conv 683 }, 684 SysAuthSvcFn: func() *automock.SystemAuthService { 685 svc := &automock.SystemAuthService{} 686 svc.On("ListForObject", txtest.CtxWithDBMatcher(), pkgmodel.IntegrationSystemReference, modelIntSys.ID).Return(testAuths, nil) 687 return svc 688 }, 689 OAuth20SvcFn: func() *automock.OAuth20Service { 690 svc := &automock.OAuth20Service{} 691 svc.On("DeleteMultipleClientCredentials", txtest.CtxWithDBMatcher(), testAuths).Return(testError) 692 return svc 693 }, 694 695 ExpectedError: testError, 696 }, 697 } 698 699 for _, testCase := range testCases { 700 t.Run(testCase.Name, func(t *testing.T) { 701 persist, transact := testCase.TxFn() 702 intSysSvc := testCase.IntSysSvcFn() 703 intSysConv := testCase.IntSysConvFn() 704 sysAuthSvc := testCase.SysAuthSvcFn() 705 oAuth20Svc := testCase.OAuth20SvcFn() 706 resolver := integrationsystem.NewResolver(transact, intSysSvc, sysAuthSvc, oAuth20Svc, intSysConv, nil) 707 708 // WHEN 709 result, err := resolver.UnregisterIntegrationSystem(ctx, testID) 710 711 // THEN 712 if testCase.ExpectedError != nil { 713 require.Error(t, err) 714 assert.Contains(t, err.Error(), testCase.ExpectedError.Error()) 715 } else { 716 assert.NoError(t, err) 717 } 718 assert.Equal(t, testCase.ExpectedOutput, result) 719 720 persist.AssertExpectations(t) 721 transact.AssertExpectations(t) 722 intSysSvc.AssertExpectations(t) 723 intSysConv.AssertExpectations(t) 724 sysAuthSvc.AssertExpectations(t) 725 oAuth20Svc.AssertExpectations(t) 726 }) 727 } 728 } 729 730 func TestResolver_Auths(t *testing.T) { 731 // GIVEN 732 ctx := tenant.SaveToContext(context.TODO(), testTenant, testExternalTenant) 733 734 parentIntegrationSystem := fixGQLIntegrationSystem(testID, testName) 735 736 modelSysAuths := []pkgmodel.SystemAuth{ 737 fixModelSystemAuth("bar", parentIntegrationSystem.ID, fixModelAuth()), 738 fixModelSystemAuth("baz", parentIntegrationSystem.ID, fixModelAuth()), 739 fixModelSystemAuth("faz", parentIntegrationSystem.ID, fixModelAuth()), 740 } 741 742 gqlSysAuths := []*graphql.IntSysSystemAuth{ 743 fixGQLSystemAuth("bar", fixGQLAuth()), 744 fixGQLSystemAuth("baz", fixGQLAuth()), 745 fixGQLSystemAuth("faz", fixGQLAuth()), 746 } 747 748 txGen := txtest.NewTransactionContextGenerator(testError) 749 750 testCases := []struct { 751 Name string 752 TransactionerFn func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) 753 SysAuthSvcFn func() *automock.SystemAuthService 754 SysAuthConvFn func() *automock.SystemAuthConverter 755 ExpectedOutput []*graphql.IntSysSystemAuth 756 ExpectedError error 757 }{ 758 { 759 Name: "Success", 760 TransactionerFn: txGen.ThatSucceeds, 761 SysAuthSvcFn: func() *automock.SystemAuthService { 762 sysAuthSvc := &automock.SystemAuthService{} 763 sysAuthSvc.On("ListForObject", txtest.CtxWithDBMatcher(), pkgmodel.IntegrationSystemReference, parentIntegrationSystem.ID).Return(modelSysAuths, nil).Once() 764 return sysAuthSvc 765 }, 766 SysAuthConvFn: func() *automock.SystemAuthConverter { 767 sysAuthConv := &automock.SystemAuthConverter{} 768 sysAuthConv.On("ToGraphQL", &modelSysAuths[0]).Return(gqlSysAuths[0], nil).Once() 769 sysAuthConv.On("ToGraphQL", &modelSysAuths[1]).Return(gqlSysAuths[1], nil).Once() 770 sysAuthConv.On("ToGraphQL", &modelSysAuths[2]).Return(gqlSysAuths[2], nil).Once() 771 return sysAuthConv 772 }, 773 ExpectedOutput: gqlSysAuths, 774 }, 775 { 776 Name: "Error when listing for object", 777 TransactionerFn: txGen.ThatDoesntExpectCommit, 778 SysAuthSvcFn: func() *automock.SystemAuthService { 779 sysAuthSvc := &automock.SystemAuthService{} 780 sysAuthSvc.On("ListForObject", txtest.CtxWithDBMatcher(), pkgmodel.IntegrationSystemReference, parentIntegrationSystem.ID).Return(nil, testError).Once() 781 return sysAuthSvc 782 }, 783 SysAuthConvFn: func() *automock.SystemAuthConverter { 784 sysAuthConv := &automock.SystemAuthConverter{} 785 return sysAuthConv 786 }, 787 ExpectedError: testError, 788 }, 789 { 790 Name: "Error when beginning transaction", 791 TransactionerFn: txGen.ThatFailsOnBegin, 792 SysAuthSvcFn: func() *automock.SystemAuthService { 793 sysAuthSvc := &automock.SystemAuthService{} 794 return sysAuthSvc 795 }, 796 SysAuthConvFn: func() *automock.SystemAuthConverter { 797 sysAuthConv := &automock.SystemAuthConverter{} 798 return sysAuthConv 799 }, 800 ExpectedError: testError, 801 }, 802 { 803 Name: "Error when committing transaction", 804 TransactionerFn: txGen.ThatFailsOnCommit, 805 SysAuthSvcFn: func() *automock.SystemAuthService { 806 sysAuthSvc := &automock.SystemAuthService{} 807 sysAuthSvc.On("ListForObject", txtest.CtxWithDBMatcher(), pkgmodel.IntegrationSystemReference, parentIntegrationSystem.ID).Return(modelSysAuths, nil).Once() 808 return sysAuthSvc 809 }, 810 SysAuthConvFn: func() *automock.SystemAuthConverter { 811 sysAuthConv := &automock.SystemAuthConverter{} 812 return sysAuthConv 813 }, 814 ExpectedError: testError, 815 }, 816 } 817 818 for _, testCase := range testCases { 819 t.Run(testCase.Name, func(t *testing.T) { 820 persist, transact := testCase.TransactionerFn() 821 sysAuthSvc := testCase.SysAuthSvcFn() 822 sysAuthConv := testCase.SysAuthConvFn() 823 824 resolver := integrationsystem.NewResolver(transact, nil, sysAuthSvc, nil, nil, sysAuthConv) 825 826 // WHEN 827 result, err := resolver.Auths(ctx, parentIntegrationSystem) 828 829 // THEN 830 if testCase.ExpectedError != nil { 831 require.Error(t, err) 832 assert.Contains(t, err.Error(), testCase.ExpectedError.Error()) 833 } else { 834 assert.NoError(t, err) 835 } 836 assert.Equal(t, testCase.ExpectedOutput, result) 837 838 persist.AssertExpectations(t) 839 transact.AssertExpectations(t) 840 sysAuthSvc.AssertExpectations(t) 841 sysAuthConv.AssertExpectations(t) 842 }) 843 } 844 845 t.Run("Error when parent object is nil", func(t *testing.T) { 846 resolver := integrationsystem.NewResolver(nil, nil, nil, nil, nil, nil) 847 848 // WHEN 849 result, err := resolver.Auths(context.TODO(), nil) 850 851 // THEN 852 require.Error(t, err) 853 assert.Contains(t, err.Error(), "Integration System cannot be empty") 854 assert.Nil(t, result) 855 }) 856 } 857 858 func fixOAuths() []pkgmodel.SystemAuth { 859 return []pkgmodel.SystemAuth{ 860 { 861 ID: "foo", 862 TenantID: nil, 863 Value: &model.Auth{ 864 Credential: model.CredentialData{ 865 Basic: nil, 866 Oauth: &model.OAuthCredentialData{ 867 ClientID: "foo", 868 ClientSecret: "foo", 869 URL: "foo", 870 }, 871 }, 872 }, 873 }, 874 { 875 ID: "bar", 876 TenantID: nil, 877 Value: nil, 878 }, 879 { 880 ID: "test", 881 TenantID: nil, 882 Value: &model.Auth{ 883 Credential: model.CredentialData{ 884 Basic: &model.BasicCredentialData{ 885 Username: "test", 886 Password: "test", 887 }, 888 Oauth: nil, 889 }, 890 }, 891 }, 892 } 893 }