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

Function Test_refreshRun_recordsTelemetry

pkg/cmd/auth/refresh/refresh_test.go:858–907  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

856}
857
858func Test_refreshRun_recordsTelemetry(t *testing.T) {
859 cfg, _ := config.NewIsolatedTestConfig(t, "")
860 _, err := cfg.Authentication().Login("github.com", "test-user", "abc123", "https", false)
861 require.NoError(t, err)
862
863 recorder := &telemetry.InvocationRecorderSpy{}
864 ios, _, _, _ := iostreams.Test()
865 ios.SetStdinTTY(false)
866 ios.SetStdoutTTY(false)
867
868 opts := &RefreshOptions{
869 IO: ios,
870 Hostname: "github.com",
871 Telemetry: recorder,
872 ResetScopes: true,
873 Config: func() (gh.Config, error) {
874 return cfg, nil
875 },
876 AuthFlow: func(_ *http.Client, _ *iostreams.IOStreams, _ string, _ []string, _, _, _ bool) (*authflow.AuthResult, error) {
877 return &authflow.AuthResult{Token: "xyz456", Username: "test-user"}, nil
878 },
879 PlainHttpClient: func() (*http.Client, error) {
880 return &http.Client{Transport: &httpmock.Registry{}}, nil
881 },
882 Prompter: &prompter.PrompterMock{},
883 helperConfig: &gitcredentials.FakeHelperConfig{
884 SelfExecutablePath: "/path/to/gh",
885 Helpers: map[string]gitcredentials.Helper{},
886 },
887 }
888
889 require.NoError(t, refreshRun(opts))
890
891 events := recorder.Events()
892 require.Len(t, events, 1)
893 assert.Equal(t, ghtelemetry.SAMPLE_ALL, recorder.LastSampleRate)
894 dims := events[0].Dimensions
895 assert.Equal(t, "auth_invocation", events[0].Type)
896 assert.Equal(t, "refresh", dims["auth_command"])
897 assert.Equal(t, "false", dims["expiring_requested"])
898 assert.Equal(t, "false", dims["expiring_setup_cred_helper_cancelled"])
899 assert.Equal(t, "https", dims["git_protocol"])
900 assert.Equal(t, "false", dims["expiring_received"])
901 assert.Equal(t, "true", dims["auth_success"])
902 // Helper telemetry is recorded even for this non-interactive refresh. No helper is configured and no setup runs,
903 // so before and after are both "none".
904 assert.Equal(t, "none", dims["git_credential_helper_before"])
905 assert.Equal(t, "none", dims["git_credential_helper_after"])
906 assert.Contains(t, []string{"keyring", "plaintext"}, dims["storage_type"])
907}
908
909func Test_refreshRun_recordsTelemetryOnFailure(t *testing.T) {
910 // Telemetry must reflect how far the refresh got: an issued expiring token is recorded even when storing it fails,

Callers

nothing calls this directly

Calls 11

NewIsolatedTestConfigFunction · 0.92
TestFunction · 0.92
refreshRunFunction · 0.85
SetStdinTTYMethod · 0.80
SetStdoutTTYMethod · 0.80
EventsMethod · 0.80
EqualMethod · 0.80
ContainsMethod · 0.80
LoginMethod · 0.65
AuthenticationMethod · 0.65
LenMethod · 0.65

Tested by

no test coverage detected