From c4fcf9ba1a18a882004b68b381abef690f49f235 Mon Sep 17 00:00:00 2001 From: William Martin Date: Wed, 15 Nov 2023 15:53:39 +0100 Subject: [PATCH] Add test to ensure login command allows multiple users --- pkg/cmd/auth/login/login.go | 13 -------- pkg/cmd/auth/login/login_test.go | 55 +++++++++++++++++--------------- 2 files changed, 29 insertions(+), 39 deletions(-) diff --git a/pkg/cmd/auth/login/login.go b/pkg/cmd/auth/login/login.go index fdfdfc244..16d0581a1 100644 --- a/pkg/cmd/auth/login/login.go +++ b/pkg/cmd/auth/login/login.go @@ -182,19 +182,6 @@ func loginRun(opts *LoginOptions) error { return loginErr } - existingToken, _ := authCfg.Token(hostname) - if existingToken != "" && opts.Interactive { - if err := shared.HasMinimumScopes(httpClient, hostname, existingToken); err == nil { - keepGoing, err := opts.Prompter.Confirm(fmt.Sprintf("You're already logged into %s. Do you want to re-authenticate?", hostname), false) - if err != nil { - return err - } - if !keepGoing { - return nil - } - } - } - return shared.Login(&shared.LoginOptions{ IO: opts.IO, Config: authCfg, diff --git a/pkg/cmd/auth/login/login_test.go b/pkg/cmd/auth/login/login_test.go index 8fa5df58e..9a909426c 100644 --- a/pkg/cmd/auth/login/login_test.go +++ b/pkg/cmd/auth/login/login_test.go @@ -18,6 +18,7 @@ import ( "github.com/cli/cli/v2/pkg/iostreams" "github.com/google/shlex" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func stubHomeDir(t *testing.T, dir string) { @@ -401,6 +402,34 @@ func Test_loginRun_nontty(t *testing.T) { wantHosts: "github.com:\n user: monalisa\n users:\n monalisa:\n", wantSecureToken: "abc123", }, + { + name: "given we are already logged in, a new user is added to the config", + opts: &LoginOptions{ + Hostname: "github.com", + Token: "newUserToken", + }, + cfgStubs: func(c *config.ConfigMock) { + _, err := c.Authentication().Login("github.com", "monalisa", "abc123", "https", false) + require.NoError(t, err) + }, + httpStubs: func(reg *httpmock.Registry) { + reg.Register(httpmock.REST("GET", ""), httpmock.ScopesResponder("repo,read:org")) + reg.Register( + httpmock.GraphQL(`query UserCurrent\b`), + httpmock.StringResponse(`{"data":{"viewer":{"login":"newUser"}}}`)) + }, + wantHosts: heredoc.Doc(` + github.com: + users: + monalisa: + oauth_token: abc123 + git_protocol: https + newUser: + user: newUser + git_protocol: https + `), + wantSecureToken: "newUserToken", + }, } for _, tt := range tests { @@ -466,32 +495,6 @@ func Test_loginRun_Survey(t *testing.T) { wantErrOut *regexp.Regexp wantSecureToken string }{ - { - name: "already authenticated", - opts: &LoginOptions{ - Interactive: true, - }, - cfgStubs: func(c *config.ConfigMock) { - authCfg := c.Authentication() - authCfg.SetToken("ghi789", "oauth_token") - c.AuthenticationFunc = func() *config.AuthConfig { - return authCfg - } - }, - httpStubs: func(reg *httpmock.Registry) { - reg.Register(httpmock.REST("GET", ""), httpmock.ScopesResponder("repo,read:org")) - }, - prompterStubs: func(pm *prompter.PrompterMock) { - pm.SelectFunc = func(prompt, _ string, opts []string) (int, error) { - if prompt == "What account do you want to log into?" { - return prompter.IndexFor(opts, "GitHub.com") - } - return -1, prompter.NoSuchPromptErr(prompt) - } - }, - wantHosts: "", - wantErrOut: nil, - }, { name: "hostname set", opts: &LoginOptions{