|
| 1 | +package crawler |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + |
| 12 | + "github.com/algolia/cli/api/dashboard" |
| 13 | + "github.com/algolia/cli/pkg/config" |
| 14 | + "github.com/algolia/cli/pkg/iostreams" |
| 15 | + "github.com/algolia/cli/test" |
| 16 | +) |
| 17 | + |
| 18 | +func Test_runCrawlerCmd_UsesDefaultProfile(t *testing.T) { |
| 19 | + io, _, stdout, _ := iostreams.Test() |
| 20 | + io.SetStdoutTTY(true) |
| 21 | + |
| 22 | + cfg := test.NewConfigStubWithProfiles([]*config.Profile{ |
| 23 | + {Name: "default", Default: true}, |
| 24 | + {Name: "other"}, |
| 25 | + }) |
| 26 | + cfg.CurrentProfile.Name = "" |
| 27 | + |
| 28 | + server := newCrawlerTestServer(t, "token-1", "crawler-user", "crawler-key") |
| 29 | + t.Cleanup(server.Close) |
| 30 | + |
| 31 | + err := runCrawlerCmd(&CrawlerOptions{ |
| 32 | + IO: io, |
| 33 | + config: cfg, |
| 34 | + NewDashboardClient: newDashboardTestClient(server), |
| 35 | + GetValidToken: func(client *dashboard.Client) (string, error) { |
| 36 | + return "token-1", nil |
| 37 | + }, |
| 38 | + }) |
| 39 | + require.NoError(t, err) |
| 40 | + |
| 41 | + assert.Equal(t, "default", cfg.CurrentProfile.Name) |
| 42 | + assert.Equal(t, test.CrawlerAuth{UserID: "crawler-user", APIKey: "crawler-key"}, cfg.CrawlerAuth["default"]) |
| 43 | + assert.Contains(t, stdout.String(), "configured for profile: default") |
| 44 | +} |
| 45 | + |
| 46 | +func Test_runCrawlerCmd_UsesExplicitProfile(t *testing.T) { |
| 47 | + io, _, stdout, _ := iostreams.Test() |
| 48 | + io.SetStdoutTTY(true) |
| 49 | + |
| 50 | + cfg := test.NewConfigStubWithProfiles([]*config.Profile{ |
| 51 | + {Name: "target"}, |
| 52 | + {Name: "default", Default: true}, |
| 53 | + }) |
| 54 | + cfg.CurrentProfile.Name = "target" |
| 55 | + |
| 56 | + server := newCrawlerTestServer(t, "token-2", "crawler-user-2", "crawler-key-2") |
| 57 | + t.Cleanup(server.Close) |
| 58 | + |
| 59 | + err := runCrawlerCmd(&CrawlerOptions{ |
| 60 | + IO: io, |
| 61 | + config: cfg, |
| 62 | + NewDashboardClient: newDashboardTestClient(server), |
| 63 | + GetValidToken: func(client *dashboard.Client) (string, error) { |
| 64 | + return "token-2", nil |
| 65 | + }, |
| 66 | + }) |
| 67 | + require.NoError(t, err) |
| 68 | + |
| 69 | + assert.Equal(t, test.CrawlerAuth{UserID: "crawler-user-2", APIKey: "crawler-key-2"}, cfg.CrawlerAuth["target"]) |
| 70 | + _, hasDefault := cfg.CrawlerAuth["default"] |
| 71 | + assert.False(t, hasDefault) |
| 72 | + assert.Contains(t, stdout.String(), "configured for profile: target") |
| 73 | +} |
| 74 | + |
| 75 | +func newCrawlerTestServer(t *testing.T, token, userID, apiKey string) *httptest.Server { |
| 76 | + t.Helper() |
| 77 | + |
| 78 | + return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 79 | + require.Equal(t, "Bearer "+token, r.Header.Get("Authorization")) |
| 80 | + |
| 81 | + switch r.URL.Path { |
| 82 | + case "/1/crawler/me": |
| 83 | + _, err := fmt.Fprintf(w, `{"success":true,"data":{"id":%q}}`, userID) |
| 84 | + require.NoError(t, err) |
| 85 | + case "/1/crawler/api_key": |
| 86 | + _, err := fmt.Fprintf(w, `{"success":true,"data":{"apiKey":%q}}`, apiKey) |
| 87 | + require.NoError(t, err) |
| 88 | + default: |
| 89 | + t.Fatalf("unexpected path: %s", r.URL.Path) |
| 90 | + } |
| 91 | + })) |
| 92 | +} |
| 93 | + |
| 94 | +func newDashboardTestClient(server *httptest.Server) func(string) *dashboard.Client { |
| 95 | + return func(clientID string) *dashboard.Client { |
| 96 | + client := dashboard.NewClientWithHTTPClient(clientID, server.Client()) |
| 97 | + client.APIURL = server.URL |
| 98 | + return client |
| 99 | + } |
| 100 | +} |
0 commit comments