From 45565cb81fa05191ba015b3faf142ca133429387 Mon Sep 17 00:00:00 2001 From: Christopher Speller Date: Mon, 6 Jan 2020 11:17:19 -0800 Subject: [PATCH] Fix CLI nil pointer panic (#13359) --- cmd/mattermost/commands/channel.go | 4 ++++ cmd/mattermost/commands/channel_test.go | 27 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/cmd/mattermost/commands/channel.go b/cmd/mattermost/commands/channel.go index fe9a039367..7ed1a908f7 100644 --- a/cmd/mattermost/commands/channel.go +++ b/cmd/mattermost/commands/channel.go @@ -519,6 +519,10 @@ func modifyChannelCmdF(command *cobra.Command, args []string) error { } user := getUserFromUserArg(a, username) + if user == nil { + return fmt.Errorf("Unable to find user: '%v'", username) + } + if _, err := a.UpdateChannelPrivacy(channel, user); err != nil { return errors.Wrapf(err, "Failed to update channel ('%s') privacy", args[0]) } diff --git a/cmd/mattermost/commands/channel_test.go b/cmd/mattermost/commands/channel_test.go index b0ef798b6f..224f84697b 100644 --- a/cmd/mattermost/commands/channel_test.go +++ b/cmd/mattermost/commands/channel_test.go @@ -227,3 +227,30 @@ func Test_searchChannelCmdF(t *testing.T) { }) } } + +func TestModifyChannel(t *testing.T) { + th := Setup().InitBasic() + defer th.TearDown() + + channel1 := th.CreatePrivateChannel() + channel2 := th.CreatePrivateChannel() + + th.CheckCommand(t, "channel", "modify", "--public", th.BasicTeam.Name+":"+channel1.Name, "--username", th.BasicUser2.Email) + res, err := th.App.Srv.Store.Channel().Get(channel1.Id, false) + require.Nil(t, err) + assert.Equal(t, model.CHANNEL_OPEN, res.Type) + + // should fail because user doesn't exist + require.Error(t, th.RunCommand(t, "channel", "modify", "--public", th.BasicTeam.Name+":"+channel2.Name, "--username", "idonotexist")) + + pchannel1 := th.CreatePublicChannel() + pchannel2 := th.CreatePublicChannel() + + th.CheckCommand(t, "channel", "modify", "--private", th.BasicTeam.Name+":"+pchannel1.Name, "--username", th.BasicUser2.Email) + res, err = th.App.Srv.Store.Channel().Get(pchannel1.Id, false) + require.Nil(t, err) + assert.Equal(t, model.CHANNEL_PRIVATE, res.Type) + + // should fail because user doesn't exist + require.Error(t, th.RunCommand(t, "channel", "modify", "--private", th.BasicTeam.Name+":"+pchannel2.Name, "--username", "idonotexist")) +}