Skip to content

Commit 1c77958

Browse files
committed
feat: migrate DeleteGroup API to Connect RPC
1 parent e918e3a commit 1c77958

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

internal/api/v1beta1connect/group.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,29 @@ func (h *ConnectHandler) DisableGroup(ctx context.Context, request *connect.Requ
415415
return connect.NewResponse(&frontierv1beta1.DisableGroupResponse{}), nil
416416
}
417417

418+
func (h *ConnectHandler) DeleteGroup(ctx context.Context, request *connect.Request[frontierv1beta1.DeleteGroupRequest]) (*connect.Response[frontierv1beta1.DeleteGroupResponse], error) {
419+
_, err := h.orgService.Get(ctx, request.Msg.GetOrgId())
420+
if err != nil {
421+
switch {
422+
case errors.Is(err, organization.ErrDisabled):
423+
return nil, connect.NewError(connect.CodeNotFound, ErrOrgDisabled)
424+
case errors.Is(err, organization.ErrNotExist):
425+
return nil, connect.NewError(connect.CodeNotFound, ErrOrgNotFound)
426+
default:
427+
return nil, connect.NewError(connect.CodeInternal, ErrInternalServerError)
428+
}
429+
}
430+
if err := h.groupService.Delete(ctx, request.Msg.GetId()); err != nil {
431+
switch {
432+
case errors.Is(err, group.ErrNotExist):
433+
return nil, connect.NewError(connect.CodeNotFound, ErrGroupNotFound)
434+
default:
435+
return nil, connect.NewError(connect.CodeInternal, ErrInternalServerError)
436+
}
437+
}
438+
return connect.NewResponse(&frontierv1beta1.DeleteGroupResponse{}), nil
439+
}
440+
418441
func transformGroupToPB(grp group.Group) (frontierv1beta1.Group, error) {
419442
metaData, err := grp.Metadata.ToStructPB()
420443
if err != nil {

internal/api/v1beta1connect/group_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,3 +1573,88 @@ func TestConnectHandler_DisableGroup(t *testing.T) {
15731573
})
15741574
}
15751575
}
1576+
1577+
func TestConnectHandler_DeleteGroup(t *testing.T) {
1578+
randomID := utils.NewString()
1579+
tests := []struct {
1580+
name string
1581+
setup func(gs *mocks.GroupService, os *mocks.OrganizationService)
1582+
request *connect.Request[frontierv1beta1.DeleteGroupRequest]
1583+
want *connect.Response[frontierv1beta1.DeleteGroupResponse]
1584+
wantErr error
1585+
}{
1586+
{
1587+
name: "should return error if organization does not exist",
1588+
setup: func(gs *mocks.GroupService, os *mocks.OrganizationService) {
1589+
os.EXPECT().Get(mock.Anything, testOrgID).Return(organization.Organization{}, organization.ErrNotExist)
1590+
},
1591+
request: connect.NewRequest(&frontierv1beta1.DeleteGroupRequest{
1592+
Id: randomID,
1593+
OrgId: testOrgID,
1594+
}),
1595+
want: nil,
1596+
wantErr: connect.NewError(connect.CodeNotFound, ErrOrgNotFound),
1597+
},
1598+
{
1599+
name: "should return error if organization is disabled",
1600+
setup: func(gs *mocks.GroupService, os *mocks.OrganizationService) {
1601+
os.EXPECT().Get(mock.Anything, testOrgID).Return(organization.Organization{}, organization.ErrDisabled)
1602+
},
1603+
request: connect.NewRequest(&frontierv1beta1.DeleteGroupRequest{
1604+
Id: randomID,
1605+
OrgId: testOrgID,
1606+
}),
1607+
want: nil,
1608+
wantErr: connect.NewError(connect.CodeNotFound, ErrOrgDisabled),
1609+
},
1610+
{
1611+
name: "should return error if group does not exist",
1612+
setup: func(gs *mocks.GroupService, os *mocks.OrganizationService) {
1613+
os.EXPECT().Get(mock.Anything, testOrgID).Return(organization.Organization{ID: testOrgID}, nil)
1614+
gs.EXPECT().Delete(mock.Anything, randomID).Return(group.ErrNotExist)
1615+
},
1616+
request: connect.NewRequest(&frontierv1beta1.DeleteGroupRequest{
1617+
Id: randomID,
1618+
OrgId: testOrgID,
1619+
}),
1620+
want: nil,
1621+
wantErr: connect.NewError(connect.CodeNotFound, ErrGroupNotFound),
1622+
},
1623+
{
1624+
name: "should delete group successfully",
1625+
setup: func(gs *mocks.GroupService, os *mocks.OrganizationService) {
1626+
os.EXPECT().Get(mock.Anything, testOrgID).Return(organization.Organization{ID: testOrgID}, nil)
1627+
gs.EXPECT().Delete(mock.Anything, randomID).Return(nil)
1628+
},
1629+
request: connect.NewRequest(&frontierv1beta1.DeleteGroupRequest{
1630+
Id: randomID,
1631+
OrgId: testOrgID,
1632+
}),
1633+
want: connect.NewResponse(&frontierv1beta1.DeleteGroupResponse{}),
1634+
wantErr: nil,
1635+
},
1636+
}
1637+
1638+
for _, tt := range tests {
1639+
t.Run(tt.name, func(t *testing.T) {
1640+
mockGroupSvc := new(mocks.GroupService)
1641+
mockOrgSvc := new(mocks.OrganizationService)
1642+
if tt.setup != nil {
1643+
tt.setup(mockGroupSvc, mockOrgSvc)
1644+
}
1645+
h := ConnectHandler{
1646+
groupService: mockGroupSvc,
1647+
orgService: mockOrgSvc,
1648+
}
1649+
got, err := h.DeleteGroup(context.Background(), tt.request)
1650+
if tt.wantErr != nil {
1651+
assert.Error(t, err)
1652+
assert.Equal(t, tt.wantErr.(*connect.Error).Code(), err.(*connect.Error).Code())
1653+
assert.Equal(t, tt.wantErr.(*connect.Error).Message(), err.(*connect.Error).Message())
1654+
} else {
1655+
assert.NoError(t, err)
1656+
assert.EqualValues(t, tt.want, got)
1657+
}
1658+
})
1659+
}
1660+
}

0 commit comments

Comments
 (0)