Includes mmctl into the mono-repo (#23091)

* Includes mmctl into the mono-repo

* Update to use the new public module paths

* Adds docs check to the mmctl CI

* Fix public utils import path

* Tidy up modules

* Fix linter

* Update CI tasks to use the new file structure

* Update CI references
Этот коммит содержится в:
Miguel de la Cruz
2023-06-05 12:42:55 +02:00
коммит произвёл GitHub
родитель 412109b02e
Коммит 951456c780
305 изменённых файлов: 47027 добавлений и 88 удалений

221
server/cmd/mmctl/commands/roles_test.go Обычный файл
Просмотреть файл

@@ -0,0 +1,221 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package commands
import (
"fmt"
"net/http"
"github.com/mattermost/mattermost-server/server/public/model"
"github.com/pkg/errors"
"github.com/mattermost/mattermost-server/server/v8/cmd/mmctl/printer"
"github.com/spf13/cobra"
)
func (s *MmctlUnitTestSuite) TestMakeAdminCmd() {
s.Run("Add admin privileges to user", func() {
printer.Clean()
mockUser := &model.User{Id: "1", Email: "u1@example.com", Roles: "system_user"}
newRoles := "system_user system_admin"
s.client.
EXPECT().
GetUserByEmail(mockUser.Email, "").
Return(mockUser, &model.Response{}, nil).
Times(1)
s.client.
EXPECT().
UpdateUserRoles(mockUser.Id, newRoles).
Return(&model.Response{StatusCode: http.StatusOK}, nil).
Times(1)
err := rolesSystemAdminCmdF(s.client, &cobra.Command{}, []string{mockUser.Email})
s.Require().Nil(err)
s.Require().Len(printer.GetLines(), 1)
s.Require().Len(printer.GetErrorLines(), 0)
s.Require().Equal(fmt.Sprintf("System admin role assigned to user %q. Current roles are: %s", mockUser.Email, "system_user, system_admin"), printer.GetLines()[0])
})
s.Run("Adding admin privileges to existing admin", func() {
printer.Clean()
roles := "system_user system_admin"
mockUser := &model.User{Id: "1", Email: "u1@example.com", Roles: roles}
s.client.
EXPECT().
GetUserByEmail(mockUser.Email, "").
Return(mockUser, &model.Response{}, nil).
Times(1)
err := rolesSystemAdminCmdF(s.client, &cobra.Command{}, []string{mockUser.Email})
s.Require().Nil(err)
s.Require().Len(printer.GetLines(), 0)
s.Require().Len(printer.GetErrorLines(), 0)
})
s.Run("Add admin to non existing user", func() {
printer.Clean()
emailArg := "doesnotexist@example.com"
s.client.
EXPECT().
GetUserByEmail(emailArg, "").
Return(nil, &model.Response{}, nil).
Times(1)
s.client.
EXPECT().
GetUserByUsername(emailArg, "").
Return(nil, &model.Response{}, nil).
Times(1)
s.client.
EXPECT().
GetUser(emailArg, "").
Return(nil, &model.Response{}, nil).
Times(1)
err := rolesSystemAdminCmdF(s.client, &cobra.Command{}, []string{emailArg})
s.Require().ErrorContains(err, "unable to find user")
s.Require().Len(printer.GetLines(), 0)
s.Require().Len(printer.GetErrorLines(), 1)
s.Require().Equal(fmt.Sprintf("unable to find user %q", emailArg), printer.GetErrorLines()[0])
})
s.Run("Error while updating admin role", func() {
printer.Clean()
mockUser := &model.User{Id: "1", Email: "u1@example.com", Roles: "system_user"}
newRoles := "system_user system_admin"
s.client.
EXPECT().
GetUserByEmail(mockUser.Email, "").
Return(mockUser, &model.Response{}, nil).
Times(1)
s.client.
EXPECT().
UpdateUserRoles(mockUser.Id, newRoles).
Return(&model.Response{StatusCode: http.StatusBadRequest}, errors.New("mock error")).
Times(1)
err := rolesSystemAdminCmdF(s.client, &cobra.Command{}, []string{mockUser.Email})
s.Require().ErrorContains(err, "can't update roles for user")
s.Require().Len(printer.GetLines(), 0)
s.Require().Len(printer.GetErrorLines(), 1)
s.Require().Contains(printer.GetErrorLines()[0], fmt.Sprintf("can't update roles for user %q", mockUser.Email))
})
}
func (s *MmctlUnitTestSuite) TestMakeMemberCmd() {
s.Run("Remove admin privileges for admin", func() {
printer.Clean()
mockUser := &model.User{Id: "1", Email: "u1@example.com", Roles: "system_user system_admin"}
s.client.
EXPECT().
GetUserByEmail(mockUser.Email, "").
Return(mockUser, &model.Response{}, nil).
Times(1)
s.client.
EXPECT().
UpdateUserRoles(mockUser.Id, "system_user").
Return(&model.Response{StatusCode: http.StatusOK}, nil).
Times(1)
err := rolesMemberCmdF(s.client, &cobra.Command{}, []string{mockUser.Email})
s.Require().Nil(err)
s.Require().Len(printer.GetLines(), 1)
s.Require().Len(printer.GetErrorLines(), 0)
s.Require().Equal(fmt.Sprintf("System admin role revoked for user %q. Current roles are: %s", mockUser.Email, "system_user"), printer.GetLines()[0])
})
s.Run("Remove admin privileges from non admin user", func() {
printer.Clean()
mockUser := &model.User{Id: "1", Email: "u1@example.com", Roles: "system_user"}
s.client.
EXPECT().
GetUserByEmail(mockUser.Email, "").
Return(mockUser, &model.Response{}, nil).
Times(1)
err := rolesMemberCmdF(s.client, &cobra.Command{}, []string{mockUser.Email})
s.Require().Nil(err)
s.Require().Len(printer.GetLines(), 0)
s.Require().Len(printer.GetErrorLines(), 0)
})
s.Run("Error while revoking admin role", func() {
printer.Clean()
mockUser := &model.User{Id: "1", Email: "u1@example.com", Roles: "system_user system_admin"}
s.client.
EXPECT().
GetUserByEmail(mockUser.Email, "").
Return(mockUser, &model.Response{}, nil).
Times(1)
s.client.
EXPECT().
UpdateUserRoles(mockUser.Id, "system_user").
Return(&model.Response{StatusCode: http.StatusBadRequest}, errors.New("mock error")).
Times(1)
err := rolesMemberCmdF(s.client, &cobra.Command{}, []string{mockUser.Email})
s.Require().ErrorContains(err, "can't update roles for user")
s.Require().Len(printer.GetLines(), 0)
s.Require().Len(printer.GetErrorLines(), 1)
s.Require().Contains(printer.GetErrorLines()[0], fmt.Sprintf("can't update roles for user %q", mockUser.Email))
})
s.Run("Remove admin from non existing user", func() {
printer.Clean()
emailArg := "doesnotexist@example.com"
s.client.
EXPECT().
GetUserByEmail(emailArg, "").
Return(nil, &model.Response{}, nil).
Times(1)
s.client.
EXPECT().
GetUserByUsername(emailArg, "").
Return(nil, &model.Response{}, nil).
Times(1)
s.client.
EXPECT().
GetUser(emailArg, "").
Return(nil, &model.Response{}, nil).
Times(1)
err := rolesMemberCmdF(s.client, &cobra.Command{}, []string{emailArg})
s.Require().ErrorContains(err, "unable to find user")
s.Require().Len(printer.GetLines(), 0)
s.Require().Len(printer.GetErrorLines(), 1)
s.Require().Equal(fmt.Sprintf("unable to find user %q", emailArg), printer.GetErrorLines()[0])
})
}