@@ -2,6 +2,9 @@ package refresh
22
33import (
44 "bytes"
5+ "io/ioutil"
6+ "net/http"
7+ "strings"
58 "testing"
69
710 "github.com/cli/cli/v2/internal/config"
@@ -134,6 +137,7 @@ func Test_refreshRun(t *testing.T) {
134137 opts * RefreshOptions
135138 askStubs func (* prompt.AskStubber )
136139 cfgHosts []string
140+ oldScopes string
137141 wantErr string
138142 nontty bool
139143 wantAuthArgs authArgs
@@ -211,6 +215,20 @@ func Test_refreshRun(t *testing.T) {
211215 scopes : []string {"repo:invite" , "public_key:read" },
212216 },
213217 },
218+ {
219+ name : "scopes provided" ,
220+ cfgHosts : []string {
221+ "github.com" ,
222+ },
223+ oldScopes : "delete_repo, codespace" ,
224+ opts : & RefreshOptions {
225+ Scopes : []string {"repo:invite" , "public_key:read" },
226+ },
227+ wantAuthArgs : authArgs {
228+ hostname : "github.com" ,
229+ scopes : []string {"repo:invite" , "public_key:read" , "delete_repo" , "codespace" },
230+ },
231+ },
214232 }
215233 for _ , tt := range tests {
216234 t .Run (tt .name , func (t * testing.T ) {
@@ -234,10 +252,26 @@ func Test_refreshRun(t *testing.T) {
234252 for _ , hostname := range tt .cfgHosts {
235253 _ = cfg .Set (hostname , "oauth_token" , "abc123" )
236254 }
237- reg := & httpmock.Registry {}
238- reg .Register (
239- httpmock .GraphQL (`query UserCurrent\b` ),
240- httpmock .StringResponse (`{"data":{"viewer":{"login":"cybilb"}}}` ))
255+
256+ httpReg := & httpmock.Registry {}
257+ httpReg .Register (
258+ httpmock .REST ("GET" , "" ),
259+ func (req * http.Request ) (* http.Response , error ) {
260+ statusCode := 200
261+ if req .Header .Get ("Authorization" ) != "token abc123" {
262+ statusCode = 400
263+ }
264+ return & http.Response {
265+ Request : req ,
266+ StatusCode : statusCode ,
267+ Body : ioutil .NopCloser (strings .NewReader (`` )),
268+ Header : http.Header {
269+ "X-Oauth-Scopes" : {tt .oldScopes },
270+ },
271+ }, nil
272+ },
273+ )
274+ tt .opts .httpClient = & http.Client {Transport : httpReg }
241275
242276 mainBuf := bytes.Buffer {}
243277 hostsBuf := bytes.Buffer {}
@@ -258,8 +292,8 @@ func Test_refreshRun(t *testing.T) {
258292 assert .NoError (t , err )
259293 }
260294
261- assert .Equal (t , aa . hostname , tt . wantAuthArgs .hostname )
262- assert .Equal (t , aa . scopes , tt . wantAuthArgs .scopes )
295+ assert .Equal (t , tt . wantAuthArgs . hostname , aa .hostname )
296+ assert .Equal (t , tt . wantAuthArgs . scopes , aa .scopes )
263297 })
264298 }
265299}
0 commit comments