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.
feat(bigquery): fetch labels for dataset and table resources (#9)
* feat(bigquery): fetch labels for dataset and table resources * refactor: rename var * fix: prevent user from updating Resource.Details["_metadata"] * test: extend test for FetchResources * refactor: simplify labels metadata assignment in dataset and table resource model * refactor(bigquery): use bqApi to fetch datasets and tables to reduce calls to bigquery * refactor(bigquery): fetch tables concurrently * test(bigquery): add unit tests for GetDatasets and GetTables methods in client * refactor: limit active goroutines when fetching datasets * refactor: rename "_metadata" to "__metadata" and store it in a const * refactor: declare var for an if block only
- Loading branch information
Showing
9 changed files
with
378 additions
and
78 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 |
---|---|---|
|
@@ -5,6 +5,8 @@ import ( | |
"errors" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
"github.com/goto/guardian/core/resource" | ||
"github.com/goto/guardian/core/resource/mocks" | ||
"github.com/goto/guardian/domain" | ||
|
@@ -115,11 +117,13 @@ func (s *ServiceTestSuite) TestUpdate() { | |
|
||
s.Run("should only allows details and labels to be edited", func() { | ||
testCases := []struct { | ||
name string | ||
resourceUpdatePayload *domain.Resource | ||
existingResource *domain.Resource | ||
expectedUpdatedValues *domain.Resource | ||
}{ | ||
{ | ||
name: "empty labels in existing resource", | ||
resourceUpdatePayload: &domain.Resource{ | ||
ID: "1", | ||
Labels: map[string]string{ | ||
|
@@ -137,6 +141,7 @@ func (s *ServiceTestSuite) TestUpdate() { | |
}, | ||
}, | ||
{ | ||
name: "empty details in existing resource", | ||
resourceUpdatePayload: &domain.Resource{ | ||
ID: "2", | ||
Details: map[string]interface{}{ | ||
|
@@ -154,6 +159,7 @@ func (s *ServiceTestSuite) TestUpdate() { | |
}, | ||
}, | ||
{ | ||
name: "trying to update resource type", | ||
resourceUpdatePayload: &domain.Resource{ | ||
ID: "2", | ||
Type: "test", | ||
|
@@ -165,17 +171,54 @@ func (s *ServiceTestSuite) TestUpdate() { | |
ID: "2", | ||
}, | ||
}, | ||
{ | ||
name: "should exclude __metadata from update payload", | ||
resourceUpdatePayload: &domain.Resource{ | ||
ID: "2", | ||
Details: map[string]interface{}{ | ||
"owner": "[email protected]", | ||
resource.ReservedDetailsKeyMetadata: map[string]string{ | ||
"new-key": "new-value", | ||
}, | ||
}, | ||
}, | ||
existingResource: &domain.Resource{ | ||
ID: "2", | ||
Details: map[string]interface{}{ | ||
"owner": "[email protected]", | ||
"foo": "bar", | ||
resource.ReservedDetailsKeyMetadata: map[string]string{ | ||
"key": "value", | ||
}, | ||
}, | ||
}, | ||
expectedUpdatedValues: &domain.Resource{ | ||
ID: "2", | ||
Details: map[string]interface{}{ | ||
"owner": "[email protected]", | ||
"foo": "bar", | ||
resource.ReservedDetailsKeyMetadata: map[string]string{ | ||
"key": "value", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
s.mockRepository.EXPECT().GetOne(mock.AnythingOfType("*context.emptyCtx"), tc.resourceUpdatePayload.ID).Return(tc.existingResource, nil).Once() | ||
s.mockRepository.EXPECT().Update(mock.AnythingOfType("*context.emptyCtx"), tc.expectedUpdatedValues).Return(nil).Once() | ||
s.mockAuditLogger.EXPECT().Log(mock.Anything, resource.AuditKeyResourceUpdate, mock.Anything).Return(nil) | ||
|
||
actualError := s.service.Update(context.Background(), tc.resourceUpdatePayload) | ||
|
||
s.Nil(actualError) | ||
s.mockRepository.AssertExpectations(s.T()) | ||
s.Run(tc.name, func() { | ||
s.mockRepository.EXPECT().GetOne(mock.AnythingOfType("*context.emptyCtx"), tc.resourceUpdatePayload.ID).Return(tc.existingResource, nil).Once() | ||
s.mockRepository.EXPECT().Update(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("*domain.Resource")). | ||
Run(func(_a0 context.Context, updateResourcePayload *domain.Resource) { | ||
s.Empty(cmp.Diff(tc.expectedUpdatedValues, updateResourcePayload, cmpopts.IgnoreFields(domain.Resource{}, "UpdatedAt", "CreatedAt"))) | ||
}).Return(nil).Once() | ||
s.mockAuditLogger.EXPECT().Log(mock.Anything, resource.AuditKeyResourceUpdate, mock.Anything).Return(nil) | ||
|
||
actualError := s.service.Update(context.Background(), tc.resourceUpdatePayload) | ||
|
||
s.Nil(actualError) | ||
s.mockRepository.AssertExpectations(s.T()) | ||
}) | ||
} | ||
}) | ||
} | ||
|
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
Oops, something went wrong.