Test git credentials are configured in LoginFlow

This commit is contained in:
William Martin 2024-05-17 14:31:21 +02:00
parent ba1afa2c5d
commit 540bda1a6b
2 changed files with 57 additions and 1 deletions

View file

@ -15,7 +15,7 @@ type HelperConfig interface {
type GitCredentialFlow struct {
Prompter Prompt
HelperConfig *gitcredentials.HelperConfig
HelperConfig HelperConfig
Updater *gitcredentials.Updater
shouldSetup bool

View file

@ -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