(t *testing.T)
| 28 | } |
| 29 | |
| 30 | func TestFetchImportData(t *testing.T) { |
| 31 | t.Run("it can successfully fetch import data for multiple resources", func(t *testing.T) { |
| 32 | mockData1 := importDataList{{ResourceName: "Resource1", ImportID: "123"}} |
| 33 | mockData2 := importDataList{{ResourceName: "Resource2", ImportID: "456"}} |
| 34 | mockFetchers := []resourceDataFetcher{ |
| 35 | &mockFetcher{mockData: mockData1}, |
| 36 | &mockFetcher{mockData: mockData2}, |
| 37 | } |
| 38 | |
| 39 | expectedData := importDataList{ |
| 40 | {ResourceName: "Resource1", ImportID: "123"}, |
| 41 | {ResourceName: "Resource2", ImportID: "456"}, |
| 42 | } |
| 43 | |
| 44 | data, err := fetchImportData(context.Background(), &cli{}, mockFetchers...) |
| 45 | assert.NoError(t, err) |
| 46 | assert.Equal(t, expectedData, data) |
| 47 | }) |
| 48 | |
| 49 | t.Run("it deduplicates same-named resources", func(t *testing.T) { |
| 50 | mockData1 := importDataList{{ResourceName: "auth0_action.same", ImportID: "action-1"}, {ResourceName: "auth0_action.same", ImportID: "action-2"}} |
| 51 | mockData2 := importDataList{{ResourceName: "auth0_client.same", ImportID: "client-1"}} |
| 52 | |
| 53 | mockFetchers := []resourceDataFetcher{ |
| 54 | &mockFetcher{mockData: mockData1}, |
| 55 | &mockFetcher{mockData: mockData2}, |
| 56 | } |
| 57 | |
| 58 | expectedData := importDataList{ |
| 59 | {ResourceName: "auth0_action.same", ImportID: "action-1"}, |
| 60 | {ResourceName: "auth0_action.same" + "_2", ImportID: "action-2"}, |
| 61 | {ResourceName: "auth0_client.same", ImportID: "client-1"}, |
| 62 | } |
| 63 | |
| 64 | data, err := fetchImportData(context.Background(), &cli{}, mockFetchers...) |
| 65 | assert.NoError(t, err) |
| 66 | assert.Equal(t, expectedData, data) |
| 67 | }) |
| 68 | |
| 69 | t.Run("it returns an error when a data fetcher fails", func(t *testing.T) { |
| 70 | expectedErr := errors.New("failed to list clients") |
| 71 | mockFetchers := []resourceDataFetcher{ |
| 72 | &mockFetcher{mockErr: expectedErr}, |
| 73 | } |
| 74 | |
| 75 | _, err := fetchImportData(context.Background(), &cli{}, mockFetchers...) |
| 76 | assert.EqualError(t, err, "failed to list clients") |
| 77 | }) |
| 78 | |
| 79 | t.Run("it skips resources with 403 Forbidden error", func(t *testing.T) { |
| 80 | mockFetchers := []resourceDataFetcher{ |
| 81 | &mockFetcher{mockErr: errors.New("403 Forbidden: access denied")}, |
| 82 | &mockFetcher{mockData: importDataList{{ResourceName: "auth0_client.allowed", ImportID: "client-allowed"}}}, |
| 83 | } |
| 84 | |
| 85 | _, err := fetchImportData(context.Background(), &cli{ |
| 86 | renderer: &display.Renderer{ |
| 87 | MessageWriter: &bytes.Buffer{}, |
nothing calls this directly
no test coverage detected