Skip to content
This repository has been archived by the owner on Jul 4, 2024. It is now read-only.

Commit

Permalink
[HOTFIX] Add check for nil values in OTT flow (#1765)
Browse files Browse the repository at this point in the history
Co-authored-by: Desislava Asenova <desislava.asenova@sap.com>
  • Loading branch information
DimitarPetrov and desislavaa committed Mar 15, 2021
1 parent 782ba84 commit cd3f148
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 39 deletions.
2 changes: 1 addition & 1 deletion chart/compass/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ global:
version: "PR-1750"
director:
dir:
version: "PR-1762"
version: "PR-1765"
gateway:
dir:
version: "PR-1750"
Expand Down
8 changes: 4 additions & 4 deletions components/director/internal/domain/onetimetoken/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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()
}
116 changes: 82 additions & 34 deletions components/director/internal/domain/onetimetoken/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,98 +549,146 @@ 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,
CreatedAt: now,
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)
})
}

0 comments on commit cd3f148

Please sign in to comment.