-
Notifications
You must be signed in to change notification settings - Fork 419
feat(login): extend --auto-open-browser to work with --web #2115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,16 @@ | ||
| package login | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "crypto/tls" | ||
| "encoding/json" | ||
| "encoding/pem" | ||
| "fmt" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "net/url" | ||
| "regexp" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/MakeNowJust/heredoc" | ||
|
|
@@ -485,6 +488,142 @@ func TestPreserveExecProviderOnUsernameLogin(t *testing.T) { | |
| } | ||
| } | ||
|
|
||
| func TestValidateAutoOpenBrowser(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| webLogin bool | ||
| autoOpenBrowser bool | ||
| expectedError string | ||
| }{ | ||
| { | ||
| name: "valid: --web with --auto-open-browser", | ||
| webLogin: true, | ||
| autoOpenBrowser: true, | ||
| expectedError: "", | ||
| }, | ||
| { | ||
| name: "valid: --web with --auto-open-browser=false", | ||
| webLogin: true, | ||
| autoOpenBrowser: false, | ||
| expectedError: "", | ||
| }, | ||
| { | ||
| name: "valid: --web without --auto-open-browser (will default to true)", | ||
| webLogin: true, | ||
| autoOpenBrowser: false, | ||
| expectedError: "", | ||
| }, | ||
| { | ||
| name: "valid: neither --web nor --auto-open-browser", | ||
| webLogin: false, | ||
| autoOpenBrowser: false, | ||
| expectedError: "", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| options := &LoginOptions{ | ||
| Server: "https://api.test.devcluster.openshift.com:6443", // Consistent with existing OpenShift tests | ||
| WebLogin: tc.webLogin, | ||
| OIDCAutoOpenBrowser: tc.autoOpenBrowser, | ||
| StartingKubeConfig: &kclientcmdapi.Config{}, | ||
| } | ||
|
|
||
| err := options.Validate(nil, "", []string{}) | ||
| if tc.expectedError == "" { | ||
| if err != nil { | ||
| t.Errorf("expected no error, but got: %v", err) | ||
| } | ||
| } else { | ||
| if err == nil { | ||
| t.Errorf("expected error '%s', but got no error", tc.expectedError) | ||
| } else if err.Error() != tc.expectedError { | ||
| t.Errorf("expected error '%s', but got: %v", tc.expectedError, err) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestAutoOpenBrowserURLHandling(t *testing.T) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can remove this test function. Because it seems that it does not exercise the behavior of oc command (it exercises its own custom loginhandler).
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. I'll do that! |
||
| testCases := []struct { | ||
| name string | ||
| autoOpenBrowser bool | ||
| expectedOutputRegex string | ||
| shouldNotContain string | ||
| }{ | ||
| { | ||
| name: "with --auto-open-browser=false prints URL without opening", | ||
| autoOpenBrowser: false, | ||
| expectedOutputRegex: "Please visit the following URL in your browser: https://example.com/oauth/authorize\\?test=1\nThe callback server is listening and will receive the authentication response.\n", | ||
| shouldNotContain: "Opening login URL in the default browser", | ||
| }, | ||
| { | ||
| name: "with --auto-open-browser=true shows opening message", | ||
| autoOpenBrowser: true, | ||
| expectedOutputRegex: "Opening login URL in the default browser: https://example.com/oauth/authorize\\?test=1\n", | ||
| shouldNotContain: "Please visit the following URL", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| // Capture output | ||
| outBuf := &bytes.Buffer{} | ||
| streams := genericiooptions.IOStreams{ | ||
| In: strings.NewReader(""), | ||
| Out: outBuf, | ||
| ErrOut: &bytes.Buffer{}, | ||
| } | ||
|
|
||
| options := &LoginOptions{ | ||
| WebLogin: true, | ||
| OIDCAutoOpenBrowser: tc.autoOpenBrowser, | ||
| IOStreams: streams, | ||
| } | ||
|
|
||
| // Create a test login URL handler that matches the actual implementation | ||
| loginURLHandler := func(u *url.URL) error { | ||
| loginURL := u.String() | ||
| if !options.OIDCAutoOpenBrowser { | ||
| fmt.Fprintf(options.Out, "Please visit the following URL in your browser: %s\n", loginURL) | ||
| fmt.Fprintf(options.Out, "The callback server is listening and will receive the authentication response.\n") | ||
| return nil | ||
| } else { | ||
| fmt.Fprintf(options.Out, "Opening login URL in the default browser: %s\n", loginURL) | ||
| // Don't actually call browser.OpenURL in tests | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| // Test the handler with a test URL | ||
| testURL, _ := url.Parse("https://example.com/oauth/authorize?test=1") | ||
| err := loginURLHandler(testURL) | ||
|
|
||
| if err != nil { | ||
| t.Errorf("unexpected error: %v", err) | ||
| } | ||
|
|
||
| output := outBuf.String() | ||
|
|
||
| // Check expected output using regex | ||
| matched, regexErr := regexp.MatchString(tc.expectedOutputRegex, output) | ||
| if regexErr != nil { | ||
| t.Fatalf("regex error: %v", regexErr) | ||
| } | ||
| if !matched { | ||
| t.Errorf("output did not match expected pattern.\nExpected pattern: %s\nActual output: %s", tc.expectedOutputRegex, output) | ||
| } | ||
|
|
||
| // Check that certain strings are not present | ||
| if tc.shouldNotContain != "" && strings.Contains(output, tc.shouldNotContain) { | ||
| t.Errorf("output should not contain '%s', but got: %s", tc.shouldNotContain, output) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
Comment on lines
+549
to
+625
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Refactor test to verify actual implementation rather than a mock. This test defines a local
Consider restructuring to:
Example approach: -// Create a test login URL handler that matches the actual implementation
-loginURLHandler := func(u *url.URL) error {
- loginURL := u.String()
- if !options.OIDCAutoOpenBrowser {
- fmt.Fprintf(options.Out, "Please visit the following URL in your browser: %s\n", loginURL)
- fmt.Fprintf(options.Out, "The callback server is listening and will receive the authentication response.\n")
- return nil
- } else {
- fmt.Fprintf(options.Out, "Opening login URL in the default browser: %s\n", loginURL)
- // Don't actually call browser.OpenURL in tests
- return nil
- }
-}
-
-// Test the handler with a test URL
-testURL, _ := url.Parse("https://example.com/oauth/authorize?test=1")
-err := loginURLHandler(testURL)
+// Mock the browser opener to avoid actually opening a browser
+options.browserOpener = func(url string) error {
+ return nil // no-op in tests
+}
+
+// Call the actual method that handles web login URL
+// (adjust based on actual method name in LoginOptions)
+err := options.handleWebLoginURL("https://example.com/oauth/authorize?test=1")This ensures the test validates actual production behavior.
|
||
|
|
||
| func newTLSServer(certString, keyString string) (*httptest.Server, error) { | ||
| invoked := make(chan struct{}, 1) | ||
| server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.