(t *testing.T)
| 251 | } |
| 252 | |
| 253 | func TestHTTPError_ScopesSuggestion(t *testing.T) { |
| 254 | makeResponse := func(s int, u, haveScopes, needScopes string) *http.Response { |
| 255 | req, err := http.NewRequest("GET", u, nil) |
| 256 | if err != nil { |
| 257 | t.Fatal(err) |
| 258 | } |
| 259 | return &http.Response{ |
| 260 | Request: req, |
| 261 | StatusCode: s, |
| 262 | Body: io.NopCloser(bytes.NewBufferString(`{}`)), |
| 263 | Header: map[string][]string{ |
| 264 | "Content-Type": {"application/json"}, |
| 265 | "X-Oauth-Scopes": {haveScopes}, |
| 266 | "X-Accepted-Oauth-Scopes": {needScopes}, |
| 267 | }, |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | tests := []struct { |
| 272 | name string |
| 273 | resp *http.Response |
| 274 | want string |
| 275 | }{ |
| 276 | { |
| 277 | name: "has necessary scopes", |
| 278 | resp: makeResponse(404, "https://api.github.com/gists", "repo, gist, read:org", "gist"), |
| 279 | want: ``, |
| 280 | }, |
| 281 | { |
| 282 | name: "normalizes scopes", |
| 283 | resp: makeResponse(404, "https://api.github.com/orgs/ORG/discussions", "admin:org, write:discussion", "read:org, read:discussion"), |
| 284 | want: ``, |
| 285 | }, |
| 286 | { |
| 287 | name: "no scopes on endpoint", |
| 288 | resp: makeResponse(404, "https://api.github.com/user", "repo", ""), |
| 289 | want: ``, |
| 290 | }, |
| 291 | { |
| 292 | name: "missing a scope", |
| 293 | resp: makeResponse(404, "https://api.github.com/gists", "repo, read:org", "gist, delete_repo"), |
| 294 | want: `This API operation needs the "gist" scope. To request it, run: gh auth refresh -h github.com -s gist`, |
| 295 | }, |
| 296 | { |
| 297 | name: "server error", |
| 298 | resp: makeResponse(500, "https://api.github.com/gists", "repo", "gist"), |
| 299 | want: ``, |
| 300 | }, |
| 301 | { |
| 302 | name: "no scopes on token", |
| 303 | resp: makeResponse(404, "https://api.github.com/gists", "", "gist, delete_repo"), |
| 304 | want: ``, |
| 305 | }, |
| 306 | { |
| 307 | name: "http code is 422", |
| 308 | resp: makeResponse(422, "https://api.github.com/gists", "", "gist"), |
| 309 | want: "", |
| 310 | }, |
nothing calls this directly
no test coverage detected