|
| 1 | +package refresh |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "os" |
| 6 | + "regexp" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/cli/cli/internal/config" |
| 10 | + "github.com/cli/cli/pkg/cmdutil" |
| 11 | + "github.com/cli/cli/pkg/httpmock" |
| 12 | + "github.com/cli/cli/pkg/iostreams" |
| 13 | + "github.com/cli/cli/pkg/prompt" |
| 14 | + "github.com/google/shlex" |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | +) |
| 17 | + |
| 18 | +func Test_NewCmdRefresh(t *testing.T) { |
| 19 | + tests := []struct { |
| 20 | + name string |
| 21 | + cli string |
| 22 | + wants RefreshOptions |
| 23 | + }{ |
| 24 | + { |
| 25 | + name: "no arguments", |
| 26 | + wants: RefreshOptions{ |
| 27 | + Hostname: "", |
| 28 | + Scopes: []string{}, |
| 29 | + }, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "hostname", |
| 33 | + cli: "-h aline.cedrac", |
| 34 | + wants: RefreshOptions{ |
| 35 | + Hostname: "aline.cedrac", |
| 36 | + Scopes: []string{}, |
| 37 | + }, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "one scope", |
| 41 | + cli: "--scopes repo:invite", |
| 42 | + wants: RefreshOptions{ |
| 43 | + Scopes: []string{"repo:invite"}, |
| 44 | + }, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "scopes", |
| 48 | + cli: "--scopes repo:invite,read:public_key", |
| 49 | + wants: RefreshOptions{ |
| 50 | + Scopes: []string{"repo:invite", "read:public_key"}, |
| 51 | + }, |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + for _, tt := range tests { |
| 56 | + t.Run(tt.name, func(t *testing.T) { |
| 57 | + io, _, _, _ := iostreams.Test() |
| 58 | + f := &cmdutil.Factory{ |
| 59 | + IOStreams: io, |
| 60 | + } |
| 61 | + |
| 62 | + argv, err := shlex.Split(tt.cli) |
| 63 | + assert.NoError(t, err) |
| 64 | + |
| 65 | + var gotOpts *RefreshOptions |
| 66 | + cmd := NewCmdRefresh(f, func(opts *RefreshOptions) error { |
| 67 | + gotOpts = opts |
| 68 | + return nil |
| 69 | + }) |
| 70 | + // TODO cobra hack-around |
| 71 | + cmd.Flags().BoolP("help", "x", false, "") |
| 72 | + |
| 73 | + cmd.SetArgs(argv) |
| 74 | + cmd.SetIn(&bytes.Buffer{}) |
| 75 | + cmd.SetOut(&bytes.Buffer{}) |
| 76 | + cmd.SetErr(&bytes.Buffer{}) |
| 77 | + |
| 78 | + _, err = cmd.ExecuteC() |
| 79 | + assert.NoError(t, err) |
| 80 | + assert.Equal(t, tt.wants.Hostname, gotOpts.Hostname) |
| 81 | + assert.Equal(t, tt.wants.Scopes, gotOpts.Scopes) |
| 82 | + }) |
| 83 | + |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +type authArgs struct { |
| 88 | + hostname string |
| 89 | + scopes []string |
| 90 | +} |
| 91 | + |
| 92 | +func Test_refreshRun(t *testing.T) { |
| 93 | + tests := []struct { |
| 94 | + name string |
| 95 | + opts *RefreshOptions |
| 96 | + askStubs func(*prompt.AskStubber) |
| 97 | + cfgHosts []string |
| 98 | + wantErr *regexp.Regexp |
| 99 | + ghtoken string |
| 100 | + nontty bool |
| 101 | + wantAuthArgs authArgs |
| 102 | + }{ |
| 103 | + { |
| 104 | + name: "GITHUB_TOKEN set", |
| 105 | + opts: &RefreshOptions{}, |
| 106 | + ghtoken: "abc123", |
| 107 | + wantErr: regexp.MustCompile(`GITHUB_TOKEN is present in your environment`), |
| 108 | + }, |
| 109 | + { |
| 110 | + name: "non tty", |
| 111 | + opts: &RefreshOptions{}, |
| 112 | + nontty: true, |
| 113 | + wantErr: regexp.MustCompile(`not attached to a terminal;`), |
| 114 | + }, |
| 115 | + { |
| 116 | + name: "no hosts configured", |
| 117 | + opts: &RefreshOptions{}, |
| 118 | + wantErr: regexp.MustCompile(`not logged in to any hosts`), |
| 119 | + }, |
| 120 | + { |
| 121 | + name: "hostname given but dne", |
| 122 | + cfgHosts: []string{ |
| 123 | + "github.com", |
| 124 | + "aline.cedrac", |
| 125 | + }, |
| 126 | + opts: &RefreshOptions{ |
| 127 | + Hostname: "obed.morton", |
| 128 | + }, |
| 129 | + wantErr: regexp.MustCompile(`not logged in to obed.morton`), |
| 130 | + }, |
| 131 | + { |
| 132 | + name: "hostname provided and is configured", |
| 133 | + cfgHosts: []string{ |
| 134 | + "obed.morton", |
| 135 | + "github.com", |
| 136 | + }, |
| 137 | + opts: &RefreshOptions{ |
| 138 | + Hostname: "obed.morton", |
| 139 | + Scopes: []string{}, |
| 140 | + }, |
| 141 | + wantAuthArgs: authArgs{ |
| 142 | + hostname: "obed.morton", |
| 143 | + scopes: []string{}, |
| 144 | + }, |
| 145 | + }, |
| 146 | + { |
| 147 | + name: "no hostname, one host configured", |
| 148 | + cfgHosts: []string{ |
| 149 | + "github.com", |
| 150 | + }, |
| 151 | + opts: &RefreshOptions{ |
| 152 | + Hostname: "", |
| 153 | + Scopes: []string{}, |
| 154 | + }, |
| 155 | + wantAuthArgs: authArgs{ |
| 156 | + hostname: "github.com", |
| 157 | + scopes: []string{}, |
| 158 | + }, |
| 159 | + }, |
| 160 | + { |
| 161 | + name: "no hostname, multiple hosts configured", |
| 162 | + cfgHosts: []string{ |
| 163 | + "github.com", |
| 164 | + "aline.cedrac", |
| 165 | + }, |
| 166 | + opts: &RefreshOptions{ |
| 167 | + Hostname: "", |
| 168 | + Scopes: []string{}, |
| 169 | + }, |
| 170 | + askStubs: func(as *prompt.AskStubber) { |
| 171 | + as.StubOne("github.com") |
| 172 | + }, |
| 173 | + wantAuthArgs: authArgs{ |
| 174 | + hostname: "github.com", |
| 175 | + scopes: []string{}, |
| 176 | + }, |
| 177 | + }, |
| 178 | + { |
| 179 | + name: "scopes provided", |
| 180 | + cfgHosts: []string{ |
| 181 | + "github.com", |
| 182 | + }, |
| 183 | + opts: &RefreshOptions{ |
| 184 | + Scopes: []string{"repo:invite", "public_key:read"}, |
| 185 | + }, |
| 186 | + wantAuthArgs: authArgs{ |
| 187 | + hostname: "github.com", |
| 188 | + scopes: []string{"repo:invite", "public_key:read"}, |
| 189 | + }, |
| 190 | + }, |
| 191 | + } |
| 192 | + for _, tt := range tests { |
| 193 | + t.Run(tt.name, func(t *testing.T) { |
| 194 | + aa := authArgs{} |
| 195 | + doAuthFlow = func(_ config.Config, hostname string, scopes []string) error { |
| 196 | + aa.hostname = hostname |
| 197 | + aa.scopes = scopes |
| 198 | + return nil |
| 199 | + } |
| 200 | + |
| 201 | + ghtoken := os.Getenv("GITHUB_TOKEN") |
| 202 | + defer func() { |
| 203 | + os.Setenv("GITHUB_TOKEN", ghtoken) |
| 204 | + }() |
| 205 | + os.Setenv("GITHUB_TOKEN", tt.ghtoken) |
| 206 | + io, _, _, _ := iostreams.Test() |
| 207 | + |
| 208 | + io.SetStdinTTY(!tt.nontty) |
| 209 | + io.SetStdoutTTY(!tt.nontty) |
| 210 | + |
| 211 | + tt.opts.IO = io |
| 212 | + cfg := config.NewBlankConfig() |
| 213 | + tt.opts.Config = func() (config.Config, error) { |
| 214 | + return cfg, nil |
| 215 | + } |
| 216 | + for _, hostname := range tt.cfgHosts { |
| 217 | + _ = cfg.Set(hostname, "oauth_token", "abc123") |
| 218 | + } |
| 219 | + reg := &httpmock.Registry{} |
| 220 | + reg.Register( |
| 221 | + httpmock.GraphQL(`query UserCurrent\b`), |
| 222 | + httpmock.StringResponse(`{"data":{"viewer":{"login":"cybilb"}}}`)) |
| 223 | + |
| 224 | + mainBuf := bytes.Buffer{} |
| 225 | + hostsBuf := bytes.Buffer{} |
| 226 | + defer config.StubWriteConfig(&mainBuf, &hostsBuf)() |
| 227 | + |
| 228 | + as, teardown := prompt.InitAskStubber() |
| 229 | + defer teardown() |
| 230 | + if tt.askStubs != nil { |
| 231 | + tt.askStubs(as) |
| 232 | + } |
| 233 | + |
| 234 | + err := refreshRun(tt.opts) |
| 235 | + assert.Equal(t, tt.wantErr == nil, err == nil) |
| 236 | + if err != nil { |
| 237 | + if tt.wantErr != nil { |
| 238 | + assert.True(t, tt.wantErr.MatchString(err.Error())) |
| 239 | + return |
| 240 | + } else { |
| 241 | + t.Fatalf("unexpected error: %s", err) |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + assert.Equal(t, aa.hostname, tt.wantAuthArgs.hostname) |
| 246 | + assert.Equal(t, aa.scopes, tt.wantAuthArgs.scopes) |
| 247 | + }) |
| 248 | + } |
| 249 | +} |
0 commit comments