Skip to content

Commit

Permalink
fix: e2e-test, user, group and servicedat tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ishanarya0 committed Jun 18, 2024
1 parent e259c45 commit 678dc51
Show file tree
Hide file tree
Showing 15 changed files with 201 additions and 123 deletions.
4 changes: 2 additions & 2 deletions core/group/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package group

type Filter struct {
OrganizationID string
Project string
ServicedataKeyResourceIds []string
ProjectID string
ServicedataKeyResourceIDs []string
}
14 changes: 7 additions & 7 deletions core/servicedata/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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: "[email protected]",
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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",
},
},
Expand Down
2 changes: 1 addition & 1 deletion core/user/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ type Filter struct {
Limit int32
Page int32
Keyword string
Project string
ProjectID string
ServiceDataKeyResourceIds []string
}
17 changes: 6 additions & 11 deletions internal/api/v1beta1/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion internal/api/v1beta1/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions internal/store/postgres/group_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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"),
Expand Down Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions internal/store/postgres/servicedata_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand All @@ -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(),
Expand All @@ -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,
},
Expand All @@ -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",
},
Expand All @@ -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,
},
Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions internal/store/postgres/testdata/mock-servicedata-keys.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
58 changes: 1 addition & 57 deletions internal/store/postgres/user_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"errors"
"fmt"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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))
Expand Down
Loading

0 comments on commit 678dc51

Please sign in to comment.