Files
upstream-mattermost/server/cmd/mmctl/commands/command_e2e_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

379 lines
12 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package commands
import (
"context"
"fmt"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/v8/channels/api4"
"github.com/spf13/cobra"
"github.com/mattermost/mattermost/server/v8/cmd/mmctl/client"
"github.com/mattermost/mattermost/server/v8/cmd/mmctl/printer"
)
func (s *MmctlE2ETestSuite) TestListCommandCmd() {
s.SetupTestHelper().InitBasic(s.T())
s.RunForAllClients("List commands for a non existing team", func(c client.Client) {
printer.Clean()
nonexistentTeamID := "nonexistent-team-id"
err := listCommandCmdF(c, &cobra.Command{}, []string{nonexistentTeamID})
s.Require().Error(err)
s.Len(printer.GetLines(), 0)
s.Len(printer.GetErrorLines(), 1)
s.Equal("Unable to find team '"+nonexistentTeamID+"'", printer.GetErrorLines()[0])
})
s.RunForAllClients("List commands for a specific team", func(c client.Client) {
printer.Clean()
team, appErr := s.th.App.CreateTeam(s.th.Context, &model.Team{
DisplayName: "dn_" + model.NewId(),
Name: api4.GenerateTestTeamName(),
Email: s.th.BasicUser.Email,
Type: model.TeamOpen,
})
s.Require().Nil(appErr)
_, _, appErr = s.th.App.AddUserToTeam(s.th.Context, team.Id, s.th.BasicUser.Id, "")
s.Require().Nil(appErr)
command, appErr := s.th.App.CreateCommand(&model.Command{
DisplayName: "command",
CreatorId: s.th.BasicUser.Id,
TeamId: team.Id,
URL: "http://localhost:8000/example",
Method: model.CommandMethodGet,
Trigger: "trigger",
})
s.Require().Nil(appErr)
defer func() {
appErr = s.th.App.DeleteCommand(command.Id)
s.Require().Nil(appErr)
}()
err := listCommandCmdF(c, &cobra.Command{}, []string{team.Id})
s.Require().Nil(err)
s.Len(printer.GetLines(), 1)
s.Equal(command, printer.GetLines()[0])
s.Len(printer.GetErrorLines(), 0)
})
s.Run("List all commands from all teams", func() {
// add team1
team1, appErr := s.th.App.CreateTeam(s.th.Context, &model.Team{
DisplayName: "dn_" + model.NewId(),
Name: api4.GenerateTestTeamName(),
Email: s.th.BasicUser.Email,
Type: model.TeamOpen,
})
s.Require().Nil(appErr)
_, _, appErr = s.th.App.AddUserToTeam(s.th.Context, team1.Id, s.th.BasicUser.Id, "")
s.Require().Nil(appErr)
command1, appErr := s.th.App.CreateCommand(&model.Command{
DisplayName: "command1",
CreatorId: s.th.BasicUser.Id,
TeamId: team1.Id,
URL: "http://localhost:8000/example",
Method: model.CommandMethodGet,
Trigger: "trigger",
})
s.Require().Nil(appErr)
defer func() {
appErr = s.th.App.DeleteCommand(command1.Id)
s.Require().Nil(appErr)
}()
// add team 2
team2, appErr := s.th.App.CreateTeam(s.th.Context, &model.Team{
DisplayName: "dn_" + model.NewId(),
Name: api4.GenerateTestTeamName(),
Email: s.th.BasicUser.Email,
Type: model.TeamOpen,
})
s.Require().Nil(appErr)
_, _, appErr = s.th.App.AddUserToTeam(s.th.Context, team2.Id, s.th.BasicUser.Id, "")
s.Require().Nil(appErr)
command2, appErr := s.th.App.CreateCommand(&model.Command{
DisplayName: "command2",
CreatorId: s.th.BasicUser.Id,
TeamId: team2.Id,
URL: "http://localhost:8000/example",
Method: model.CommandMethodGet,
Trigger: "trigger",
})
s.Require().Nil(appErr)
defer func() {
appErr = s.th.App.DeleteCommand(command2.Id)
s.Require().Nil(appErr)
}()
s.RunForSystemAdminAndLocal("Run list command", func(c client.Client) {
printer.Clean()
err := listCommandCmdF(c, &cobra.Command{}, []string{})
s.Require().Nil(err)
s.Len(printer.GetLines(), 2)
s.ElementsMatch([]*model.Command{command1, command2}, printer.GetLines())
s.Len(printer.GetErrorLines(), 0)
})
})
s.Run("List commands for a specific team without permission", func() {
printer.Clean()
team, appErr := s.th.App.CreateTeam(s.th.Context, &model.Team{
DisplayName: "dn_" + model.NewId(),
Name: api4.GenerateTestTeamName(),
Email: s.th.BasicUser.Email,
Type: model.TeamOpen,
})
s.Require().Nil(appErr)
command, appErr := s.th.App.CreateCommand(&model.Command{
DisplayName: "command",
CreatorId: s.th.BasicUser.Id,
TeamId: team.Id,
URL: "http://localhost:8000/example",
Method: model.CommandMethodGet,
Trigger: "trigger",
})
s.Require().Nil(appErr)
defer func() {
appErr = s.th.App.DeleteCommand(command.Id)
s.Require().Nil(appErr)
}()
err := listCommandCmdF(s.th.Client, &cobra.Command{}, []string{team.Id})
s.Require().Error(err)
s.Len(printer.GetLines(), 0)
s.Len(printer.GetErrorLines(), 1)
s.Equal("Unable to find team '"+team.Id+"'", printer.GetErrorLines()[0])
})
}
func (s *MmctlE2ETestSuite) TestArchiveCommandCmdF() {
s.SetupTestHelper().InitBasic(s.T())
teamOfBasicUser, appErr := s.th.App.CreateTeam(s.th.Context, &model.Team{
DisplayName: "dn_" + model.NewId(),
Name: api4.GenerateTestTeamName(),
Email: s.th.BasicUser.Email,
Type: model.TeamOpen,
})
s.Require().Nil(appErr)
_, _, appErr = s.th.App.AddUserToTeam(s.th.Context, teamOfBasicUser.Id, s.th.BasicUser.Id, "")
s.Require().Nil(appErr)
s.RunForAllClients("Archive nonexistent command", func(c client.Client) {
printer.Clean()
nonexistentCommandID := "nonexistent-command-id"
err := archiveCommandCmdF(c, &cobra.Command{}, []string{nonexistentCommandID})
s.Require().NotNil(err)
s.Require().Equal(fmt.Sprintf("Unable to archive command '%s' error: Sorry, we could not find the page., There doesn't appear to be an api call for the url='/api/v4/commands/nonexistent-command-id'. Typo? are you missing a team_id or user_id as part of the url?", nonexistentCommandID), err.Error())
s.Require().Len(printer.GetLines(), 0)
s.Require().Len(printer.GetErrorLines(), 0)
})
s.RunForAllClients("Archive command", func(c client.Client) {
printer.Clean()
command, appErr := s.th.App.CreateCommand(&model.Command{
TeamId: teamOfBasicUser.Id,
DisplayName: "command",
Description: "command",
Trigger: api4.GenerateTestID(),
URL: "http://localhost:8000/example",
CreatorId: s.th.BasicUser.Id,
Username: s.th.BasicUser.Username,
IconURL: "http://localhost:8000/icon.ico",
Method: model.CommandMethodGet,
})
s.Require().Nil(appErr)
err := archiveCommandCmdF(c, &cobra.Command{}, []string{command.Id})
s.Require().Nil(err)
s.Require().Len(printer.GetLines(), 1)
s.Require().Equal(map[string]any{"status": "ok"}, printer.GetLines()[0])
s.Require().Len(printer.GetErrorLines(), 0)
rcommand, err := s.th.App.GetCommand(command.Id)
s.Require().NotNil(err)
s.Require().Nil(rcommand)
s.Require().Contains(err.Error(), "SqlCommandStore.Get: Command does not exist., ")
})
s.Run("Archive command without permission", func() {
printer.Clean()
teamOfAdminUser, appErr := s.th.App.CreateTeam(s.th.Context, &model.Team{
DisplayName: "dn_" + model.NewId(),
Name: api4.GenerateTestTeamName(),
Email: s.th.SystemAdminUser.Email,
Type: model.TeamOpen,
})
s.Require().Nil(appErr)
command, appErr := s.th.App.CreateCommand(&model.Command{
TeamId: teamOfAdminUser.Id,
DisplayName: "command",
Description: "command",
Trigger: api4.GenerateTestID(),
URL: "http://localhost:8000/example",
CreatorId: s.th.SystemAdminUser.Id,
Username: s.th.SystemAdminUser.Username,
IconURL: "http://localhost:8000/icon.ico",
Method: model.CommandMethodGet,
})
s.Require().Nil(appErr)
err := archiveCommandCmdF(s.th.Client, &cobra.Command{}, []string{command.Id})
s.Require().NotNil(err)
s.Require().Equal(fmt.Sprintf("Unable to archive command '%s' error: Unable to get the command.", command.Id), err.Error())
rcommand, err := s.th.App.GetCommand(command.Id)
s.Require().Nil(err)
s.Require().NotNil(rcommand)
s.Require().Equal(int64(0), rcommand.DeleteAt)
})
}
func (s *MmctlE2ETestSuite) TestModifyCommandCmdF() {
s.SetupTestHelper().InitBasic(s.T())
// create new command
newCmd := &model.Command{
CreatorId: s.th.BasicUser.Id,
TeamId: s.th.BasicTeam.Id,
URL: "http://nowhere.com",
Method: model.CommandMethodPost,
Trigger: "trigger",
}
command, _, _ := s.th.SystemAdminClient.CreateCommand(context.Background(), newCmd)
index := 0
s.RunForSystemAdminAndLocal("modifyCommandCmdF", func(c client.Client) {
printer.Clean()
// Reset the cmd and parse to force Flag.Changed to be true.
cmd := CommandModifyCmd
cmd.ResetFlags()
addCommandFieldsFlags(cmd)
url := fmt.Sprintf("%s-%d", command.URL, index)
index++
err := cmd.ParseFlags([]string{
command.Id,
"--url=" + url,
})
s.Require().Nil(err)
err = modifyCommandCmdF(c, cmd, []string{command.Id})
s.Require().Nil(err)
s.Len(printer.GetLines(), 1)
s.Len(printer.GetErrorLines(), 0)
changedCommand, err := s.th.App.GetCommand(command.Id)
s.Require().Nil(err)
s.Require().Equal(url, changedCommand.URL)
})
s.RunForSystemAdminAndLocal("modifyCommandCmdF for command that does not exist", func(c client.Client) {
printer.Clean()
cmd := &cobra.Command{}
err := modifyCommandCmdF(c, cmd, []string{"nothing"})
s.Require().NotNil(err)
s.Require().Equal("unable to find command 'nothing'", err.Error())
})
s.RunForSystemAdminAndLocal("modifyCommandCmdF with a space in trigger word", func(c client.Client) {
printer.Clean()
// Reset the cmd and parse to force Flag.Changed to be true.
cmd := CommandModifyCmd
cmd.ResetFlags()
addCommandFieldsFlags(cmd)
err := cmd.ParseFlags([]string{
command.Id,
"--trigger-word=modified with space",
})
s.Require().Nil(err)
err = modifyCommandCmdF(c, cmd, []string{command.Id})
s.Require().NotNil(err)
s.Len(printer.GetLines(), 0)
s.Len(printer.GetErrorLines(), 0)
s.EqualError(err, "a trigger word must not contain spaces")
})
}
func (s *MmctlE2ETestSuite) TestShowCommandCmdF() {
s.SetupTestHelper().InitBasic(s.T())
s.RunForSystemAdminAndLocal("Show non existent cmd", func(c client.Client) {
printer.Clean()
err := showCommandCmdF(c, &cobra.Command{}, []string{"nonexistent-command-id"})
s.Require().Error(err)
s.Require().Equal("unable to find command 'nonexistent-command-id'", err.Error())
s.Require().Len(printer.GetLines(), 0)
s.Require().Len(printer.GetErrorLines(), 0)
})
s.RunForSystemAdminAndLocal("Show commands with cmd id", func(c client.Client) {
// create new command
printer.Clean()
newCmd := &model.Command{
CreatorId: s.th.BasicUser.Id,
TeamId: s.th.BasicTeam.Id,
URL: "http://nowhere.com",
Method: model.CommandMethodPost,
Trigger: model.NewRandomString(6),
}
command, _, err := c.CreateCommand(context.Background(), newCmd)
s.Require().NoError(err)
err = showCommandCmdF(c, &cobra.Command{}, []string{command.Id})
s.Require().NoError(err)
s.Len(printer.GetLines(), 1)
s.Len(printer.GetErrorLines(), 0)
s.Equal(command, printer.GetLines()[0])
})
s.RunForSystemAdminAndLocal("Show commands with team:trigger", func(c client.Client) {
// create new command
printer.Clean()
trigger := model.NewRandomString(6)
newCmd := &model.Command{
CreatorId: s.th.BasicUser.Id,
TeamId: s.th.BasicTeam.Id,
URL: "http://nowhere.com",
Method: model.CommandMethodPost,
Trigger: trigger,
}
command, _, err := c.CreateCommand(context.Background(), newCmd)
s.Require().NoError(err)
err = showCommandCmdF(c, &cobra.Command{}, []string{s.th.BasicTeam.Name + ":" + trigger})
s.Require().NoError(err)
s.Len(printer.GetLines(), 1)
s.Len(printer.GetErrorLines(), 0)
outputCmd := printer.GetLines()[0].(*model.Command)
s.Require().Equal(command.TeamId, outputCmd.TeamId)
s.Require().Equal(command.Trigger, outputCmd.Trigger)
})
}