[MM-47821]: fix searchUserCmdF to return proper errors (#24700)

* fix: fix searchUserCmdF to return proper errors

* fix: fix broken tests

* fix: fix go-ci issues

* fix: fix more broken tests

* fix: fix wrong function call on error getting user test

* fix: fix e2e test

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
Neto Costa
2023-10-25 15:56:12 -03:00
коммит произвёл GitHub
родитель f01e8f6c6d
Коммит 00108d44bb
3 изменённых файлов: 73 добавлений и 3 удалений

Просмотреть файл

@@ -693,6 +693,7 @@ func searchUserCmdF(c client.Client, cmd *cobra.Command, args []string) error {
users, err := getUsersFromArgs(c, args)
if err != nil {
printer.PrintError(err.Error())
return err
}
for i, user := range users {

Просмотреть файл

@@ -134,7 +134,7 @@ func (s *MmctlE2ETestSuite) TestSearchUserCmd() {
emailArg := "nonexistentUser@example.com"
err := searchUserCmdF(c, &cobra.Command{}, []string{emailArg})
s.Require().Nil(err)
s.Require().Error(err)
s.Len(printer.GetLines(), 0)
s.Len(printer.GetErrorLines(), 1)
s.Equal(fmt.Sprintf("1 error occurred:\n\t* user %s not found\n\n", emailArg), printer.GetErrorLines()[0])

Просмотреть файл

@@ -615,7 +615,7 @@ func (s *MmctlUnitTestSuite) TestSearchUserCmd() {
Times(1)
err := searchUserCmdF(s.client, &cobra.Command{}, []string{arg})
s.Require().Nil(err)
s.Require().NotNil(err)
s.Require().Len(printer.GetLines(), 0)
s.Require().Equal("1 error occurred:\n\t* user example@example.com not found\n\n", printer.GetErrorLines()[0])
})
@@ -625,9 +625,78 @@ func (s *MmctlUnitTestSuite) TestSearchUserCmd() {
arg := "test/../hello?@mattermost.com"
err := searchUserCmdF(s.client, &cobra.Command{}, []string{arg})
s.Require().Nil(err)
s.Require().NotNil(err)
s.Require().Equal("1 error occurred:\n\t* user test/../hello?@mattermost.com not found\n\n", printer.GetErrorLines()[0])
})
s.Run("Error while getting user by email", func() {
printer.Clean()
emailArg := "example@mailexample.com"
s.client.
EXPECT().
GetUserByEmail(context.Background(), emailArg, "").
Return(nil, &model.Response{}, errors.New("Error while getting user by email")).
Times(1)
err := searchUserCmdF(s.client, &cobra.Command{}, []string{emailArg})
s.Require().NotNil(err)
s.Require().Len(printer.GetLines(), 0)
s.Require().Equal("1 error occurred:\n\t* Error while getting user by email\n\n", err.Error())
s.Require().Equal("1 error occurred:\n\t* Error while getting user by email\n\n", printer.GetErrorLines()[0])
})
s.Run("Error while getting user by username", func() {
printer.Clean()
usernameArg := "exampleUser"
s.client.
EXPECT().
GetUserByEmail(context.Background(), usernameArg, "").
Return(nil, &model.Response{}, nil).
Times(1)
s.client.
EXPECT().
GetUserByUsername(context.Background(), usernameArg, "").
Return(nil, &model.Response{}, errors.New("Error while getting user by username")).
Times(1)
err := searchUserCmdF(s.client, &cobra.Command{}, []string{usernameArg})
s.Require().NotNil(err)
s.Require().Len(printer.GetLines(), 0)
s.Require().Equal("1 error occurred:\n\t* Error while getting user by username\n\n", err.Error())
s.Require().Equal("1 error occurred:\n\t* Error while getting user by username\n\n", printer.GetErrorLines()[0])
})
s.Run("Error while getting user", func() {
printer.Clean()
userArg := "exampleUser"
s.client.
EXPECT().
GetUserByEmail(context.Background(), userArg, "").
Return(nil, &model.Response{}, nil).
Times(1)
s.client.
EXPECT().
GetUserByUsername(context.Background(), userArg, "").
Return(nil, &model.Response{}, nil).
Times(1)
s.client.
EXPECT().
GetUser(context.Background(), userArg, "").
Return(nil, &model.Response{}, errors.New("Error while getting user")).
Times(1)
err := searchUserCmdF(s.client, &cobra.Command{}, []string{userArg})
s.Require().NotNil(err)
s.Require().Len(printer.GetLines(), 0)
s.Require().Equal("1 error occurred:\n\t* Error while getting user\n\n", err.Error())
s.Require().Equal("1 error occurred:\n\t* Error while getting user\n\n", printer.GetErrorLines()[0])
})
}
func (s *MmctlUnitTestSuite) TestChangePasswordUserCmdF() {