@@ -2,14 +2,14 @@ package login
22
33import (
44 "bytes"
5- "io/ioutil"
65 "net/http"
76 "os"
87 "regexp"
98 "testing"
109
1110 "github.com/cli/cli/api"
1211 "github.com/cli/cli/internal/config"
12+ "github.com/cli/cli/pkg/cmd/auth/client"
1313 "github.com/cli/cli/pkg/cmdutil"
1414 "github.com/cli/cli/pkg/httpmock"
1515 "github.com/cli/cli/pkg/iostreams"
@@ -164,19 +164,6 @@ func Test_NewCmdLogin(t *testing.T) {
164164 }
165165}
166166
167- func scopesResponder (scopes string ) func (* http.Request ) (* http.Response , error ) {
168- return func (req * http.Request ) (* http.Response , error ) {
169- return & http.Response {
170- StatusCode : 200 ,
171- Request : req ,
172- Header : map [string ][]string {
173- "X-Oauth-Scopes" : {scopes },
174- },
175- Body : ioutil .NopCloser (bytes .NewBufferString ("" )),
176- }, nil
177- }
178- }
179-
180167func Test_loginRun_nontty (t * testing.T ) {
181168 tests := []struct {
182169 name string
@@ -200,7 +187,7 @@ func Test_loginRun_nontty(t *testing.T) {
200187 Token : "abc123" ,
201188 },
202189 httpStubs : func (reg * httpmock.Registry ) {
203- reg .Register (httpmock .REST ("GET" , "api/v3/" ), scopesResponder ("repo,read:org" ))
190+ reg .Register (httpmock .REST ("GET" , "api/v3/" ), httpmock . ScopesResponder ("repo,read:org" ))
204191 },
205192 wantHosts : "albert.wesker:\n oauth_token: abc123\n " ,
206193 },
@@ -211,7 +198,7 @@ func Test_loginRun_nontty(t *testing.T) {
211198 Token : "abc456" ,
212199 },
213200 httpStubs : func (reg * httpmock.Registry ) {
214- reg .Register (httpmock .REST ("GET" , "" ), scopesResponder ("read:org" ))
201+ reg .Register (httpmock .REST ("GET" , "" ), httpmock . ScopesResponder ("read:org" ))
215202 },
216203 wantErr : regexp .MustCompile (`missing required scope 'repo'` ),
217204 },
@@ -222,7 +209,7 @@ func Test_loginRun_nontty(t *testing.T) {
222209 Token : "abc456" ,
223210 },
224211 httpStubs : func (reg * httpmock.Registry ) {
225- reg .Register (httpmock .REST ("GET" , "" ), scopesResponder ("repo" ))
212+ reg .Register (httpmock .REST ("GET" , "" ), httpmock . ScopesResponder ("repo" ))
226213 },
227214 wantErr : regexp .MustCompile (`missing required scope 'read:org'` ),
228215 },
@@ -233,7 +220,7 @@ func Test_loginRun_nontty(t *testing.T) {
233220 Token : "abc456" ,
234221 },
235222 httpStubs : func (reg * httpmock.Registry ) {
236- reg .Register (httpmock .REST ("GET" , "" ), scopesResponder ("repo,admin:org" ))
223+ reg .Register (httpmock .REST ("GET" , "" ), httpmock . ScopesResponder ("repo,admin:org" ))
237224 },
238225 wantHosts : "github.com:\n oauth_token: abc456\n " ,
239226 },
@@ -252,19 +239,19 @@ func Test_loginRun_nontty(t *testing.T) {
252239 tt .opts .IO = io
253240 t .Run (tt .name , func (t * testing.T ) {
254241 reg := & httpmock.Registry {}
255- origClientFromCfg := clientFromCfg
242+ origClientFromCfg := client . ClientFromCfg
256243 defer func () {
257- clientFromCfg = origClientFromCfg
244+ client . ClientFromCfg = origClientFromCfg
258245 }()
259- clientFromCfg = func (_ string , _ config.Config ) (* api.Client , error ) {
246+ client . ClientFromCfg = func (_ string , _ config.Config ) (* api.Client , error ) {
260247 httpClient := & http.Client {Transport : reg }
261248 return api .NewClientFromHTTP (httpClient ), nil
262249 }
263250
264251 if tt .httpStubs != nil {
265252 tt .httpStubs (reg )
266253 } else {
267- reg .Register (httpmock .REST ("GET" , "" ), scopesResponder ("repo,read:org" ))
254+ reg .Register (httpmock .REST ("GET" , "" ), httpmock . ScopesResponder ("repo,read:org" ))
268255 }
269256
270257 mainBuf := bytes.Buffer {}
@@ -305,7 +292,7 @@ func Test_loginRun_Survey(t *testing.T) {
305292 _ = cfg .Set ("github.com" , "oauth_token" , "ghi789" )
306293 },
307294 httpStubs : func (reg * httpmock.Registry ) {
308- reg .Register (httpmock .REST ("GET" , "" ), scopesResponder ("repo,read:org," ))
295+ reg .Register (httpmock .REST ("GET" , "" ), httpmock . ScopesResponder ("repo,read:org," ))
309296 reg .Register (
310297 httpmock .GraphQL (`query UserCurrent\b` ),
311298 httpmock .StringResponse (`{"data":{"viewer":{"login":"jillv"}}}` ))
@@ -328,7 +315,7 @@ func Test_loginRun_Survey(t *testing.T) {
328315 as .StubOne ("HTTPS" ) // git_protocol
329316 },
330317 httpStubs : func (reg * httpmock.Registry ) {
331- reg .Register (httpmock .REST ("GET" , "api/v3/" ), scopesResponder ("repo,read:org," ))
318+ reg .Register (httpmock .REST ("GET" , "api/v3/" ), httpmock . ScopesResponder ("repo,read:org," ))
332319 reg .Register (
333320 httpmock .GraphQL (`query UserCurrent\b` ),
334321 httpmock .StringResponse (`{"data":{"viewer":{"login":"jillv"}}}` ))
@@ -345,7 +332,7 @@ func Test_loginRun_Survey(t *testing.T) {
345332 as .StubOne ("HTTPS" ) // git_protocol
346333 },
347334 httpStubs : func (reg * httpmock.Registry ) {
348- reg .Register (httpmock .REST ("GET" , "api/v3/" ), scopesResponder ("repo,read:org," ))
335+ reg .Register (httpmock .REST ("GET" , "api/v3/" ), httpmock . ScopesResponder ("repo,read:org," ))
349336 reg .Register (
350337 httpmock .GraphQL (`query UserCurrent\b` ),
351338 httpmock .StringResponse (`{"data":{"viewer":{"login":"jillv"}}}` ))
@@ -397,18 +384,18 @@ func Test_loginRun_Survey(t *testing.T) {
397384
398385 t .Run (tt .name , func (t * testing.T ) {
399386 reg := & httpmock.Registry {}
400- origClientFromCfg := clientFromCfg
387+ origClientFromCfg := client . ClientFromCfg
401388 defer func () {
402- clientFromCfg = origClientFromCfg
389+ client . ClientFromCfg = origClientFromCfg
403390 }()
404- clientFromCfg = func (_ string , _ config.Config ) (* api.Client , error ) {
391+ client . ClientFromCfg = func (_ string , _ config.Config ) (* api.Client , error ) {
405392 httpClient := & http.Client {Transport : reg }
406393 return api .NewClientFromHTTP (httpClient ), nil
407394 }
408395 if tt .httpStubs != nil {
409396 tt .httpStubs (reg )
410397 } else {
411- reg .Register (httpmock .REST ("GET" , "" ), scopesResponder ("repo,read:org," ))
398+ reg .Register (httpmock .REST ("GET" , "" ), httpmock . ScopesResponder ("repo,read:org," ))
412399 reg .Register (
413400 httpmock .GraphQL (`query UserCurrent\b` ),
414401 httpmock .StringResponse (`{"data":{"viewer":{"login":"jillv"}}}` ))
0 commit comments