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

Function TestLogin_recordsAuthTelemetry

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

Source from the content-addressed store, hash-verified

704}
705
706func TestLogin_recordsAuthTelemetry(t *testing.T) {
707 // An interactive https login to github.com that configures gh as the credential helper should record a complete
708 // auth_invocation event.
709 ios, _, _, _ := iostreams.Test()
710 recorder := &telemetry.InvocationRecorderSpy{}
711
712 opts := &LoginOptions{
713 IO: ios,
714 Config: tinyConfig{},
715 HTTPClient: &http.Client{},
716 Hostname: "github.com",
717 Interactive: true,
718 GitProtocol: "https",
719 Telemetry: recorder,
720 Prompter: &prompter.PrompterMock{
721 SelectFunc: func(prompt, _ string, opts []string) (int, error) {
722 if prompt == "How would you like to authenticate GitHub CLI?" {
723 return prompter.IndexFor(opts, "Login with a web browser")
724 }
725 return -1, prompter.NoSuchPromptErr(prompt)
726 },
727 },
728 AuthFlow: func(_ *http.Client, _ string, _ *iostreams.IOStreams, _ string, _ []string, _ bool, _ browser.Browser, _, _ bool) (*authflow.AuthResult, error) {
729 return &authflow.AuthResult{Token: "ATOKEN", Username: "monalisa"}, nil
730 },
731 CredentialFlow: &GitCredentialFlow{
732 Prompter: &prompter.PrompterMock{
733 ConfirmFunc: func(prompt string, _ bool) (bool, error) {
734 return true, nil
735 },
736 },
737 HelperConfig: &gitcredentials.FakeHelperConfig{
738 SelfExecutablePath: "/path/to/gh",
739 Helpers: map[string]gitcredentials.Helper{},
740 },
741 },
742 }
743
744 require.NoError(t, Login(opts))
745
746 events := recorder.Events()
747 require.Len(t, events, 1)
748 assert.Equal(t, ghtelemetry.SAMPLE_ALL, recorder.LastSampleRate)
749 dims := events[0].Dimensions
750 assert.Equal(t, "auth_invocation", events[0].Type)
751 assert.Equal(t, "login", dims["auth_command"])
752 assert.Equal(t, "false", dims["expiring_requested"])
753 assert.Equal(t, "false", dims["expiring_setup_cred_helper_cancelled"])
754 assert.Equal(t, "https", dims["git_protocol"])
755 assert.Equal(t, "none", dims["git_credential_helper_before"])
756 assert.Equal(t, "ours", dims["git_credential_helper_after"])
757 assert.Equal(t, "false", dims["expiring_received"])
758 assert.Equal(t, "keyring", dims["storage_type"])
759 assert.Equal(t, "true", dims["auth_success"])
760}
761
762func TestLogin_recordsAuthTelemetryOnFailure(t *testing.T) {
763 // Telemetry must reflect how far the login got: an issued expiring token is recorded even when storing it fails,

Callers

nothing calls this directly

Calls 7

TestFunction · 0.92
IndexForFunction · 0.92
NoSuchPromptErrFunction · 0.92
LoginFunction · 0.85
EventsMethod · 0.80
EqualMethod · 0.80
LenMethod · 0.65

Tested by

no test coverage detected