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
4 changes: 4 additions & 0 deletions internal/api/v1beta1connect/organization_billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

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

orgBillingData, err := h.orgBillingService.Search(ctx, rqlQuery)
if err != nil {
if errors.Is(err, postgres.ErrBadInput) {
return nil, connect.NewError(connect.CodeInvalidArgument, err)
}
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("SearchOrganizations.Search: %w", err))
}

Expand Down
63 changes: 63 additions & 0 deletions internal/api/v1beta1connect/organization_billing_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package v1beta1connect

import (
"context"
"fmt"
"testing"

"connectrpc.com/connect"
"github.com/raystack/frontier/core/aggregates/orgbilling"
"github.com/raystack/frontier/internal/api/v1beta1connect/mocks"
"github.com/raystack/frontier/internal/store/postgres"
frontierv1beta1 "github.com/raystack/frontier/proto/v1beta1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

func TestSearchOrganizations(t *testing.T) {
tests := []struct {
name string
searchErr error
wantCode connect.Code
wantMsg string
}{
{
name: "bad input from the store maps to invalid argument and keeps the reason",
searchErr: fmt.Errorf("%w: value is not a valid uuid", postgres.ErrBadInput),
wantCode: connect.CodeInvalidArgument,
wantMsg: "value is not a valid uuid",
},
{
name: "any other store failure is internal",
searchErr: fmt.Errorf("connection refused"),
wantCode: connect.CodeInternal,
},
{
name: "a query the store accepts answers normally",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
svc := mocks.NewOrgBillingService(t)
svc.EXPECT().Search(mock.Anything, mock.Anything).Return(orgbilling.OrgBilling{}, tt.searchErr)
handler := &ConnectHandler{orgBillingService: svc}

resp, err := handler.SearchOrganizations(context.Background(),
connect.NewRequest(&frontierv1beta1.SearchOrganizationsRequest{
Query: &frontierv1beta1.RQLRequest{Limit: 10},
}))

if tt.wantCode == 0 {
assert.NoError(t, err)
assert.NotNil(t, resp)
return
}
assert.Error(t, err)
assert.Equal(t, tt.wantCode, connect.CodeOf(err))
if tt.wantMsg != "" {
assert.Contains(t, err.Error(), tt.wantMsg)
}
})
}
}
2 changes: 1 addition & 1 deletion internal/api/v1beta1connect/organization_invoices.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (h *ConnectHandler) SearchOrganizationInvoices(ctx context.Context, request
invoicesData, err := h.orgInvoicesService.Search(ctx, request.Msg.GetId(), rqlQuery)
if err != nil {
if errors.Is(err, postgres.ErrBadInput) {
return nil, connect.NewError(connect.CodeInvalidArgument, ErrInternalServerError)
return nil, connect.NewError(connect.CodeInvalidArgument, err)
}
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("SearchOrganizationInvoices.Search: org_id=%s: %w", request.Msg.GetId(), err))
}
Expand Down
2 changes: 1 addition & 1 deletion internal/api/v1beta1connect/organization_projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (h *ConnectHandler) SearchOrganizationProjects(ctx context.Context, request
orgProjectsData, err := h.orgProjectsService.Search(ctx, request.Msg.GetId(), rqlQuery)
if err != nil {
if errors.Is(err, postgres.ErrBadInput) {
return nil, connect.NewError(connect.CodeInvalidArgument, ErrInternalServerError)
return nil, connect.NewError(connect.CodeInvalidArgument, err)
}
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("SearchOrganizationProjects.Search: org_id=%s: %w", request.Msg.GetId(), err))
}
Expand Down
11 changes: 9 additions & 2 deletions internal/store/postgres/billing_invoice_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func (r BillingInvoiceRepository) List(ctx context.Context, flt invoice.Filter)
if err = r.dbc.WithTimeout(ctx, TABLE_BILLING_INVOICES, "List", func(ctx context.Context) error {
return r.dbc.SelectContext(ctx, &invoiceModels, query, params...)
}); err != nil {
return nil, fmt.Errorf("%w: %s", errDB, err)
return nil, fmt.Errorf("%w: %w", errDB, err)
}

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

// Transform results
Expand Down
9 changes: 8 additions & 1 deletion internal/store/postgres/org_billing_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package postgres
import (
"context"
"database/sql"
"errors"
"fmt"
"strings"

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

if err != nil {
return svc.OrgBilling{}, err
err = checkPostgresError(err)
switch {
case errors.Is(err, ErrInvalidTextRepresentation):
return svc.OrgBilling{}, fmt.Errorf("%w: value is not a valid uuid", ErrBadInput)
default:
return svc.OrgBilling{}, err
}
}

res := make([]svc.AggregatedOrganization, 0)
Expand Down
9 changes: 8 additions & 1 deletion internal/store/postgres/org_invoices_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package postgres
import (
"context"
"database/sql"
"errors"
"fmt"

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

if err != nil {
return svc.OrganizationInvoices{}, err
err = checkPostgresError(err)
switch {
case errors.Is(err, ErrInvalidTextRepresentation):
return svc.OrganizationInvoices{}, fmt.Errorf("%w: value is not a valid uuid", ErrBadInput)
default:
return svc.OrganizationInvoices{}, err
}
}

res := make([]svc.AggregatedInvoice, 0)
Expand Down
9 changes: 8 additions & 1 deletion internal/store/postgres/org_pats_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package postgres
import (
"context"
"database/sql"
"errors"
"fmt"
"time"

Expand Down Expand Up @@ -118,7 +119,13 @@ func (r OrgPATsRepository) Search(ctx context.Context, orgID string, rqlQuery *r
})
})
if err != nil {
return svc.OrganizationPATs{}, fmt.Errorf("querying org PATs: %w", err)
err = checkPostgresError(err)
switch {
case errors.Is(err, ErrInvalidTextRepresentation):
return svc.OrganizationPATs{}, fmt.Errorf("%w: value is not a valid uuid", ErrBadInput)
default:
return svc.OrganizationPATs{}, fmt.Errorf("querying org PATs: %w", err)
}
}

return svc.OrganizationPATs{
Expand Down
9 changes: 8 additions & 1 deletion internal/store/postgres/org_projects_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package postgres
import (
"context"
"database/sql"
"errors"

"fmt"
"strings"
Expand Down Expand Up @@ -97,7 +98,13 @@ func (r OrgProjectsRepository) Search(ctx context.Context, orgID string, rql *rq
})

if err != nil {
return svc.OrgProjects{}, err
err = checkPostgresError(err)
switch {
case errors.Is(err, ErrInvalidTextRepresentation):
return svc.OrgProjects{}, fmt.Errorf("%w: value is not a valid uuid", ErrBadInput)
default:
return svc.OrgProjects{}, err
}
}

res := make([]svc.AggregatedProject, 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package postgres
import (
"context"
"database/sql"
"errors"
"fmt"

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

if err != nil {
return svc.OrganizationServiceUserCredentials{}, err
err = checkPostgresError(err)
switch {
case errors.Is(err, ErrInvalidTextRepresentation):
return svc.OrganizationServiceUserCredentials{}, fmt.Errorf("%w: value is not a valid uuid", ErrBadInput)
default:
return svc.OrganizationServiceUserCredentials{}, err
}
}

res := make([]svc.AggregatedServiceUserCredential, 0)
Expand Down
9 changes: 8 additions & 1 deletion internal/store/postgres/org_serviceuser_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"slices"

Expand Down Expand Up @@ -83,7 +84,13 @@ func (r OrgServiceUserRepository) Search(ctx context.Context, orgID string, rqlQ
})

if err != nil {
return svc.OrganizationServiceUsers{}, err
err = checkPostgresError(err)
switch {
case errors.Is(err, ErrInvalidTextRepresentation):
return svc.OrganizationServiceUsers{}, fmt.Errorf("%w: value is not a valid uuid", ErrBadInput)
default:
return svc.OrganizationServiceUsers{}, err
}
}

res := make([]svc.AggregatedServiceUser, 0)
Expand Down
9 changes: 8 additions & 1 deletion internal/store/postgres/org_tokens_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package postgres
import (
"context"
"database/sql"
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -77,7 +78,13 @@ func (r OrgTokensRepository) Search(ctx context.Context, orgID string, rql *rql.
})

if err != nil {
return svc.OrganizationTokens{}, err
err = checkPostgresError(err)
switch {
case errors.Is(err, ErrInvalidTextRepresentation):
return svc.OrganizationTokens{}, fmt.Errorf("%w: value is not a valid uuid", ErrBadInput)
default:
return svc.OrganizationTokens{}, err
}
}

res := make([]svc.AggregatedToken, 0)
Expand Down
6 changes: 4 additions & 2 deletions internal/store/postgres/org_users_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,12 @@ func (r OrgUsersRepository) Search(ctx context.Context, orgID string, rql *rql.Q

if err != nil {
err = checkPostgresError(err)
if errors.Is(err, ErrInvalidTextRepresentation) {
switch {
case errors.Is(err, ErrInvalidTextRepresentation):
return svc.OrgUsers{}, fmt.Errorf("%w: value is not a valid uuid", ErrBadInput)
default:
return svc.OrgUsers{}, err
}
return svc.OrgUsers{}, err
}

res := make([]svc.AggregatedUser, 0)
Expand Down
10 changes: 9 additions & 1 deletion internal/store/postgres/project_users_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package postgres
import (
"context"
"database/sql"
"errors"
"fmt"
"strings"

"github.com/doug-martin/goqu/v9"
Expand Down Expand Up @@ -81,7 +83,13 @@ func (r ProjectUsersRepository) Search(ctx context.Context, projectID string, rq
})

if err != nil {
return svc.ProjectUsers{}, err
err = checkPostgresError(err)
switch {
case errors.Is(err, ErrInvalidTextRepresentation):
return svc.ProjectUsers{}, fmt.Errorf("%w: value is not a valid uuid", ErrBadInput)
default:
return svc.ProjectUsers{}, err
}
}

res := make([]svc.AggregatedUser, 0)
Expand Down
Loading
Loading