github.com/mad-app/mattermost-server@v5.11.1+incompatible/store/storetest/file_info_store.go (about) 1 // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package storetest 5 6 import ( 7 "fmt" 8 "testing" 9 10 "github.com/mattermost/mattermost-server/model" 11 "github.com/mattermost/mattermost-server/store" 12 13 "github.com/stretchr/testify/assert" 14 "github.com/stretchr/testify/require" 15 ) 16 17 func TestFileInfoStore(t *testing.T, ss store.Store) { 18 t.Run("FileInfoSaveGet", func(t *testing.T) { testFileInfoSaveGet(t, ss) }) 19 t.Run("FileInfoSaveGetByPath", func(t *testing.T) { testFileInfoSaveGetByPath(t, ss) }) 20 t.Run("FileInfoGetForPost", func(t *testing.T) { testFileInfoGetForPost(t, ss) }) 21 t.Run("FileInfoGetForUser", func(t *testing.T) { testFileInfoGetForUser(t, ss) }) 22 t.Run("FileInfoAttachToPost", func(t *testing.T) { testFileInfoAttachToPost(t, ss) }) 23 t.Run("FileInfoDeleteForPost", func(t *testing.T) { testFileInfoDeleteForPost(t, ss) }) 24 t.Run("FileInfoPermanentDelete", func(t *testing.T) { testFileInfoPermanentDelete(t, ss) }) 25 t.Run("FileInfoPermanentDeleteBatch", func(t *testing.T) { testFileInfoPermanentDeleteBatch(t, ss) }) 26 t.Run("FileInfoPermanentDeleteByUser", func(t *testing.T) { testFileInfoPermanentDeleteByUser(t, ss) }) 27 } 28 29 func testFileInfoSaveGet(t *testing.T, ss store.Store) { 30 info := &model.FileInfo{ 31 CreatorId: model.NewId(), 32 Path: "file.txt", 33 } 34 35 if result := <-ss.FileInfo().Save(info); result.Err != nil { 36 t.Fatal(result.Err) 37 } else if returned := result.Data.(*model.FileInfo); len(returned.Id) == 0 { 38 t.Fatal("should've assigned an id to FileInfo") 39 } else { 40 info = returned 41 } 42 defer func() { 43 <-ss.FileInfo().PermanentDelete(info.Id) 44 }() 45 46 if result := <-ss.FileInfo().Get(info.Id); result.Err != nil { 47 t.Fatal(result.Err) 48 } else if returned := result.Data.(*model.FileInfo); returned.Id != info.Id { 49 t.Log(info) 50 t.Log(returned) 51 t.Fatal("should've returned correct FileInfo") 52 } 53 54 info2 := store.Must(ss.FileInfo().Save(&model.FileInfo{ 55 CreatorId: model.NewId(), 56 Path: "file.txt", 57 DeleteAt: 123, 58 })).(*model.FileInfo) 59 60 if result := <-ss.FileInfo().Get(info2.Id); result.Err == nil { 61 t.Fatal("shouldn't have gotten deleted file") 62 } 63 defer func() { 64 <-ss.FileInfo().PermanentDelete(info2.Id) 65 }() 66 } 67 68 func testFileInfoSaveGetByPath(t *testing.T, ss store.Store) { 69 info := &model.FileInfo{ 70 CreatorId: model.NewId(), 71 Path: fmt.Sprintf("%v/file.txt", model.NewId()), 72 } 73 74 if result := <-ss.FileInfo().Save(info); result.Err != nil { 75 t.Fatal(result.Err) 76 } else if returned := result.Data.(*model.FileInfo); len(returned.Id) == 0 { 77 t.Fatal("should've assigned an id to FileInfo") 78 } else { 79 info = returned 80 } 81 defer func() { 82 <-ss.FileInfo().PermanentDelete(info.Id) 83 }() 84 85 if result := <-ss.FileInfo().GetByPath(info.Path); result.Err != nil { 86 t.Fatal(result.Err) 87 } else if returned := result.Data.(*model.FileInfo); returned.Id != info.Id { 88 t.Log(info) 89 t.Log(returned) 90 t.Fatal("should've returned correct FileInfo") 91 } 92 93 info2 := store.Must(ss.FileInfo().Save(&model.FileInfo{ 94 CreatorId: model.NewId(), 95 Path: "file.txt", 96 DeleteAt: 123, 97 })).(*model.FileInfo) 98 99 if result := <-ss.FileInfo().GetByPath(info2.Id); result.Err == nil { 100 t.Fatal("shouldn't have gotten deleted file") 101 } 102 defer func() { 103 <-ss.FileInfo().PermanentDelete(info2.Id) 104 }() 105 } 106 107 func testFileInfoGetForPost(t *testing.T, ss store.Store) { 108 userId := model.NewId() 109 postId := model.NewId() 110 111 infos := []*model.FileInfo{ 112 { 113 PostId: postId, 114 CreatorId: userId, 115 Path: "file.txt", 116 }, 117 { 118 PostId: postId, 119 CreatorId: userId, 120 Path: "file.txt", 121 }, 122 { 123 PostId: postId, 124 CreatorId: userId, 125 Path: "file.txt", 126 DeleteAt: 123, 127 }, 128 { 129 PostId: model.NewId(), 130 CreatorId: userId, 131 Path: "file.txt", 132 }, 133 } 134 135 for i, info := range infos { 136 infos[i] = store.Must(ss.FileInfo().Save(info)).(*model.FileInfo) 137 defer func(id string) { 138 <-ss.FileInfo().PermanentDelete(id) 139 }(infos[i].Id) 140 } 141 142 if result := <-ss.FileInfo().GetForPost(postId, true, false); result.Err != nil { 143 t.Fatal(result.Err) 144 } else if returned := result.Data.([]*model.FileInfo); len(returned) != 2 { 145 t.Fatal("should've returned exactly 2 file infos") 146 } 147 148 if result := <-ss.FileInfo().GetForPost(postId, false, false); result.Err != nil { 149 t.Fatal(result.Err) 150 } else if returned := result.Data.([]*model.FileInfo); len(returned) != 2 { 151 t.Fatal("should've returned exactly 2 file infos") 152 } 153 154 if result := <-ss.FileInfo().GetForPost(postId, true, true); result.Err != nil { 155 t.Fatal(result.Err) 156 } else if returned := result.Data.([]*model.FileInfo); len(returned) != 2 { 157 t.Fatal("should've returned exactly 2 file infos") 158 } 159 } 160 161 func testFileInfoGetForUser(t *testing.T, ss store.Store) { 162 userId := model.NewId() 163 userId2 := model.NewId() 164 postId := model.NewId() 165 166 infos := []*model.FileInfo{ 167 { 168 PostId: postId, 169 CreatorId: userId, 170 Path: "file.txt", 171 }, 172 { 173 PostId: postId, 174 CreatorId: userId, 175 Path: "file.txt", 176 }, 177 { 178 PostId: postId, 179 CreatorId: userId, 180 Path: "file.txt", 181 }, 182 { 183 PostId: model.NewId(), 184 CreatorId: userId2, 185 Path: "file.txt", 186 }, 187 } 188 189 for i, info := range infos { 190 infos[i] = store.Must(ss.FileInfo().Save(info)).(*model.FileInfo) 191 defer func(id string) { 192 <-ss.FileInfo().PermanentDelete(id) 193 }(infos[i].Id) 194 } 195 196 if result := <-ss.FileInfo().GetForUser(userId); result.Err != nil { 197 t.Fatal(result.Err) 198 } else if returned := result.Data.([]*model.FileInfo); len(returned) != 3 { 199 t.Fatal("should've returned exactly 3 file infos") 200 } 201 202 if result := <-ss.FileInfo().GetForUser(userId2); result.Err != nil { 203 t.Fatal(result.Err) 204 } else if returned := result.Data.([]*model.FileInfo); len(returned) != 1 { 205 t.Fatal("should've returned exactly 1 file infos") 206 } 207 } 208 209 func testFileInfoAttachToPost(t *testing.T, ss store.Store) { 210 t.Run("should attach files", func(t *testing.T) { 211 userId := model.NewId() 212 postId := model.NewId() 213 214 info1 := store.Must(ss.FileInfo().Save(&model.FileInfo{ 215 CreatorId: userId, 216 Path: "file.txt", 217 })).(*model.FileInfo) 218 info2 := store.Must(ss.FileInfo().Save(&model.FileInfo{ 219 CreatorId: userId, 220 Path: "file2.txt", 221 })).(*model.FileInfo) 222 223 require.Equal(t, "", info1.PostId) 224 require.Equal(t, "", info2.PostId) 225 226 result := <-ss.FileInfo().AttachToPost(info1.Id, postId, userId) 227 assert.Nil(t, result.Err) 228 229 result = <-ss.FileInfo().AttachToPost(info2.Id, postId, userId) 230 assert.Nil(t, result.Err) 231 232 result = <-ss.FileInfo().GetForPost(postId, true, false) 233 assert.Nil(t, result.Err) 234 235 data := result.Data.([]*model.FileInfo) 236 237 assert.Len(t, data, 2) 238 assert.True(t, data[0].Id == info1.Id || data[0].Id == info2.Id) 239 assert.True(t, data[1].Id == info1.Id || data[1].Id == info2.Id) 240 }) 241 242 t.Run("should not attach files to multiple posts", func(t *testing.T) { 243 userId := model.NewId() 244 postId := model.NewId() 245 246 info := store.Must(ss.FileInfo().Save(&model.FileInfo{ 247 CreatorId: userId, 248 Path: "file.txt", 249 })).(*model.FileInfo) 250 251 require.Equal(t, "", info.PostId) 252 253 result := <-ss.FileInfo().AttachToPost(info.Id, model.NewId(), userId) 254 assert.Nil(t, result.Err) 255 256 result = <-ss.FileInfo().AttachToPost(info.Id, postId, userId) 257 assert.NotNil(t, result.Err) 258 }) 259 260 t.Run("should not attach files owned from a different user", func(t *testing.T) { 261 userId := model.NewId() 262 postId := model.NewId() 263 264 info := store.Must(ss.FileInfo().Save(&model.FileInfo{ 265 CreatorId: model.NewId(), 266 Path: "file.txt", 267 })).(*model.FileInfo) 268 269 require.Equal(t, "", info.PostId) 270 271 result := <-ss.FileInfo().AttachToPost(info.Id, postId, userId) 272 273 assert.NotNil(t, result.Err) 274 }) 275 } 276 277 func testFileInfoDeleteForPost(t *testing.T, ss store.Store) { 278 userId := model.NewId() 279 postId := model.NewId() 280 281 infos := []*model.FileInfo{ 282 { 283 PostId: postId, 284 CreatorId: userId, 285 Path: "file.txt", 286 }, 287 { 288 PostId: postId, 289 CreatorId: userId, 290 Path: "file.txt", 291 }, 292 { 293 PostId: postId, 294 CreatorId: userId, 295 Path: "file.txt", 296 DeleteAt: 123, 297 }, 298 { 299 PostId: model.NewId(), 300 CreatorId: userId, 301 Path: "file.txt", 302 }, 303 } 304 305 for i, info := range infos { 306 infos[i] = store.Must(ss.FileInfo().Save(info)).(*model.FileInfo) 307 defer func(id string) { 308 <-ss.FileInfo().PermanentDelete(id) 309 }(infos[i].Id) 310 } 311 312 if result := <-ss.FileInfo().DeleteForPost(postId); result.Err != nil { 313 t.Fatal(result.Err) 314 } 315 316 if infos := store.Must(ss.FileInfo().GetForPost(postId, true, false)).([]*model.FileInfo); len(infos) != 0 { 317 t.Fatal("shouldn't have returned any file infos") 318 } 319 } 320 321 func testFileInfoPermanentDelete(t *testing.T, ss store.Store) { 322 info := store.Must(ss.FileInfo().Save(&model.FileInfo{ 323 PostId: model.NewId(), 324 CreatorId: model.NewId(), 325 Path: "file.txt", 326 })).(*model.FileInfo) 327 328 if result := <-ss.FileInfo().PermanentDelete(info.Id); result.Err != nil { 329 t.Fatal(result.Err) 330 } 331 } 332 333 func testFileInfoPermanentDeleteBatch(t *testing.T, ss store.Store) { 334 postId := model.NewId() 335 336 store.Must(ss.FileInfo().Save(&model.FileInfo{ 337 PostId: postId, 338 CreatorId: model.NewId(), 339 Path: "file.txt", 340 CreateAt: 1000, 341 })) 342 343 store.Must(ss.FileInfo().Save(&model.FileInfo{ 344 PostId: postId, 345 CreatorId: model.NewId(), 346 Path: "file.txt", 347 CreateAt: 1200, 348 })) 349 350 store.Must(ss.FileInfo().Save(&model.FileInfo{ 351 PostId: postId, 352 CreatorId: model.NewId(), 353 Path: "file.txt", 354 CreateAt: 2000, 355 })) 356 357 if result := <-ss.FileInfo().GetForPost(postId, true, false); result.Err != nil { 358 t.Fatal(result.Err) 359 } else if len(result.Data.([]*model.FileInfo)) != 3 { 360 t.Fatal("Expected 3 fileInfos") 361 } 362 363 store.Must(ss.FileInfo().PermanentDeleteBatch(1500, 1000)) 364 365 if result := <-ss.FileInfo().GetForPost(postId, true, false); result.Err != nil { 366 t.Fatal(result.Err) 367 } else if len(result.Data.([]*model.FileInfo)) != 1 { 368 t.Fatal("Expected 3 fileInfos") 369 } 370 } 371 372 func testFileInfoPermanentDeleteByUser(t *testing.T, ss store.Store) { 373 userId := model.NewId() 374 postId := model.NewId() 375 376 store.Must(ss.FileInfo().Save(&model.FileInfo{ 377 PostId: postId, 378 CreatorId: userId, 379 Path: "file.txt", 380 })) 381 382 if result := <-ss.FileInfo().PermanentDeleteByUser(userId); result.Err != nil { 383 t.Fatal(result.Err) 384 } 385 }