From 46bd4599f0a14a4981524acec9861342f5bbc206 Mon Sep 17 00:00:00 2001 From: FutureFoodBanker <66010078+Wing0515@users.noreply.github.com> Date: Fri, 22 Mar 2024 20:42:37 +0800 Subject: [PATCH] [MM-47824] fix promoteGuestToUserCmdF to handle the proper errors (#26397) Co-authored-by: Mattermost Build --- server/cmd/mmctl/commands/user.go | 11 ++++++++--- server/cmd/mmctl/commands/user_e2e_test.go | 4 ++-- server/cmd/mmctl/commands/user_test.go | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/server/cmd/mmctl/commands/user.go b/server/cmd/mmctl/commands/user.go index 2fe310e9db..e1559b1232 100644 --- a/server/cmd/mmctl/commands/user.go +++ b/server/cmd/mmctl/commands/user.go @@ -1024,21 +1024,26 @@ func migrateAuthToLdapCmdF(c client.Client, cmd *cobra.Command, userArgs []strin } func promoteGuestToUserCmdF(c client.Client, _ *cobra.Command, userArgs []string) error { + var errs *multierror.Error for i, user := range getUsersFromUserArgs(c, userArgs) { if user == nil { - printer.PrintError(fmt.Sprintf("can't find guest '%v'", userArgs[i])) + err := fmt.Errorf("can't find guest '%s'", userArgs[i]) + errs = multierror.Append(errs, err) + printer.PrintError(err.Error()) continue } if _, err := c.PromoteGuestToUser(context.TODO(), user.Id); err != nil { - printer.PrintError(fmt.Sprintf("unable to promote guest %s: %s", userArgs[i], err)) + err = fmt.Errorf("unable to promote guest %s: %w", userArgs[i], err) + errs = multierror.Append(errs, err) + printer.PrintError(err.Error()) continue } printer.PrintT("User {{.Username}} promoted.", user) } - return nil + return errs.ErrorOrNil() } func demoteUserToGuestCmdF(c client.Client, _ *cobra.Command, userArgs []string) error { diff --git a/server/cmd/mmctl/commands/user_e2e_test.go b/server/cmd/mmctl/commands/user_e2e_test.go index ff51344222..45fce2d3f0 100644 --- a/server/cmd/mmctl/commands/user_e2e_test.go +++ b/server/cmd/mmctl/commands/user_e2e_test.go @@ -928,7 +928,7 @@ func (s *MmctlE2ETestSuite) TestPromoteGuestToUserCmd() { printer.Clean() err := promoteGuestToUserCmdF(c, nil, []string{user.Email}) - s.Require().Nil(err) + s.Require().NoError(err) defer s.Require().Nil(s.th.App.DemoteUserToGuest(s.th.Context, user)) s.Require().Len(printer.GetLines(), 1) s.Require().Len(printer.GetErrorLines(), 0) @@ -938,7 +938,7 @@ func (s *MmctlE2ETestSuite) TestPromoteGuestToUserCmd() { printer.Clean() err := promoteGuestToUserCmdF(s.th.Client, nil, []string{user.Email}) - s.Require().Nil(err) + s.Require().Error(err) s.Require().Len(printer.GetLines(), 0) s.Require().Len(printer.GetErrorLines(), 1) s.Require().Equal(fmt.Sprintf("unable to promote guest %s: You do not have the appropriate permissions.", user.Email), printer.GetErrorLines()[0]) diff --git a/server/cmd/mmctl/commands/user_test.go b/server/cmd/mmctl/commands/user_test.go index 6f67761d9c..b1c7b75c5f 100644 --- a/server/cmd/mmctl/commands/user_test.go +++ b/server/cmd/mmctl/commands/user_test.go @@ -2747,7 +2747,7 @@ func (s *MmctlUnitTestSuite) TestPromoteGuestToUserCmd() { Times(1) err := promoteGuestToUserCmdF(s.client, nil, []string{emailArg}) - s.Require().NoError(err) + s.Require().ErrorContains(err, "unable to promote guest") s.Require().Len(printer.GetLines(), 0) s.Require().Len(printer.GetErrorLines(), 1) s.Require().Equal(fmt.Sprintf("unable to promote guest %s: %s", emailArg, "some-error"), printer.GetErrorLines()[0])