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