Skip to content

Commit 5234d20

Browse files
committed
restore and fix login tests
1 parent a48cf3a commit 5234d20

1 file changed

Lines changed: 23 additions & 12 deletions

File tree

cmd/src/login_test.go

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,34 @@ import (
1717
)
1818

1919
func TestLogin(t *testing.T) {
20-
check := func(t *testing.T, cfg *config) (output string, err error) {
20+
check := func(t *testing.T, cfg *config, endpointArgURL *url.URL) (output string, err error) {
2121
t.Helper()
2222

2323
var out bytes.Buffer
2424
err = loginCmd(context.Background(), loginParams{
25-
cfg: cfg,
26-
client: cfg.apiClient(nil, io.Discard),
27-
out: &out,
28-
oauthClient: fakeOAuthClient{startErr: fmt.Errorf("oauth unavailable")},
25+
cfg: cfg,
26+
client: cfg.apiClient(nil, io.Discard),
27+
out: &out,
28+
oauthClient: fakeOAuthClient{startErr: fmt.Errorf("oauth unavailable")},
29+
loginEndpointURL: endpointArgURL,
2930
})
3031
return strings.TrimSpace(out.String()), err
3132
}
3233

33-
t.Run("no access token", func(t *testing.T) {
34-
endpoint := &url.URL{Scheme: "https", Host: "sourcegraph.example.com"}
35-
out, err := check(t, &config{endpointURL: endpoint})
36-
if err != cmderrors.ExitCode1 {
34+
t.Run("different endpoint in config vs. arg", func(t *testing.T) {
35+
out, err := check(t, &config{endpointURL: &url.URL{Scheme: "https", Host: "example.com"}}, &url.URL{Scheme: "https", Host: "sourcegraph.example.com"})
36+
if err == nil {
37+
t.Fatal(err)
38+
}
39+
if !strings.Contains(out, "The configured endpoint is https://example.com, not https://sourcegraph.example.com.") {
40+
t.Errorf("got output %q, want configured endpoint error", out)
41+
}
42+
})
43+
44+
t.Run("no access token triggers oauth flow", func(t *testing.T) {
45+
u := &url.URL{Scheme: "https", Host: "example.com"}
46+
out, err := check(t, &config{endpointURL: u}, u)
47+
if err == nil {
3748
t.Fatal(err)
3849
}
3950
if !strings.Contains(out, "OAuth Device flow authentication failed:") {
@@ -43,7 +54,7 @@ func TestLogin(t *testing.T) {
4354

4455
t.Run("warning when using config file", func(t *testing.T) {
4556
endpoint := &url.URL{Scheme: "https", Host: "example.com"}
46-
out, err := check(t, &config{endpointURL: endpoint, configFilePath: "f"})
57+
out, err := check(t, &config{endpointURL: endpoint, configFilePath: "f"}, endpoint)
4758
if err != cmderrors.ExitCode1 {
4859
t.Fatal(err)
4960
}
@@ -62,7 +73,7 @@ func TestLogin(t *testing.T) {
6273
defer s.Close()
6374

6475
u, _ := url.ParseRequestURI(s.URL)
65-
out, err := check(t, &config{endpointURL: u, accessToken: "x"})
76+
out, err := check(t, &config{endpointURL: u, accessToken: "x"}, u)
6677
if err != cmderrors.ExitCode1 {
6778
t.Fatal(err)
6879
}
@@ -80,7 +91,7 @@ func TestLogin(t *testing.T) {
8091
defer s.Close()
8192

8293
u, _ := url.ParseRequestURI(s.URL)
83-
out, err := check(t, &config{endpointURL: u, accessToken: "x"})
94+
out, err := check(t, &config{endpointURL: u, accessToken: "x"}, u)
8495
if err != nil {
8596
t.Fatal(err)
8697
}

0 commit comments

Comments
 (0)