Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 0 additions & 57 deletions core/organization/mocks/repository.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions core/organization/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ type Repository interface {
Create(ctx context.Context, org Organization) (Organization, error)
List(ctx context.Context, flt Filter) ([]Organization, error)
UpdateByID(ctx context.Context, org Organization) (Organization, error)
UpdateByName(ctx context.Context, org Organization) (Organization, error)
SetState(ctx context.Context, id string, state State) error
Delete(ctx context.Context, id string) error
}
Expand Down Expand Up @@ -252,10 +251,17 @@ func (s Service) List(ctx context.Context, f Filter) ([]Organization, error) {
}

func (s Service) Update(ctx context.Context, org Organization) (Organization, error) {
if org.ID != "" {
return s.repository.UpdateByID(ctx, org)
if org.ID == "" {
return Organization{}, ErrInvalidID
}
if !utils.IsValidUUID(org.ID) {
existing, err := s.repository.GetByName(ctx, org.ID)
if err != nil {
return Organization{}, err
}
org.ID = existing.ID
}
return s.repository.UpdateByName(ctx, org)
return s.repository.UpdateByID(ctx, org)
}

func (s Service) Enable(ctx context.Context, id string) error {
Expand Down
46 changes: 46 additions & 0 deletions core/organization/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,3 +337,49 @@ func TestService_List(t *testing.T) {
assert.Empty(t, got)
})
}

func TestService_Update(t *testing.T) {
ctx := context.Background()
targetID := uuid.New().String()
newService := func(repo *mocks.Repository) *organization.Service {
return organization.NewService(repo, mocks.NewRelationService(t), mocks.NewUserService(t), mocks.NewAuthnService(t),
mocks.NewPolicyService(t), mocks.NewPreferencesService(t), mocks.NewRoleService(t))
}

t.Run("update by id writes to the request id", func(t *testing.T) {
repo := mocks.NewRepository(t)
input := organization.Organization{ID: targetID, Name: "other-org", Title: "new title"}
want := organization.Organization{ID: targetID, Name: "target-org", Title: "new title"}
repo.EXPECT().UpdateByID(ctx, input).Return(want, nil)

got, err := newService(repo).Update(ctx, input)
assert.NoError(t, err)
assert.Equal(t, want, got)
})

t.Run("update by name writes to the org named in the request id, not the body name", func(t *testing.T) {
repo := mocks.NewRepository(t)
want := organization.Organization{ID: targetID, Name: "target-org", Title: "new title"}
repo.EXPECT().GetByName(ctx, "target-org").Return(organization.Organization{ID: targetID, Name: "target-org"}, nil)
repo.EXPECT().UpdateByID(ctx, organization.Organization{ID: targetID, Name: "other-org", Title: "new title"}).Return(want, nil)

got, err := newService(repo).Update(ctx, organization.Organization{ID: "target-org", Name: "other-org", Title: "new title"})
assert.NoError(t, err)
assert.Equal(t, want, got)
})

t.Run("update by unknown name returns not found without writing", func(t *testing.T) {
repo := mocks.NewRepository(t)
repo.EXPECT().GetByName(ctx, "missing-org").Return(organization.Organization{}, organization.ErrNotExist)

_, err := newService(repo).Update(ctx, organization.Organization{ID: "missing-org", Name: "other-org"})
assert.ErrorIs(t, err, organization.ErrNotExist)
})

t.Run("update without a request id returns invalid id without writing", func(t *testing.T) {
repo := mocks.NewRepository(t)

_, err := newService(repo).Update(ctx, organization.Organization{Name: "other-org"})
assert.ErrorIs(t, err, organization.ErrInvalidID)
})
}
57 changes: 0 additions & 57 deletions core/project/mocks/repository.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion core/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ type Repository interface {
Create(ctx context.Context, org Project) (Project, error)
List(ctx context.Context, f Filter) ([]Project, error)
UpdateByID(ctx context.Context, toUpdate Project) (Project, error)
UpdateByName(ctx context.Context, toUpdate Project) (Project, error)
Delete(ctx context.Context, id string) error
SetState(ctx context.Context, id string, state State) error
}
Expand Down
13 changes: 10 additions & 3 deletions core/project/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,17 @@ func (s Service) List(ctx context.Context, f Filter) ([]Project, error) {
}

func (s Service) Update(ctx context.Context, prj Project) (Project, error) {
if utils.IsValidUUID(prj.ID) {
return s.repository.UpdateByID(ctx, prj)
if prj.ID == "" {
return Project{}, ErrInvalidID
}
return s.repository.UpdateByName(ctx, prj)
if !utils.IsValidUUID(prj.ID) {
existing, err := s.repository.GetByName(ctx, prj.ID)
if err != nil {
return Project{}, err
}
prj.ID = existing.ID
}
return s.repository.UpdateByID(ctx, prj)
}

func (s Service) Enable(ctx context.Context, id string) error {
Expand Down
61 changes: 61 additions & 0 deletions core/project/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,3 +530,64 @@ func TestService_DeleteModel(t *testing.T) {
})
}
}

func TestService_Update(t *testing.T) {
ctx := context.Background()
targetID := uuid.New().String()
tests := []struct {
name string
input project.Project
setup func(repo *mocks.Repository)
want project.Project
wantErr error
}{
{
name: "update by id writes to the request id",
input: project.Project{ID: targetID, Name: "other-project", Title: "new title"},
setup: func(repo *mocks.Repository) {
repo.EXPECT().UpdateByID(ctx, project.Project{ID: targetID, Name: "other-project", Title: "new title"}).
Return(project.Project{ID: targetID, Name: "target-project", Title: "new title"}, nil)
},
want: project.Project{ID: targetID, Name: "target-project", Title: "new title"},
},
{
name: "update by name writes to the project named in the request id, not the body name",
input: project.Project{ID: "target-project", Name: "other-project", Title: "new title"},
setup: func(repo *mocks.Repository) {
repo.EXPECT().GetByName(ctx, "target-project").
Return(project.Project{ID: targetID, Name: "target-project"}, nil)
repo.EXPECT().UpdateByID(ctx, project.Project{ID: targetID, Name: "other-project", Title: "new title"}).
Return(project.Project{ID: targetID, Name: "target-project", Title: "new title"}, nil)
},
want: project.Project{ID: targetID, Name: "target-project", Title: "new title"},
},
{
name: "update by unknown name returns not found without writing",
input: project.Project{ID: "missing-project", Name: "other-project", Title: "new title"},
setup: func(repo *mocks.Repository) {
repo.EXPECT().GetByName(ctx, "missing-project").Return(project.Project{}, project.ErrNotExist)
},
wantErr: project.ErrNotExist,
},
{
name: "update without a request id returns invalid id without writing",
input: project.Project{Name: "other-project", Title: "new title"},
setup: func(repo *mocks.Repository) {},
wantErr: project.ErrInvalidID,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
repo, relationService, policyService, authnService := mockService(t)
tt.setup(repo)
s := project.NewService(repo, relationService, policyService, authnService)
got, err := s.Update(ctx, tt.input)
if !errors.Is(err, tt.wantErr) {
t.Fatalf("Update() error = %v, wantErr %v", err, tt.wantErr)
}
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("Update() mismatch (-want +got):\n%s", diff)
}
})
}
}
27 changes: 8 additions & 19 deletions internal/api/v1beta1connect/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,32 +206,21 @@ func (h *ConnectHandler) UpdateOrganization(ctx context.Context, request *connec
}
}

var updatedOrg organization.Organization
var err error
if utils.IsValidUUID(request.Msg.GetId()) {
updatedOrg, err = h.orgService.Update(ctx, organization.Organization{
ID: request.Msg.GetId(),
Name: request.Msg.GetBody().GetName(),
Title: request.Msg.GetBody().GetTitle(),
Avatar: request.Msg.GetBody().GetAvatar(),
Metadata: metaDataMap,
})
} else {
updatedOrg, err = h.orgService.Update(ctx, organization.Organization{
Name: request.Msg.GetBody().GetName(),
Title: request.Msg.GetBody().GetTitle(),
Avatar: request.Msg.GetBody().GetAvatar(),
Metadata: metaDataMap,
})
}
updatedOrg, err := h.orgService.Update(ctx, organization.Organization{
ID: request.Msg.GetId(),
Name: request.Msg.GetBody().GetName(),
Title: request.Msg.GetBody().GetTitle(),
Avatar: request.Msg.GetBody().GetAvatar(),
Metadata: metaDataMap,
})
if err != nil {
switch {
case errors.Is(err, organization.ErrNotExist), errors.Is(err, organization.ErrInvalidID):
return nil, connect.NewError(connect.CodeNotFound, ErrNotFound)
case errors.Is(err, organization.ErrConflict):
return nil, connect.NewError(connect.CodeAlreadyExists, ErrConflictRequest)
default:
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("UpdateOrganization.Update: org_id=%s org_name=%s org_title=%s: %w", request.Msg.GetId(), request.Msg.GetBody().GetName(), request.Msg.GetBody().GetTitle(), err))
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("UpdateOrganization.Update: org_id=%s org_title=%s: %w", request.Msg.GetId(), request.Msg.GetBody().GetTitle(), err))
}
}

Expand Down
9 changes: 6 additions & 3 deletions internal/api/v1beta1connect/organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ func TestHandler_UpdateOrganization(t *testing.T) {
name: "should not touch metadata when it is not set and the org is addressed by name",
setup: func(os *mocks.OrganizationService, ms *mocks.MetaSchemaService) {
os.EXPECT().Update(mock.AnythingOfType("context.backgroundCtx"), organization.Organization{
ID: "org-slug",
Name: "new-org",
Metadata: nil,
}).Return(organization.Organization{}, errors.New("test error"))
Expand All @@ -323,7 +324,7 @@ func TestHandler_UpdateOrganization(t *testing.T) {
Body: &frontierv1beta1.OrganizationRequestBody{Name: "new-org"},
}),
want: nil,
wantErr: connect.NewError(connect.CodeInternal, fmt.Errorf("UpdateOrganization.Update: org_id=%s org_name=%s org_title=%s: %w", "org-slug", "new-org", "", errors.New("test error"))),
wantErr: connect.NewError(connect.CodeInternal, fmt.Errorf("UpdateOrganization.Update: org_id=%s org_title=%s: %w", "org-slug", "", errors.New("test error"))),
},
{
name: "should pass empty metadata through when metadata is set to an empty object",
Expand All @@ -340,7 +341,7 @@ func TestHandler_UpdateOrganization(t *testing.T) {
Body: &frontierv1beta1.OrganizationRequestBody{Name: "new-org", Metadata: &structpb.Struct{}},
}),
want: nil,
wantErr: connect.NewError(connect.CodeInternal, fmt.Errorf("UpdateOrganization.Update: org_id=%s org_name=%s org_title=%s: %w", someOrgID, "new-org", "", errors.New("test error"))),
wantErr: connect.NewError(connect.CodeInternal, fmt.Errorf("UpdateOrganization.Update: org_id=%s org_title=%s: %w", someOrgID, "", errors.New("test error"))),
},
{
name: "should return error if meta schema service gives error",
Expand Down Expand Up @@ -385,7 +386,7 @@ func TestHandler_UpdateOrganization(t *testing.T) {
},
}),
want: nil,
wantErr: connect.NewError(connect.CodeInternal, fmt.Errorf("UpdateOrganization.Update: org_id=%s org_name=%s org_title=%s: %w", someOrgID, "new-org", "", errors.New("test error"))),
wantErr: connect.NewError(connect.CodeInternal, fmt.Errorf("UpdateOrganization.Update: org_id=%s org_title=%s: %w", someOrgID, "", errors.New("test error"))),
},
{
name: "should return not found error if org id is not uuid (slug) and not exist",
Expand Down Expand Up @@ -535,6 +536,7 @@ func TestHandler_UpdateOrganization(t *testing.T) {
setup: func(os *mocks.OrganizationService, ms *mocks.MetaSchemaService) {
ms.EXPECT().Validate(mock.AnythingOfType("metadata.Metadata"), orgMetaSchema).Return(nil)
os.EXPECT().Update(mock.AnythingOfType("context.backgroundCtx"), organization.Organization{
ID: "org-slug",
Name: "new-org",
Metadata: metadata.Metadata{
"email": "org1@org1.com",
Expand All @@ -553,6 +555,7 @@ func TestHandler_UpdateOrganization(t *testing.T) {
}, nil)
},
request: connect.NewRequest(&frontierv1beta1.UpdateOrganizationRequest{
Id: "org-slug",
Body: &frontierv1beta1.OrganizationRequestBody{
Name: "new-org",
Metadata: &structpb.Struct{
Expand Down
Loading
Loading