88 "net/http"
99 "net/http/httptest"
1010 "net/url"
11- "os"
1211 "strings"
1312 "testing"
1413 "time"
@@ -26,69 +25,6 @@ func mustParseURL(t *testing.T, raw string) *url.URL {
2625 return u
2726}
2827
29- func loginCommand (t * testing.T ) * command {
30- t .Helper ()
31- for _ , cmd := range commands {
32- if cmd .matches ("login" ) {
33- return cmd
34- }
35- }
36- t .Fatal ("login command not found" )
37- return nil
38- }
39-
40- func captureProcessOutput (t * testing.T , fn func () error ) (stdout string , stderr string , err error ) {
41- t .Helper ()
42-
43- stdoutR , stdoutW , err := os .Pipe ()
44- if err != nil {
45- t .Fatal (err )
46- }
47- stderrR , stderrW , err := os .Pipe ()
48- if err != nil {
49- _ = stdoutR .Close ()
50- _ = stdoutW .Close ()
51- t .Fatal (err )
52- }
53-
54- oldStdout := os .Stdout
55- oldStderr := os .Stderr
56- os .Stdout = stdoutW
57- os .Stderr = stderrW
58- defer func () {
59- os .Stdout = oldStdout
60- os .Stderr = oldStderr
61- }()
62-
63- err = fn ()
64-
65- _ = stdoutW .Close ()
66- _ = stderrW .Close ()
67-
68- stdoutBytes , readErr := io .ReadAll (stdoutR )
69- if readErr != nil {
70- t .Fatal (readErr )
71- }
72- stderrBytes , readErr := io .ReadAll (stderrR )
73- if readErr != nil {
74- t .Fatal (readErr )
75- }
76-
77- return strings .TrimSpace (string (stdoutBytes )), strings .TrimSpace (string (stderrBytes )), err
78- }
79-
80- func runLoginHandler (t * testing.T , cfgValue * config , args ... string ) (stdout string , stderr string , err error ) {
81- t .Helper ()
82-
83- oldCfg := cfg
84- cfg = cfgValue
85- t .Cleanup (func () { cfg = oldCfg })
86-
87- return captureProcessOutput (t , func () error {
88- return loginCommand (t ).handler (args )
89- })
90- }
91-
9228func TestLogin (t * testing.T ) {
9329 check := func (t * testing.T , cfg * config ) (output string , err error ) {
9430 t .Helper ()
@@ -203,87 +139,6 @@ func TestLogin(t *testing.T) {
203139 })
204140}
205141
206- func TestLoginHandler (t * testing.T ) {
207- t .Run ("warns when login endpoint differs from configured endpoint" , func (t * testing.T ) {
208- t .Setenv ("SRC_ENDPOINT" , "https://example.com" )
209-
210- s := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
211- fmt .Fprintln (w , `{"data":{"currentUser":{"username":"alice"}}}` )
212- }))
213- defer s .Close ()
214-
215- stdout , stderr , err := runLoginHandler (t , & config {
216- endpointURL : mustParseURL (t , "https://example.com" ),
217- accessToken : "x" ,
218- }, s .URL )
219- if err != nil {
220- t .Fatal (err )
221- }
222- if ! strings .Contains (stderr , "Warning: Logging into " + s .URL + " instead of the configured endpoint https://example.com." ) {
223- t .Fatalf ("stderr = %q, want endpoint warning" , stderr )
224- }
225- if ! strings .Contains (stderr , "export SRC_ENDPOINT=" + s .URL ) {
226- t .Fatalf ("stderr = %q, want shell tip" , stderr )
227- }
228- if ! strings .Contains (stdout , "✔︎ Authenticated as alice on " + s .URL ) {
229- t .Fatalf ("stdout = %q, want validation output" , stdout )
230- }
231- })
232-
233- t .Run ("warns when no SRC_ENDPOINT is configured in the environment" , func (t * testing.T ) {
234- if oldValue , ok := os .LookupEnv ("SRC_ENDPOINT" ); ok {
235- _ = os .Unsetenv ("SRC_ENDPOINT" )
236- t .Cleanup (func () { _ = os .Setenv ("SRC_ENDPOINT" , oldValue ) })
237- } else {
238- _ = os .Unsetenv ("SRC_ENDPOINT" )
239- }
240-
241- s := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
242- fmt .Fprintln (w , `{"data":{"currentUser":{"username":"alice"}}}` )
243- }))
244- defer s .Close ()
245-
246- stdout , stderr , err := runLoginHandler (t , & config {
247- endpointURL : mustParseURL (t , SGDotComEndpoint ),
248- accessToken : "x" ,
249- }, s .URL )
250- if err != nil {
251- t .Fatal (err )
252- }
253- if ! strings .Contains (stderr , "Warning: No SRC_ENDPOINT is configured in the environment. Logging in using \" " + s .URL + "\" ." ) {
254- t .Fatalf ("stderr = %q, want default-endpoint warning" , stderr )
255- }
256- if ! strings .Contains (stderr , "NOTE: By default src will use \" " + SGDotComEndpoint + "\" if SRC_ENDPOINT is not set." ) {
257- t .Fatalf ("stderr = %q, want default endpoint note" , stderr )
258- }
259- if ! strings .Contains (stdout , "✔︎ Authenticated as alice on " + s .URL ) {
260- t .Fatalf ("stdout = %q, want validation output" , stdout )
261- }
262- })
263-
264- t .Run ("warns when using config file" , func (t * testing.T ) {
265- s := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
266- fmt .Fprintln (w , `{"data":{"currentUser":{"username":"alice"}}}` )
267- }))
268- defer s .Close ()
269-
270- stdout , stderr , err := runLoginHandler (t , & config {
271- endpointURL : mustParseURL (t , s .URL ),
272- accessToken : "x" ,
273- configFilePath : "f" ,
274- })
275- if err != nil {
276- t .Fatal (err )
277- }
278- if ! strings .Contains (stderr , "Configuring src with a JSON file is deprecated" ) {
279- t .Fatalf ("stderr = %q, want deprecation warning" , stderr )
280- }
281- if ! strings .Contains (stdout , "✔︎ Authenticated as alice on " + s .URL ) {
282- t .Fatalf ("stdout = %q, want validation output" , stdout )
283- }
284- })
285- }
286-
287142type fakeOAuthClient struct {
288143 startErr error
289144 startCalled * bool
0 commit comments