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