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

Function TestHTTPError_ScopesSuggestion

api/client_test.go:253–320  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

251}
252
253func 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 },

Callers

nothing calls this directly

Calls 4

HandleHTTPErrorFunction · 0.85
ScopesSuggestionMethod · 0.80
RunMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected