|
| 1 | +package current |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + "github.com/zalando/go-keyring" |
| 14 | + |
| 15 | + "github.com/algolia/cli/api/dashboard" |
| 16 | + "github.com/algolia/cli/pkg/auth" |
| 17 | + "github.com/algolia/cli/pkg/cmdutil" |
| 18 | + "github.com/algolia/cli/pkg/config" |
| 19 | + "github.com/algolia/cli/pkg/iostreams" |
| 20 | + "github.com/algolia/cli/test" |
| 21 | +) |
| 22 | + |
| 23 | +func seedToken(t *testing.T) { |
| 24 | + t.Helper() |
| 25 | + keyring.MockInit() |
| 26 | + require.NoError(t, auth.SaveToken(&dashboard.OAuthTokenResponse{ |
| 27 | + AccessToken: "test-token", |
| 28 | + ExpiresIn: 3600, |
| 29 | + CreatedAt: time.Now().Unix(), |
| 30 | + })) |
| 31 | +} |
| 32 | + |
| 33 | +func newServer(t *testing.T, status int) *httptest.Server { |
| 34 | + t.Helper() |
| 35 | + mux := http.NewServeMux() |
| 36 | + mux.HandleFunc("/1/application/APP1", func(w http.ResponseWriter, _ *http.Request) { |
| 37 | + if status != http.StatusOK { |
| 38 | + w.WriteHeader(status) |
| 39 | + return |
| 40 | + } |
| 41 | + require.NoError(t, json.NewEncoder(w).Encode(dashboard.SingleApplicationResponse{ |
| 42 | + Data: dashboard.ApplicationResource{ |
| 43 | + ID: "APP1", |
| 44 | + Type: "application", |
| 45 | + Attributes: dashboard.ApplicationAttributes{ |
| 46 | + ApplicationID: "APP1", |
| 47 | + Name: "My App", |
| 48 | + Plan: dashboard.ApplicationPlan{Label: "Grow Plus"}, |
| 49 | + }, |
| 50 | + }, |
| 51 | + })) |
| 52 | + }) |
| 53 | + return httptest.NewServer(mux) |
| 54 | +} |
| 55 | + |
| 56 | +func newOpts( |
| 57 | + t *testing.T, |
| 58 | + srv *httptest.Server, |
| 59 | + cfg *test.ConfigStub, |
| 60 | + output string, |
| 61 | + signedIn bool, |
| 62 | +) (*CurrentOptions, *bytes.Buffer) { |
| 63 | + t.Helper() |
| 64 | + if signedIn { |
| 65 | + seedToken(t) |
| 66 | + } else { |
| 67 | + keyring.MockInit() |
| 68 | + auth.ClearToken() |
| 69 | + } |
| 70 | + |
| 71 | + io, _, stdout, _ := iostreams.Test() |
| 72 | + pf := cmdutil.NewPrintFlags() |
| 73 | + *pf.OutputFormat = output |
| 74 | + pf.OutputFlagSpecified = func() bool { return output != "" } |
| 75 | + |
| 76 | + opts := &CurrentOptions{ |
| 77 | + IO: io, |
| 78 | + Config: cfg, |
| 79 | + PrintFlags: pf, |
| 80 | + NewDashboardClient: func(string) *dashboard.Client { |
| 81 | + c := dashboard.NewClientWithHTTPClient("test", srv.Client()) |
| 82 | + c.APIURL = srv.URL |
| 83 | + return c |
| 84 | + }, |
| 85 | + } |
| 86 | + return opts, stdout |
| 87 | +} |
| 88 | + |
| 89 | +func configWithApp(appID, alias string) *test.ConfigStub { |
| 90 | + cfg := &test.ConfigStub{ |
| 91 | + CurrentProfile: config.Profile{ApplicationID: appID}, |
| 92 | + } |
| 93 | + if alias != "" { |
| 94 | + cfg.SavedApps = map[string]test.SavedApplication{ |
| 95 | + appID: {Alias: alias}, |
| 96 | + } |
| 97 | + } |
| 98 | + return cfg |
| 99 | +} |
| 100 | + |
| 101 | +func Test_runCurrentCmd(t *testing.T) { |
| 102 | + srv := newServer(t, http.StatusOK) |
| 103 | + defer srv.Close() |
| 104 | + |
| 105 | + opts, out := newOpts(t, srv, configWithApp("APP1", "my-alias"), "", true) |
| 106 | + require.NoError(t, runCurrentCmd(opts)) |
| 107 | + |
| 108 | + got := out.String() |
| 109 | + assert.Contains(t, got, "APP1") |
| 110 | + assert.Contains(t, got, "my-alias") |
| 111 | + assert.Contains(t, got, "My App") |
| 112 | + assert.Contains(t, got, "Grow Plus") |
| 113 | +} |
| 114 | + |
| 115 | +func Test_runCurrentCmd_notConfigured(t *testing.T) { |
| 116 | + srv := newServer(t, http.StatusOK) |
| 117 | + defer srv.Close() |
| 118 | + |
| 119 | + opts, _ := newOpts(t, srv, configWithApp("", ""), "", true) |
| 120 | + err := runCurrentCmd(opts) |
| 121 | + require.Error(t, err) |
| 122 | + assert.Contains(t, err.Error(), "no current application configured") |
| 123 | +} |
| 124 | + |
| 125 | +func Test_runCurrentCmd_apiFailure(t *testing.T) { |
| 126 | + srv := newServer(t, http.StatusInternalServerError) |
| 127 | + defer srv.Close() |
| 128 | + |
| 129 | + opts, out := newOpts(t, srv, configWithApp("APP1", "my-alias"), "", true) |
| 130 | + require.NoError(t, runCurrentCmd(opts)) |
| 131 | + |
| 132 | + got := out.String() |
| 133 | + assert.Contains(t, got, "APP1") |
| 134 | + assert.Contains(t, got, "my-alias") |
| 135 | + assert.NotContains(t, got, "My App") |
| 136 | + assert.Contains(t, got, "Couldn't fetch the application name and plan") |
| 137 | +} |
| 138 | + |
| 139 | +func Test_runCurrentCmd_signedOut(t *testing.T) { |
| 140 | + hit := false |
| 141 | + srv := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) { |
| 142 | + hit = true |
| 143 | + })) |
| 144 | + defer srv.Close() |
| 145 | + |
| 146 | + opts, out := newOpts(t, srv, configWithApp("APP1", "my-alias"), "", false) |
| 147 | + require.NoError(t, runCurrentCmd(opts)) |
| 148 | + |
| 149 | + got := out.String() |
| 150 | + assert.Contains(t, got, "APP1") |
| 151 | + assert.Contains(t, got, "my-alias") |
| 152 | + assert.NotContains(t, got, "My App") |
| 153 | + assert.Contains(t, got, `Sign in with "algolia auth login"`) |
| 154 | + assert.False(t, hit, "expected no API/login call when signed out") |
| 155 | +} |
| 156 | + |
| 157 | +func Test_runCurrentCmd_outputJSON(t *testing.T) { |
| 158 | + srv := newServer(t, http.StatusOK) |
| 159 | + defer srv.Close() |
| 160 | + |
| 161 | + opts, out := newOpts(t, srv, configWithApp("APP1", "my-alias"), "json", true) |
| 162 | + require.NoError(t, runCurrentCmd(opts)) |
| 163 | + |
| 164 | + got := out.String() |
| 165 | + assert.Contains(t, got, `"id":"APP1"`) |
| 166 | + assert.Contains(t, got, `"alias":"my-alias"`) |
| 167 | + assert.Contains(t, got, `"name":"My App"`) |
| 168 | + assert.Contains(t, got, `"plan":"Grow Plus"`) |
| 169 | +} |
0 commit comments