github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/app/auto_responder_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package app 5 6 import ( 7 "testing" 8 9 "github.com/mattermost/mattermost-server/v5/model" 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestSetAutoResponderStatus(t *testing.T) { 15 th := Setup(t) 16 defer th.TearDown() 17 18 user := th.CreateUser() 19 defer th.App.PermanentDeleteUser(user) 20 21 th.App.SetStatusOnline(user.Id, true) 22 23 patch := &model.UserPatch{} 24 patch.NotifyProps = make(map[string]string) 25 patch.NotifyProps["auto_responder_active"] = "true" 26 patch.NotifyProps["auto_responder_message"] = "Hello, I'm unavailable today." 27 28 userUpdated1, _ := th.App.PatchUser(user.Id, patch, true) 29 30 // autoResponder is enabled, status should be OOO 31 th.App.SetAutoResponderStatus(userUpdated1, user.NotifyProps) 32 33 status, err := th.App.GetStatus(userUpdated1.Id) 34 require.Nil(t, err) 35 assert.Equal(t, model.STATUS_OUT_OF_OFFICE, status.Status) 36 37 patch2 := &model.UserPatch{} 38 patch2.NotifyProps = make(map[string]string) 39 patch2.NotifyProps["auto_responder_active"] = "false" 40 patch2.NotifyProps["auto_responder_message"] = "Hello, I'm unavailable today." 41 42 userUpdated2, _ := th.App.PatchUser(user.Id, patch2, true) 43 44 // autoResponder is disabled, status should be ONLINE 45 th.App.SetAutoResponderStatus(userUpdated2, userUpdated1.NotifyProps) 46 47 status, err = th.App.GetStatus(userUpdated2.Id) 48 require.Nil(t, err) 49 assert.Equal(t, model.STATUS_ONLINE, status.Status) 50 51 } 52 53 func TestDisableAutoResponder(t *testing.T) { 54 th := Setup(t) 55 defer th.TearDown() 56 57 user := th.CreateUser() 58 defer th.App.PermanentDeleteUser(user) 59 60 th.App.SetStatusOnline(user.Id, true) 61 62 patch := &model.UserPatch{} 63 patch.NotifyProps = make(map[string]string) 64 patch.NotifyProps["auto_responder_active"] = "true" 65 patch.NotifyProps["auto_responder_message"] = "Hello, I'm unavailable today." 66 67 th.App.PatchUser(user.Id, patch, true) 68 69 th.App.DisableAutoResponder(user.Id, true) 70 71 userUpdated1, err := th.App.GetUser(user.Id) 72 require.Nil(t, err) 73 assert.Equal(t, userUpdated1.NotifyProps["auto_responder_active"], "false") 74 75 th.App.DisableAutoResponder(user.Id, true) 76 77 userUpdated2, err := th.App.GetUser(user.Id) 78 require.Nil(t, err) 79 assert.Equal(t, userUpdated2.NotifyProps["auto_responder_active"], "false") 80 } 81 82 func TestSendAutoResponseIfNecessary(t *testing.T) { 83 t.Run("should send auto response when enabled", func(t *testing.T) { 84 th := Setup(t).InitBasic() 85 defer th.TearDown() 86 87 receiver := th.CreateUser() 88 89 patch := &model.UserPatch{ 90 NotifyProps: map[string]string{ 91 "auto_responder_active": "true", 92 "auto_responder_message": "Hello, I'm unavailable today.", 93 }, 94 } 95 receiver, err := th.App.PatchUser(receiver.Id, patch, true) 96 require.Nil(t, err) 97 98 channel := th.CreateDmChannel(receiver) 99 100 sent, err := th.App.SendAutoResponseIfNecessary(channel, th.BasicUser) 101 102 assert.Nil(t, err) 103 assert.True(t, sent) 104 }) 105 106 t.Run("should not send auto response when disabled", func(t *testing.T) { 107 th := Setup(t).InitBasic() 108 defer th.TearDown() 109 110 receiver := th.CreateUser() 111 112 patch := &model.UserPatch{ 113 NotifyProps: map[string]string{ 114 "auto_responder_active": "false", 115 "auto_responder_message": "Hello, I'm unavailable today.", 116 }, 117 } 118 receiver, err := th.App.PatchUser(receiver.Id, patch, true) 119 require.Nil(t, err) 120 121 channel := th.CreateDmChannel(receiver) 122 123 sent, err := th.App.SendAutoResponseIfNecessary(channel, th.BasicUser) 124 125 assert.Nil(t, err) 126 assert.False(t, sent) 127 }) 128 129 t.Run("should not send auto response for non-DM channel", func(t *testing.T) { 130 th := Setup(t).InitBasic() 131 defer th.TearDown() 132 133 sent, err := th.App.SendAutoResponseIfNecessary(th.BasicChannel, th.BasicUser) 134 135 assert.Nil(t, err) 136 assert.False(t, sent) 137 }) 138 139 t.Run("should not send auto response for bot", func(t *testing.T) { 140 th := Setup(t).InitBasic() 141 defer th.TearDown() 142 143 receiver := th.CreateUser() 144 145 patch := &model.UserPatch{ 146 NotifyProps: map[string]string{ 147 "auto_responder_active": "true", 148 "auto_responder_message": "Hello, I'm unavailable today.", 149 }, 150 } 151 receiver, err := th.App.PatchUser(receiver.Id, patch, true) 152 require.Nil(t, err) 153 154 channel := th.CreateDmChannel(receiver) 155 156 bot, err := th.App.CreateBot(&model.Bot{ 157 Username: "botusername", 158 Description: "bot", 159 OwnerId: th.BasicUser.Id, 160 }) 161 assert.Nil(t, err) 162 163 botUser, err := th.App.GetUser(bot.UserId) 164 assert.Nil(t, err) 165 166 sent, err := th.App.SendAutoResponseIfNecessary(channel, botUser) 167 168 assert.Nil(t, err) 169 assert.False(t, sent) 170 }) 171 } 172 173 func TestSendAutoResponseSuccess(t *testing.T) { 174 th := Setup(t).InitBasic() 175 defer th.TearDown() 176 177 user := th.CreateUser() 178 defer th.App.PermanentDeleteUser(user) 179 180 patch := &model.UserPatch{} 181 patch.NotifyProps = make(map[string]string) 182 patch.NotifyProps["auto_responder_active"] = "true" 183 patch.NotifyProps["auto_responder_message"] = "Hello, I'm unavailable today." 184 185 userUpdated1, err := th.App.PatchUser(user.Id, patch, true) 186 require.Nil(t, err) 187 188 th.App.CreatePost(&model.Post{ 189 ChannelId: th.BasicChannel.Id, 190 Message: "zz" + model.NewId() + "a", 191 UserId: th.BasicUser.Id}, 192 th.BasicChannel, 193 false, true) 194 195 sent, err := th.App.SendAutoResponse(th.BasicChannel, userUpdated1) 196 197 assert.Nil(t, err) 198 assert.True(t, sent) 199 200 if list, err := th.App.GetPosts(th.BasicChannel.Id, 0, 1); err != nil { 201 require.Nil(t, err) 202 } else { 203 autoResponderPostFound := false 204 for _, post := range list.Posts { 205 if post.Type == model.POST_AUTO_RESPONDER { 206 autoResponderPostFound = true 207 } 208 } 209 assert.True(t, autoResponderPostFound) 210 } 211 } 212 213 func TestSendAutoResponseFailure(t *testing.T) { 214 th := Setup(t).InitBasic() 215 defer th.TearDown() 216 217 user := th.CreateUser() 218 defer th.App.PermanentDeleteUser(user) 219 220 patch := &model.UserPatch{} 221 patch.NotifyProps = make(map[string]string) 222 patch.NotifyProps["auto_responder_active"] = "false" 223 patch.NotifyProps["auto_responder_message"] = "Hello, I'm unavailable today." 224 225 userUpdated1, err := th.App.PatchUser(user.Id, patch, true) 226 require.Nil(t, err) 227 228 th.App.CreatePost(&model.Post{ 229 ChannelId: th.BasicChannel.Id, 230 Message: "zz" + model.NewId() + "a", 231 UserId: th.BasicUser.Id}, 232 th.BasicChannel, 233 false, true) 234 235 sent, err := th.App.SendAutoResponse(th.BasicChannel, userUpdated1) 236 237 assert.Nil(t, err) 238 assert.False(t, sent) 239 240 if list, err := th.App.GetPosts(th.BasicChannel.Id, 0, 1); err != nil { 241 require.Nil(t, err) 242 } else { 243 autoResponderPostFound := false 244 for _, post := range list.Posts { 245 if post.Type == model.POST_AUTO_RESPONDER { 246 autoResponderPostFound = true 247 } 248 } 249 assert.False(t, autoResponderPostFound) 250 } 251 }