(t *testing.T)
| 907 | } |
| 908 | |
| 909 | func 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, |
| 911 | // and a stored credential counts as a successful refresh even when the credential-helper setup fails afterwards. |
| 912 | tests := []struct { |
| 913 | name string |
| 914 | interactive bool |
| 915 | breakStorage bool |
| 916 | helperErr error |
| 917 | wantSuccess string |
| 918 | }{ |
| 919 | { |
| 920 | name: "storage fails after an expiring token is issued", |
| 921 | breakStorage: true, |
| 922 | wantSuccess: "false", |
| 923 | }, |
| 924 | { |
| 925 | name: "credential helper setup fails after storage", |
| 926 | interactive: true, |
| 927 | helperErr: errors.New("helper setup failed"), |
| 928 | wantSuccess: "true", |
| 929 | }, |
| 930 | } |
| 931 | |
| 932 | for _, tt := range tests { |
| 933 | t.Run(tt.name, func(t *testing.T) { |
| 934 | cfg, _ := config.NewIsolatedTestConfig(t, "") |
| 935 | _, err := cfg.Authentication().Login("github.com", "test-user", "abc123", "https", false) |
| 936 | require.NoError(t, err) |
| 937 | |
| 938 | if tt.breakStorage { |
| 939 | // Point the config directory under a regular file so persisting the refreshed credential fails |
| 940 | // regardless of privileges. |
| 941 | file := filepath.Join(t.TempDir(), "not-a-dir") |
| 942 | require.NoError(t, os.WriteFile(file, nil, 0600)) |
| 943 | t.Setenv("GH_CONFIG_DIR", filepath.Join(file, "config")) |
| 944 | } |
| 945 | |
| 946 | recorder := &telemetry.InvocationRecorderSpy{} |
| 947 | ios, _, _, _ := iostreams.Test() |
| 948 | |
| 949 | opts := &RefreshOptions{ |
| 950 | IO: ios, |
| 951 | Hostname: "github.com", |
| 952 | Interactive: tt.interactive, |
| 953 | Telemetry: recorder, |
| 954 | ResetScopes: true, |
| 955 | Config: func() (gh.Config, error) { |
| 956 | return cfg, nil |
| 957 | }, |
| 958 | AuthFlow: func(_ *http.Client, _ *iostreams.IOStreams, _ string, _ []string, _, _, _ bool) (*authflow.AuthResult, error) { |
| 959 | return &authflow.AuthResult{ |
| 960 | Token: "xyz456", |
| 961 | Username: "test-user", |
| 962 | Refreshable: &gh.Credential{Token: "xyz456", RefreshToken: "rt456"}, |
| 963 | }, nil |
| 964 | }, |
| 965 | PlainHttpClient: func() (*http.Client, error) { |
| 966 | return &http.Client{Transport: &httpmock.Registry{}}, nil |
nothing calls this directly
no test coverage detected