|
4 | 4 | "context" |
5 | 5 | "errors" |
6 | 6 | "fmt" |
| 7 | + "io" |
7 | 8 | "path/filepath" |
8 | 9 | "testing" |
9 | 10 | "time" |
@@ -539,3 +540,61 @@ func TestIsOauthLoginDisabled(t *testing.T) { |
539 | 540 | assert.Equal(t, disabled, tc.disabled) |
540 | 541 | } |
541 | 542 | } |
| 543 | + |
| 544 | +func TestLoginValidateFlags(t *testing.T) { |
| 545 | + for _, tc := range []struct { |
| 546 | + name string |
| 547 | + args []string |
| 548 | + expectedErr string |
| 549 | + }{ |
| 550 | + { |
| 551 | + name: "--password-stdin without --username", |
| 552 | + args: []string{"--password-stdin"}, |
| 553 | + expectedErr: `the --password-stdin option requires --username to be set`, |
| 554 | + }, |
| 555 | + { |
| 556 | + name: "--password-stdin with empty --username", |
| 557 | + args: []string{"--password-stdin", "--username", ""}, |
| 558 | + expectedErr: `username is empty`, |
| 559 | + }, |
| 560 | + { |
| 561 | + name: "empty --username", |
| 562 | + args: []string{"--username", ""}, |
| 563 | + expectedErr: `username is empty`, |
| 564 | + }, |
| 565 | + { |
| 566 | + name: "--username without value", |
| 567 | + args: []string{"--username"}, |
| 568 | + expectedErr: `flag needs an argument: --username`, |
| 569 | + }, |
| 570 | + { |
| 571 | + name: "conflicting options --password-stdin and --password", |
| 572 | + args: []string{"--password-stdin", "--password", ""}, |
| 573 | + expectedErr: `conflicting options: cannot specify both --password and --password-stdin`, |
| 574 | + }, |
| 575 | + { |
| 576 | + name: "empty --password", |
| 577 | + args: []string{"--password", ""}, |
| 578 | + expectedErr: `password is empty`, |
| 579 | + }, |
| 580 | + { |
| 581 | + name: "--password without value", |
| 582 | + args: []string{"--password"}, |
| 583 | + expectedErr: `flag needs an argument: --password`, |
| 584 | + }, |
| 585 | + } { |
| 586 | + t.Run(tc.name, func(t *testing.T) { |
| 587 | + cmd := NewLoginCommand(test.NewFakeCli(&fakeClient{})) |
| 588 | + cmd.SetOut(io.Discard) |
| 589 | + cmd.SetErr(io.Discard) |
| 590 | + cmd.SetArgs(tc.args) |
| 591 | + |
| 592 | + err := cmd.Execute() |
| 593 | + if tc.expectedErr != "" { |
| 594 | + assert.Check(t, is.ErrorContains(err, tc.expectedErr)) |
| 595 | + } else { |
| 596 | + assert.Check(t, is.Nil(err)) |
| 597 | + } |
| 598 | + }) |
| 599 | + } |
| 600 | +} |
0 commit comments