|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | +) |
| 12 | + |
| 13 | +func TestLogin(t *testing.T) { |
| 14 | + check := func(t *testing.T, cfg *config, endpointArg string) (output string, err error) { |
| 15 | + t.Helper() |
| 16 | + |
| 17 | + var out bytes.Buffer |
| 18 | + err = loginCmd(context.Background(), cfg, endpointArg, &out) |
| 19 | + return strings.TrimSpace(out.String()), err |
| 20 | + } |
| 21 | + |
| 22 | + t.Run("different endpoint in config vs. arg", func(t *testing.T) { |
| 23 | + out, err := check(t, &config{Endpoint: "https://example.com"}, "https://sourcegraph.example.com") |
| 24 | + if err != exitCode1 { |
| 25 | + t.Fatal(err) |
| 26 | + } |
| 27 | + wantOut := "❌ Problem: No access token is configured.\n\n🛠 To fix: Create an access token at https://sourcegraph.example.com/user/settings/tokens, then set the following environment variables:\n\n SRC_ENDPOINT=https://sourcegraph.example.com\n SRC_ACCESS_TOKEN=(the access token you just created)\n\n To verify that it's working, run this command again." |
| 28 | + if out != wantOut { |
| 29 | + t.Errorf("got output %q, want %q", out, wantOut) |
| 30 | + } |
| 31 | + }) |
| 32 | + |
| 33 | + t.Run("no access token", func(t *testing.T) { |
| 34 | + out, err := check(t, &config{Endpoint: "https://example.com"}, "https://sourcegraph.example.com") |
| 35 | + if err != exitCode1 { |
| 36 | + t.Fatal(err) |
| 37 | + } |
| 38 | + wantOut := "❌ Problem: No access token is configured.\n\n🛠 To fix: Create an access token at https://sourcegraph.example.com/user/settings/tokens, then set the following environment variables:\n\n SRC_ENDPOINT=https://sourcegraph.example.com\n SRC_ACCESS_TOKEN=(the access token you just created)\n\n To verify that it's working, run this command again." |
| 39 | + if out != wantOut { |
| 40 | + t.Errorf("got output %q, want %q", out, wantOut) |
| 41 | + } |
| 42 | + }) |
| 43 | + |
| 44 | + t.Run("warning when using config file", func(t *testing.T) { |
| 45 | + out, err := check(t, &config{Endpoint: "https://example.com", ConfigFilePath: "f"}, "https://example.com") |
| 46 | + if err != exitCode1 { |
| 47 | + t.Fatal(err) |
| 48 | + } |
| 49 | + wantOut := "⚠️ Warning: Configuring src with a JSON file is deprecated. Please migrate to using the env vars SRC_ENDPOINT and SRC_ACCESS_TOKEN instead, and then remove f. See https://github.com/sourcegraph/src-cli#readme for more information.\n\n❌ Problem: No access token is configured.\n\n🛠 To fix: Create an access token at https://example.com/user/settings/tokens, then set the following environment variables:\n\n SRC_ENDPOINT=https://example.com\n SRC_ACCESS_TOKEN=(the access token you just created)\n\n To verify that it's working, run this command again." |
| 50 | + if out != wantOut { |
| 51 | + t.Errorf("got output %q, want %q", out, wantOut) |
| 52 | + } |
| 53 | + }) |
| 54 | + |
| 55 | + t.Run("invalid access token", func(t *testing.T) { |
| 56 | + // Dummy HTTP server to return HTTP 401/403. |
| 57 | + s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 58 | + http.Error(w, "", http.StatusUnauthorized) |
| 59 | + })) |
| 60 | + defer s.Close() |
| 61 | + |
| 62 | + endpoint := s.URL |
| 63 | + out, err := check(t, &config{Endpoint: endpoint, AccessToken: "x"}, endpoint) |
| 64 | + if err != exitCode1 { |
| 65 | + t.Fatal(err) |
| 66 | + } |
| 67 | + wantOut := "❌ Problem: Invalid access token.\n\n🛠 To fix: Create an access token at $ENDPOINT/user/settings/tokens, then set the following environment variables:\n\n SRC_ENDPOINT=$ENDPOINT\n SRC_ACCESS_TOKEN=(the access token you just created)\n\n To verify that it's working, run this command again.\n\n (If you need to supply custom HTTP request headers, see information about SRC_HEADER_* env vars at https://github.com/sourcegraph/src-cli/blob/main/AUTH_PROXY.md.)" |
| 68 | + wantOut = strings.Replace(wantOut, "$ENDPOINT", endpoint, -1) |
| 69 | + if out != wantOut { |
| 70 | + t.Errorf("got output %q, want %q", out, wantOut) |
| 71 | + } |
| 72 | + }) |
| 73 | + |
| 74 | + t.Run("valid", func(t *testing.T) { |
| 75 | + // Dummy HTTP server to return JSON response with currentUser. |
| 76 | + s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 77 | + fmt.Fprintln(w, `{"data":{"currentUser":{"username":"alice"}}}`) |
| 78 | + })) |
| 79 | + defer s.Close() |
| 80 | + |
| 81 | + endpoint := s.URL |
| 82 | + out, err := check(t, &config{Endpoint: endpoint, AccessToken: "x"}, endpoint) |
| 83 | + if err != nil { |
| 84 | + t.Fatal(err) |
| 85 | + } |
| 86 | + wantOut := "✔️ Authenticated as alice on $ENDPOINT" |
| 87 | + wantOut = strings.Replace(wantOut, "$ENDPOINT", endpoint, -1) |
| 88 | + if out != wantOut { |
| 89 | + t.Errorf("got output %q, want %q", out, wantOut) |
| 90 | + } |
| 91 | + }) |
| 92 | +} |
0 commit comments