Commit d3c3b150 authored by Jesús Espino's avatar Jesús Espino

MM-10699: Disallow renaming direct and group message channels (#9518)

* MM-10699: Disallow renaming direct and group message channels

* Replacing errors.New with errors.Wrapf
parent afe70295
......@@ -163,6 +163,14 @@ func (a *App) CreateChannelWithUser(channel *model.Channel, userId string) (*mod
// RenameChannel is used to rename the channel Name and the DisplayName fields
func (a *App) RenameChannel(channel *model.Channel, newChannelName string, newDisplayName string) (*model.Channel, *model.AppError) {
if channel.Type == model.CHANNEL_DIRECT {
return nil, model.NewAppError("RenameChannel", "api.channel.rename_channel.cant_rename_direct_messages.app_error", nil, "", http.StatusBadRequest)
}
if channel.Type == model.CHANNEL_GROUP {
return nil, model.NewAppError("RenameChannel", "api.channel.rename_channel.cant_rename_group_messages.app_error", nil, "", http.StatusBadRequest)
}
channel.Name = newChannelName
if newDisplayName != "" {
channel.DisplayName = newDisplayName
......
......@@ -640,12 +640,45 @@ func TestRenameChannel(t *testing.T) {
th := Setup().InitBasic()
defer th.TearDown()
channel := th.createChannel(th.BasicTeam, model.CHANNEL_OPEN)
channel, err := th.App.RenameChannel(channel, "newchannelname", "New Display Name")
if err != nil {
t.Fatal("Failed to update channel name. Error: " + err.Error())
testCases := []struct {
Name string
Channel *model.Channel
ExpectError bool
ExpectedName string
ExpectedDisplayName string
}{
{
"Rename open channel",
th.createChannel(th.BasicTeam, model.CHANNEL_OPEN),
false,
"newchannelname",
"New Display Name",
},
{
"Fail on rename direct message channel",
th.CreateDmChannel(th.BasicUser2),
true,
"",
"",
},
{
"Fail on rename direct message channel",
th.CreateGroupChannel(th.BasicUser2, th.CreateUser()),
true,
"",
"",
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
channel, err := th.App.RenameChannel(tc.Channel, "newchannelname", "New Display Name")
if tc.ExpectError {
assert.NotNil(t, err)
} else {
assert.Equal(t, tc.ExpectedName, channel.Name)
assert.Equal(t, tc.ExpectedDisplayName, channel.DisplayName)
}
})
}
assert.Equal(t, "newchannelname", channel.Name)
assert.Equal(t, "New Display Name", channel.DisplayName)
}
......@@ -4,11 +4,11 @@
package commands
import (
"errors"
"fmt"
"github.com/mattermost/mattermost-server/app"
"github.com/mattermost/mattermost-server/model"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
......@@ -518,7 +518,7 @@ func modifyChannelCmdF(command *cobra.Command, args []string) error {
user := getUserFromUserArg(a, username)
if _, err := a.UpdateChannelPrivacy(channel, user); err != nil {
return errors.New("Failed to update channel ('" + args[0] + "') privacy - " + err.Error())
return errors.Wrapf(err, "Failed to update channel ('%s') privacy", args[0])
}
return nil
......@@ -549,7 +549,7 @@ func renameChannelCmdF(command *cobra.Command, args []string) error {
_, errch := a.RenameChannel(channel, newChannelName, newDisplayName)
if errch != nil {
return errors.New("Error in updating channel from " + channel.Name + " to " + newChannelName + err.Error())
return errors.Wrapf(errch, "Error in updating channel from %s to %s", channel.Name, newChannelName)
}
return nil
......
......@@ -239,6 +239,14 @@
"id": "api.channel.remove_member.removed",
"translation": "%v removed from the channel."
},
{
"id": "api.channel.rename_channel.cant_rename_direct_messages.app_error",
"translation": "You cannot rename a direct message channel"
},
{
"id": "api.channel.rename_channel.cant_rename_group_messages.app_error",
"translation": "You cannot rename a group message channel"
},
{
"id": "api.channel.update_channel.deleted.app_error",
"translation": "The channel has been archived or deleted"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment