Files
upstream-mattermost/server/cmd/mmctl/commands/permission_role_test.go
upstream-mirror 240db4e2a7
Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
snapshot of https://github.com/mattermost/mattermost.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 00:29:55 +05:00

409 lines
10 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package commands
import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/v8/cmd/mmctl/printer"
"github.com/hashicorp/go-multierror"
"github.com/spf13/cobra"
)
func (s *MmctlUnitTestSuite) TestAssignUsersCmd() {
s.Run("Assigning a user to a role", func() {
mockRole := &model.Role{
Id: "mock-id",
Name: "mock-role",
Permissions: []string{"view", "edit"},
}
mockUser := &model.User{
Id: model.NewId(),
Username: "user1",
Roles: "system_user",
}
s.client.
EXPECT().
GetRoleByName(context.TODO(), mockRole.Name).
Return(mockRole, &model.Response{}, nil).
Times(1)
s.client.
EXPECT().
GetUserByUsername(context.TODO(), mockUser.Username, "").
Return(mockUser, &model.Response{}, nil).
Times(1)
s.client.
EXPECT().
UpdateUserRoles(context.TODO(), mockUser.Id, fmt.Sprintf("%s %s", mockUser.Roles, mockRole.Name)).
Return(&model.Response{StatusCode: http.StatusOK}, nil).
Times(1)
args := []string{mockRole.Name, mockUser.Username}
err := assignUsersCmdF(s.client, &cobra.Command{}, args)
s.Require().Nil(err)
})
s.Run("Assigning multiple users to a role", func() {
mockRole := &model.Role{
Id: "mock-id",
Name: "mock-role",
Permissions: []string{"view", "edit"},
}
mockUser1 := &model.User{
Id: model.NewId(),
Username: "user1",
Roles: "system_user",
}
mockUser2 := &model.User{
Id: model.NewId(),
Username: "user2",
Roles: "system_user system_admin",
}
notFoundUser := &model.User{
Username: "notfound",
}
s.client.
EXPECT().
GetRoleByName(context.TODO(), mockRole.Name).
Return(mockRole, &model.Response{}, nil).
Times(1)
for _, user := range []*model.User{mockUser1, mockUser2} {
s.client.
EXPECT().
GetUserByUsername(context.TODO(), user.Username, "").
Return(user, &model.Response{}, nil).
Times(1)
s.client.
EXPECT().
UpdateUserRoles(context.TODO(), user.Id, fmt.Sprintf("%s %s", user.Roles, mockRole.Name)).
Return(&model.Response{StatusCode: http.StatusOK}, nil).
Times(1)
}
s.client.
EXPECT().
GetUserByUsername(context.TODO(), notFoundUser.Username, "").
Return(nil, &model.Response{}, nil).
Times(1)
s.client.
EXPECT().
GetUser(context.TODO(), notFoundUser.Username, "").
Return(nil, &model.Response{}, nil).
Times(1)
expectedError := &multierror.Error{}
expectedError = multierror.Append(expectedError, fmt.Errorf("couldn't find user 'notfound'"))
args := []string{mockRole.Name, mockUser1.Username, notFoundUser.Username, mockUser2.Username}
err := assignUsersCmdF(s.client, &cobra.Command{}, args)
s.Require().NotNil(err)
s.Require().Equal(expectedError.ErrorOrNil(), err)
})
s.Run("Assigning to a non-existent role", func() {
expectedError := errors.New("role_not_found")
s.client.
EXPECT().
GetRoleByName(context.TODO(), "non-existent").
Return(nil, &model.Response{StatusCode: http.StatusNotFound}, expectedError).
Times(1)
args := []string{"non-existent", "user1"}
err := assignUsersCmdF(s.client, &cobra.Command{}, args)
s.Require().NotNil(err)
s.Require().Equal(expectedError, err)
})
s.Run("Assigning a user to a role that is already assigned", func() {
mockRole := &model.Role{
Id: "mock-id",
Name: "mock-role",
Permissions: []string{"view", "edit"},
}
mockUser := &model.User{
Id: model.NewId(),
Username: "user1",
Roles: "system_user mock-role",
}
s.client.
EXPECT().
GetRoleByName(context.TODO(), mockRole.Name).
Return(mockRole, &model.Response{}, nil).
Times(1)
s.client.
EXPECT().
GetUserByUsername(context.TODO(), mockUser.Username, "").
Return(mockUser, &model.Response{}, nil).
Times(1)
args := []string{mockRole.Name, mockUser.Username}
err := assignUsersCmdF(s.client, &cobra.Command{}, args)
s.Require().Nil(err)
})
s.Run("Assigning a user that is not found", func() {
mockRole := &model.Role{
Id: "mock-id",
Name: "mock-role",
Permissions: []string{"view", "edit"},
}
requestedUser := "user99"
s.client.
EXPECT().
GetRoleByName(context.TODO(), mockRole.Name).
Return(mockRole, &model.Response{}, nil).
Times(1)
s.client.
EXPECT().
GetUserByUsername(context.TODO(), requestedUser, "").
Return(nil, &model.Response{}, nil).
Times(1)
s.client.
EXPECT().
GetUser(context.TODO(), requestedUser, "").
Return(nil, &model.Response{}, nil).
Times(1)
expectedError := &multierror.Error{}
expectedError = multierror.Append(expectedError, fmt.Errorf("couldn't find user '%s'", requestedUser))
args := []string{mockRole.Name, requestedUser}
err := assignUsersCmdF(s.client, &cobra.Command{}, args)
s.Require().NotNil(err)
s.Require().Equal(expectedError.ErrorOrNil(), err)
})
}
func (s *MmctlUnitTestSuite) TestUnassignUsersCmd() {
s.Run("Unassigning a user from a role", func() {
roleName := "mock-role"
mockUser := &model.User{
Id: model.NewId(),
Username: "user1",
Roles: fmt.Sprintf("system_user %s team_admin", roleName),
}
s.client.
EXPECT().
GetUserByUsername(context.TODO(), mockUser.Username, "").
Return(mockUser, &model.Response{}, nil).
Times(1)
s.client.
EXPECT().
UpdateUserRoles(context.TODO(), mockUser.Id, "system_user team_admin").
Return(&model.Response{StatusCode: http.StatusOK}, nil).
Times(1)
args := []string{roleName, mockUser.Username}
err := unassignUsersCmdF(s.client, &cobra.Command{}, args)
s.Require().Nil(err)
})
s.Run("Unassign multiple users from a role", func() {
roleName := "mock-role"
mockUser1 := &model.User{
Id: model.NewId(),
Username: "user1",
Roles: "system_user mock-role",
}
mockUser2 := &model.User{
Id: model.NewId(),
Username: "user2",
Roles: "system_user system_admin mock-role",
}
notFoundUser := &model.User{
Username: "notfound",
}
for _, user := range []*model.User{mockUser1, mockUser2} {
s.client.
EXPECT().
GetUserByUsername(context.TODO(), user.Username, "").
Return(user, &model.Response{}, nil).
Times(1)
s.client.
EXPECT().
UpdateUserRoles(context.TODO(), user.Id, strings.TrimSpace(strings.ReplaceAll(user.Roles, roleName, ""))).
Return(&model.Response{StatusCode: http.StatusOK}, nil).
Times(1)
}
s.client.
EXPECT().
GetUserByUsername(context.TODO(), notFoundUser.Username, "").
Return(nil, &model.Response{}, nil).
Times(1)
s.client.
EXPECT().
GetUser(context.TODO(), notFoundUser.Username, "").
Return(nil, &model.Response{}, nil).
Times(1)
args := []string{roleName, mockUser1.Username, notFoundUser.Username, mockUser2.Username}
err := unassignUsersCmdF(s.client, &cobra.Command{}, args)
s.Require().Nil(err)
})
s.Run("Unassign from a non-assigned or role", func() {
roleName := "mock-role"
mockUser := &model.User{
Id: model.NewId(),
Username: "user1",
Roles: "system_user",
}
s.client.
EXPECT().
GetUserByUsername(context.TODO(), mockUser.Username, "").
Return(mockUser, &model.Response{}, nil).
Times(1)
args := []string{roleName, mockUser.Username}
err := unassignUsersCmdF(s.client, &cobra.Command{}, args)
s.Require().Nil(err)
})
s.Run("Unassigning a user that is not found", func() {
requestedUser := "user99"
s.client.
EXPECT().
GetUserByUsername(context.TODO(), requestedUser, "").
Return(nil, &model.Response{}, nil).
Times(1)
s.client.
EXPECT().
GetUser(context.TODO(), requestedUser, "").
Return(nil, &model.Response{}, nil).
Times(1)
args := []string{"mock-role-id", requestedUser}
err := unassignUsersCmdF(s.client, &cobra.Command{}, args)
s.Require().Nil(err)
})
}
func (s *MmctlUnitTestSuite) TestShowRoleCmd() {
s.Run("Show custom role", func() {
printer.Clean()
printer.SetFormat(printer.FormatPlain)
defer printer.SetFormat(printer.FormatJSON)
commandArg := "example-role-name"
mockRole := &model.Role{
Id: "example-mock-id",
Name: commandArg,
}
s.client.
EXPECT().
GetRoleByName(context.TODO(), mockRole.Name).
Return(mockRole, &model.Response{}, nil).
Times(1)
err := showRoleCmdF(s.client, &cobra.Command{}, []string{commandArg})
s.Require().Nil(err)
s.Require().Len(printer.GetLines(), 1)
s.Require().Len(printer.GetErrorLines(), 0)
s.Equal(`
Property Value
-------- -----
Name example-role-name
DisplayName
BuiltIn false
SchemeManaged false
`, printer.GetLines()[0])
})
s.Run("Show a role with a sysconsole_* permission", func() {
printer.Clean()
printer.SetFormat(printer.FormatPlain)
defer printer.SetFormat(printer.FormatJSON)
commandArg := "example-role-name"
mockRole := &model.Role{
Id: "example-mock-id",
Name: commandArg,
Permissions: []string{"sysconsole_write_site", "edit_brand"},
}
s.client.
EXPECT().
GetRoleByName(context.TODO(), mockRole.Name).
Return(mockRole, &model.Response{}, nil).
Times(1)
err := showRoleCmdF(s.client, &cobra.Command{}, []string{commandArg})
s.Require().Nil(err)
s.Require().Len(printer.GetLines(), 1)
s.Require().Len(printer.GetErrorLines(), 0)
s.Equal(`
Property Value Used by
-------- ----- -------
Name example-role-name
DisplayName
BuiltIn false
SchemeManaged false
Permissions edit_brand
sysconsole_write_site
`, printer.GetLines()[0])
})
s.Run("Show custom role with invalid name", func() {
printer.Clean()
expectedError := errors.New("role_not_found")
commandArgBogus := "bogus-role-name"
// showRoleCmdF will look up role by name
s.client.
EXPECT().
GetRoleByName(context.TODO(), commandArgBogus).
Return(nil, &model.Response{StatusCode: http.StatusNotFound}, expectedError).
Times(1)
err := showRoleCmdF(s.client, &cobra.Command{}, []string{commandArgBogus})
s.Require().NotNil(err)
s.Require().Equal(expectedError, err)
s.Require().Len(printer.GetLines(), 0)
s.Require().Len(printer.GetErrorLines(), 0)
})
}