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

Function TestLogin_recordsAuthTelemetryOnFailure

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

Source from the content-addressed store, hash-verified

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,
764 // and a stored credential counts as a successful sign-in even when a later side effect fails.
765 tests := []struct {
766 name string
767 config iconfig
768 gitProtocol string
769 helperErr error
770 setup func(t *testing.T, opts *LoginOptions, reg *httpmock.Registry)
771 wantSuccess string
772 }{
773 {
774 name: "storage fails after an expiring token is issued",
775 config: failingStorageConfig{tinyConfig{}},
776 gitProtocol: "https",
777 wantSuccess: "false",
778 },
779 {
780 name: "credential helper setup fails after storage",
781 config: tinyConfig{},
782 gitProtocol: "https",
783 helperErr: errors.New("helper setup failed"),
784 wantSuccess: "true",
785 },
786 {
787 name: "SSH key upload fails after storage",
788 config: tinyConfig{},
789 gitProtocol: "ssh",
790 setup: func(t *testing.T, opts *LoginOptions, reg *httpmock.Registry) {
791 dir := t.TempDir()
792 require.NoError(t, os.WriteFile(filepath.Join(dir, "id_ed25519.pub"), []byte("PUBKEY asdf"), 0600))
793 opts.sshContext = ssh.NewContextForTests(dir, "")
794 reg.Register(httpmock.REST("GET", "user/keys"), httpmock.StatusStringResponse(500, "{}"))
795 },
796 wantSuccess: "true",
797 },
798 }
799
800 for _, tt := range tests {
801 t.Run(tt.name, func(t *testing.T) {
802 ios, _, _, _ := iostreams.Test()
803 recorder := &telemetry.InvocationRecorderSpy{}
804 reg := &httpmock.Registry{}
805 defer reg.Verify(t)
806
807 opts := &LoginOptions{
808 IO: ios,
809 Config: tt.config,
810 HTTPClient: &http.Client{Transport: reg},
811 Hostname: "github.com",
812 Interactive: true,
813 GitProtocol: tt.gitProtocol,
814 Telemetry: recorder,
815 Prompter: &prompter.PrompterMock{
816 SelectFunc: func(prompt, _ string, opts []string) (int, error) {
817 switch prompt {
818 case "How would you like to authenticate GitHub CLI?":
819 return prompter.IndexFor(opts, "Login with a web browser")

Callers

nothing calls this directly

Calls 15

RegisterMethod · 0.95
VerifyMethod · 0.95
NewContextForTestsFunction · 0.92
RESTFunction · 0.92
StatusStringResponseFunction · 0.92
TestFunction · 0.92
IndexForFunction · 0.92
NoSuchPromptErrFunction · 0.92
LoginFunction · 0.85
WriteFileMethod · 0.80
JoinMethod · 0.80
setupMethod · 0.80

Tested by

no test coverage detected