From 4ae87c280ed69c353a81ffe620af1f26a06fa1dd Mon Sep 17 00:00:00 2001 From: sreya Date: Thu, 30 Jun 2022 17:21:36 +0000 Subject: [PATCH] fix: reprompt for matching passwords on mismatch - Previously we only re-prompted for the password confirmation. --- cli/login.go | 43 +++++++++++++++++++++++-------------------- cli/login_test.go | 13 ++++++++++--- 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/cli/login.go b/cli/login.go index 6ff76992596cb..8543e5747afc5 100644 --- a/cli/login.go +++ b/cli/login.go @@ -131,26 +131,29 @@ func login() *cobra.Command { } if password == "" { - password, err = cliui.Prompt(cmd, cliui.PromptOptions{ - Text: "Enter a " + cliui.Styles.Field.Render("password") + ":", - Secret: true, - Validate: cliui.ValidateNotEmpty, - }) - if err != nil { - return xerrors.Errorf("specify password prompt: %w", err) - } - _, err = cliui.Prompt(cmd, cliui.PromptOptions{ - Text: "Confirm " + cliui.Styles.Field.Render("password") + ":", - Secret: true, - Validate: func(s string) error { - if s != password { - return xerrors.Errorf("Passwords do not match") - } - return nil - }, - }) - if err != nil { - return xerrors.Errorf("confirm password prompt: %w", err) + var matching bool + + for !matching { + password, err = cliui.Prompt(cmd, cliui.PromptOptions{ + Text: "Enter a " + cliui.Styles.Field.Render("password") + ":", + Secret: true, + Validate: cliui.ValidateNotEmpty, + }) + if err != nil { + return xerrors.Errorf("specify password prompt: %w", err) + } + confirm, err := cliui.Prompt(cmd, cliui.PromptOptions{ + Text: "Confirm " + cliui.Styles.Field.Render("password") + ":", + Secret: true, + }) + if err != nil { + return xerrors.Errorf("confirm password prompt: %w", err) + } + + matching = confirm == password + if !matching { + _, _ = fmt.Fprintln(cmd.OutOrStdout(), cliui.Styles.Error.Render("Passwords do not match")) + } } } diff --git a/cli/login_test.go b/cli/login_test.go index 316f14871e43f..1a6c502b78237 100644 --- a/cli/login_test.go +++ b/cli/login_test.go @@ -8,6 +8,7 @@ import ( "github.com/stretchr/testify/require" "github.com/coder/coder/cli/clitest" + "github.com/coder/coder/cli/cliui" "github.com/coder/coder/coderd/coderdtest" "github.com/coder/coder/pty/ptytest" ) @@ -92,7 +93,7 @@ func TestLogin(t *testing.T) { go func() { defer close(doneChan) err := root.ExecuteContext(ctx) - assert.ErrorIs(t, err, context.Canceled) + assert.NoError(t, err) }() matches := []string{ @@ -108,9 +109,15 @@ func TestLogin(t *testing.T) { pty.ExpectMatch(match) pty.WriteLine(value) } + + // Validate that we reprompt for matching passwords. pty.ExpectMatch("Passwords do not match") - pty.ExpectMatch("password") // Re-prompt password. - cancel() + pty.ExpectMatch("Enter a " + cliui.Styles.Field.Render("password")) + + pty.WriteLine("pass") + pty.ExpectMatch("Confirm") + pty.WriteLine("pass") + pty.ExpectMatch("Welcome to Coder") <-doneChan })