diff --git a/core/group/filter.go b/core/group/filter.go index ad403d4b1..6d371dd95 100644 --- a/core/group/filter.go +++ b/core/group/filter.go @@ -2,6 +2,6 @@ package group type Filter struct { OrganizationID string - Project string - ServicedataKeyResourceIds []string + ProjectID string + ServicedataKeyResourceIDs []string } diff --git a/core/servicedata/service_test.go b/core/servicedata/service_test.go index 3dac8b80f..169112450 100644 --- a/core/servicedata/service_test.go +++ b/core/servicedata/service_test.go @@ -26,21 +26,21 @@ var ( testProjectSlug = "test-project-slug" testKey = servicedata.Key{ ProjectID: "test-project-slug", - Key: "test-key", + Name: "test-key", Description: "test key no 01", } testCreateKey = servicedata.Key{ URN: "test-project-slug:servicedata_key:test-key", ProjectID: testProjectID, ProjectSlug: testProjectSlug, - Key: "test-key", + Name: "test-key", Description: "test key no 01", ResourceID: testResourceID, } testCreatedKey = servicedata.Key{ URN: "test-project-slug:servicedata_key:test-key", ProjectID: testProjectID, - Key: "test-key", + Name: "test-key", Description: "test key no 01", ResourceID: testResourceID, } @@ -125,7 +125,7 @@ func TestService_CreateKey(t *testing.T) { name: "CreateKeyEmpty", key: servicedata.Key{ ProjectID: testKey.ProjectID, - Key: "", + Name: "", Description: testKey.Description, }, setup: func(t *testing.T) *servicedata.Service { @@ -174,7 +174,7 @@ func TestService_CreateKey(t *testing.T) { name: "CreateKeyInvalidProjectID", key: servicedata.Key{ ProjectID: "invalid-test-project-slug", - Key: testKey.Key, + Name: testKey.Name, Description: testKey.Description, }, email: "jane.doe@gotocompany.com", @@ -352,7 +352,7 @@ func TestService_Upsert(t *testing.T) { name: "UpsertKeyEmpty", data: servicedata.ServiceData{ Key: servicedata.Key{ - Key: "", + Name: "", }, }, setup: func(t *testing.T) *servicedata.Service { @@ -386,7 +386,7 @@ func TestService_Upsert(t *testing.T) { name: "UpsertInvalidProjectID", data: servicedata.ServiceData{ Key: servicedata.Key{ - Key: testKey.Key, + Name: testKey.Name, ProjectID: "invalid-test-project-slug", }, }, diff --git a/core/user/filter.go b/core/user/filter.go index 19e192814..5093f4a12 100644 --- a/core/user/filter.go +++ b/core/user/filter.go @@ -4,6 +4,6 @@ type Filter struct { Limit int32 Page int32 Keyword string - Project string + ProjectID string ServiceDataKeyResourceIds []string } diff --git a/internal/api/v1beta1/group.go b/internal/api/v1beta1/group.go index fb44b4cf7..6cc531b6f 100644 --- a/internal/api/v1beta1/group.go +++ b/internal/api/v1beta1/group.go @@ -65,8 +65,8 @@ func (h Handler) ListGroups(ctx context.Context, request *shieldv1beta1.ListGrou groupList, err := h.groupService.List(ctx, group.Filter{ OrganizationID: request.GetOrgId(), - Project: prj.ID, - ServicedataKeyResourceIds: servicedataKeyResourceIds, + ProjectID: prj.ID, + ServicedataKeyResourceIDs: servicedataKeyResourceIds, }) if err != nil { logger.Error(err.Error()) @@ -93,15 +93,10 @@ func (h Handler) CreateGroup(ctx context.Context, request *shieldv1beta1.CreateG return nil, grpcBadBodyError } - currentUserEmail, ok := user.GetEmailFromContext(ctx) - if !ok { - return nil, grpcUnauthenticated - } - - currentUserEmail = strings.TrimSpace(currentUserEmail) - if currentUserEmail == "" { - logger.Error(ErrEmptyEmailID.Error()) - return nil, grpcUnauthenticated + _, err := h.userService.FetchCurrentUser(ctx) + if err != nil { + logger.Error(err.Error()) + return nil, grpcInternalServerError } metaDataMap, err := metadata.Build(request.GetBody().GetMetadata().AsMap()) diff --git a/internal/api/v1beta1/user.go b/internal/api/v1beta1/user.go index 9feaddf0b..0fa5b5069 100644 --- a/internal/api/v1beta1/user.go +++ b/internal/api/v1beta1/user.go @@ -66,7 +66,7 @@ func (h Handler) ListUsers(ctx context.Context, request *shieldv1beta1.ListUsers Limit: request.GetPageSize(), Page: request.GetPageNum(), Keyword: request.GetKeyword(), - Project: prj.ID, + ProjectID: prj.ID, ServiceDataKeyResourceIds: servicedataKeyResourceIds, }) if err != nil { diff --git a/internal/store/postgres/group_repository.go b/internal/store/postgres/group_repository.go index 0f8eb5dae..2efa7a55e 100644 --- a/internal/store/postgres/group_repository.go +++ b/internal/store/postgres/group_repository.go @@ -290,7 +290,7 @@ func (r GroupRepository) List(ctx context.Context, flt group.Filter) ([]group.Gr goqu.I("updated_at"), ) - if len(flt.ServicedataKeyResourceIds) > 0 { + if len(flt.ServicedataKeyResourceIDs) > 0 { subquery := dialect.Select( goqu.I("sd.namespace_id"), goqu.I("sd.entity_id"), @@ -301,10 +301,10 @@ func (r GroupRepository) List(ctx context.Context, flt group.Filter) ([]group.Gr RightJoin(goqu.T(TABLE_SERVICE_DATA).As("sd"), goqu.On( goqu.I("sk.id").Eq(goqu.I("sd.key_id")))). Where(goqu.Ex{"sd.namespace_id": schema.GroupPrincipal}, - goqu.Ex{"sk.project_id": flt.Project}, + goqu.Ex{"sk.project_id": flt.ProjectID}, goqu.L( "sk.resource_id", - ).In(flt.ServicedataKeyResourceIds)) + ).In(flt.ServicedataKeyResourceIDs)) sqlStatement = dialect.Select( goqu.I("g.id"), @@ -369,6 +369,7 @@ func (r GroupRepository) List(ctx context.Context, flt group.Filter) ([]group.Gr currentGroup := groupedMetadataByGroup[g.ID] currentGroup.ID = g.ID currentGroup.Slug = g.Slug + currentGroup.Name = g.Name currentGroup.OrganizationID = g.OrgId currentGroup.CreatedAt = g.CreatedAt currentGroup.UpdatedAt = g.UpdatedAt diff --git a/internal/store/postgres/servicedata_repository_test.go b/internal/store/postgres/servicedata_repository_test.go index 15da67ccc..88b4c2210 100644 --- a/internal/store/postgres/servicedata_repository_test.go +++ b/internal/store/postgres/servicedata_repository_test.go @@ -130,14 +130,14 @@ func (s *ServiceDataRepositoryTestSuite) TestCreateKey() { KeyToCreate: servicedata.Key{ URN: "test-urn", ProjectID: s.projects[0].ID, - Key: "test-key", + Name: "test-key", Description: "description for test-key", ResourceID: s.resources[0].Idxa, }, ExpectedKey: servicedata.Key{ URN: "test-urn", ProjectID: s.projects[0].ID, - Key: "test-key", + Name: "test-key", Description: "description for test-key", ResourceID: s.resources[0].Idxa, }, @@ -147,8 +147,8 @@ func (s *ServiceDataRepositoryTestSuite) TestCreateKey() { KeyToCreate: servicedata.Key{ URN: s.keys[0].URN, ProjectID: s.projects[0].ID, - Key: s.keys[0].Key, - Description: s.keys[0].Key, + Name: s.keys[0].Name, + Description: s.keys[0].Name, ResourceID: s.resources[0].Idxa, }, ErrString: servicedata.ErrConflict.Error(), @@ -158,7 +158,7 @@ func (s *ServiceDataRepositoryTestSuite) TestCreateKey() { KeyToCreate: servicedata.Key{ URN: "test-urn-00", ProjectID: "00000000-0000-0000-0000-000000000000", - Key: "test-key", + Name: "test-key", Description: "description for test-key", ResourceID: s.resources[0].Idxa, }, @@ -169,7 +169,7 @@ func (s *ServiceDataRepositoryTestSuite) TestCreateKey() { KeyToCreate: servicedata.Key{ URN: "test-urn-00", ProjectID: s.projects[0].ID, - Key: "test-key", + Name: "test-key", Description: "description for test-key", ResourceID: "00000000-0000-0000-0000-000000000000", }, @@ -180,7 +180,7 @@ func (s *ServiceDataRepositoryTestSuite) TestCreateKey() { KeyToCreate: servicedata.Key{ URN: "test-urn-00", ProjectID: s.projects[0].ID, - Key: "", + Name: "", Description: "description for test-key", ResourceID: s.resources[0].Idxa, }, @@ -228,7 +228,7 @@ func (s *ServiceDataRepositoryTestSuite) TestUpsert() { }, ExpectedServiceData: servicedata.ServiceData{ Key: servicedata.Key{ - Key: s.keys[0].Key, + Name: s.keys[0].Name, }, Value: testValue, }, @@ -304,7 +304,7 @@ func (s *ServiceDataRepositoryTestSuite) TestGet() { Key: servicedata.Key{ URN: s.keys[0].URN, ProjectID: s.keys[0].ProjectID, - Key: s.keys[0].Key, + Name: s.keys[0].Name, ResourceID: s.keys[0].ResourceID, }, Value: s.data[0].Value, diff --git a/internal/store/postgres/testdata/mock-servicedata-keys.json b/internal/store/postgres/testdata/mock-servicedata-keys.json index 368dd5b49..b7cb14c02 100644 --- a/internal/store/postgres/testdata/mock-servicedata-keys.json +++ b/internal/store/postgres/testdata/mock-servicedata-keys.json @@ -1,14 +1,14 @@ [ { - "key": "test-key-01", + "name": "test-key-01", "description": "description for test-key-01" }, { - "key": "test-key-02", + "name": "test-key-02", "description": "description for test-key-02" }, { - "key": "test-key-03", + "name": "test-key-03", "description": "description for test-key-03" } ] \ No newline at end of file diff --git a/internal/store/postgres/user_repository.go b/internal/store/postgres/user_repository.go index 57cf5b129..22f98eabe 100644 --- a/internal/store/postgres/user_repository.go +++ b/internal/store/postgres/user_repository.go @@ -6,7 +6,6 @@ import ( "encoding/json" "errors" "fmt" - "regexp" "strings" "time" @@ -164,66 +163,11 @@ func (r UserRepository) Create(ctx context.Context, usr user.User) (user.User, e return user.User{}, fmt.Errorf("%w: %s", parseErr, err) } - var rows []interface{} - for k, v := range usr.Metadata { - valuejson, err := json.Marshal(v) - if err != nil { - valuejson = []byte{} - } - - rows = append(rows, goqu.Record{ - "user_id": transformedUser.ID, - "key": k, - "value": valuejson, - }) - } - metadataQuery, _, err := dialect.Insert(TABLE_METADATA).Rows(rows...).ToSQL() - if err != nil { - return user.User{}, err - } - - if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error { - nrCtx := newrelic.FromContext(ctx) - if nrCtx != nil { - nr := newrelic.DatastoreSegment{ - Product: newrelic.DatastorePostgres, - Collection: TABLE_METADATA, - Operation: "Create", - StartTime: nrCtx.StartSegmentNow(), - } - defer nr.End() - } - - _, err := tx.ExecContext(ctx, metadataQuery, params...) - if err != nil { - return err - } - return nil - }); err != nil { - err = checkPostgresError(err) - switch { - case errors.Is(err, errDuplicateKey): - return user.User{}, user.ErrConflict - case errors.Is(err, errForeignKeyViolation): - re := regexp.MustCompile(`\(([^)]+)\) `) - match := re.FindStringSubmatch(err.Error()) - if len(match) > 1 { - return user.User{}, fmt.Errorf("%w:%s", user.ErrKeyDoesNotExists, match[1]) - } - return user.User{}, user.ErrKeyDoesNotExists - - default: - tx.Rollback() - return user.User{}, err - } - } - err = tx.Commit() if err != nil { return user.User{}, err } - transformedUser.Metadata = usr.Metadata return transformedUser, nil } @@ -268,7 +212,7 @@ func (r UserRepository) List(ctx context.Context, flt user.Filter) ([]user.User, RightJoin(goqu.T(TABLE_SERVICE_DATA).As("sd"), goqu.On( goqu.I("sk.id").Eq(goqu.I("sd.key_id")))). Where(goqu.Ex{"sd.namespace_id": schema.UserPrincipal}, - goqu.Ex{"sk.project_id": flt.Project}, + goqu.Ex{"sk.project_id": flt.ProjectID}, goqu.L( "sk.resource_id", ).In(flt.ServiceDataKeyResourceIds)) diff --git a/internal/store/postgres/user_repository_test.go b/internal/store/postgres/user_repository_test.go index a74e35b37..5369b843f 100644 --- a/internal/store/postgres/user_repository_test.go +++ b/internal/store/postgres/user_repository_test.go @@ -10,10 +10,13 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" - "github.com/goto/salt/log" "github.com/ory/dockertest" "github.com/stretchr/testify/suite" + "github.com/goto/salt/log" + "github.com/goto/shield/core/project" + "github.com/goto/shield/core/resource" + "github.com/goto/shield/core/servicedata" "github.com/goto/shield/core/user" "github.com/goto/shield/internal/store/postgres" "github.com/goto/shield/pkg/db" @@ -26,6 +29,10 @@ type UserRepositoryTestSuite struct { pool *dockertest.Pool resource *dockertest.Resource repository *postgres.UserRepository + keys []servicedata.Key + projects []project.Project + resources []resource.Resource + data []servicedata.ServiceData users []user.User } @@ -53,6 +60,41 @@ func (s *UserRepositoryTestSuite) SetupTest() { if err != nil { s.T().Fatal(err) } + + namespaces, err := bootstrapNamespace(s.client) + if err != nil { + s.T().Fatal(err) + } + + _, err = bootstrapMetadataKeys(s.client) + if err != nil { + s.T().Fatal(err) + } + + organizations, err := bootstrapOrganization(s.client) + if err != nil { + s.T().Fatal(err) + } + + s.projects, err = bootstrapProject(s.client, organizations) + if err != nil { + s.T().Fatal(err) + } + + s.resources, err = bootstrapResource(s.client, s.projects, organizations, namespaces, s.users) + if err != nil { + s.T().Fatal(err) + } + + s.keys, err = bootstrapServiceDataKey(s.client, s.resources, s.projects) + if err != nil { + s.T().Fatal(err) + } + + s.data, err = bootstrapServiceData(s.client, s.users, s.keys) + if err != nil { + s.T().Fatal(err) + } } func (s *UserRepositoryTestSuite) TearDownSuite() { @@ -73,6 +115,11 @@ func (s *UserRepositoryTestSuite) cleanup() error { fmt.Sprintf("TRUNCATE TABLE %s RESTART IDENTITY CASCADE", postgres.TABLE_METADATA), fmt.Sprintf("TRUNCATE TABLE %s RESTART IDENTITY CASCADE", postgres.TABLE_USERS), fmt.Sprintf("TRUNCATE TABLE %s RESTART IDENTITY CASCADE", postgres.TABLE_METADATA_KEYS), + fmt.Sprintf("TRUNCATE TABLE %s RESTART IDENTITY CASCADE", postgres.TABLE_SERVICE_DATA_KEYS), + fmt.Sprintf("TRUNCATE TABLE %s RESTART IDENTITY CASCADE", postgres.TABLE_SERVICE_DATA), + fmt.Sprintf("TRUNCATE TABLE %s RESTART IDENTITY CASCADE", postgres.TABLE_ORGANIZATIONS), + fmt.Sprintf("TRUNCATE TABLE %s RESTART IDENTITY CASCADE", postgres.TABLE_PROJECTS), + fmt.Sprintf("TRUNCATE TABLE %s RESTART IDENTITY CASCADE", postgres.TABLE_RESOURCES), } return execQueries(context.TODO(), s.client, queries) } @@ -281,8 +328,8 @@ func (s *UserRepositoryTestSuite) TestList() { }, ExpectedUsers: []user.User{ { - Name: s.users[3].Name, - Email: s.users[3].Email, + Name: s.users[0].Name, + Email: s.users[0].Email, }, }, }, diff --git a/test/e2e_test/regression/api_test.go b/test/e2e_test/regression/api_test.go index 148c39c53..96f7c57c3 100644 --- a/test/e2e_test/regression/api_test.go +++ b/test/e2e_test/regression/api_test.go @@ -528,8 +528,10 @@ func (s *EndToEndAPIRegressionTestSuite) TestServiceDataAPI() { UserId: "invalid-user-id", Body: &shieldv1beta1.UpsertServiceDataRequestBody{ Project: myProject.Id, - Data: map[string]string{ - "update-key": "update value", + Data: &structpb.Struct{ + Fields: map[string]*structpb.Value{ + "update-key": structpb.NewStringValue("update value"), + }, }, }, }) @@ -541,8 +543,10 @@ func (s *EndToEndAPIRegressionTestSuite) TestServiceDataAPI() { UserId: testbench.OrgAdminEmail, Body: &shieldv1beta1.UpsertServiceDataRequestBody{ Project: "invalid-project-id", - Data: map[string]string{ - "update-key": "update value", + Data: &structpb.Struct{ + Fields: map[string]*structpb.Value{ + "update-key": structpb.NewStringValue("update value"), + }, }, }, }) @@ -554,9 +558,11 @@ func (s *EndToEndAPIRegressionTestSuite) TestServiceDataAPI() { UserId: testbench.OrgAdminEmail, Body: &shieldv1beta1.UpsertServiceDataRequestBody{ Project: myProject.Id, - Data: map[string]string{ - "update-key-1": "update value-1", - "update-key-2": "update value-2", + Data: &structpb.Struct{ + Fields: map[string]*structpb.Value{ + "update-key-1": structpb.NewStringValue("update value-1"), + "update-key-2": structpb.NewStringValue("update value-2"), + }, }, }, }) @@ -583,8 +589,10 @@ func (s *EndToEndAPIRegressionTestSuite) TestServiceDataAPI() { UserId: testbench.OrgAdminEmail, Body: &shieldv1beta1.UpsertServiceDataRequestBody{ Project: myProject.Id, - Data: map[string]string{ - "new-key": "new-value", + Data: &structpb.Struct{ + Fields: map[string]*structpb.Value{ + "new-key": structpb.NewStringValue("new-value"), + }, }, }, }) @@ -596,8 +604,10 @@ func (s *EndToEndAPIRegressionTestSuite) TestServiceDataAPI() { GroupId: "invalid-group-id", Body: &shieldv1beta1.UpsertServiceDataRequestBody{ Project: myProject.Id, - Data: map[string]string{ - "update-key": "update value", + Data: &structpb.Struct{ + Fields: map[string]*structpb.Value{ + "update-key": structpb.NewStringValue("update value"), + }, }, }, }) diff --git a/test/e2e_test/smoke/api_test.go b/test/e2e_test/smoke/api_test.go index c51e711f4..c13992503 100644 --- a/test/e2e_test/smoke/api_test.go +++ b/test/e2e_test/smoke/api_test.go @@ -24,6 +24,10 @@ type EndToEndAPISmokeTestSuite struct { func (s *EndToEndAPISmokeTestSuite) SetupTest() { ctx := context.Background() + ctx = metadata.NewOutgoingContext(ctx, metadata.New(map[string]string{ + testbench.IdentityHeader: testbench.OrgAdminEmail, + })) + s.client, _, s.appConfig, s.cancelClient, _, _ = testbench.SetupTests(s.T()) // validate @@ -39,11 +43,15 @@ func (s *EndToEndAPISmokeTestSuite) SetupTest() { pRes, err := s.client.ListProjects(ctx, &shieldv1beta1.ListProjectsRequest{}) s.Require().NoError(err) - s.Require().Equal(1, len(pRes.GetProjects())) + s.Require().Equal(2, len(pRes.GetProjects())) gRes, err := s.client.ListGroups(ctx, &shieldv1beta1.ListGroupsRequest{}) s.Require().NoError(err) s.Require().Equal(3, len(gRes.GetGroups())) + + rRes, err := s.client.ListResources(ctx, &shieldv1beta1.ListResourcesRequest{}) + s.Require().NoError(err) + s.Require().Equal(5, len(rRes.GetResources())) } func (s *EndToEndAPISmokeTestSuite) TearDownTest() { diff --git a/test/e2e_test/smoke/proxy_test.go b/test/e2e_test/smoke/proxy_test.go index 092798d5b..0d80426b2 100644 --- a/test/e2e_test/smoke/proxy_test.go +++ b/test/e2e_test/smoke/proxy_test.go @@ -9,6 +9,7 @@ import ( "testing" "github.com/stretchr/testify/suite" + "google.golang.org/grpc/metadata" "github.com/goto/shield/config" "github.com/goto/shield/pkg/db" @@ -35,6 +36,10 @@ type EndToEndProxySmokeTestSuite struct { func (s *EndToEndProxySmokeTestSuite) SetupTest() { ctx := context.Background() + ctx = metadata.NewOutgoingContext(ctx, metadata.New(map[string]string{ + testbench.IdentityHeader: testbench.OrgAdminEmail, + })) + s.client, s.serviceDataClient, s.appConfig, s.cancelClient, s.cancelServiceDataClient, _ = testbench.SetupTests(s.T()) dbClient, err := testbench.SetupDB(s.appConfig.DB) @@ -59,7 +64,7 @@ func (s *EndToEndProxySmokeTestSuite) SetupTest() { pRes, err := s.client.ListProjects(ctx, &shieldv1beta1.ListProjectsRequest{}) s.Require().NoError(err) - s.Require().Equal(1, len(pRes.GetProjects())) + s.Require().Equal(2, len(pRes.GetProjects())) s.projID = pRes.GetProjects()[0].GetId() s.projSlug = pRes.GetProjects()[0].GetSlug() diff --git a/test/e2e_test/testbench/helper.go b/test/e2e_test/testbench/helper.go index 7a3a11734..ca4a1f825 100644 --- a/test/e2e_test/testbench/helper.go +++ b/test/e2e_test/testbench/helper.go @@ -8,19 +8,22 @@ import ( "net" "os" + "github.com/goto/shield/core/servicedata" "github.com/goto/shield/internal/schema" "github.com/goto/shield/pkg/db" shieldv1beta1 "github.com/goto/shield/proto/v1beta1" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" "google.golang.org/grpc/metadata" + "google.golang.org/protobuf/types/known/structpb" ) const ( - OrgAdminEmail = "admin1-group1-org1@gotocompany.com" - DefaultSystemEmail = "shield-service@gotocompany.com" - IdentityHeader = "X-Shield-Email" - userIDHeaderKey = "X-Shield-UserID" + OrgAdminEmail = "admin1-group1-org1@gotocompany.com" + DefaultSystemEmail = "shield-service@gotocompany.com" + IdentityHeader = "X-Shield-Email" + userIDHeaderKey = "X-Shield-UserID" + DefaultServiceDataProjectName = "system" ) func GetFreePort() (int, error) { @@ -124,6 +127,69 @@ func BootstrapMetadataKey(ctx context.Context, cl shieldv1beta1.ShieldServiceCli return nil } +func BootstrapServiceDataKey(ctx context.Context, cl shieldv1beta1.ServiceDataServiceClient, creatorEmail, defaultServiceDataProjectName, testDataPath string) error { + testFixtureJSON, err := os.ReadFile(testDataPath + "/mocks/mock-servicedata-keys.json") + if err != nil { + return err + } + + var data []servicedata.Key + if err = json.Unmarshal(testFixtureJSON, &data); err != nil { + return err + } + + for _, d := range data { + ctx = metadata.NewOutgoingContext(ctx, metadata.New(map[string]string{ + IdentityHeader: creatorEmail, + })) + _, err := cl.CreateServiceDataKey(ctx, &shieldv1beta1.CreateServiceDataKeyRequest{ + Body: &shieldv1beta1.ServiceDataKeyRequestBody{ + Project: DefaultServiceDataProjectName, + Key: d.Name, + Description: d.Description, + }, + }) + if err != nil { + return err + } + } + + return nil +} + +func BootstrapUserServiceData(ctx context.Context, cl shieldv1beta1.ServiceDataServiceClient, userID, creatorEmail, defaultServiceDataProjectName, testDataPath string) error { + testFixtureJSON, err := os.ReadFile(testDataPath + "/mocks/mock-servicedata.json") + if err != nil { + return err + } + var data []servicedata.ServiceData + if err = json.Unmarshal(testFixtureJSON, &data); err != nil { + return err + } + + for _, d := range data { + ctx = metadata.NewOutgoingContext(ctx, metadata.New(map[string]string{ + IdentityHeader: creatorEmail, + })) + _, err := cl.UpsertUserServiceData(ctx, &shieldv1beta1.UpsertUserServiceDataRequest{ + UserId: userID, + Body: &shieldv1beta1.UpsertServiceDataRequestBody{ + Project: DefaultServiceDataProjectName, + Data: &structpb.Struct{ + Fields: map[string]*structpb.Value{ + d.Key.Name: structpb.NewStringValue(d.Value.(string)), + }, + }, + }, + }) + if err != nil { + return err + } + } + + return nil +} + func BootstrapOrganization(ctx context.Context, cl shieldv1beta1.ShieldServiceClient, creatorEmail string, testDataPath string) error { testFixtureJSON, err := os.ReadFile(testDataPath + "/mocks/mock-organization.json") if err != nil { @@ -170,6 +236,7 @@ func BootstrapProject(ctx context.Context, cl shieldv1beta1.ShieldServiceClient, } data[0].OrgId = orgResp.GetOrganizations()[0].GetId() + data[1].OrgId = orgResp.GetOrganizations()[0].GetId() for _, d := range data { ctx = metadata.NewOutgoingContext(ctx, metadata.New(map[string]string{ diff --git a/test/e2e_test/testbench/testbench.go b/test/e2e_test/testbench/testbench.go index 24bf46fff..a5480ed51 100644 --- a/test/e2e_test/testbench/testbench.go +++ b/test/e2e_test/testbench/testbench.go @@ -52,7 +52,7 @@ func initTestBench(ctx context.Context, appConfig *config.Shield, mockServerPort resources: []*dockertest.Resource{}, } - te.pool, err = dockertest.NewPool("") + te.pool, err = dockertest.NewPool("unix:///Users/ishanarya/.rd/docker.sock") if err != nil { return nil, nil, err } @@ -202,8 +202,9 @@ func SetupTests(t *testing.T) (shieldv1beta1.ShieldServiceClient, shieldv1beta1. ResourcesConfigPath: fmt.Sprintf("file://%s/%s", testDataPath, "configs/resources"), RulesPath: fmt.Sprintf("file://%s/%s", testDataPath, "configs/rules"), ServiceData: server.ServiceDataConfig{ - BootstrapEnabled: true, - MaxNumUpsertData: 1, + BootstrapEnabled: true, + MaxNumUpsertData: 1, + DefaultServiceDataProject: DefaultServiceDataProjectName, }, PublicAPIPrefix: "/shield", }, @@ -247,18 +248,18 @@ func SetupTests(t *testing.T) (shieldv1beta1.ShieldServiceClient, shieldv1beta1. }); err != nil { t.Fatal(err.Error()) } - - if err := BootstrapMetadataKey(ctx, client, OrgAdminEmail, testDataPath); err != nil { + if err := BootstrapOrganization(ctx, client, OrgAdminEmail, testDataPath); err != nil { t.Fatal(err) } - - if err := BootstrapUser(ctx, client, OrgAdminEmail, testDataPath); err != nil { + if err := BootstrapProject(ctx, client, OrgAdminEmail, testDataPath); err != nil { t.Fatal(err) } - if err := BootstrapOrganization(ctx, client, OrgAdminEmail, testDataPath); err != nil { + + if err := BootstrapServiceDataKey(ctx, serviceDataClient, OrgAdminEmail, DefaultServiceDataProjectName, testDataPath); err != nil { t.Fatal(err) } - if err := BootstrapProject(ctx, client, OrgAdminEmail, testDataPath); err != nil { + + if err := BootstrapUser(ctx, client, OrgAdminEmail, testDataPath); err != nil { t.Fatal(err) } if err := BootstrapGroup(ctx, client, OrgAdminEmail, testDataPath); err != nil {