github.com/mad-app/mattermost-server@v5.11.1+incompatible/store/storetest/bot_store.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package storetest 5 6 import ( 7 "net/http" 8 "testing" 9 10 "github.com/stretchr/testify/require" 11 12 "github.com/mattermost/mattermost-server/model" 13 "github.com/mattermost/mattermost-server/store" 14 ) 15 16 func makeBotWithUser(ss store.Store, bot *model.Bot) (*model.Bot, *model.User) { 17 user := store.Must(ss.User().Save(model.UserFromBot(bot))).(*model.User) 18 19 bot.UserId = user.Id 20 bot = store.Must(ss.Bot().Save(bot)).(*model.Bot) 21 22 return bot, user 23 } 24 25 func TestBotStore(t *testing.T, ss store.Store) { 26 t.Run("Get", func(t *testing.T) { testBotStoreGet(t, ss) }) 27 t.Run("GetAll", func(t *testing.T) { testBotStoreGetAll(t, ss) }) 28 t.Run("Save", func(t *testing.T) { testBotStoreSave(t, ss) }) 29 t.Run("Update", func(t *testing.T) { testBotStoreUpdate(t, ss) }) 30 t.Run("PermanentDelete", func(t *testing.T) { testBotStorePermanentDelete(t, ss) }) 31 } 32 33 func testBotStoreGet(t *testing.T, ss store.Store) { 34 deletedBot, _ := makeBotWithUser(ss, &model.Bot{ 35 Username: "deleted_bot", 36 Description: "A deleted bot", 37 OwnerId: model.NewId(), 38 }) 39 deletedBot.DeleteAt = 1 40 deletedBot = store.Must(ss.Bot().Update(deletedBot)).(*model.Bot) 41 defer func() { store.Must(ss.Bot().PermanentDelete(deletedBot.UserId)) }() 42 defer func() { store.Must(ss.User().PermanentDelete(deletedBot.UserId)) }() 43 44 permanentlyDeletedBot, _ := makeBotWithUser(ss, &model.Bot{ 45 Username: "permanently_deleted_bot", 46 Description: "A permanently deleted bot", 47 OwnerId: model.NewId(), 48 DeleteAt: 0, 49 }) 50 store.Must(ss.Bot().PermanentDelete(permanentlyDeletedBot.UserId)) 51 52 b1, _ := makeBotWithUser(ss, &model.Bot{ 53 Username: "b1", 54 Description: "The first bot", 55 OwnerId: model.NewId(), 56 }) 57 defer func() { store.Must(ss.Bot().PermanentDelete(b1.UserId)) }() 58 defer func() { store.Must(ss.User().PermanentDelete(b1.UserId)) }() 59 60 b2, _ := makeBotWithUser(ss, &model.Bot{ 61 Username: "b2", 62 Description: "The second bot", 63 OwnerId: model.NewId(), 64 }) 65 defer func() { store.Must(ss.Bot().PermanentDelete(b2.UserId)) }() 66 defer func() { store.Must(ss.User().PermanentDelete(b2.UserId)) }() 67 68 t.Run("get non-existent bot", func(t *testing.T) { 69 result := <-ss.Bot().Get("unknown", false) 70 require.NotNil(t, result.Err) 71 require.Equal(t, http.StatusNotFound, result.Err.StatusCode) 72 }) 73 74 t.Run("get deleted bot", func(t *testing.T) { 75 result := <-ss.Bot().Get(deletedBot.UserId, false) 76 require.NotNil(t, result.Err) 77 require.Equal(t, http.StatusNotFound, result.Err.StatusCode) 78 }) 79 80 t.Run("get deleted bot, include deleted", func(t *testing.T) { 81 result := <-ss.Bot().Get(deletedBot.UserId, true) 82 require.Nil(t, result.Err) 83 require.Equal(t, deletedBot, result.Data.(*model.Bot)) 84 }) 85 86 t.Run("get permanently deleted bot", func(t *testing.T) { 87 result := <-ss.Bot().Get(permanentlyDeletedBot.UserId, false) 88 require.NotNil(t, result.Err) 89 require.Equal(t, http.StatusNotFound, result.Err.StatusCode) 90 }) 91 92 t.Run("get bot 1", func(t *testing.T) { 93 result := <-ss.Bot().Get(b1.UserId, false) 94 require.Nil(t, result.Err) 95 require.Equal(t, b1, result.Data.(*model.Bot)) 96 }) 97 98 t.Run("get bot 2", func(t *testing.T) { 99 result := <-ss.Bot().Get(b2.UserId, false) 100 require.Nil(t, result.Err) 101 require.Equal(t, b2, result.Data.(*model.Bot)) 102 }) 103 } 104 105 func testBotStoreGetAll(t *testing.T, ss store.Store) { 106 OwnerId1 := model.NewId() 107 OwnerId2 := model.NewId() 108 109 deletedBot, _ := makeBotWithUser(ss, &model.Bot{ 110 Username: "deleted_bot", 111 Description: "A deleted bot", 112 OwnerId: OwnerId1, 113 }) 114 deletedBot.DeleteAt = 1 115 deletedBot = store.Must(ss.Bot().Update(deletedBot)).(*model.Bot) 116 defer func() { store.Must(ss.Bot().PermanentDelete(deletedBot.UserId)) }() 117 defer func() { store.Must(ss.User().PermanentDelete(deletedBot.UserId)) }() 118 119 permanentlyDeletedBot, _ := makeBotWithUser(ss, &model.Bot{ 120 Username: "permanently_deleted_bot", 121 Description: "A permanently deleted bot", 122 OwnerId: OwnerId1, 123 DeleteAt: 0, 124 }) 125 store.Must(ss.Bot().PermanentDelete(permanentlyDeletedBot.UserId)) 126 127 b1, _ := makeBotWithUser(ss, &model.Bot{ 128 Username: "b1", 129 Description: "The first bot", 130 OwnerId: OwnerId1, 131 }) 132 defer func() { store.Must(ss.Bot().PermanentDelete(b1.UserId)) }() 133 defer func() { store.Must(ss.User().PermanentDelete(b1.UserId)) }() 134 135 b2, _ := makeBotWithUser(ss, &model.Bot{ 136 Username: "b2", 137 Description: "The second bot", 138 OwnerId: OwnerId1, 139 }) 140 defer func() { store.Must(ss.Bot().PermanentDelete(b2.UserId)) }() 141 defer func() { store.Must(ss.User().PermanentDelete(b2.UserId)) }() 142 143 t.Run("get original bots", func(t *testing.T) { 144 result := <-ss.Bot().GetAll(&model.BotGetOptions{Page: 0, PerPage: 10}) 145 require.Nil(t, result.Err) 146 require.Equal(t, []*model.Bot{ 147 b1, 148 b2, 149 }, result.Data.([]*model.Bot)) 150 }) 151 152 b3, _ := makeBotWithUser(ss, &model.Bot{ 153 Username: "b3", 154 Description: "The third bot", 155 OwnerId: OwnerId1, 156 }) 157 defer func() { store.Must(ss.Bot().PermanentDelete(b3.UserId)) }() 158 defer func() { store.Must(ss.User().PermanentDelete(b3.UserId)) }() 159 160 b4, _ := makeBotWithUser(ss, &model.Bot{ 161 Username: "b4", 162 Description: "The fourth bot", 163 OwnerId: OwnerId2, 164 }) 165 defer func() { store.Must(ss.Bot().PermanentDelete(b4.UserId)) }() 166 defer func() { store.Must(ss.User().PermanentDelete(b4.UserId)) }() 167 168 deletedUser := model.User{ 169 Email: MakeEmail(), 170 Username: model.NewId(), 171 } 172 if err := (<-ss.User().Save(&deletedUser)).Err; err != nil { 173 t.Fatal("couldn't save user", err) 174 } 175 deletedUser.DeleteAt = model.GetMillis() 176 if err := (<-ss.User().Update(&deletedUser, true)).Err; err != nil { 177 t.Fatal("couldn't delete user", err) 178 } 179 defer func() { store.Must(ss.User().PermanentDelete(deletedUser.Id)) }() 180 ob5, _ := makeBotWithUser(ss, &model.Bot{ 181 Username: "ob5", 182 Description: "Orphaned bot 5", 183 OwnerId: deletedUser.Id, 184 }) 185 defer func() { store.Must(ss.Bot().PermanentDelete(b4.UserId)) }() 186 defer func() { store.Must(ss.User().PermanentDelete(b4.UserId)) }() 187 188 t.Run("get newly created bot stoo", func(t *testing.T) { 189 result := <-ss.Bot().GetAll(&model.BotGetOptions{Page: 0, PerPage: 10}) 190 require.Nil(t, result.Err) 191 require.Equal(t, []*model.Bot{ 192 b1, 193 b2, 194 b3, 195 b4, 196 ob5, 197 }, result.Data.([]*model.Bot)) 198 }) 199 200 t.Run("get orphaned", func(t *testing.T) { 201 result := <-ss.Bot().GetAll(&model.BotGetOptions{Page: 0, PerPage: 10, OnlyOrphaned: true}) 202 require.Nil(t, result.Err) 203 require.Equal(t, []*model.Bot{ 204 ob5, 205 }, result.Data.([]*model.Bot)) 206 }) 207 208 t.Run("get page=0, per_page=2", func(t *testing.T) { 209 result := <-ss.Bot().GetAll(&model.BotGetOptions{Page: 0, PerPage: 2}) 210 require.Nil(t, result.Err) 211 require.Equal(t, []*model.Bot{ 212 b1, 213 b2, 214 }, result.Data.([]*model.Bot)) 215 }) 216 217 t.Run("get page=1, limit=2", func(t *testing.T) { 218 result := <-ss.Bot().GetAll(&model.BotGetOptions{Page: 1, PerPage: 2}) 219 require.Nil(t, result.Err) 220 require.Equal(t, []*model.Bot{ 221 b3, 222 b4, 223 }, result.Data.([]*model.Bot)) 224 }) 225 226 t.Run("get page=5, perpage=1000", func(t *testing.T) { 227 result := <-ss.Bot().GetAll(&model.BotGetOptions{Page: 5, PerPage: 1000}) 228 require.Nil(t, result.Err) 229 require.Equal(t, []*model.Bot{}, result.Data.([]*model.Bot)) 230 }) 231 232 t.Run("get offset=0, limit=2, include deleted", func(t *testing.T) { 233 result := <-ss.Bot().GetAll(&model.BotGetOptions{Page: 0, PerPage: 2, IncludeDeleted: true}) 234 require.Nil(t, result.Err) 235 require.Equal(t, []*model.Bot{ 236 deletedBot, 237 b1, 238 }, result.Data.([]*model.Bot)) 239 }) 240 241 t.Run("get offset=2, limit=2, include deleted", func(t *testing.T) { 242 result := <-ss.Bot().GetAll(&model.BotGetOptions{Page: 1, PerPage: 2, IncludeDeleted: true}) 243 require.Nil(t, result.Err) 244 require.Equal(t, []*model.Bot{ 245 b2, 246 b3, 247 }, result.Data.([]*model.Bot)) 248 }) 249 250 t.Run("get offset=0, limit=10, creator id 1", func(t *testing.T) { 251 result := <-ss.Bot().GetAll(&model.BotGetOptions{Page: 0, PerPage: 10, OwnerId: OwnerId1}) 252 require.Nil(t, result.Err) 253 require.Equal(t, []*model.Bot{ 254 b1, 255 b2, 256 b3, 257 }, result.Data.([]*model.Bot)) 258 }) 259 260 t.Run("get offset=0, limit=10, creator id 2", func(t *testing.T) { 261 result := <-ss.Bot().GetAll(&model.BotGetOptions{Page: 0, PerPage: 10, OwnerId: OwnerId2}) 262 require.Nil(t, result.Err) 263 require.Equal(t, []*model.Bot{ 264 b4, 265 }, result.Data.([]*model.Bot)) 266 }) 267 268 t.Run("get offset=0, limit=10, include deleted, creator id 1", func(t *testing.T) { 269 result := <-ss.Bot().GetAll(&model.BotGetOptions{Page: 0, PerPage: 10, IncludeDeleted: true, OwnerId: OwnerId1}) 270 require.Nil(t, result.Err) 271 require.Equal(t, []*model.Bot{ 272 deletedBot, 273 b1, 274 b2, 275 b3, 276 }, result.Data.([]*model.Bot)) 277 }) 278 279 t.Run("get offset=0, limit=10, include deleted, creator id 2", func(t *testing.T) { 280 result := <-ss.Bot().GetAll(&model.BotGetOptions{Page: 0, PerPage: 10, IncludeDeleted: true, OwnerId: OwnerId2}) 281 require.Nil(t, result.Err) 282 require.Equal(t, []*model.Bot{ 283 b4, 284 }, result.Data.([]*model.Bot)) 285 }) 286 } 287 288 func testBotStoreSave(t *testing.T, ss store.Store) { 289 t.Run("invalid bot", func(t *testing.T) { 290 bot := &model.Bot{ 291 UserId: model.NewId(), 292 Username: "invalid bot", 293 Description: "description", 294 } 295 296 result := <-ss.Bot().Save(bot) 297 require.NotNil(t, result.Err) 298 require.Equal(t, "model.bot.is_valid.username.app_error", result.Err.Id) 299 }) 300 301 t.Run("normal bot", func(t *testing.T) { 302 bot := &model.Bot{ 303 Username: "normal_bot", 304 Description: "description", 305 OwnerId: model.NewId(), 306 } 307 308 user := store.Must(ss.User().Save(model.UserFromBot(bot))).(*model.User) 309 defer func() { store.Must(ss.User().PermanentDelete(user.Id)) }() 310 bot.UserId = user.Id 311 312 result := <-ss.Bot().Save(bot) 313 require.Nil(t, result.Err) 314 defer func() { store.Must(ss.Bot().PermanentDelete(bot.UserId)) }() 315 316 // Verify the returned bot matches the saved bot, modulo expected changes 317 returnedNewBot := result.Data.(*model.Bot) 318 require.NotEqual(t, 0, returnedNewBot.CreateAt) 319 require.NotEqual(t, 0, returnedNewBot.UpdateAt) 320 require.Equal(t, returnedNewBot.CreateAt, returnedNewBot.UpdateAt) 321 bot.UserId = returnedNewBot.UserId 322 bot.CreateAt = returnedNewBot.CreateAt 323 bot.UpdateAt = returnedNewBot.UpdateAt 324 bot.DeleteAt = 0 325 require.Equal(t, bot, returnedNewBot) 326 327 // Verify the actual bot in the database matches the saved bot. 328 result = <-ss.Bot().Get(bot.UserId, false) 329 require.Nil(t, result.Err) 330 actualNewBot := result.Data.(*model.Bot) 331 require.Equal(t, bot, actualNewBot) 332 }) 333 } 334 335 func testBotStoreUpdate(t *testing.T, ss store.Store) { 336 t.Run("invalid bot should fail to update", func(t *testing.T) { 337 existingBot, _ := makeBotWithUser(ss, &model.Bot{ 338 Username: "existing_bot", 339 OwnerId: model.NewId(), 340 }) 341 defer func() { store.Must(ss.Bot().PermanentDelete(existingBot.UserId)) }() 342 defer func() { store.Must(ss.User().PermanentDelete(existingBot.UserId)) }() 343 344 bot := existingBot.Clone() 345 bot.Username = "invalid username" 346 result := <-ss.Bot().Update(bot) 347 require.NotNil(t, result.Err) 348 require.Equal(t, "model.bot.is_valid.username.app_error", result.Err.Id) 349 }) 350 351 t.Run("existing bot should update", func(t *testing.T) { 352 existingBot, _ := makeBotWithUser(ss, &model.Bot{ 353 Username: "existing_bot", 354 OwnerId: model.NewId(), 355 }) 356 defer func() { store.Must(ss.Bot().PermanentDelete(existingBot.UserId)) }() 357 defer func() { store.Must(ss.User().PermanentDelete(existingBot.UserId)) }() 358 359 bot := existingBot.Clone() 360 bot.OwnerId = model.NewId() 361 bot.Description = "updated description" 362 bot.CreateAt = 999999 // Ignored 363 bot.UpdateAt = 999999 // Ignored 364 bot.DeleteAt = 100000 // Allowed 365 366 result := <-ss.Bot().Update(bot) 367 require.Nil(t, result.Err) 368 369 // Verify the returned bot matches the updated bot, modulo expected timestamp changes 370 returnedBot := result.Data.(*model.Bot) 371 require.Equal(t, existingBot.CreateAt, returnedBot.CreateAt) 372 require.NotEqual(t, bot.UpdateAt, returnedBot.UpdateAt, "update should have advanced UpdateAt") 373 require.True(t, returnedBot.UpdateAt > bot.UpdateAt, "update should have advanced UpdateAt") 374 require.NotEqual(t, 99999, returnedBot.UpdateAt, "should have ignored user-provided UpdateAt") 375 bot.CreateAt = returnedBot.CreateAt 376 bot.UpdateAt = returnedBot.UpdateAt 377 378 // Verify the actual (now deleted) bot in the database 379 result = <-ss.Bot().Get(bot.UserId, true) 380 require.Nil(t, result.Err) 381 require.Equal(t, bot, result.Data.(*model.Bot)) 382 }) 383 384 t.Run("deleted bot should update, restoring", func(t *testing.T) { 385 existingBot, _ := makeBotWithUser(ss, &model.Bot{ 386 Username: "existing_bot", 387 OwnerId: model.NewId(), 388 }) 389 defer func() { store.Must(ss.Bot().PermanentDelete(existingBot.UserId)) }() 390 defer func() { store.Must(ss.User().PermanentDelete(existingBot.UserId)) }() 391 392 existingBot.DeleteAt = 100000 393 existingBot = store.Must(ss.Bot().Update(existingBot)).(*model.Bot) 394 395 bot := existingBot.Clone() 396 bot.DeleteAt = 0 397 398 result := <-ss.Bot().Update(bot) 399 require.Nil(t, result.Err) 400 401 // Verify the returned bot matches the updated bot, modulo expected timestamp changes 402 returnedBot := result.Data.(*model.Bot) 403 require.EqualValues(t, 0, returnedBot.DeleteAt) 404 bot.UpdateAt = returnedBot.UpdateAt 405 406 // Verify the actual bot in the database 407 result = <-ss.Bot().Get(bot.UserId, false) 408 require.Nil(t, result.Err) 409 require.Equal(t, bot, result.Data.(*model.Bot)) 410 }) 411 } 412 413 func testBotStorePermanentDelete(t *testing.T, ss store.Store) { 414 b1, _ := makeBotWithUser(ss, &model.Bot{ 415 Username: "b1", 416 OwnerId: model.NewId(), 417 }) 418 defer func() { store.Must(ss.Bot().PermanentDelete(b1.UserId)) }() 419 defer func() { store.Must(ss.User().PermanentDelete(b1.UserId)) }() 420 421 b2, _ := makeBotWithUser(ss, &model.Bot{ 422 Username: "b2", 423 OwnerId: model.NewId(), 424 }) 425 defer func() { store.Must(ss.Bot().PermanentDelete(b2.UserId)) }() 426 defer func() { store.Must(ss.User().PermanentDelete(b2.UserId)) }() 427 428 t.Run("permanently delete a non-existent bot", func(t *testing.T) { 429 result := <-ss.Bot().PermanentDelete("unknown") 430 require.Nil(t, result.Err) 431 }) 432 433 t.Run("permanently delete bot", func(t *testing.T) { 434 result := <-ss.Bot().PermanentDelete(b1.UserId) 435 require.Nil(t, result.Err) 436 437 result = <-ss.Bot().Get(b1.UserId, false) 438 require.NotNil(t, result.Err) 439 require.Equal(t, http.StatusNotFound, result.Err.StatusCode) 440 }) 441 }