github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/systemauth/resolver_test.go (about) 1 package systemauth_test 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/kyma-incubator/compass/components/director/internal/model" 8 9 "github.com/kyma-incubator/compass/components/director/internal/domain/systemauth" 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 13 pkgmodel "github.com/kyma-incubator/compass/components/director/pkg/model" 14 15 "github.com/kyma-incubator/compass/components/director/internal/domain/systemauth/automock" 16 "github.com/kyma-incubator/compass/components/director/pkg/graphql" 17 persistenceautomock "github.com/kyma-incubator/compass/components/director/pkg/persistence/automock" 18 "github.com/kyma-incubator/compass/components/director/pkg/persistence/txtest" 19 "github.com/pkg/errors" 20 ) 21 22 var contextParam = txtest.CtxWithDBMatcher() 23 24 func TestResolver_GenericDeleteSystemAuth(t *testing.T) { 25 // GIVEN 26 testErr := errors.New("Test error") 27 txGen := txtest.NewTransactionContextGenerator(testErr) 28 29 id := "foo" 30 objectID := "bar" 31 objectType := pkgmodel.RuntimeReference 32 modelSystemAuth := fixModelSystemAuth(id, objectType, objectID, fixModelAuth()) 33 oauthModelSystemAuth := fixModelSystemAuth(id, objectType, objectID, &model.Auth{ 34 Credential: model.CredentialData{ 35 Oauth: &model.OAuthCredentialData{ 36 ClientID: "clientid", 37 ClientSecret: "clientsecret", 38 URL: "foo.bar/token", 39 }, 40 }, 41 }) 42 gqlSystemAuth := fixGQLIntSysSystemAuth(id, fixGQLAuth(), objectID) 43 44 testCases := []struct { 45 Name string 46 TransactionerFn func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) 47 ServiceFn func() *automock.SystemAuthService 48 OAuthServiceFn func() *automock.OAuth20Service 49 OneTimeTokenFn func() *automock.OneTimeTokenService 50 ConverterFn func() *automock.SystemAuthConverter 51 AuthConverterFn func() *automock.AuthConverter 52 ExpectedSystemAuth graphql.SystemAuth 53 ExpectedErr error 54 }{ 55 { 56 Name: "Success - Basic Auth", 57 TransactionerFn: txGen.ThatSucceeds, 58 ServiceFn: func() *automock.SystemAuthService { 59 svc := &automock.SystemAuthService{} 60 svc.On("GetByIDForObject", contextParam, objectType, id).Return(modelSystemAuth, nil).Once() 61 svc.On("DeleteByIDForObject", contextParam, objectType, id).Return(nil).Once() 62 return svc 63 }, 64 OAuthServiceFn: func() *automock.OAuth20Service { 65 svc := &automock.OAuth20Service{} 66 return svc 67 }, 68 OneTimeTokenFn: unusedOneTimeTokenSvc, 69 ConverterFn: func() *automock.SystemAuthConverter { 70 conv := &automock.SystemAuthConverter{} 71 conv.On("ToGraphQL", modelSystemAuth).Return(gqlSystemAuth, nil).Once() 72 return conv 73 }, 74 AuthConverterFn: func() *automock.AuthConverter { 75 return &automock.AuthConverter{} 76 }, 77 ExpectedSystemAuth: gqlSystemAuth, 78 ExpectedErr: nil, 79 }, 80 { 81 Name: "Success - OAuth", 82 TransactionerFn: txGen.ThatSucceeds, 83 ServiceFn: func() *automock.SystemAuthService { 84 svc := &automock.SystemAuthService{} 85 svc.On("GetByIDForObject", contextParam, objectType, id).Return(oauthModelSystemAuth, nil).Once() 86 svc.On("DeleteByIDForObject", contextParam, objectType, id).Return(nil).Once() 87 return svc 88 }, 89 OAuthServiceFn: func() *automock.OAuth20Service { 90 svc := &automock.OAuth20Service{} 91 svc.On("DeleteClientCredentials", contextParam, oauthModelSystemAuth.Value.Credential.Oauth.ClientID).Return(nil) 92 return svc 93 }, 94 OneTimeTokenFn: unusedOneTimeTokenSvc, 95 ConverterFn: func() *automock.SystemAuthConverter { 96 conv := &automock.SystemAuthConverter{} 97 conv.On("ToGraphQL", oauthModelSystemAuth).Return(gqlSystemAuth, nil).Once() 98 return conv 99 }, 100 AuthConverterFn: func() *automock.AuthConverter { 101 return &automock.AuthConverter{} 102 }, 103 ExpectedSystemAuth: gqlSystemAuth, 104 ExpectedErr: nil, 105 }, 106 { 107 Name: "Error - GetByIDForObject SystemAuth", 108 TransactionerFn: txGen.ThatDoesntExpectCommit, 109 ServiceFn: func() *automock.SystemAuthService { 110 svc := &automock.SystemAuthService{} 111 svc.On("GetByIDForObject", contextParam, objectType, id).Return(nil, testErr).Once() 112 return svc 113 }, 114 OAuthServiceFn: func() *automock.OAuth20Service { 115 svc := &automock.OAuth20Service{} 116 return svc 117 }, 118 OneTimeTokenFn: unusedOneTimeTokenSvc, 119 ConverterFn: func() *automock.SystemAuthConverter { 120 conv := &automock.SystemAuthConverter{} 121 return conv 122 }, 123 AuthConverterFn: func() *automock.AuthConverter { 124 return &automock.AuthConverter{} 125 }, 126 ExpectedSystemAuth: nil, 127 ExpectedErr: testErr, 128 }, 129 { 130 Name: "Error - Delete from DB", 131 TransactionerFn: txGen.ThatDoesntExpectCommit, 132 ServiceFn: func() *automock.SystemAuthService { 133 svc := &automock.SystemAuthService{} 134 svc.On("GetByIDForObject", contextParam, objectType, id).Return(modelSystemAuth, nil).Once() 135 svc.On("DeleteByIDForObject", contextParam, objectType, id).Return(testErr).Once() 136 return svc 137 }, 138 OAuthServiceFn: func() *automock.OAuth20Service { 139 svc := &automock.OAuth20Service{} 140 return svc 141 }, 142 OneTimeTokenFn: unusedOneTimeTokenSvc, 143 ConverterFn: func() *automock.SystemAuthConverter { 144 conv := &automock.SystemAuthConverter{} 145 conv.On("ToGraphQL", modelSystemAuth).Return(gqlSystemAuth, nil).Once() 146 return conv 147 }, 148 AuthConverterFn: func() *automock.AuthConverter { 149 return &automock.AuthConverter{} 150 }, 151 ExpectedSystemAuth: nil, 152 ExpectedErr: testErr, 153 }, 154 { 155 Name: "Error - Delete Client", 156 TransactionerFn: txGen.ThatDoesntExpectCommit, 157 ServiceFn: func() *automock.SystemAuthService { 158 svc := &automock.SystemAuthService{} 159 svc.On("GetByIDForObject", contextParam, objectType, id).Return(oauthModelSystemAuth, nil).Once() 160 return svc 161 }, 162 OAuthServiceFn: func() *automock.OAuth20Service { 163 svc := &automock.OAuth20Service{} 164 svc.On("DeleteClientCredentials", contextParam, oauthModelSystemAuth.Value.Credential.Oauth.ClientID).Return(testErr) 165 return svc 166 }, 167 OneTimeTokenFn: unusedOneTimeTokenSvc, 168 ConverterFn: func() *automock.SystemAuthConverter { 169 conv := &automock.SystemAuthConverter{} 170 conv.On("ToGraphQL", oauthModelSystemAuth).Return(gqlSystemAuth, nil).Once() 171 return conv 172 }, 173 AuthConverterFn: func() *automock.AuthConverter { 174 return &automock.AuthConverter{} 175 }, 176 ExpectedSystemAuth: nil, 177 ExpectedErr: errors.Wrap(testErr, "while deleting OAuth 2.0 client"), 178 }, 179 { 180 Name: "Error - Transaction Begin", 181 TransactionerFn: txGen.ThatFailsOnBegin, 182 ServiceFn: func() *automock.SystemAuthService { 183 svc := &automock.SystemAuthService{} 184 return svc 185 }, 186 OAuthServiceFn: func() *automock.OAuth20Service { 187 svc := &automock.OAuth20Service{} 188 return svc 189 }, 190 OneTimeTokenFn: unusedOneTimeTokenSvc, 191 ConverterFn: func() *automock.SystemAuthConverter { 192 conv := &automock.SystemAuthConverter{} 193 return conv 194 }, 195 AuthConverterFn: func() *automock.AuthConverter { 196 return &automock.AuthConverter{} 197 }, 198 ExpectedSystemAuth: nil, 199 ExpectedErr: testErr, 200 }, 201 { 202 Name: "Error - Transaction Commit", 203 TransactionerFn: txGen.ThatFailsOnCommit, 204 ServiceFn: func() *automock.SystemAuthService { 205 svc := &automock.SystemAuthService{} 206 svc.On("GetByIDForObject", contextParam, objectType, id).Return(oauthModelSystemAuth, nil).Once() 207 svc.On("DeleteByIDForObject", contextParam, objectType, id).Return(nil).Once() 208 return svc 209 }, 210 OAuthServiceFn: func() *automock.OAuth20Service { 211 svc := &automock.OAuth20Service{} 212 svc.On("DeleteClientCredentials", contextParam, oauthModelSystemAuth.Value.Credential.Oauth.ClientID).Return(nil) 213 return svc 214 }, 215 OneTimeTokenFn: unusedOneTimeTokenSvc, 216 ConverterFn: func() *automock.SystemAuthConverter { 217 conv := &automock.SystemAuthConverter{} 218 conv.On("ToGraphQL", oauthModelSystemAuth).Return(gqlSystemAuth, nil).Once() 219 return conv 220 }, 221 AuthConverterFn: func() *automock.AuthConverter { 222 return &automock.AuthConverter{} 223 }, 224 ExpectedErr: testErr, 225 }, 226 } 227 228 for _, testCase := range testCases { 229 t.Run(testCase.Name, func(t *testing.T) { 230 persist, transact := testCase.TransactionerFn() 231 defer persist.AssertExpectations(t) 232 defer transact.AssertExpectations(t) 233 svc := testCase.ServiceFn() 234 defer svc.AssertExpectations(t) 235 oauthSvc := testCase.OAuthServiceFn() 236 defer oauthSvc.AssertExpectations(t) 237 converter := testCase.ConverterFn() 238 defer converter.AssertExpectations(t) 239 authConverter := testCase.AuthConverterFn() 240 defer authConverter.AssertExpectations(t) 241 ottSvc := testCase.OneTimeTokenFn() 242 defer ottSvc.AssertExpectations(t) 243 244 resolver := systemauth.NewResolver(transact, svc, oauthSvc, ottSvc, converter, authConverter) 245 246 // WHEN 247 fn := resolver.GenericDeleteSystemAuth(objectType) 248 result, err := fn(context.TODO(), id) 249 250 // then 251 assert.Equal(t, testCase.ExpectedSystemAuth, result) 252 if testCase.ExpectedErr != nil { 253 require.Error(t, err) 254 assert.Equal(t, testCase.ExpectedErr.Error(), err.Error()) 255 } 256 }) 257 } 258 } 259 260 func TestResolver_SystemAuth(t *testing.T) { 261 // GIVEN 262 testErr := errors.New("Test error") 263 txGen := txtest.NewTransactionContextGenerator(testErr) 264 265 id := "foo" 266 objectID := "bar" 267 objectType := pkgmodel.RuntimeReference 268 modelSystemAuth := fixModelSystemAuth(id, objectType, objectID, fixModelAuth()) 269 gqlSystemAuth := fixGQLIntSysSystemAuth(id, fixGQLAuth(), objectID) 270 271 testCases := []struct { 272 Name string 273 TransactionerFn func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) 274 ServiceFn func() *automock.SystemAuthService 275 OAuthServiceFn func() *automock.OAuth20Service 276 OneTimeTokenFn func() *automock.OneTimeTokenService 277 ConverterFn func() *automock.SystemAuthConverter 278 AuthConverterFn func() *automock.AuthConverter 279 InputID string 280 ExpectedSystemAuth graphql.SystemAuth 281 ExpectedErr error 282 }{ 283 { 284 Name: "Success", 285 TransactionerFn: txGen.ThatSucceeds, 286 ServiceFn: func() *automock.SystemAuthService { 287 svc := &automock.SystemAuthService{} 288 svc.On("GetGlobal", contextParam, id).Return(modelSystemAuth, nil).Once() 289 return svc 290 }, 291 OAuthServiceFn: func() *automock.OAuth20Service { 292 svc := &automock.OAuth20Service{} 293 return svc 294 }, 295 OneTimeTokenFn: unusedOneTimeTokenSvc, 296 ConverterFn: func() *automock.SystemAuthConverter { 297 conv := &automock.SystemAuthConverter{} 298 conv.On("ToGraphQL", modelSystemAuth).Return(gqlSystemAuth, nil).Once() 299 return conv 300 }, 301 AuthConverterFn: func() *automock.AuthConverter { 302 return &automock.AuthConverter{} 303 }, 304 InputID: id, 305 ExpectedSystemAuth: gqlSystemAuth, 306 ExpectedErr: nil, 307 }, 308 { 309 Name: "Error - Transaction Begin", 310 TransactionerFn: txGen.ThatFailsOnBegin, 311 ServiceFn: func() *automock.SystemAuthService { 312 svc := &automock.SystemAuthService{} 313 svc.AssertNotCalled(t, "GetGlobal") 314 return svc 315 }, 316 OAuthServiceFn: func() *automock.OAuth20Service { 317 svc := &automock.OAuth20Service{} 318 return svc 319 }, 320 OneTimeTokenFn: unusedOneTimeTokenSvc, 321 ConverterFn: func() *automock.SystemAuthConverter { 322 conv := &automock.SystemAuthConverter{} 323 conv.AssertNotCalled(t, "ToGraphQL") 324 return conv 325 }, 326 AuthConverterFn: func() *automock.AuthConverter { 327 return &automock.AuthConverter{} 328 }, 329 InputID: id, 330 ExpectedSystemAuth: nil, 331 ExpectedErr: testErr, 332 }, 333 { 334 Name: "Error - Transaction Commit", 335 TransactionerFn: txGen.ThatFailsOnCommit, 336 ServiceFn: func() *automock.SystemAuthService { 337 svc := &automock.SystemAuthService{} 338 svc.On("GetGlobal", contextParam, id).Return(modelSystemAuth, nil).Once() 339 return svc 340 }, 341 OAuthServiceFn: func() *automock.OAuth20Service { 342 svc := &automock.OAuth20Service{} 343 return svc 344 }, 345 OneTimeTokenFn: unusedOneTimeTokenSvc, 346 ConverterFn: func() *automock.SystemAuthConverter { 347 conv := &automock.SystemAuthConverter{} 348 conv.AssertNotCalled(t, "ToGraphQL") 349 return conv 350 }, 351 AuthConverterFn: func() *automock.AuthConverter { 352 return &automock.AuthConverter{} 353 }, 354 InputID: id, 355 ExpectedErr: testErr, 356 }, 357 { 358 Name: "Error - GetGlobal", 359 TransactionerFn: txGen.ThatDoesntExpectCommit, 360 ServiceFn: func() *automock.SystemAuthService { 361 svc := &automock.SystemAuthService{} 362 svc.On("GetGlobal", contextParam, id).Return(nil, testErr).Once() 363 return svc 364 }, 365 OAuthServiceFn: func() *automock.OAuth20Service { 366 svc := &automock.OAuth20Service{} 367 return svc 368 }, 369 OneTimeTokenFn: unusedOneTimeTokenSvc, 370 ConverterFn: func() *automock.SystemAuthConverter { 371 conv := &automock.SystemAuthConverter{} 372 conv.AssertNotCalled(t, "ToGraphQL") 373 return conv 374 }, 375 AuthConverterFn: func() *automock.AuthConverter { 376 return &automock.AuthConverter{} 377 }, 378 InputID: id, 379 ExpectedErr: testErr, 380 }, 381 { 382 Name: "Error - ToGraphQL", 383 TransactionerFn: txGen.ThatSucceeds, 384 ServiceFn: func() *automock.SystemAuthService { 385 svc := &automock.SystemAuthService{} 386 svc.On("GetGlobal", contextParam, id).Return(modelSystemAuth, nil).Once() 387 return svc 388 }, 389 OAuthServiceFn: func() *automock.OAuth20Service { 390 svc := &automock.OAuth20Service{} 391 return svc 392 }, 393 OneTimeTokenFn: unusedOneTimeTokenSvc, 394 ConverterFn: func() *automock.SystemAuthConverter { 395 conv := &automock.SystemAuthConverter{} 396 conv.On("ToGraphQL", modelSystemAuth).Return(nil, testErr).Once() 397 return conv 398 }, 399 AuthConverterFn: func() *automock.AuthConverter { 400 return &automock.AuthConverter{} 401 }, 402 InputID: id, 403 ExpectedErr: testErr, 404 }, 405 } 406 407 for _, testCase := range testCases { 408 t.Run(testCase.Name, func(t *testing.T) { 409 persist, transact := testCase.TransactionerFn() 410 defer persist.AssertExpectations(t) 411 defer transact.AssertExpectations(t) 412 svc := testCase.ServiceFn() 413 defer svc.AssertExpectations(t) 414 oauthSvc := testCase.OAuthServiceFn() 415 defer oauthSvc.AssertExpectations(t) 416 converter := testCase.ConverterFn() 417 defer converter.AssertExpectations(t) 418 authConverter := testCase.AuthConverterFn() 419 defer authConverter.AssertExpectations(t) 420 ottSvc := testCase.OneTimeTokenFn() 421 defer ottSvc.AssertExpectations(t) 422 423 resolver := systemauth.NewResolver(transact, svc, oauthSvc, ottSvc, converter, authConverter) 424 425 // WHEN 426 result, err := resolver.SystemAuth(context.TODO(), testCase.InputID) 427 428 // then 429 assert.Equal(t, testCase.ExpectedSystemAuth, result) 430 if testCase.ExpectedErr != nil { 431 require.Error(t, err) 432 assert.Equal(t, testCase.ExpectedErr.Error(), err.Error()) 433 } 434 }) 435 } 436 } 437 438 func TestResolver_SystemAuthByToken(t *testing.T) { 439 // GIVEN 440 testErr := errors.New("Test error") 441 txGen := txtest.NewTransactionContextGenerator(testErr) 442 443 id := "foo" 444 token := "token" 445 objectID := "bar" 446 objectType := pkgmodel.RuntimeReference 447 modelSystemAuth := fixModelSystemAuth(id, objectType, objectID, fixModelAuth()) 448 gqlSystemAuth := fixGQLIntSysSystemAuth(id, fixGQLAuth(), objectID) 449 450 testCases := []struct { 451 Name string 452 TransactionerFn func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) 453 ServiceFn func() *automock.SystemAuthService 454 OAuthServiceFn func() *automock.OAuth20Service 455 OneTimeTokenFn func() *automock.OneTimeTokenService 456 ConverterFn func() *automock.SystemAuthConverter 457 AuthConverterFn func() *automock.AuthConverter 458 Token string 459 ExpectedSystemAuth graphql.SystemAuth 460 ExpectedErr error 461 }{ 462 { 463 Name: "Success", 464 TransactionerFn: txGen.ThatSucceeds, 465 ServiceFn: func() *automock.SystemAuthService { 466 svc := &automock.SystemAuthService{} 467 svc.On("GetByToken", contextParam, token).Return(modelSystemAuth, nil).Once() 468 return svc 469 }, 470 OAuthServiceFn: func() *automock.OAuth20Service { 471 svc := &automock.OAuth20Service{} 472 return svc 473 }, 474 OneTimeTokenFn: func() *automock.OneTimeTokenService { 475 svc := &automock.OneTimeTokenService{} 476 svc.On("IsTokenValid", modelSystemAuth).Return(true, nil).Once() 477 return svc 478 }, 479 ConverterFn: func() *automock.SystemAuthConverter { 480 conv := &automock.SystemAuthConverter{} 481 conv.On("ToGraphQL", modelSystemAuth).Return(gqlSystemAuth, nil).Once() 482 return conv 483 }, 484 AuthConverterFn: func() *automock.AuthConverter { 485 return &automock.AuthConverter{} 486 }, 487 Token: token, 488 ExpectedSystemAuth: gqlSystemAuth, 489 ExpectedErr: nil, 490 }, 491 { 492 Name: "Error - Transaction Begin", 493 TransactionerFn: txGen.ThatFailsOnBegin, 494 ServiceFn: func() *automock.SystemAuthService { 495 svc := &automock.SystemAuthService{} 496 svc.AssertNotCalled(t, "GetByToken") 497 return svc 498 }, 499 OAuthServiceFn: func() *automock.OAuth20Service { 500 svc := &automock.OAuth20Service{} 501 return svc 502 }, 503 OneTimeTokenFn: func() *automock.OneTimeTokenService { 504 svc := &automock.OneTimeTokenService{} 505 svc.AssertNotCalled(t, "IsTokenValid") 506 return svc 507 }, 508 ConverterFn: func() *automock.SystemAuthConverter { 509 conv := &automock.SystemAuthConverter{} 510 conv.AssertNotCalled(t, "ToGraphQL") 511 return conv 512 }, 513 AuthConverterFn: func() *automock.AuthConverter { 514 return &automock.AuthConverter{} 515 }, 516 Token: token, 517 ExpectedSystemAuth: nil, 518 ExpectedErr: testErr, 519 }, 520 { 521 Name: "Error - Transaction Commit", 522 TransactionerFn: txGen.ThatFailsOnCommit, 523 ServiceFn: func() *automock.SystemAuthService { 524 svc := &automock.SystemAuthService{} 525 svc.On("GetByToken", contextParam, token).Return(modelSystemAuth, nil).Once() 526 return svc 527 }, 528 OAuthServiceFn: func() *automock.OAuth20Service { 529 svc := &automock.OAuth20Service{} 530 return svc 531 }, 532 OneTimeTokenFn: func() *automock.OneTimeTokenService { 533 svc := &automock.OneTimeTokenService{} 534 svc.AssertNotCalled(t, "IsTokenValid") 535 return svc 536 }, 537 ConverterFn: func() *automock.SystemAuthConverter { 538 conv := &automock.SystemAuthConverter{} 539 conv.AssertNotCalled(t, "ToGraphQL") 540 return conv 541 }, 542 AuthConverterFn: func() *automock.AuthConverter { 543 return &automock.AuthConverter{} 544 }, 545 Token: token, 546 ExpectedErr: testErr, 547 }, 548 { 549 Name: "Error - GetByToken", 550 TransactionerFn: txGen.ThatDoesntExpectCommit, 551 ServiceFn: func() *automock.SystemAuthService { 552 svc := &automock.SystemAuthService{} 553 svc.On("GetByToken", contextParam, token).Return(nil, testErr).Once() 554 return svc 555 }, 556 OAuthServiceFn: func() *automock.OAuth20Service { 557 svc := &automock.OAuth20Service{} 558 return svc 559 }, 560 OneTimeTokenFn: func() *automock.OneTimeTokenService { 561 svc := &automock.OneTimeTokenService{} 562 svc.AssertNotCalled(t, "IsTokenValid") 563 return svc 564 }, 565 ConverterFn: func() *automock.SystemAuthConverter { 566 conv := &automock.SystemAuthConverter{} 567 conv.AssertNotCalled(t, "ToGraphQL") 568 return conv 569 }, 570 AuthConverterFn: func() *automock.AuthConverter { 571 return &automock.AuthConverter{} 572 }, 573 Token: token, 574 ExpectedErr: testErr, 575 }, 576 { 577 Name: "Error - IsTokenValid", 578 TransactionerFn: txGen.ThatSucceeds, 579 ServiceFn: func() *automock.SystemAuthService { 580 svc := &automock.SystemAuthService{} 581 svc.On("GetByToken", contextParam, token).Return(modelSystemAuth, nil).Once() 582 return svc 583 }, 584 OAuthServiceFn: func() *automock.OAuth20Service { 585 return &automock.OAuth20Service{} 586 }, 587 OneTimeTokenFn: func() *automock.OneTimeTokenService { 588 svc := &automock.OneTimeTokenService{} 589 svc.On("IsTokenValid", modelSystemAuth).Return(false, testErr).Once() 590 return svc 591 }, 592 ConverterFn: func() *automock.SystemAuthConverter { 593 conv := &automock.SystemAuthConverter{} 594 conv.AssertNotCalled(t, "ToGraphQL") 595 //conv.On("ToGraphQL", modelSystemAuth).Return(nil, testErr).Once() 596 return conv 597 }, 598 AuthConverterFn: func() *automock.AuthConverter { 599 return &automock.AuthConverter{} 600 }, 601 Token: token, 602 ExpectedErr: testErr, 603 }, 604 { 605 Name: "Error - ToGraphQL", 606 TransactionerFn: txGen.ThatSucceeds, 607 ServiceFn: func() *automock.SystemAuthService { 608 svc := &automock.SystemAuthService{} 609 svc.On("GetByToken", contextParam, token).Return(modelSystemAuth, nil).Once() 610 return svc 611 }, 612 OAuthServiceFn: func() *automock.OAuth20Service { 613 return &automock.OAuth20Service{} 614 }, 615 OneTimeTokenFn: func() *automock.OneTimeTokenService { 616 svc := &automock.OneTimeTokenService{} 617 svc.On("IsTokenValid", modelSystemAuth).Return(true, nil).Once() 618 return svc 619 }, 620 ConverterFn: func() *automock.SystemAuthConverter { 621 conv := &automock.SystemAuthConverter{} 622 conv.On("ToGraphQL", modelSystemAuth).Return(nil, testErr).Once() 623 return conv 624 }, 625 AuthConverterFn: func() *automock.AuthConverter { 626 return &automock.AuthConverter{} 627 }, 628 Token: token, 629 ExpectedErr: testErr, 630 }, 631 } 632 633 for _, testCase := range testCases { 634 t.Run(testCase.Name, func(t *testing.T) { 635 persist, transact := testCase.TransactionerFn() 636 defer persist.AssertExpectations(t) 637 defer transact.AssertExpectations(t) 638 svc := testCase.ServiceFn() 639 defer svc.AssertExpectations(t) 640 oauthSvc := testCase.OAuthServiceFn() 641 defer oauthSvc.AssertExpectations(t) 642 converter := testCase.ConverterFn() 643 defer converter.AssertExpectations(t) 644 authConverter := testCase.AuthConverterFn() 645 defer authConverter.AssertExpectations(t) 646 ottSvc := testCase.OneTimeTokenFn() 647 defer ottSvc.AssertExpectations(t) 648 649 resolver := systemauth.NewResolver(transact, svc, oauthSvc, ottSvc, converter, authConverter) 650 651 // WHEN 652 result, err := resolver.SystemAuthByToken(context.TODO(), testCase.Token) 653 654 // then 655 assert.Equal(t, testCase.ExpectedSystemAuth, result) 656 if testCase.ExpectedErr != nil { 657 require.Error(t, err) 658 assert.Equal(t, testCase.ExpectedErr.Error(), err.Error()) 659 } 660 }) 661 } 662 } 663 664 func TestResolver_UpdateSystemAuth(t *testing.T) { 665 // GIVEN 666 testErr := errors.New("Test error") 667 txGen := txtest.NewTransactionContextGenerator(testErr) 668 669 id := "foo" 670 objectID := "bar" 671 objectType := pkgmodel.RuntimeReference 672 modelSystemAuth := fixModelSystemAuth(id, objectType, objectID, fixModelAuth()) 673 gqlSystemAuth := fixGQLIntSysSystemAuth(id, fixGQLAuth(), objectID) 674 inputAuth := fixGQLAuthInput() 675 modelAuth := fixModelAuth() 676 677 testCases := []struct { 678 Name string 679 TransactionerFn func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) 680 ServiceFn func() *automock.SystemAuthService 681 OAuthServiceFn func() *automock.OAuth20Service 682 OneTimeTokenFn func() *automock.OneTimeTokenService 683 ConverterFn func() *automock.SystemAuthConverter 684 AuthConverterFn func() *automock.AuthConverter 685 ID string 686 InputAuth graphql.AuthInput 687 ExpectedSystemAuth graphql.SystemAuth 688 ExpectedErr error 689 }{ 690 { 691 Name: "Success", 692 TransactionerFn: txGen.ThatSucceeds, 693 ServiceFn: func() *automock.SystemAuthService { 694 svc := &automock.SystemAuthService{} 695 svc.On("UpdateValue", contextParam, id, modelAuth).Return(modelSystemAuth, nil).Once() 696 return svc 697 }, 698 OAuthServiceFn: func() *automock.OAuth20Service { 699 svc := &automock.OAuth20Service{} 700 return svc 701 }, 702 OneTimeTokenFn: func() *automock.OneTimeTokenService { 703 svc := &automock.OneTimeTokenService{} 704 return svc 705 }, 706 ConverterFn: func() *automock.SystemAuthConverter { 707 conv := &automock.SystemAuthConverter{} 708 conv.On("ToGraphQL", modelSystemAuth).Return(gqlSystemAuth, nil).Once() 709 return conv 710 }, 711 AuthConverterFn: func() *automock.AuthConverter { 712 conv := &automock.AuthConverter{} 713 conv.On("ModelFromGraphQLInput", *inputAuth).Return(modelAuth, nil).Once() 714 return conv 715 }, 716 ID: id, 717 InputAuth: *inputAuth, 718 ExpectedSystemAuth: gqlSystemAuth, 719 ExpectedErr: nil, 720 }, 721 { 722 Name: "Error - Transaction Begin", 723 TransactionerFn: txGen.ThatFailsOnBegin, 724 ServiceFn: func() *automock.SystemAuthService { 725 svc := &automock.SystemAuthService{} 726 svc.AssertNotCalled(t, "UpdateValue") 727 return svc 728 }, 729 OAuthServiceFn: func() *automock.OAuth20Service { 730 svc := &automock.OAuth20Service{} 731 return svc 732 }, 733 OneTimeTokenFn: func() *automock.OneTimeTokenService { 734 svc := &automock.OneTimeTokenService{} 735 return svc 736 }, 737 ConverterFn: func() *automock.SystemAuthConverter { 738 conv := &automock.SystemAuthConverter{} 739 conv.AssertNotCalled(t, "ToGraphQL") 740 return conv 741 }, 742 AuthConverterFn: func() *automock.AuthConverter { 743 conv := &automock.AuthConverter{} 744 conv.AssertNotCalled(t, "ModelFromGraphQLInput") 745 return conv 746 }, 747 ID: id, 748 InputAuth: *inputAuth, 749 ExpectedSystemAuth: nil, 750 ExpectedErr: testErr, 751 }, 752 { 753 Name: "Error - Transaction Commit", 754 TransactionerFn: txGen.ThatFailsOnCommit, 755 ServiceFn: func() *automock.SystemAuthService { 756 svc := &automock.SystemAuthService{} 757 svc.On("UpdateValue", contextParam, id, modelAuth).Return(modelSystemAuth, nil).Once() 758 return svc 759 }, 760 OAuthServiceFn: func() *automock.OAuth20Service { 761 svc := &automock.OAuth20Service{} 762 return svc 763 }, 764 OneTimeTokenFn: func() *automock.OneTimeTokenService { 765 svc := &automock.OneTimeTokenService{} 766 return svc 767 }, 768 ConverterFn: func() *automock.SystemAuthConverter { 769 conv := &automock.SystemAuthConverter{} 770 conv.AssertNotCalled(t, "ToGraphQL") 771 return conv 772 }, 773 AuthConverterFn: func() *automock.AuthConverter { 774 conv := &automock.AuthConverter{} 775 conv.On("ModelFromGraphQLInput", *inputAuth).Return(modelAuth, nil).Once() 776 return conv 777 }, 778 ID: id, 779 InputAuth: *inputAuth, 780 ExpectedErr: testErr, 781 }, 782 { 783 Name: "Error - ModelFromGraphQLInput", 784 TransactionerFn: txGen.ThatDoesntExpectCommit, 785 ServiceFn: func() *automock.SystemAuthService { 786 svc := &automock.SystemAuthService{} 787 svc.AssertNotCalled(t, "UpdateValue") 788 return svc 789 }, 790 OAuthServiceFn: func() *automock.OAuth20Service { 791 svc := &automock.OAuth20Service{} 792 return svc 793 }, 794 OneTimeTokenFn: func() *automock.OneTimeTokenService { 795 svc := &automock.OneTimeTokenService{} 796 return svc 797 }, 798 ConverterFn: func() *automock.SystemAuthConverter { 799 conv := &automock.SystemAuthConverter{} 800 conv.AssertNotCalled(t, "ToGraphQL") 801 return conv 802 }, 803 AuthConverterFn: func() *automock.AuthConverter { 804 conv := &automock.AuthConverter{} 805 conv.On("ModelFromGraphQLInput", *inputAuth).Return(nil, testErr).Once() 806 return conv 807 }, 808 ID: id, 809 InputAuth: *inputAuth, 810 ExpectedErr: testErr, 811 }, 812 { 813 Name: "Error - UpdateValue", 814 TransactionerFn: txGen.ThatDoesntExpectCommit, 815 ServiceFn: func() *automock.SystemAuthService { 816 svc := &automock.SystemAuthService{} 817 svc.On("UpdateValue", contextParam, id, modelAuth).Return(nil, testErr).Once() 818 return svc 819 }, 820 OAuthServiceFn: func() *automock.OAuth20Service { 821 return &automock.OAuth20Service{} 822 }, 823 OneTimeTokenFn: func() *automock.OneTimeTokenService { 824 svc := &automock.OneTimeTokenService{} 825 return svc 826 }, 827 ConverterFn: func() *automock.SystemAuthConverter { 828 conv := &automock.SystemAuthConverter{} 829 conv.AssertNotCalled(t, "ToGraphQL") 830 return conv 831 }, 832 AuthConverterFn: func() *automock.AuthConverter { 833 conv := &automock.AuthConverter{} 834 conv.On("ModelFromGraphQLInput", *inputAuth).Return(modelAuth, nil).Once() 835 return conv 836 }, 837 ID: id, 838 InputAuth: *inputAuth, 839 ExpectedErr: testErr, 840 }, 841 { 842 Name: "Error - ToGraphQL", 843 TransactionerFn: txGen.ThatSucceeds, 844 ServiceFn: func() *automock.SystemAuthService { 845 svc := &automock.SystemAuthService{} 846 svc.On("UpdateValue", contextParam, id, modelAuth).Return(modelSystemAuth, nil).Once() 847 return svc 848 }, 849 OAuthServiceFn: func() *automock.OAuth20Service { 850 return &automock.OAuth20Service{} 851 }, 852 OneTimeTokenFn: func() *automock.OneTimeTokenService { 853 svc := &automock.OneTimeTokenService{} 854 return svc 855 }, 856 ConverterFn: func() *automock.SystemAuthConverter { 857 conv := &automock.SystemAuthConverter{} 858 conv.On("ToGraphQL", modelSystemAuth).Return(nil, testErr).Once() 859 return conv 860 }, 861 AuthConverterFn: func() *automock.AuthConverter { 862 conv := &automock.AuthConverter{} 863 conv.On("ModelFromGraphQLInput", *inputAuth).Return(modelAuth, nil).Once() 864 return conv 865 }, 866 ID: id, 867 InputAuth: *inputAuth, 868 ExpectedErr: testErr, 869 }, 870 } 871 872 for _, testCase := range testCases { 873 t.Run(testCase.Name, func(t *testing.T) { 874 persist, transact := testCase.TransactionerFn() 875 defer persist.AssertExpectations(t) 876 defer transact.AssertExpectations(t) 877 svc := testCase.ServiceFn() 878 defer svc.AssertExpectations(t) 879 oauthSvc := testCase.OAuthServiceFn() 880 defer oauthSvc.AssertExpectations(t) 881 converter := testCase.ConverterFn() 882 defer converter.AssertExpectations(t) 883 authConverter := testCase.AuthConverterFn() 884 defer authConverter.AssertExpectations(t) 885 ottSvc := testCase.OneTimeTokenFn() 886 defer ottSvc.AssertExpectations(t) 887 888 resolver := systemauth.NewResolver(transact, svc, oauthSvc, ottSvc, converter, authConverter) 889 890 // WHEN 891 result, err := resolver.UpdateSystemAuth(context.TODO(), testCase.ID, testCase.InputAuth) 892 893 // then 894 assert.Equal(t, testCase.ExpectedSystemAuth, result) 895 if testCase.ExpectedErr != nil { 896 require.Error(t, err) 897 assert.Equal(t, testCase.ExpectedErr.Error(), err.Error()) 898 } 899 }) 900 } 901 } 902 903 func TestResolver_InvalidateSystemAuthOneTimeToken(t *testing.T) { 904 // GIVEN 905 testErr := errors.New("Test error") 906 txGen := txtest.NewTransactionContextGenerator(testErr) 907 908 id := "foo" 909 objectID := "bar" 910 objectType := pkgmodel.RuntimeReference 911 modelSystemAuth := fixModelSystemAuth(id, objectType, objectID, fixModelAuth()) 912 gqlSystemAuth := fixGQLIntSysSystemAuth(id, fixGQLAuth(), objectID) 913 914 testCases := []struct { 915 Name string 916 TransactionerFn func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) 917 ServiceFn func() *automock.SystemAuthService 918 OAuthServiceFn func() *automock.OAuth20Service 919 OneTimeTokenFn func() *automock.OneTimeTokenService 920 ConverterFn func() *automock.SystemAuthConverter 921 AuthConverterFn func() *automock.AuthConverter 922 ID string 923 InputAuth graphql.AuthInput 924 ExpectedSystemAuth graphql.SystemAuth 925 ExpectedErr error 926 }{ 927 { 928 Name: "Success", 929 TransactionerFn: txGen.ThatSucceeds, 930 ServiceFn: func() *automock.SystemAuthService { 931 svc := &automock.SystemAuthService{} 932 svc.On("GetGlobal", contextParam, id).Return(modelSystemAuth, nil).Once() 933 svc.On("InvalidateToken", contextParam, id).Return(modelSystemAuth, nil).Once() 934 return svc 935 }, 936 OAuthServiceFn: func() *automock.OAuth20Service { 937 svc := &automock.OAuth20Service{} 938 return svc 939 }, 940 OneTimeTokenFn: func() *automock.OneTimeTokenService { 941 svc := &automock.OneTimeTokenService{} 942 svc.On("IsTokenValid", modelSystemAuth).Return(true, nil).Once() 943 return svc 944 }, 945 ConverterFn: func() *automock.SystemAuthConverter { 946 conv := &automock.SystemAuthConverter{} 947 conv.On("ToGraphQL", modelSystemAuth).Return(gqlSystemAuth, nil).Once() 948 return conv 949 }, 950 AuthConverterFn: func() *automock.AuthConverter { 951 conv := &automock.AuthConverter{} 952 return conv 953 }, 954 ID: id, 955 ExpectedSystemAuth: gqlSystemAuth, 956 ExpectedErr: nil, 957 }, 958 { 959 Name: "Error - Transaction Begin", 960 TransactionerFn: txGen.ThatFailsOnBegin, 961 ServiceFn: func() *automock.SystemAuthService { 962 svc := &automock.SystemAuthService{} 963 svc.AssertNotCalled(t, "GetGlobal") 964 svc.AssertNotCalled(t, "InvalidateToken") 965 return svc 966 }, 967 OAuthServiceFn: func() *automock.OAuth20Service { 968 svc := &automock.OAuth20Service{} 969 return svc 970 }, 971 OneTimeTokenFn: func() *automock.OneTimeTokenService { 972 svc := &automock.OneTimeTokenService{} 973 svc.AssertNotCalled(t, "IsTokenValid") 974 return svc 975 }, 976 ConverterFn: func() *automock.SystemAuthConverter { 977 conv := &automock.SystemAuthConverter{} 978 conv.AssertNotCalled(t, "ToGraphQL") 979 return conv 980 }, 981 AuthConverterFn: func() *automock.AuthConverter { 982 conv := &automock.AuthConverter{} 983 return conv 984 }, 985 ID: id, 986 ExpectedSystemAuth: nil, 987 ExpectedErr: testErr, 988 }, 989 { 990 Name: "Error - Transaction Commit", 991 TransactionerFn: txGen.ThatFailsOnCommit, 992 ServiceFn: func() *automock.SystemAuthService { 993 svc := &automock.SystemAuthService{} 994 svc.On("GetGlobal", contextParam, id).Return(modelSystemAuth, nil).Once() 995 svc.On("InvalidateToken", contextParam, id).Return(modelSystemAuth, nil).Once() 996 return svc 997 }, 998 OAuthServiceFn: func() *automock.OAuth20Service { 999 svc := &automock.OAuth20Service{} 1000 return svc 1001 }, 1002 OneTimeTokenFn: func() *automock.OneTimeTokenService { 1003 svc := &automock.OneTimeTokenService{} 1004 svc.On("IsTokenValid", modelSystemAuth).Return(true, nil).Once() 1005 return svc 1006 }, 1007 ConverterFn: func() *automock.SystemAuthConverter { 1008 conv := &automock.SystemAuthConverter{} 1009 conv.AssertNotCalled(t, "ToGraphQL") 1010 return conv 1011 }, 1012 AuthConverterFn: func() *automock.AuthConverter { 1013 conv := &automock.AuthConverter{} 1014 return conv 1015 }, 1016 ID: id, 1017 ExpectedErr: testErr, 1018 }, 1019 { 1020 Name: "Error - GetGlobal", 1021 TransactionerFn: txGen.ThatDoesntExpectCommit, 1022 ServiceFn: func() *automock.SystemAuthService { 1023 svc := &automock.SystemAuthService{} 1024 svc.On("GetGlobal", contextParam, id).Return(nil, testErr).Once() 1025 svc.AssertNotCalled(t, "InvalidateToken") 1026 return svc 1027 }, 1028 OAuthServiceFn: func() *automock.OAuth20Service { 1029 svc := &automock.OAuth20Service{} 1030 return svc 1031 }, 1032 OneTimeTokenFn: func() *automock.OneTimeTokenService { 1033 svc := &automock.OneTimeTokenService{} 1034 svc.AssertNotCalled(t, "IsTokenValid") 1035 return svc 1036 }, 1037 ConverterFn: func() *automock.SystemAuthConverter { 1038 conv := &automock.SystemAuthConverter{} 1039 conv.AssertNotCalled(t, "ToGraphQL") 1040 return conv 1041 }, 1042 AuthConverterFn: func() *automock.AuthConverter { 1043 conv := &automock.AuthConverter{} 1044 return conv 1045 }, 1046 ID: id, 1047 ExpectedErr: testErr, 1048 }, 1049 { 1050 Name: "Error - IsTokenValid", 1051 TransactionerFn: txGen.ThatDoesntExpectCommit, 1052 ServiceFn: func() *automock.SystemAuthService { 1053 svc := &automock.SystemAuthService{} 1054 svc.On("GetGlobal", contextParam, id).Return(modelSystemAuth, nil).Once() 1055 svc.AssertNotCalled(t, "InvalidateToken") 1056 return svc 1057 }, 1058 OAuthServiceFn: func() *automock.OAuth20Service { 1059 return &automock.OAuth20Service{} 1060 }, 1061 OneTimeTokenFn: func() *automock.OneTimeTokenService { 1062 svc := &automock.OneTimeTokenService{} 1063 svc.On("IsTokenValid", modelSystemAuth).Return(false, testErr).Once() 1064 return svc 1065 }, 1066 ConverterFn: func() *automock.SystemAuthConverter { 1067 conv := &automock.SystemAuthConverter{} 1068 conv.AssertNotCalled(t, "ToGraphQL") 1069 return conv 1070 }, 1071 AuthConverterFn: func() *automock.AuthConverter { 1072 conv := &automock.AuthConverter{} 1073 return conv 1074 }, 1075 ID: id, 1076 ExpectedErr: testErr, 1077 }, 1078 { 1079 Name: "Error - IsTokenValid", 1080 TransactionerFn: txGen.ThatDoesntExpectCommit, 1081 ServiceFn: func() *automock.SystemAuthService { 1082 svc := &automock.SystemAuthService{} 1083 svc.On("GetGlobal", contextParam, id).Return(modelSystemAuth, nil).Once() 1084 svc.On("InvalidateToken", contextParam, id).Return(nil, testErr) 1085 return svc 1086 }, 1087 OAuthServiceFn: func() *automock.OAuth20Service { 1088 return &automock.OAuth20Service{} 1089 }, 1090 OneTimeTokenFn: func() *automock.OneTimeTokenService { 1091 svc := &automock.OneTimeTokenService{} 1092 svc.On("IsTokenValid", modelSystemAuth).Return(true, nil).Once() 1093 return svc 1094 }, 1095 ConverterFn: func() *automock.SystemAuthConverter { 1096 conv := &automock.SystemAuthConverter{} 1097 conv.AssertNotCalled(t, "ToGraphQL") 1098 return conv 1099 }, 1100 AuthConverterFn: func() *automock.AuthConverter { 1101 conv := &automock.AuthConverter{} 1102 return conv 1103 }, 1104 ID: id, 1105 ExpectedErr: testErr, 1106 }, 1107 { 1108 Name: "Error - ToGraphQL", 1109 TransactionerFn: txGen.ThatSucceeds, 1110 ServiceFn: func() *automock.SystemAuthService { 1111 svc := &automock.SystemAuthService{} 1112 svc.On("GetGlobal", contextParam, id).Return(modelSystemAuth, nil).Once() 1113 svc.On("InvalidateToken", contextParam, id).Return(modelSystemAuth, nil) 1114 return svc 1115 }, 1116 OAuthServiceFn: func() *automock.OAuth20Service { 1117 return &automock.OAuth20Service{} 1118 }, 1119 OneTimeTokenFn: func() *automock.OneTimeTokenService { 1120 svc := &automock.OneTimeTokenService{} 1121 svc.On("IsTokenValid", modelSystemAuth).Return(true, nil) 1122 return svc 1123 }, 1124 ConverterFn: func() *automock.SystemAuthConverter { 1125 conv := &automock.SystemAuthConverter{} 1126 conv.On("ToGraphQL", modelSystemAuth).Return(nil, testErr).Once() 1127 return conv 1128 }, 1129 AuthConverterFn: func() *automock.AuthConverter { 1130 conv := &automock.AuthConverter{} 1131 return conv 1132 }, 1133 ID: id, 1134 ExpectedErr: testErr, 1135 }, 1136 } 1137 1138 for _, testCase := range testCases { 1139 t.Run(testCase.Name, func(t *testing.T) { 1140 persist, transact := testCase.TransactionerFn() 1141 defer persist.AssertExpectations(t) 1142 defer transact.AssertExpectations(t) 1143 svc := testCase.ServiceFn() 1144 defer svc.AssertExpectations(t) 1145 oauthSvc := testCase.OAuthServiceFn() 1146 defer oauthSvc.AssertExpectations(t) 1147 converter := testCase.ConverterFn() 1148 defer converter.AssertExpectations(t) 1149 authConverter := testCase.AuthConverterFn() 1150 defer authConverter.AssertExpectations(t) 1151 ottSvc := testCase.OneTimeTokenFn() 1152 defer ottSvc.AssertExpectations(t) 1153 1154 resolver := systemauth.NewResolver(transact, svc, oauthSvc, ottSvc, converter, authConverter) 1155 1156 // WHEN 1157 result, err := resolver.InvalidateSystemAuthOneTimeToken(context.TODO(), testCase.ID) 1158 1159 // then 1160 assert.Equal(t, testCase.ExpectedSystemAuth, result) 1161 if testCase.ExpectedErr != nil { 1162 require.Error(t, err) 1163 assert.Equal(t, testCase.ExpectedErr.Error(), err.Error()) 1164 } 1165 }) 1166 } 1167 } 1168 1169 func unusedOneTimeTokenSvc() *automock.OneTimeTokenService { 1170 return &automock.OneTimeTokenService{} 1171 }