github.com/haalcala/mattermost-server-change-repo@v0.0.0-20210713015153-16753fbeee5f/app/slashcommands/command_mute_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package slashcommands 5 6 import ( 7 "testing" 8 "time" 9 10 "github.com/mattermost/go-i18n/i18n" 11 "github.com/stretchr/testify/assert" 12 13 "github.com/mattermost/mattermost-server/v5/model" 14 ) 15 16 func TestMuteCommandNoChannel(t *testing.T) { 17 th := setup(t).initBasic() 18 defer th.tearDown() 19 20 if testing.Short() { 21 t.SkipNow() 22 } 23 24 channel1 := th.BasicChannel 25 channel1M, channel1MError := th.App.GetChannelMember(channel1.Id, th.BasicUser.Id) 26 27 assert.Nil(t, channel1MError, "User is not a member of channel 1") 28 assert.NotEqual( 29 t, 30 channel1M.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP], 31 model.CHANNEL_NOTIFY_MENTION, 32 "Channel shouldn't be muted on initial setup", 33 ) 34 35 cmd := &MuteProvider{} 36 resp := cmd.DoCommand(th.App, &model.CommandArgs{ 37 T: i18n.IdentityTfunc(), 38 UserId: th.BasicUser.Id, 39 }, "") 40 assert.Equal(t, "api.command_mute.no_channel.error", resp.Text) 41 } 42 43 func TestMuteCommandNoArgs(t *testing.T) { 44 th := setup(t).initBasic() 45 defer th.tearDown() 46 47 channel1 := th.BasicChannel 48 channel1M, _ := th.App.GetChannelMember(channel1.Id, th.BasicUser.Id) 49 50 assert.Equal(t, model.CHANNEL_NOTIFY_ALL, channel1M.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP]) 51 52 cmd := &MuteProvider{} 53 54 // First mute the channel 55 resp := cmd.DoCommand(th.App, &model.CommandArgs{ 56 T: i18n.IdentityTfunc(), 57 ChannelId: channel1.Id, 58 UserId: th.BasicUser.Id, 59 }, "") 60 assert.Equal(t, "api.command_mute.success_mute", resp.Text) 61 62 // Now unmute the channel 63 time.Sleep(time.Millisecond) 64 resp = cmd.DoCommand(th.App, &model.CommandArgs{ 65 T: i18n.IdentityTfunc(), 66 ChannelId: channel1.Id, 67 UserId: th.BasicUser.Id, 68 }, "") 69 70 assert.Equal(t, "api.command_mute.success_unmute", resp.Text) 71 } 72 73 func TestMuteCommandSpecificChannel(t *testing.T) { 74 th := setup(t).initBasic() 75 defer th.tearDown() 76 77 if testing.Short() { 78 t.SkipNow() 79 } 80 81 channel1 := th.BasicChannel 82 channel2, _ := th.App.CreateChannel(&model.Channel{ 83 DisplayName: "AA", 84 Name: "aa" + model.NewId() + "a", 85 Type: model.CHANNEL_OPEN, 86 TeamId: th.BasicTeam.Id, 87 CreatorId: th.BasicUser.Id, 88 }, true) 89 90 channel2M, _ := th.App.GetChannelMember(channel2.Id, th.BasicUser.Id) 91 92 assert.Equal(t, model.CHANNEL_NOTIFY_ALL, channel2M.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP]) 93 94 cmd := &MuteProvider{} 95 96 // First mute the channel 97 resp := cmd.DoCommand(th.App, &model.CommandArgs{ 98 T: i18n.IdentityTfunc(), 99 ChannelId: channel1.Id, 100 UserId: th.BasicUser.Id, 101 }, channel2.Name) 102 assert.Equal(t, "api.command_mute.success_mute", resp.Text) 103 channel2M, _ = th.App.GetChannelMember(channel2.Id, th.BasicUser.Id) 104 assert.Equal(t, model.CHANNEL_NOTIFY_MENTION, channel2M.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP]) 105 106 // Now unmute the channel 107 resp = cmd.DoCommand(th.App, &model.CommandArgs{ 108 T: i18n.IdentityTfunc(), 109 ChannelId: channel1.Id, 110 UserId: th.BasicUser.Id, 111 }, "~"+channel2.Name) 112 113 assert.Equal(t, "api.command_mute.success_unmute", resp.Text) 114 channel2M, _ = th.App.GetChannelMember(channel2.Id, th.BasicUser.Id) 115 assert.Equal(t, model.CHANNEL_NOTIFY_ALL, channel2M.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP]) 116 } 117 118 func TestMuteCommandNotMember(t *testing.T) { 119 th := setup(t).initBasic() 120 defer th.tearDown() 121 122 if testing.Short() { 123 t.SkipNow() 124 } 125 126 channel1 := th.BasicChannel 127 channel2, _ := th.App.CreateChannel(&model.Channel{ 128 DisplayName: "AA", 129 Name: "aa" + model.NewId() + "a", 130 Type: model.CHANNEL_OPEN, 131 TeamId: th.BasicTeam.Id, 132 CreatorId: th.BasicUser.Id, 133 }, false) 134 135 cmd := &MuteProvider{} 136 137 // First mute the channel 138 resp := cmd.DoCommand(th.App, &model.CommandArgs{ 139 T: i18n.IdentityTfunc(), 140 ChannelId: channel1.Id, 141 UserId: th.BasicUser.Id, 142 }, channel2.Name) 143 assert.Equal(t, "api.command_mute.not_member.error", resp.Text) 144 } 145 146 func TestMuteCommandNotChannel(t *testing.T) { 147 th := setup(t).initBasic() 148 defer th.tearDown() 149 150 if testing.Short() { 151 t.SkipNow() 152 } 153 154 channel1 := th.BasicChannel 155 156 cmd := &MuteProvider{} 157 158 // First mute the channel 159 resp := cmd.DoCommand(th.App, &model.CommandArgs{ 160 T: i18n.IdentityTfunc(), 161 ChannelId: channel1.Id, 162 UserId: th.BasicUser.Id, 163 }, "~noexists") 164 assert.Equal(t, "api.command_mute.error", resp.Text) 165 } 166 167 func TestMuteCommandDMChannel(t *testing.T) { 168 th := setup(t).initBasic() 169 defer th.tearDown() 170 171 if testing.Short() { 172 t.SkipNow() 173 } 174 175 channel2, _ := th.App.GetOrCreateDirectChannel(th.BasicUser.Id, th.BasicUser2.Id) 176 channel2M, _ := th.App.GetChannelMember(channel2.Id, th.BasicUser.Id) 177 178 assert.Equal(t, model.CHANNEL_NOTIFY_ALL, channel2M.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP]) 179 180 cmd := &MuteProvider{} 181 182 // First mute the channel 183 resp := cmd.DoCommand(th.App, &model.CommandArgs{ 184 T: i18n.IdentityTfunc(), 185 ChannelId: channel2.Id, 186 UserId: th.BasicUser.Id, 187 }, "") 188 assert.Equal(t, "api.command_mute.success_mute_direct_msg", resp.Text) 189 time.Sleep(time.Millisecond) 190 channel2M, _ = th.App.GetChannelMember(channel2.Id, th.BasicUser.Id) 191 assert.Equal(t, model.CHANNEL_NOTIFY_MENTION, channel2M.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP]) 192 193 // Now unmute the channel 194 resp = cmd.DoCommand(th.App, &model.CommandArgs{ 195 T: i18n.IdentityTfunc(), 196 ChannelId: channel2.Id, 197 UserId: th.BasicUser.Id, 198 }, "") 199 200 assert.Equal(t, "api.command_mute.success_unmute_direct_msg", resp.Text) 201 time.Sleep(time.Millisecond) 202 channel2M, _ = th.App.GetChannelMember(channel2.Id, th.BasicUser.Id) 203 assert.Equal(t, model.CHANNEL_NOTIFY_ALL, channel2M.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP]) 204 }