forked from raystack/guardian
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: mark grant.IsPermanent as false when updating the exp date
- Loading branch information
Showing
2 changed files
with
99 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -892,3 +892,100 @@ func (s *ServiceTestSuite) TestImportFromProvider() { | |
} | ||
}) | ||
} | ||
|
||
func (s *ServiceTestSuite) TestDormancyCheck() { | ||
s.Run("should update grants and return nil error on success", func() { | ||
s.setup() | ||
|
||
timeNow := time.Now() | ||
dummyProvider := &domain.Provider{ | ||
ID: "test-provider-id", | ||
} | ||
dummyGrants := []domain.Grant{ | ||
{ | ||
ID: "g1", | ||
AccountID: "[email protected]", | ||
Permissions: []string{"role-1"}, | ||
IsPermanent: true, | ||
}, | ||
{ | ||
ID: "g2", | ||
AccountID: "[email protected]", | ||
Permissions: []string{"role-2"}, | ||
}, | ||
} | ||
dummyActivities := []*domain.Activity{} | ||
|
||
dormancyCheckCriteria := domain.DormancyCheckCriteria{ | ||
ProviderID: dummyProvider.ID, | ||
Period: 30 * 24 * time.Hour, // 30 days back | ||
RetainDuration: 7 * 24 * time.Hour, // update grant exp date to 7 days from now | ||
} | ||
|
||
newExpDate := timeNow.Add(dormancyCheckCriteria.RetainDuration) | ||
expectedReason := fmt.Sprintf("%s: %s", domain.GrantExpirationReasonDormant, dormancyCheckCriteria.RetainDuration) | ||
expectedUpdatedGrants := []*domain.Grant{ | ||
{ | ||
ID: "g1", | ||
AccountID: "[email protected]", | ||
Permissions: []string{"role-1"}, | ||
ExpirationDate: &newExpDate, | ||
IsPermanent: false, | ||
ExpirationDateReason: expectedReason, | ||
}, | ||
{ | ||
ID: "g2", | ||
AccountID: "[email protected]", | ||
Permissions: []string{"role-2"}, | ||
ExpirationDate: &newExpDate, | ||
ExpirationDateReason: expectedReason, | ||
}, | ||
} | ||
|
||
s.mockProviderService.EXPECT(). | ||
GetByID(mock.AnythingOfType("*context.emptyCtx"), dummyProvider.ID). | ||
Return(dummyProvider, nil).Once() | ||
expectedListGrantsFilter := domain.ListGrantsFilter{ | ||
Statuses: []string{string(domain.GrantStatusActive)}, | ||
ProviderTypes: []string{dummyProvider.Type}, | ||
ProviderURNs: []string{dummyProvider.URN}, | ||
CreatedAtLte: timeNow.Add(-dormancyCheckCriteria.Period), | ||
} | ||
s.mockRepository.EXPECT(). | ||
List(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("domain.ListGrantsFilter")). | ||
Run(func(_a0 context.Context, f domain.ListGrantsFilter) { | ||
s.Empty(cmp.Diff(expectedListGrantsFilter, f, cmpopts.EquateApproxTime(time.Second))) | ||
}). | ||
Return(dummyGrants, nil).Once() | ||
timestampGte := timeNow.Add(-dormancyCheckCriteria.Period) | ||
expectedListActivitiesFilter := domain.ListActivitiesFilter{ | ||
AccountIDs: []string{"[email protected]", "[email protected]"}, | ||
TimestampGte: ×tampGte, | ||
} | ||
s.mockProviderService.EXPECT(). | ||
ListActivities(mock.AnythingOfType("*context.emptyCtx"), *dummyProvider, mock.AnythingOfType("domain.ListActivitiesFilter")). | ||
Run(func(_a0 context.Context, _a1 domain.Provider, f domain.ListActivitiesFilter) { | ||
s.Empty(cmp.Diff(expectedListActivitiesFilter, f, | ||
cmpopts.EquateApproxTime(time.Second), | ||
cmpopts.IgnoreUnexported(domain.ListActivitiesFilter{}), | ||
)) | ||
}). | ||
Return(dummyActivities, nil).Once() | ||
s.mockProviderService.EXPECT(). | ||
CorrelateGrantActivities(mock.AnythingOfType("*context.emptyCtx"), *dummyProvider, mock.AnythingOfType("[]*domain.Grant"), dummyActivities). | ||
Return(nil).Once() | ||
s.mockRepository.EXPECT(). | ||
BulkUpsert(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("[]*domain.Grant")). | ||
Run(func(_a0 context.Context, grants []*domain.Grant) { | ||
s.Empty(cmp.Diff(expectedUpdatedGrants, grants, | ||
cmpopts.EquateApproxTime(time.Second), | ||
)) | ||
}). | ||
Return(nil).Once() | ||
|
||
s.mockNotifier.EXPECT().Notify(mock.Anything).Return(nil).Once() // TODO | ||
|
||
err := s.service.DormancyCheck(context.Background(), dormancyCheckCriteria) | ||
s.NoError(err) | ||
}) | ||
} |