From 540bda1a6b0bb4ecaaba75a56663a878a443ae34 Mon Sep 17 00:00:00 2001 From: William Martin Date: Fri, 17 May 2024 14:31:21 +0200 Subject: [PATCH] Test git credentials are configured in LoginFlow --- pkg/cmd/auth/shared/git_credential.go | 2 +- pkg/cmd/auth/shared/login_flow_test.go | 56 ++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/pkg/cmd/auth/shared/git_credential.go b/pkg/cmd/auth/shared/git_credential.go index ad552730e..e3136de43 100644 --- a/pkg/cmd/auth/shared/git_credential.go +++ b/pkg/cmd/auth/shared/git_credential.go @@ -15,7 +15,7 @@ type HelperConfig interface { type GitCredentialFlow struct { Prompter Prompt - HelperConfig *gitcredentials.HelperConfig + HelperConfig HelperConfig Updater *gitcredentials.Updater shouldSetup bool diff --git a/pkg/cmd/auth/shared/login_flow_test.go b/pkg/cmd/auth/shared/login_flow_test.go index fc58ea5ea..2f8f7c338 100644 --- a/pkg/cmd/auth/shared/login_flow_test.go +++ b/pkg/cmd/auth/shared/login_flow_test.go @@ -15,6 +15,7 @@ import ( "github.com/cli/cli/v2/pkg/iostreams" "github.com/cli/cli/v2/pkg/ssh" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) type tinyConfig map[string]string @@ -288,6 +289,61 @@ func TestLogin(t *testing.T) { } } +func TestAuthenticatingGitCredentials(t *testing.T) { + // Given we have no host or global credential helpers configured + // And given they have chosen https as their git protocol + // When they choose to authenticate git with their GitHub credentials + // Then gh is configured as their credential helper for that host + ios, _, _, _ := iostreams.Test() + + reg := &httpmock.Registry{} + defer reg.Verify(t) + reg.Register( + httpmock.REST("GET", "api/v3/"), + httpmock.ScopesResponder("repo,read:org")) + reg.Register( + httpmock.GraphQL(`query UserCurrent\b`), + httpmock.StringResponse(`{"data":{"viewer":{ "login": "monalisa" }}}`)) + + opts := &LoginOptions{ + IO: ios, + Config: tinyConfig{}, + HTTPClient: &http.Client{Transport: reg}, + Hostname: "example.com", + Interactive: true, + GitProtocol: "https", + Prompter: &prompter.PrompterMock{ + SelectFunc: func(prompt, _ string, opts []string) (int, error) { + if prompt == "How would you like to authenticate GitHub CLI?" { + return prompter.IndexFor(opts, "Paste an authentication token") + } + return -1, prompter.NoSuchPromptErr(prompt) + }, + AuthTokenFunc: func() (string, error) { + return "ATOKEN", nil + }, + }, + CredentialFlow: &GitCredentialFlow{ + Prompter: &prompter.PrompterMock{ + ConfirmFunc: func(prompt string, _ bool) (bool, error) { + return true, nil + }, + }, + HelperConfig: &gitcredentials.FakeHelperConfig{ + SelfExecutablePath: "/path/to/gh", + Helpers: map[string]gitcredentials.Helper{}, + }, + // Updater not required for this test as we will be setting gh as the helper + }, + } + + require.NoError(t, Login(opts)) + + helper, err := opts.CredentialFlow.HelperConfig.ConfiguredHelper("example.com") + require.NoError(t, err) + require.True(t, helper.IsOurs(), "expected gh to be the configured helper") +} + func Test_scopesSentence(t *testing.T) { type args struct { scopes []string