Skip to content

Commit 60ac2de

Browse files
authored
fix(store): a search value that is not a uuid reports bad input (#1952)
1 parent 397098b commit 60ac2de

17 files changed

Lines changed: 284 additions & 16 deletions

‎internal/api/v1beta1connect/organization_billing.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"connectrpc.com/connect"
99
"github.com/raystack/frontier/core/aggregates/orgbilling"
10+
"github.com/raystack/frontier/internal/store/postgres"
1011
frontierv1beta1 "github.com/raystack/frontier/proto/v1beta1"
1112
"github.com/raystack/salt/rql"
1213
"google.golang.org/genproto/googleapis/api/httpbody"
@@ -28,6 +29,9 @@ func (h *ConnectHandler) SearchOrganizations(ctx context.Context, request *conne
2829

2930
orgBillingData, err := h.orgBillingService.Search(ctx, rqlQuery)
3031
if err != nil {
32+
if errors.Is(err, postgres.ErrBadInput) {
33+
return nil, connect.NewError(connect.CodeInvalidArgument, err)
34+
}
3135
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("SearchOrganizations.Search: %w", err))
3236
}
3337

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package v1beta1connect
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
8+
"connectrpc.com/connect"
9+
"github.com/raystack/frontier/core/aggregates/orgbilling"
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+
func TestSearchOrganizations(t *testing.T) {
18+
tests := []struct {
19+
name string
20+
searchErr error
21+
wantCode connect.Code
22+
wantMsg string
23+
}{
24+
{
25+
name: "bad input from the store maps to invalid argument and keeps the reason",
26+
searchErr: fmt.Errorf("%w: value is not a valid uuid", postgres.ErrBadInput),
27+
wantCode: connect.CodeInvalidArgument,
28+
wantMsg: "value is not a valid uuid",
29+
},
30+
{
31+
name: "any other store failure is internal",
32+
searchErr: fmt.Errorf("connection refused"),
33+
wantCode: connect.CodeInternal,
34+
},
35+
{
36+
name: "a query the store accepts answers normally",
37+
},
38+
}
39+
40+
for _, tt := range tests {
41+
t.Run(tt.name, func(t *testing.T) {
42+
svc := mocks.NewOrgBillingService(t)
43+
svc.EXPECT().Search(mock.Anything, mock.Anything).Return(orgbilling.OrgBilling{}, tt.searchErr)
44+
handler := &ConnectHandler{orgBillingService: svc}
45+
46+
resp, err := handler.SearchOrganizations(context.Background(),
47+
connect.NewRequest(&frontierv1beta1.SearchOrganizationsRequest{
48+
Query: &frontierv1beta1.RQLRequest{Limit: 10},
49+
}))
50+
51+
if tt.wantCode == 0 {
52+
assert.NoError(t, err)
53+
assert.NotNil(t, resp)
54+
return
55+
}
56+
assert.Error(t, err)
57+
assert.Equal(t, tt.wantCode, connect.CodeOf(err))
58+
if tt.wantMsg != "" {
59+
assert.Contains(t, err.Error(), tt.wantMsg)
60+
}
61+
})
62+
}
63+
}

‎internal/api/v1beta1connect/organization_invoices.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (h *ConnectHandler) SearchOrganizationInvoices(ctx context.Context, request
3030
invoicesData, err := h.orgInvoicesService.Search(ctx, request.Msg.GetId(), rqlQuery)
3131
if err != nil {
3232
if errors.Is(err, postgres.ErrBadInput) {
33-
return nil, connect.NewError(connect.CodeInvalidArgument, ErrInternalServerError)
33+
return nil, connect.NewError(connect.CodeInvalidArgument, err)
3434
}
3535
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("SearchOrganizationInvoices.Search: org_id=%s: %w", request.Msg.GetId(), err))
3636
}

‎internal/api/v1beta1connect/organization_projects.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func (h *ConnectHandler) SearchOrganizationProjects(ctx context.Context, request
3131
orgProjectsData, err := h.orgProjectsService.Search(ctx, request.Msg.GetId(), rqlQuery)
3232
if err != nil {
3333
if errors.Is(err, postgres.ErrBadInput) {
34-
return nil, connect.NewError(connect.CodeInvalidArgument, ErrInternalServerError)
34+
return nil, connect.NewError(connect.CodeInvalidArgument, err)
3535
}
3636
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("SearchOrganizationProjects.Search: org_id=%s: %w", request.Msg.GetId(), err))
3737
}

‎internal/store/postgres/billing_invoice_repository.go‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ func (r BillingInvoiceRepository) List(ctx context.Context, flt invoice.Filter)
259259
if err = r.dbc.WithTimeout(ctx, TABLE_BILLING_INVOICES, "List", func(ctx context.Context) error {
260260
return r.dbc.SelectContext(ctx, &invoiceModels, query, params...)
261261
}); err != nil {
262-
return nil, fmt.Errorf("%w: %s", errDB, err)
262+
return nil, fmt.Errorf("%w: %w", errDB, err)
263263
}
264264

265265
invoices := make([]invoice.Invoice, 0, len(invoiceModels))
@@ -352,7 +352,14 @@ func (r BillingInvoiceRepository) Search(ctx context.Context, rqlQuery *rql.Quer
352352
if err = r.dbc.WithTimeout(ctx, TABLE_BILLING_INVOICES, "Search", func(ctx context.Context) error {
353353
return r.dbc.SelectContext(ctx, &invoiceModels, dataQuery, params...)
354354
}); err != nil {
355-
return nil, fmt.Errorf("%w: %s", errDB, err)
355+
err = checkPostgresError(err)
356+
switch {
357+
case errors.Is(err, ErrInvalidTextRepresentation):
358+
// this repository's handler matches the billing sentinel, not the postgres one
359+
return nil, fmt.Errorf("%w: value is not a valid uuid", invoice.ErrBadInput)
360+
default:
361+
return nil, fmt.Errorf("%w: %w", errDB, err)
362+
}
356363
}
357364

358365
// Transform results

‎internal/store/postgres/org_billing_repository.go‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package postgres
33
import (
44
"context"
55
"database/sql"
6+
"errors"
67
"fmt"
78
"strings"
89

@@ -170,7 +171,13 @@ func (r OrgBillingRepository) Search(ctx context.Context, rql *rql.Query) (svc.O
170171
})
171172

172173
if err != nil {
173-
return svc.OrgBilling{}, err
174+
err = checkPostgresError(err)
175+
switch {
176+
case errors.Is(err, ErrInvalidTextRepresentation):
177+
return svc.OrgBilling{}, fmt.Errorf("%w: value is not a valid uuid", ErrBadInput)
178+
default:
179+
return svc.OrgBilling{}, err
180+
}
174181
}
175182

176183
res := make([]svc.AggregatedOrganization, 0)

‎internal/store/postgres/org_invoices_repository.go‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package postgres
33
import (
44
"context"
55
"database/sql"
6+
"errors"
67
"fmt"
78

89
"github.com/doug-martin/goqu/v9"
@@ -123,7 +124,13 @@ func (r OrgInvoicesRepository) Search(ctx context.Context, orgID string, rql *rq
123124
})
124125

125126
if err != nil {
126-
return svc.OrganizationInvoices{}, err
127+
err = checkPostgresError(err)
128+
switch {
129+
case errors.Is(err, ErrInvalidTextRepresentation):
130+
return svc.OrganizationInvoices{}, fmt.Errorf("%w: value is not a valid uuid", ErrBadInput)
131+
default:
132+
return svc.OrganizationInvoices{}, err
133+
}
127134
}
128135

129136
res := make([]svc.AggregatedInvoice, 0)

‎internal/store/postgres/org_pats_repository.go‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package postgres
33
import (
44
"context"
55
"database/sql"
6+
"errors"
67
"fmt"
78
"time"
89

@@ -118,7 +119,13 @@ func (r OrgPATsRepository) Search(ctx context.Context, orgID string, rqlQuery *r
118119
})
119120
})
120121
if err != nil {
121-
return svc.OrganizationPATs{}, fmt.Errorf("querying org PATs: %w", err)
122+
err = checkPostgresError(err)
123+
switch {
124+
case errors.Is(err, ErrInvalidTextRepresentation):
125+
return svc.OrganizationPATs{}, fmt.Errorf("%w: value is not a valid uuid", ErrBadInput)
126+
default:
127+
return svc.OrganizationPATs{}, fmt.Errorf("querying org PATs: %w", err)
128+
}
122129
}
123130

124131
return svc.OrganizationPATs{

‎internal/store/postgres/org_projects_repository.go‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package postgres
33
import (
44
"context"
55
"database/sql"
6+
"errors"
67

78
"fmt"
89
"strings"
@@ -97,7 +98,13 @@ func (r OrgProjectsRepository) Search(ctx context.Context, orgID string, rql *rq
9798
})
9899

99100
if err != nil {
100-
return svc.OrgProjects{}, err
101+
err = checkPostgresError(err)
102+
switch {
103+
case errors.Is(err, ErrInvalidTextRepresentation):
104+
return svc.OrgProjects{}, fmt.Errorf("%w: value is not a valid uuid", ErrBadInput)
105+
default:
106+
return svc.OrgProjects{}, err
107+
}
101108
}
102109

103110
res := make([]svc.AggregatedProject, 0)

‎internal/store/postgres/org_serviceuser_credentials_repository.go‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package postgres
33
import (
44
"context"
55
"database/sql"
6+
"errors"
67
"fmt"
78

89
"github.com/doug-martin/goqu/v9"
@@ -65,7 +66,13 @@ func (r OrgServiceUserCredentialsRepository) Search(ctx context.Context, orgID s
6566
})
6667

6768
if err != nil {
68-
return svc.OrganizationServiceUserCredentials{}, err
69+
err = checkPostgresError(err)
70+
switch {
71+
case errors.Is(err, ErrInvalidTextRepresentation):
72+
return svc.OrganizationServiceUserCredentials{}, fmt.Errorf("%w: value is not a valid uuid", ErrBadInput)
73+
default:
74+
return svc.OrganizationServiceUserCredentials{}, err
75+
}
6976
}
7077

7178
res := make([]svc.AggregatedServiceUserCredential, 0)

0 commit comments

Comments
 (0)