MCPcopy Create free account
hub / github.com/cli/cli / TestAuthenticatingGitCredentials

Function TestAuthenticatingGitCredentials

pkg/cmd/auth/shared/login_flow_test.go:593–646  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

591}
592
593func TestAuthenticatingGitCredentials(t *testing.T) {
594 // Given we have no host or global credential helpers configured
595 // And given they have chosen https as their git protocol
596 // When they choose to authenticate git with their GitHub credentials
597 // Then gh is configured as their credential helper for that host
598 ios, _, _, _ := iostreams.Test()
599
600 reg := &httpmock.Registry{}
601 defer reg.Verify(t)
602 reg.Register(
603 httpmock.REST("GET", "api/v3/"),
604 httpmock.ScopesResponder("repo,read:org"))
605 reg.Register(
606 httpmock.GraphQL(`query UserCurrent\b`),
607 httpmock.StringResponse(`{"data":{"viewer":{ "login": "monalisa" }}}`))
608
609 opts := &LoginOptions{
610 IO: ios,
611 Config: tinyConfig{},
612 HTTPClient: &http.Client{Transport: reg},
613 Hostname: "example.com",
614 Interactive: true,
615 GitProtocol: "https",
616 Prompter: &prompter.PrompterMock{
617 SelectFunc: func(prompt, _ string, opts []string) (int, error) {
618 if prompt == "How would you like to authenticate GitHub CLI?" {
619 return prompter.IndexFor(opts, "Paste an authentication token")
620 }
621 return -1, prompter.NoSuchPromptErr(prompt)
622 },
623 AuthTokenFunc: func() (string, error) {
624 return "ATOKEN", nil
625 },
626 },
627 CredentialFlow: &GitCredentialFlow{
628 Prompter: &prompter.PrompterMock{
629 ConfirmFunc: func(prompt string, _ bool) (bool, error) {
630 return true, nil
631 },
632 },
633 HelperConfig: &gitcredentials.FakeHelperConfig{
634 SelfExecutablePath: "/path/to/gh",
635 Helpers: map[string]gitcredentials.Helper{},
636 },
637 // Updater not required for this test as we will be setting gh as the helper
638 },
639 }
640
641 require.NoError(t, Login(opts))
642
643 helper, err := opts.CredentialFlow.HelperConfig.ConfiguredHelper("example.com")
644 require.NoError(t, err)
645 require.True(t, helper.IsOurs(), "expected gh to be the configured helper")
646}
647
648func TestLogin_pastedTokenIgnoresConfiguredExpiringPreference(t *testing.T) {
649 // A config-derived expiring preference must not leak into a login where the user pastes a token, which can never

Callers

nothing calls this directly

Calls 12

VerifyMethod · 0.95
RegisterMethod · 0.95
TestFunction · 0.92
RESTFunction · 0.92
ScopesResponderFunction · 0.92
GraphQLFunction · 0.92
StringResponseFunction · 0.92
IndexForFunction · 0.92
NoSuchPromptErrFunction · 0.92
LoginFunction · 0.85
IsOursMethod · 0.80
ConfiguredHelperMethod · 0.65

Tested by

no test coverage detected