From 81caade918825db20a3bd76a5f1fa1ecb1f8c0a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Montes?= Date: Tue, 22 Sep 2020 12:08:16 +0200 Subject: [PATCH 1/3] Access token tip message using hostname --- pkg/cmd/auth/login/login.go | 14 +++++++++++--- pkg/cmd/auth/login/login_test.go | 27 +++++++++++++++++++-------- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/pkg/cmd/auth/login/login.go b/pkg/cmd/auth/login/login.go index b6d2a2f99..a3691f56c 100644 --- a/pkg/cmd/auth/login/login.go +++ b/pkg/cmd/auth/login/login.go @@ -228,9 +228,7 @@ func loginRun(opts *LoginOptions) error { } } else { fmt.Fprintln(opts.IO.ErrOut) - fmt.Fprintln(opts.IO.ErrOut, heredoc.Doc(` - Tip: you can generate a Personal Access Token here https://github.com/settings/tokens - The minimum required scopes are 'repo' and 'read:org'.`)) + fmt.Fprintln(opts.IO.ErrOut, heredoc.Doc(getAccessTokenTip(hostname))) var token string err := prompt.SurveyAskOne(&survey.Password{ Message: "Paste your authentication token:", @@ -313,3 +311,13 @@ func hostnameValidator(v interface{}) error { } return nil } + +func getAccessTokenTip(hostname string) string { + ghHostname := hostname + if ghHostname == "" { + ghHostname = "github.com" + } + return fmt.Sprintf(` + Tip: you can generate a Personal Access Token here https://%s/settings/tokens + The minimum required scopes are 'repo' and 'read:org'.`, ghHostname) +} diff --git a/pkg/cmd/auth/login/login_test.go b/pkg/cmd/auth/login/login_test.go index 7166eac92..53439ef8e 100644 --- a/pkg/cmd/auth/login/login_test.go +++ b/pkg/cmd/auth/login/login_test.go @@ -279,12 +279,13 @@ func Test_loginRun_nontty(t *testing.T) { func Test_loginRun_Survey(t *testing.T) { tests := []struct { - name string - opts *LoginOptions - httpStubs func(*httpmock.Registry) - askStubs func(*prompt.AskStubber) - wantHosts string - cfg func(config.Config) + name string + opts *LoginOptions + httpStubs func(*httpmock.Registry) + askStubs func(*prompt.AskStubber) + wantHosts string + wantErrOut *regexp.Regexp + cfg func(config.Config) }{ { name: "already authenticated", @@ -304,7 +305,8 @@ func Test_loginRun_Survey(t *testing.T) { as.StubOne(0) // host type github.com as.StubOne(false) // do not continue }, - wantHosts: "", // nothing should have been written to hosts + wantHosts: "", // nothing should have been written to hosts + wantErrOut: regexp.MustCompile("Logging into github.com"), }, { name: "hostname set", @@ -324,6 +326,7 @@ func Test_loginRun_Survey(t *testing.T) { httpmock.GraphQL(`query UserCurrent\b`), httpmock.StringResponse(`{"data":{"viewer":{"login":"jillv"}}}`)) }, + wantErrOut: regexp.MustCompile("Tip: you can generate a Personal Access Token here https://rebecca.chambers/settings/tokens"), }, { name: "choose enterprise", @@ -344,6 +347,7 @@ func Test_loginRun_Survey(t *testing.T) { httpmock.GraphQL(`query UserCurrent\b`), httpmock.StringResponse(`{"data":{"viewer":{"login":"jillv"}}}`)) }, + wantErrOut: regexp.MustCompile("Tip: you can generate a Personal Access Token here https://brad.vickers/settings/tokens"), }, { name: "choose github.com", @@ -357,6 +361,7 @@ func Test_loginRun_Survey(t *testing.T) { as.StubOne("def456") // auth token as.StubOne("HTTPS") // git_protocol }, + wantErrOut: regexp.MustCompile("Tip: you can generate a Personal Access Token here https://github.com/settings/tokens"), }, { name: "sets git_protocol", @@ -370,6 +375,7 @@ func Test_loginRun_Survey(t *testing.T) { as.StubOne("def456") // auth token as.StubOne("SSH") // git_protocol }, + wantErrOut: regexp.MustCompile("Tip: you can generate a Personal Access Token here https://github.com/settings/tokens"), }, // TODO how to test browser auth? } @@ -378,7 +384,7 @@ func Test_loginRun_Survey(t *testing.T) { if tt.opts == nil { tt.opts = &LoginOptions{} } - io, _, _, _ := iostreams.Test() + io, _, _, stderr := iostreams.Test() io.SetStdinTTY(true) io.SetStderrTTY(true) @@ -430,6 +436,11 @@ func Test_loginRun_Survey(t *testing.T) { } assert.Equal(t, tt.wantHosts, hostsBuf.String()) + if tt.wantErrOut == nil { + assert.Equal(t, "", stderr.String()) + } else { + assert.True(t, tt.wantErrOut.MatchString(stderr.String())) + } reg.Verify(t) }) } From 793e866850d8c7b30bce402b697493d5fccaf637 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Montes?= Date: Tue, 22 Sep 2020 17:50:35 +0200 Subject: [PATCH 2/3] Using assert.Regexp for testing stderr's content --- pkg/cmd/auth/login/login_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cmd/auth/login/login_test.go b/pkg/cmd/auth/login/login_test.go index 53439ef8e..0abfeb7e3 100644 --- a/pkg/cmd/auth/login/login_test.go +++ b/pkg/cmd/auth/login/login_test.go @@ -439,7 +439,7 @@ func Test_loginRun_Survey(t *testing.T) { if tt.wantErrOut == nil { assert.Equal(t, "", stderr.String()) } else { - assert.True(t, tt.wantErrOut.MatchString(stderr.String())) + assert.Regexp(t, tt.wantErrOut, stderr.String()) } reg.Verify(t) }) From cacf0157b31d95ee3cb7cd7baf65c45b84d3655d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Montes?= Date: Tue, 22 Sep 2020 18:22:29 +0200 Subject: [PATCH 3/3] Using ghinstance.OverridableDefault() instead of a harcoded hostname --- pkg/cmd/auth/login/login.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cmd/auth/login/login.go b/pkg/cmd/auth/login/login.go index a3691f56c..30c60bdc0 100644 --- a/pkg/cmd/auth/login/login.go +++ b/pkg/cmd/auth/login/login.go @@ -315,7 +315,7 @@ func hostnameValidator(v interface{}) error { func getAccessTokenTip(hostname string) string { ghHostname := hostname if ghHostname == "" { - ghHostname = "github.com" + ghHostname = ghinstance.OverridableDefault() } return fmt.Sprintf(` Tip: you can generate a Personal Access Token here https://%s/settings/tokens