|
| 1 | +package list |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "net/http" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + |
| 12 | + "github.com/algolia/cli/api/crawler" |
| 13 | + "github.com/algolia/cli/pkg/cmdutil" |
| 14 | + "github.com/algolia/cli/pkg/iostreams" |
| 15 | +) |
| 16 | + |
| 17 | +type roundTripFunc func(*http.Request) (*http.Response, error) |
| 18 | + |
| 19 | +func (f roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) { |
| 20 | + return f(r) |
| 21 | +} |
| 22 | + |
| 23 | +func jsonResponse(status int, body string) *http.Response { |
| 24 | + return &http.Response{ |
| 25 | + StatusCode: status, |
| 26 | + Header: http.Header{"Content-Type": []string{"application/json"}}, |
| 27 | + Body: io.NopCloser(strings.NewReader(body)), |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func Test_runListCmd_skipsForbiddenCrawlers(t *testing.T) { |
| 32 | + transport := roundTripFunc(func(r *http.Request) (*http.Response, error) { |
| 33 | + switch { |
| 34 | + case strings.HasSuffix(r.URL.Path, "/crawlers"): |
| 35 | + return jsonResponse(200, `{ |
| 36 | + "items": [ |
| 37 | + {"id": "ok-id", "name": "accessible"}, |
| 38 | + {"id": "forbidden-id", "name": "not-mine"} |
| 39 | + ], |
| 40 | + "page": 1, "itemsPerPage": 20, "total": 2 |
| 41 | + }`), nil |
| 42 | + case strings.Contains(r.URL.Path, "forbidden-id"): |
| 43 | + return &http.Response{ |
| 44 | + StatusCode: http.StatusForbidden, |
| 45 | + Header: http.Header{"Content-Type": []string{"text/plain; charset=utf-8"}}, |
| 46 | + Body: io.NopCloser(strings.NewReader("Forbidden")), |
| 47 | + }, nil |
| 48 | + case strings.Contains(r.URL.Path, "ok-id"): |
| 49 | + return jsonResponse(200, `{ |
| 50 | + "name": "accessible", |
| 51 | + "createdAt": "2026-01-01T00:00:00.000Z", |
| 52 | + "updatedAt": "2026-01-02T00:00:00.000Z", |
| 53 | + "running": false, |
| 54 | + "blocked": false, |
| 55 | + "config": {"appId": "APP_ID"} |
| 56 | + }`), nil |
| 57 | + default: |
| 58 | + t.Fatalf("unexpected request: %s", r.URL.Path) |
| 59 | + return nil, nil |
| 60 | + } |
| 61 | + }) |
| 62 | + |
| 63 | + client := crawler.NewClientWithHTTPClient("user", "key", &http.Client{Transport: transport}) |
| 64 | + |
| 65 | + ios, _, stdout, _ := iostreams.Test() |
| 66 | + printFlags := cmdutil.NewPrintFlags() |
| 67 | + printFlags.OutputFlagSpecified = func() bool { return false } |
| 68 | + opts := &ListOptions{ |
| 69 | + IO: ios, |
| 70 | + CrawlerClient: func() (*crawler.Client, error) { |
| 71 | + return client, nil |
| 72 | + }, |
| 73 | + PrintFlags: printFlags, |
| 74 | + } |
| 75 | + |
| 76 | + err := runListCmd(opts) |
| 77 | + require.NoError(t, err) |
| 78 | + |
| 79 | + assert.Contains(t, stdout.String(), "accessible") |
| 80 | + assert.NotContains(t, stdout.String(), "not-mine") |
| 81 | +} |
0 commit comments