| 704 | } |
| 705 | |
| 706 | func 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 | |
| 762 | func 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, |