(t *testing.T)
| 156 | } |
| 157 | |
| 158 | func TestHTTPError_ScopesSuggestion(t *testing.T) { |
| 159 | makeResponse := func(s int, u, haveScopes, needScopes string) *http.Response { |
| 160 | req, err := http.NewRequest("GET", u, nil) |
| 161 | if err != nil { |
| 162 | t.Fatal(err) |
| 163 | } |
| 164 | return &http.Response{ |
| 165 | Request: req, |
| 166 | StatusCode: s, |
| 167 | Body: io.NopCloser(bytes.NewBufferString(`{}`)), |
| 168 | Header: map[string][]string{ |
| 169 | "Content-Type": {"application/json"}, |
| 170 | "X-Oauth-Scopes": {haveScopes}, |
| 171 | "X-Accepted-Oauth-Scopes": {needScopes}, |
| 172 | }, |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | tests := []struct { |
| 177 | name string |
| 178 | resp *http.Response |
| 179 | want string |
| 180 | }{ |
| 181 | { |
| 182 | name: "has necessary scopes", |
| 183 | resp: makeResponse(404, "https://api.github.com/gists", "repo, gist, read:org", "gist"), |
| 184 | want: ``, |
| 185 | }, |
| 186 | { |
| 187 | name: "normalizes scopes", |
| 188 | resp: makeResponse(404, "https://api.github.com/orgs/ORG/discussions", "admin:org, write:discussion", "read:org, read:discussion"), |
| 189 | want: ``, |
| 190 | }, |
| 191 | { |
| 192 | name: "no scopes on endpoint", |
| 193 | resp: makeResponse(404, "https://api.github.com/user", "repo", ""), |
| 194 | want: ``, |
| 195 | }, |
| 196 | { |
| 197 | name: "missing a scope", |
| 198 | resp: makeResponse(404, "https://api.github.com/gists", "repo, read:org", "gist, delete_repo"), |
| 199 | want: `This API operation needs the "gist" scope. To request it, run: gh auth refresh -h github.com -s gist`, |
| 200 | }, |
| 201 | { |
| 202 | name: "server error", |
| 203 | resp: makeResponse(500, "https://api.github.com/gists", "repo", "gist"), |
| 204 | want: ``, |
| 205 | }, |
| 206 | { |
| 207 | name: "no scopes on token", |
| 208 | resp: makeResponse(404, "https://api.github.com/gists", "", "gist, delete_repo"), |
| 209 | want: ``, |
| 210 | }, |
| 211 | { |
| 212 | name: "http code is 422", |
| 213 | resp: makeResponse(422, "https://api.github.com/gists", "", "gist"), |
| 214 | want: "", |
| 215 | }, |
nothing calls this directly
no test coverage detected