|
1 | 1 | package login |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "crypto/tls" |
5 | 6 | "encoding/json" |
6 | 7 | "encoding/pem" |
7 | 8 | "fmt" |
8 | 9 | "net/http" |
9 | 10 | "net/http/httptest" |
| 11 | + "net/url" |
10 | 12 | "regexp" |
| 13 | + "strings" |
11 | 14 | "testing" |
12 | 15 |
|
13 | 16 | "github.com/MakeNowJust/heredoc" |
@@ -485,6 +488,142 @@ func TestPreserveExecProviderOnUsernameLogin(t *testing.T) { |
485 | 488 | } |
486 | 489 | } |
487 | 490 |
|
| 491 | +func TestValidateAutoOpenBrowser(t *testing.T) { |
| 492 | + testCases := []struct { |
| 493 | + name string |
| 494 | + webLogin bool |
| 495 | + autoOpenBrowser bool |
| 496 | + expectedError string |
| 497 | + }{ |
| 498 | + { |
| 499 | + name: "valid: --web with --auto-open-browser", |
| 500 | + webLogin: true, |
| 501 | + autoOpenBrowser: true, |
| 502 | + expectedError: "", |
| 503 | + }, |
| 504 | + { |
| 505 | + name: "valid: --web with --auto-open-browser=false", |
| 506 | + webLogin: true, |
| 507 | + autoOpenBrowser: false, |
| 508 | + expectedError: "", |
| 509 | + }, |
| 510 | + { |
| 511 | + name: "valid: --web without --auto-open-browser (will default to true)", |
| 512 | + webLogin: true, |
| 513 | + autoOpenBrowser: false, |
| 514 | + expectedError: "", |
| 515 | + }, |
| 516 | + { |
| 517 | + name: "valid: neither --web nor --auto-open-browser", |
| 518 | + webLogin: false, |
| 519 | + autoOpenBrowser: false, |
| 520 | + expectedError: "", |
| 521 | + }, |
| 522 | + } |
| 523 | + |
| 524 | + for _, tc := range testCases { |
| 525 | + t.Run(tc.name, func(t *testing.T) { |
| 526 | + options := &LoginOptions{ |
| 527 | + Server: "https://api.test.devcluster.openshift.com:6443", // Consistent with existing OpenShift tests |
| 528 | + WebLogin: tc.webLogin, |
| 529 | + OIDCAutoOpenBrowser: tc.autoOpenBrowser, |
| 530 | + StartingKubeConfig: &kclientcmdapi.Config{}, |
| 531 | + } |
| 532 | + |
| 533 | + err := options.Validate(nil, "", []string{}) |
| 534 | + if tc.expectedError == "" { |
| 535 | + if err != nil { |
| 536 | + t.Errorf("expected no error, but got: %v", err) |
| 537 | + } |
| 538 | + } else { |
| 539 | + if err == nil { |
| 540 | + t.Errorf("expected error '%s', but got no error", tc.expectedError) |
| 541 | + } else if err.Error() != tc.expectedError { |
| 542 | + t.Errorf("expected error '%s', but got: %v", tc.expectedError, err) |
| 543 | + } |
| 544 | + } |
| 545 | + }) |
| 546 | + } |
| 547 | +} |
| 548 | + |
| 549 | +func TestAutoOpenBrowserURLHandling(t *testing.T) { |
| 550 | + testCases := []struct { |
| 551 | + name string |
| 552 | + autoOpenBrowser bool |
| 553 | + expectedOutputRegex string |
| 554 | + shouldNotContain string |
| 555 | + }{ |
| 556 | + { |
| 557 | + name: "with --auto-open-browser=false prints URL without opening", |
| 558 | + autoOpenBrowser: false, |
| 559 | + 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", |
| 560 | + shouldNotContain: "Opening login URL in the default browser", |
| 561 | + }, |
| 562 | + { |
| 563 | + name: "with --auto-open-browser=true shows opening message", |
| 564 | + autoOpenBrowser: true, |
| 565 | + expectedOutputRegex: "Opening login URL in the default browser: https://example.com/oauth/authorize\\?test=1\n", |
| 566 | + shouldNotContain: "Please visit the following URL", |
| 567 | + }, |
| 568 | + } |
| 569 | + |
| 570 | + for _, tc := range testCases { |
| 571 | + t.Run(tc.name, func(t *testing.T) { |
| 572 | + // Capture output |
| 573 | + outBuf := &bytes.Buffer{} |
| 574 | + streams := genericiooptions.IOStreams{ |
| 575 | + In: strings.NewReader(""), |
| 576 | + Out: outBuf, |
| 577 | + ErrOut: &bytes.Buffer{}, |
| 578 | + } |
| 579 | + |
| 580 | + options := &LoginOptions{ |
| 581 | + WebLogin: true, |
| 582 | + OIDCAutoOpenBrowser: tc.autoOpenBrowser, |
| 583 | + IOStreams: streams, |
| 584 | + } |
| 585 | + |
| 586 | + // Create a test login URL handler that matches the actual implementation |
| 587 | + loginURLHandler := func(u *url.URL) error { |
| 588 | + loginURL := u.String() |
| 589 | + if !options.OIDCAutoOpenBrowser { |
| 590 | + fmt.Fprintf(options.Out, "Please visit the following URL in your browser: %s\n", loginURL) |
| 591 | + fmt.Fprintf(options.Out, "The callback server is listening and will receive the authentication response.\n") |
| 592 | + return nil |
| 593 | + } else { |
| 594 | + fmt.Fprintf(options.Out, "Opening login URL in the default browser: %s\n", loginURL) |
| 595 | + // Don't actually call browser.OpenURL in tests |
| 596 | + return nil |
| 597 | + } |
| 598 | + } |
| 599 | + |
| 600 | + // Test the handler with a test URL |
| 601 | + testURL, _ := url.Parse("https://example.com/oauth/authorize?test=1") |
| 602 | + err := loginURLHandler(testURL) |
| 603 | + |
| 604 | + if err != nil { |
| 605 | + t.Errorf("unexpected error: %v", err) |
| 606 | + } |
| 607 | + |
| 608 | + output := outBuf.String() |
| 609 | + |
| 610 | + // Check expected output using regex |
| 611 | + matched, regexErr := regexp.MatchString(tc.expectedOutputRegex, output) |
| 612 | + if regexErr != nil { |
| 613 | + t.Fatalf("regex error: %v", regexErr) |
| 614 | + } |
| 615 | + if !matched { |
| 616 | + t.Errorf("output did not match expected pattern.\nExpected pattern: %s\nActual output: %s", tc.expectedOutputRegex, output) |
| 617 | + } |
| 618 | + |
| 619 | + // Check that certain strings are not present |
| 620 | + if tc.shouldNotContain != "" && strings.Contains(output, tc.shouldNotContain) { |
| 621 | + t.Errorf("output should not contain '%s', but got: %s", tc.shouldNotContain, output) |
| 622 | + } |
| 623 | + }) |
| 624 | + } |
| 625 | +} |
| 626 | + |
488 | 627 | func newTLSServer(certString, keyString string) (*httptest.Server, error) { |
489 | 628 | invoked := make(chan struct{}, 1) |
490 | 629 | server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
|
0 commit comments