Skip to content

Commit 057a60c

Browse files
authored
fix(store): org users search reports a value that is not a uuid as bad input (#1942)
1 parent a2ba3a2 commit 057a60c

4 files changed

Lines changed: 79 additions & 0 deletions

File tree

‎internal/api/v1beta1connect/organization_users.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ func (h *ConnectHandler) ExportOrganizationUsers(ctx context.Context, request *c
8686
if errors.Is(err, orgusers.ErrNoContent) {
8787
return connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("no data to export: %v", err))
8888
}
89+
if errors.Is(err, postgres.ErrBadInput) {
90+
return connect.NewError(connect.CodeInvalidArgument, err)
91+
}
8992
return connect.NewError(connect.CodeInternal, fmt.Errorf("ExportOrganizationUsers.Export: org_id=%s: %w", request.Msg.GetId(), err))
9093
}
9194
return streamBytesInChunks(orgUsersDataBytes, contentType, stream)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package v1beta1connect
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
8+
"connectrpc.com/connect"
9+
"github.com/raystack/frontier/core/aggregates/orgusers"
10+
"github.com/raystack/frontier/internal/api/v1beta1connect/mocks"
11+
"github.com/raystack/frontier/internal/store/postgres"
12+
frontierv1beta1 "github.com/raystack/frontier/proto/v1beta1"
13+
"github.com/stretchr/testify/assert"
14+
"github.com/stretchr/testify/mock"
15+
)
16+
17+
// Every case fails before the handler writes to the stream, so it is never used.
18+
func TestExportOrganizationUsers(t *testing.T) {
19+
const orgID = "1a02f4ae-9d76-446d-bc9c-a42c46e4f852"
20+
21+
tests := []struct {
22+
name string
23+
exportErr error
24+
wantCode connect.Code
25+
wantMsg string
26+
}{
27+
{
28+
name: "bad input from the store maps to invalid argument and keeps the reason",
29+
exportErr: fmt.Errorf("%w: value is not a valid uuid", postgres.ErrBadInput),
30+
wantCode: connect.CodeInvalidArgument,
31+
wantMsg: "value is not a valid uuid",
32+
},
33+
{
34+
name: "an org with nothing to export maps to invalid argument",
35+
exportErr: fmt.Errorf("%w: no users found for organization %s", orgusers.ErrNoContent, orgID),
36+
wantCode: connect.CodeInvalidArgument,
37+
wantMsg: "no data to export",
38+
},
39+
{
40+
name: "any other store failure is internal",
41+
exportErr: fmt.Errorf("connection refused"),
42+
wantCode: connect.CodeInternal,
43+
},
44+
}
45+
46+
for _, tt := range tests {
47+
t.Run(tt.name, func(t *testing.T) {
48+
svc := mocks.NewOrgUsersService(t)
49+
svc.EXPECT().Export(mock.Anything, orgID).Return(nil, "", tt.exportErr)
50+
handler := &ConnectHandler{orgUsersService: svc}
51+
52+
err := handler.ExportOrganizationUsers(context.Background(),
53+
connect.NewRequest(&frontierv1beta1.ExportOrganizationUsersRequest{Id: orgID}), nil)
54+
55+
assert.Error(t, err)
56+
assert.Equal(t, tt.wantCode, connect.CodeOf(err))
57+
if tt.wantMsg != "" {
58+
assert.Contains(t, err.Error(), tt.wantMsg)
59+
}
60+
})
61+
}
62+
}

‎internal/store/postgres/org_users_repository.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ func (r OrgUsersRepository) Search(ctx context.Context, orgID string, rql *rql.Q
125125
})
126126

127127
if err != nil {
128+
err = checkPostgresError(err)
129+
if errors.Is(err, ErrInvalidTextRepresentation) {
130+
return svc.OrgUsers{}, fmt.Errorf("%w: value is not a valid uuid", ErrBadInput)
131+
}
128132
return svc.OrgUsers{}, err
129133
}
130134

‎internal/store/postgres/org_users_repository_pg_test.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,16 @@ func (s *OrgUsersRepositoryPGTestSuite) TestPolicyWithoutRoleListsTheMemberWithN
175175
}
176176
}
177177

178+
func (s *OrgUsersRepositoryPGTestSuite) TestValueThatIsNotAUUIDIsBadInput() {
179+
_, err := s.repository.Search(s.ctx, s.orgID, &rql.Query{Limit: 50, Filters: []rql.Filter{
180+
{Name: "role_ids", Operator: "eq", Value: "not-a-uuid"},
181+
}})
182+
s.ErrorIs(err, postgres.ErrBadInput)
183+
184+
_, err = s.repository.Search(s.ctx, "not-a-uuid", &rql.Query{Limit: 50})
185+
s.ErrorIs(err, postgres.ErrBadInput)
186+
}
187+
178188
func TestOrgUsersRepositoryPG(t *testing.T) {
179189
suite.Run(t, new(OrgUsersRepositoryPGTestSuite))
180190
}

0 commit comments

Comments
 (0)