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(appeal): handle duplicate active grants (#120)
* fix(appeal): handle duplicate active grants * chore: refactor error handling * chore: remove unused type * fix(appeal): handle duplicate active grants * chore: remove unused error * feat(appeal): update repository test * chore: resolve comment
- Loading branch information
1 parent
57cd794
commit 4cdfc56
Showing
4 changed files
with
63 additions
and
6 deletions.
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
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 |
---|---|---|
|
@@ -18,10 +18,11 @@ import ( | |
|
||
type AppealRepositoryTestSuite struct { | ||
suite.Suite | ||
store *postgres.Store | ||
pool *dockertest.Pool | ||
resource *dockertest.Resource | ||
repository *postgres.AppealRepository | ||
store *postgres.Store | ||
pool *dockertest.Pool | ||
resource *dockertest.Resource | ||
repository *postgres.AppealRepository | ||
grantRepository *postgres.GrantRepository | ||
|
||
dummyProvider *domain.Provider | ||
dummyPolicy *domain.Policy | ||
|
@@ -40,6 +41,8 @@ func (s *AppealRepositoryTestSuite) SetupSuite() { | |
|
||
s.repository = postgres.NewAppealRepository(s.store.DB()) | ||
|
||
s.grantRepository = postgres.NewGrantRepository(s.store.DB()) | ||
|
||
s.dummyPolicy = &domain.Policy{ | ||
ID: "policy_test", | ||
Version: 1, | ||
|
@@ -455,6 +458,46 @@ func (s *AppealRepositoryTestSuite) TestUpdate() { | |
s.EqualError(actualError, "json: unsupported type: chan int") | ||
}) | ||
|
||
s.Run("should return error if grant already exists", func() { | ||
pendingAppeal := &domain.Appeal{ | ||
ID: uuid.NewString(), | ||
ResourceID: s.dummyResource.ID, | ||
PolicyID: s.dummyPolicy.ID, | ||
PolicyVersion: s.dummyPolicy.Version, | ||
AccountID: "[email protected]", | ||
AccountType: domain.DefaultAppealAccountType, | ||
Role: "role_test", | ||
Permissions: []string{"permission_test"}, | ||
CreatedBy: "[email protected]", | ||
Status: domain.AppealStatusPending, | ||
} | ||
dummyAppealError := s.repository.BulkUpsert(context.Background(), []*domain.Appeal{pendingAppeal}) | ||
s.Require().NoError(dummyAppealError) | ||
|
||
dummyGrants := &domain.Grant{ | ||
Status: domain.GrantStatusActive, | ||
AccountID: "[email protected]", | ||
ResourceID: s.dummyResource.ID, | ||
Permissions: []string{"permission_test"}, | ||
} | ||
|
||
dummyGrantError := s.grantRepository.BulkUpsert(context.Background(), []*domain.Grant{dummyGrants}) | ||
s.Require().NoError(dummyGrantError) | ||
|
||
appealApprovalErr := pendingAppeal.Approve() | ||
s.Require().NoError(appealApprovalErr) | ||
|
||
pendingAppeal.Grant = &domain.Grant{ //new duplicate grant | ||
Status: domain.GrantStatusActive, | ||
AccountID: "[email protected]", | ||
ResourceID: s.dummyResource.ID, | ||
Permissions: []string{"permission_test"}, | ||
} | ||
|
||
err := s.repository.Update(context.Background(), pendingAppeal) | ||
s.ErrorIs(err, domain.ErrDuplicateActiveGrant) | ||
}) | ||
|
||
s.Run("should return nil on success", func() { | ||
dummyAppeals := []*domain.Appeal{ | ||
{ | ||
|