forked from raystack/frontier
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(e2etest): add e2e test for hook overwrite case
- Loading branch information
Showing
5 changed files
with
218 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ import ( | |
"github.com/stretchr/testify/suite" | ||
|
||
"github.com/goto/shield/config" | ||
"github.com/goto/shield/internal/store/postgres" | ||
"github.com/goto/shield/pkg/db" | ||
shieldv1beta1 "github.com/goto/shield/proto/v1beta1" | ||
"github.com/goto/shield/test/e2e_test/testbench" | ||
|
@@ -101,6 +102,36 @@ func (s *EndToEndProxySmokeTestSuite) TestProxyToEchoServer() { | |
defer res.Body.Close() | ||
s.Assert().Equal(200, res.StatusCode) | ||
}) | ||
|
||
s.Run("user not part of group will not be authenticated by middleware auth", func() { | ||
groupDetail, err := s.client.GetGroup(context.Background(), &shieldv1beta1.GetGroupRequest{Id: s.groupID}) | ||
s.Require().NoError(err) | ||
|
||
url := fmt.Sprintf("http://localhost:%d/api/resource_slug", s.appConfig.Proxy.Services[0].Port) | ||
reqBodyMap := map[string]string{ | ||
"project": s.projID, | ||
"name": "test-resource-group-slug", | ||
"group_slug": groupDetail.GetGroup().GetSlug(), | ||
} | ||
reqBodyBytes, err := json.Marshal(reqBodyMap) | ||
s.Require().NoError(err) | ||
|
||
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(reqBodyBytes)) | ||
s.Require().NoError(err) | ||
|
||
req.Header.Set(testbench.IdentityHeader, "[email protected]") | ||
req.Header.Set("X-Shield-Org", s.orgID) | ||
|
||
res, err := http.DefaultClient.Do(req) | ||
s.Require().NoError(err) | ||
|
||
defer res.Body.Close() | ||
|
||
s.Assert().Equal(401, res.StatusCode) | ||
}) | ||
} | ||
|
||
func (s *EndToEndProxySmokeTestSuite) TestResourceRelation() { | ||
s.Run("resource created on echo server should persist in shieldDB", func() { | ||
url := fmt.Sprintf("http://localhost:%d/api/resource", s.appConfig.Proxy.Services[0].Port) | ||
reqBodyMap := map[string]string{ | ||
|
@@ -369,6 +400,7 @@ func (s *EndToEndProxySmokeTestSuite) TestProxyToEchoServer() { | |
} | ||
s.Assert().Equal(s.userID, subjectID) | ||
}) | ||
|
||
s.Run("resource created on echo server should persist in shieldDB when using user e-mail", func() { | ||
userDetail, err := s.client.GetUser(context.Background(), &shieldv1beta1.GetUserRequest{Id: s.userID}) | ||
s.Require().NoError(err) | ||
|
@@ -423,6 +455,81 @@ func (s *EndToEndProxySmokeTestSuite) TestProxyToEchoServer() { | |
}) | ||
} | ||
|
||
func (s *EndToEndProxySmokeTestSuite) TestEdgeCases() { | ||
s.Run("Two relations created in POST hook won't be overwritten when 1 relation in PUT hook is created", func() { | ||
var ( | ||
resourceName = "test-resource-overwrite" | ||
staticGroupUUID = "6b591fe0-fc94-4fd2-82bc-45f8a5d12a88" | ||
) | ||
|
||
userDetail, err := s.client.GetUser(context.Background(), &shieldv1beta1.GetUserRequest{Id: s.userID}) | ||
s.Require().NoError(err) | ||
sqlRes, err := s.dbClient.DB.Exec(fmt.Sprintf("UPDATE groups SET id = '%s' WHERE slug = 'org1-group3'", staticGroupUUID)) | ||
Check failure on line 467 in test/e2e_test/smoke/proxy_test.go GitHub Actions / golangci
|
||
s.Require().NoError(err) | ||
rowsAffected, err := sqlRes.RowsAffected() | ||
s.Require().NoError(err) | ||
s.Require().Equal(int64(1), rowsAffected) | ||
|
||
// POST | ||
url := fmt.Sprintf("http://localhost:%d/api/resource", s.appConfig.Proxy.Services[0].Port) | ||
reqBodyMap := map[string]string{ | ||
"project": s.projID, | ||
"group": s.groupID, | ||
"name": resourceName, | ||
"user_email": userDetail.GetUser().GetEmail(), | ||
} | ||
reqBodyBytes, err := json.Marshal(reqBodyMap) | ||
s.Require().NoError(err) | ||
|
||
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(reqBodyBytes)) | ||
s.Require().NoError(err) | ||
|
||
req.Header.Set(testbench.IdentityHeader, "[email protected]") | ||
req.Header.Set("X-Shield-Org", s.orgID) | ||
|
||
res, err := http.DefaultClient.Do(req) | ||
s.Require().NoError(err) | ||
defer res.Body.Close() | ||
|
||
s.Require().Equal(200, res.StatusCode) | ||
|
||
// Validate resource & relation | ||
var resourceShield = struct { | ||
ID string `db:"id"` | ||
Name string `db:"name"` | ||
}{} | ||
|
||
s.Require().NoError(s.dbClient.DB.Get(&resourceShield, fmt.Sprintf("SELECT id, name FROM resources WHERE name = '%s'", resourceName))) | ||
Check failure on line 502 in test/e2e_test/smoke/proxy_test.go GitHub Actions / golangci
|
||
s.Assert().Equal(resourceShield.Name, resourceName) | ||
|
||
var relationsShield []postgres.Relation | ||
s.Require().NoError(s.dbClient.DB.Select(&relationsShield, "SELECT * FROM relations WHERE role_id = 'entropy/firehose:owner'")) | ||
Check failure on line 506 in test/e2e_test/smoke/proxy_test.go GitHub Actions / golangci
|
||
s.Assert().Len(relationsShield, 2) | ||
|
||
// PUT | ||
req, err = http.NewRequest(http.MethodPut, url, bytes.NewBuffer(reqBodyBytes)) | ||
s.Require().NoError(err) | ||
|
||
req.Header.Set(testbench.IdentityHeader, "[email protected]") | ||
req.Header.Set("X-Shield-Org", s.orgID) | ||
|
||
res, err = http.DefaultClient.Do(req) | ||
s.Require().NoError(err) | ||
defer res.Body.Close() | ||
|
||
s.Require().Equal(200, res.StatusCode) | ||
|
||
// Validate resource & relation | ||
s.Require().NoError(s.dbClient.DB.Get(&resourceShield, fmt.Sprintf("SELECT id, name FROM resources WHERE name = '%s'", resourceName))) | ||
Check failure on line 523 in test/e2e_test/smoke/proxy_test.go GitHub Actions / golangci
|
||
s.Assert().Equal(resourceShield.Name, resourceName) | ||
|
||
var updatedRelations []postgres.Relation | ||
s.Require().NoError(s.dbClient.DB.Select(&updatedRelations, "SELECT * FROM relations WHERE role_id = 'entropy/firehose:owner'")) | ||
Check failure on line 527 in test/e2e_test/smoke/proxy_test.go GitHub Actions / golangci
|
||
fmt.Println(updatedRelations) | ||
s.Assert().Len(updatedRelations, 2) | ||
}) | ||
} | ||
|
||
func TestEndToEndProxySmokeTestSuite(t *testing.T) { | ||
suite.Run(t, new(EndToEndProxySmokeTestSuite)) | ||
} |
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