|
| 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 | +} |
0 commit comments