diff --git a/chart/compass/values.yaml b/chart/compass/values.yaml index 4c03cd7163..2380550f93 100644 --- a/chart/compass/values.yaml +++ b/chart/compass/values.yaml @@ -22,7 +22,7 @@ global: version: "PR-1750" director: dir: - version: "PR-1762" + version: "PR-1765" gateway: dir: version: "PR-1750" diff --git a/components/director/internal/domain/onetimetoken/service.go b/components/director/internal/domain/onetimetoken/service.go index d9b7035bb6..9fd6fe891c 100644 --- a/components/director/internal/domain/onetimetoken/service.go +++ b/components/director/internal/domain/onetimetoken/service.go @@ -131,6 +131,10 @@ func (s *service) RegenerateOneTimeToken(ctx context.Context, sysAuthID string, if err != nil { return model.OneTimeToken{}, err } + if sysAuth.Value == nil { + sysAuth.Value = &model.Auth{} + } + tokenString, err := s.tokenGenerator.NewToken() if err != nil { return model.OneTimeToken{}, errors.Wrapf(err, "while generating onetime token") @@ -214,7 +218,3 @@ func (s *service) getTokenFromAdapter(ctx context.Context, adapterURL string, ap Token: externalToken, }, nil } - -func (s service) getOneTimeToken(ctx context.Context, id string, tokenType model.SystemAuthReferenceObjectType) (string, error) { - return s.tokenGenerator.NewToken() -} diff --git a/components/director/internal/domain/onetimetoken/service_test.go b/components/director/internal/domain/onetimetoken/service_test.go index 6a89b15c53..60789bd40a 100644 --- a/components/director/internal/domain/onetimetoken/service_test.go +++ b/components/director/internal/domain/onetimetoken/service_test.go @@ -549,85 +549,130 @@ func TestRegenerateOneTimeToken(t *testing.T) { ) t.Run("fails when systemAuth cannot be fetched", func(t *testing.T) { + // GIVEN sysAuthSvc := &automock.SystemAuthService{} - appSvc := &automock.ApplicationService{} - appConverter := &automock.ApplicationConverter{} - extTenantsSvc := &automock.ExternalTenantsService{} - doer := &automock.HTTPDoer{} tokenGenerator := &automock.TokenGenerator{} timeService := &timeMocks.Service{} intSystemToAdapterMapping := make(map[string]string) sysAuthSvc.On("GetGlobal", context.Background(), systemAuthID).Return(nil, errors.New("error while fetching")) - tokenService := onetimetoken.NewTokenService(sysAuthSvc, appSvc, appConverter, extTenantsSvc, doer, tokenGenerator, connectorURL, - intSystemToAdapterMapping, timeService) + defer sysAuthSvc.AssertExpectations(t) + + tokenService := onetimetoken.NewTokenService(sysAuthSvc, &automock.ApplicationService{}, &automock.ApplicationConverter{}, &automock.ExternalTenantsService{}, + &automock.HTTPDoer{}, tokenGenerator, connectorURL, intSystemToAdapterMapping, timeService) + // WHEN token, err := tokenService.RegenerateOneTimeToken(context.Background(), systemAuthID, tokens.ApplicationToken) + + // THEN assert.Equal(t, model.OneTimeToken{}, token) - assert.Error(t, err, "error while fetching") + assert.Error(t, err) + assert.Contains(t, err.Error(), "error while fetching") }) t.Run("fails when new token cannot be generated", func(t *testing.T) { + // GIVEN sysAuthSvc := &automock.SystemAuthService{} - appSvc := &automock.ApplicationService{} - appConverter := &automock.ApplicationConverter{} - extTenantsSvc := &automock.ExternalTenantsService{} - doer := &automock.HTTPDoer{} tokenGenerator := &automock.TokenGenerator{} timeService := &timeMocks.Service{} intSystemToAdapterMapping := make(map[string]string) sysAuthSvc.On("GetGlobal", context.Background(), systemAuthID).Return(&model.SystemAuth{Value: &model.Auth{}}, nil) + defer sysAuthSvc.AssertExpectations(t) tokenGenerator.On("NewToken").Return("", errors.New("error while token generating")) + defer tokenGenerator.AssertExpectations(t) - tokenService := onetimetoken.NewTokenService(sysAuthSvc, appSvc, appConverter, extTenantsSvc, doer, tokenGenerator, connectorURL, - intSystemToAdapterMapping, timeService) + tokenService := onetimetoken.NewTokenService(sysAuthSvc, &automock.ApplicationService{}, &automock.ApplicationConverter{}, &automock.ExternalTenantsService{}, + &automock.HTTPDoer{}, tokenGenerator, connectorURL, intSystemToAdapterMapping, timeService) + // WHEN token, err := tokenService.RegenerateOneTimeToken(context.Background(), systemAuthID, tokens.ApplicationToken) + // THEN assert.Equal(t, model.OneTimeToken{}, token) - assert.Error(t, err, "while generating onetime token error while token generating") + assert.Error(t, err) + assert.Contains(t, err.Error(), "while generating onetime token: error while token generating") }) - t.Run("succeeds when systemAuth cannot be updated", func(t *testing.T) { + t.Run("fails when systemAuth cannot be updated", func(t *testing.T) { + // GIVEN + updateErrMsg := "error while updating" sysAuthSvc := &automock.SystemAuthService{} - appSvc := &automock.ApplicationService{} - appConverter := &automock.ApplicationConverter{} - extTenantsSvc := &automock.ExternalTenantsService{} - doer := &automock.HTTPDoer{} tokenGenerator := &automock.TokenGenerator{} timeService := &timeMocks.Service{} intSystemToAdapterMapping := make(map[string]string) + timeService.On("Now").Return(time.Now()) sysAuthSvc.On("GetGlobal", context.Background(), systemAuthID).Return(&model.SystemAuth{Value: &model.Auth{}}, nil) - sysAuthSvc.On("Update", context.Background(), mock.Anything).Return(errors.New("error while updating")) + sysAuthSvc.On("Update", context.Background(), mock.Anything).Return(errors.New(updateErrMsg)) + defer sysAuthSvc.AssertExpectations(t) + tokenGenerator.On("NewToken").Return(token, nil) - timeService.On("Now").Return(time.Now()) - tokenService := onetimetoken.NewTokenService(sysAuthSvc, appSvc, appConverter, extTenantsSvc, doer, tokenGenerator, connectorURL, - intSystemToAdapterMapping, timeService) + defer tokenGenerator.AssertExpectations(t) + + tokenService := onetimetoken.NewTokenService(sysAuthSvc, &automock.ApplicationService{}, &automock.ApplicationConverter{}, &automock.ExternalTenantsService{}, + &automock.HTTPDoer{}, tokenGenerator, connectorURL, intSystemToAdapterMapping, timeService) + // WHEN token, err := tokenService.RegenerateOneTimeToken(context.Background(), systemAuthID, tokens.ApplicationToken) + // THEN assert.Equal(t, model.OneTimeToken{}, token) assert.Error(t, err) + assert.Contains(t, err.Error(), updateErrMsg) }) - t.Run("succeeds when no errors are thrown", func(t *testing.T) { + t.Run("succeeds when systemAuth has missing 'value' value", func(t *testing.T) { + // GIVEN sysAuthSvc := &automock.SystemAuthService{} - appSvc := &automock.ApplicationService{} - appConverter := &automock.ApplicationConverter{} - extTenantsSvc := &automock.ExternalTenantsService{} - doer := &automock.HTTPDoer{} tokenGenerator := &automock.TokenGenerator{} timeService := &timeMocks.Service{} intSystemToAdapterMapping := make(map[string]string) + now := time.Now() + timeService.On("Now").Return(now) - sysAuthSvc.On("GetGlobal", context.Background(), systemAuthID).Return(&model.SystemAuth{Value: &model.Auth{}}, nil) + sysAuthSvc.On("GetGlobal", context.Background(), systemAuthID).Return(&model.SystemAuth{}, nil) sysAuthSvc.On("Update", context.Background(), mock.Anything).Return(nil) + defer sysAuthSvc.AssertExpectations(t) + tokenGenerator.On("NewToken").Return(token, nil) + defer tokenGenerator.AssertExpectations(t) + + tokenService := onetimetoken.NewTokenService(sysAuthSvc, &automock.ApplicationService{}, &automock.ApplicationConverter{}, &automock.ExternalTenantsService{}, + &automock.HTTPDoer{}, tokenGenerator, connectorURL, intSystemToAdapterMapping, timeService) + expectedToken := model.OneTimeToken{ + Token: token, + ConnectorURL: connectorURL, + Type: tokens.ApplicationToken, + CreatedAt: now, + Used: false, + UsedAt: time.Time{}, + } + + // WHEN + token, err := tokenService.RegenerateOneTimeToken(context.Background(), systemAuthID, tokens.ApplicationToken) + + // THEN + assert.Equal(t, expectedToken, token) + assert.NoError(t, err) + }) + + t.Run("succeeds when no errors are thrown", func(t *testing.T) { + // GIVEN + sysAuthSvc := &automock.SystemAuthService{} + tokenGenerator := &automock.TokenGenerator{} + timeService := &timeMocks.Service{} + intSystemToAdapterMapping := make(map[string]string) now := time.Now() timeService.On("Now").Return(now) - expectedToken := &model.OneTimeToken{ + + sysAuthSvc.On("GetGlobal", context.Background(), systemAuthID).Return(&model.SystemAuth{Value: &model.Auth{}}, nil) + sysAuthSvc.On("Update", context.Background(), mock.Anything).Return(nil) + defer sysAuthSvc.AssertExpectations(t) + + tokenGenerator.On("NewToken").Return(token, nil) + defer tokenGenerator.AssertExpectations(t) + expectedToken := model.OneTimeToken{ Token: token, ConnectorURL: connectorURL, Type: tokens.ApplicationToken, @@ -635,12 +680,15 @@ func TestRegenerateOneTimeToken(t *testing.T) { Used: false, UsedAt: time.Time{}, } - tokenService := onetimetoken.NewTokenService(sysAuthSvc, appSvc, appConverter, extTenantsSvc, doer, tokenGenerator, connectorURL, - intSystemToAdapterMapping, timeService) + tokenService := onetimetoken.NewTokenService(sysAuthSvc, &automock.ApplicationService{}, &automock.ApplicationConverter{}, &automock.ExternalTenantsService{}, + &automock.HTTPDoer{}, tokenGenerator, connectorURL, intSystemToAdapterMapping, timeService) + + // WHEN token, err := tokenService.RegenerateOneTimeToken(context.Background(), systemAuthID, tokens.ApplicationToken) - assert.Equal(t, expectedToken, &token) - assert.Nil(t, err) + // THEN + assert.Equal(t, expectedToken, token) + assert.NoError(t, err) }) }