github.com/mad-app/mattermost-server@v5.11.1+incompatible/store/storetest/channel_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 "sort" 8 "strconv" 9 "strings" 10 "testing" 11 "time" 12 13 "github.com/stretchr/testify/assert" 14 "github.com/stretchr/testify/require" 15 16 "github.com/mattermost/gorp" 17 "github.com/mattermost/mattermost-server/model" 18 "github.com/mattermost/mattermost-server/store" 19 ) 20 21 type SqlSupplier interface { 22 GetMaster() *gorp.DbMap 23 } 24 25 func cleanupChannels(t *testing.T, ss store.Store) { 26 result := <-ss.Channel().GetAllChannels(0, 100000, true) 27 if result.Err != nil { 28 t.Fatal("error cleaning all channels") 29 } 30 list := result.Data.(*model.ChannelListWithTeamData) 31 for _, channel := range *list { 32 ss.Channel().PermanentDelete(channel.Id) 33 } 34 } 35 36 func TestChannelStore(t *testing.T, ss store.Store, s SqlSupplier) { 37 createDefaultRoles(t, ss) 38 39 t.Run("Save", func(t *testing.T) { testChannelStoreSave(t, ss) }) 40 t.Run("SaveDirectChannel", func(t *testing.T) { testChannelStoreSaveDirectChannel(t, ss, s) }) 41 t.Run("CreateDirectChannel", func(t *testing.T) { testChannelStoreCreateDirectChannel(t, ss) }) 42 t.Run("Update", func(t *testing.T) { testChannelStoreUpdate(t, ss) }) 43 t.Run("GetChannelUnread", func(t *testing.T) { testGetChannelUnread(t, ss) }) 44 t.Run("Get", func(t *testing.T) { testChannelStoreGet(t, ss, s) }) 45 t.Run("GetChannelsByIds", func(t *testing.T) { testChannelStoreGetChannelsByIds(t, ss) }) 46 t.Run("GetForPost", func(t *testing.T) { testChannelStoreGetForPost(t, ss) }) 47 t.Run("Restore", func(t *testing.T) { testChannelStoreRestore(t, ss) }) 48 t.Run("Delete", func(t *testing.T) { testChannelStoreDelete(t, ss) }) 49 t.Run("GetByName", func(t *testing.T) { testChannelStoreGetByName(t, ss) }) 50 t.Run("GetByNames", func(t *testing.T) { testChannelStoreGetByNames(t, ss) }) 51 t.Run("GetDeletedByName", func(t *testing.T) { testChannelStoreGetDeletedByName(t, ss) }) 52 t.Run("GetDeleted", func(t *testing.T) { testChannelStoreGetDeleted(t, ss) }) 53 t.Run("ChannelMemberStore", func(t *testing.T) { testChannelMemberStore(t, ss) }) 54 t.Run("ChannelDeleteMemberStore", func(t *testing.T) { testChannelDeleteMemberStore(t, ss) }) 55 t.Run("GetChannels", func(t *testing.T) { testChannelStoreGetChannels(t, ss) }) 56 t.Run("GetAllChannels", func(t *testing.T) { testChannelStoreGetAllChannels(t, ss, s) }) 57 t.Run("GetMoreChannels", func(t *testing.T) { testChannelStoreGetMoreChannels(t, ss) }) 58 t.Run("GetPublicChannelsForTeam", func(t *testing.T) { testChannelStoreGetPublicChannelsForTeam(t, ss) }) 59 t.Run("GetPublicChannelsByIdsForTeam", func(t *testing.T) { testChannelStoreGetPublicChannelsByIdsForTeam(t, ss) }) 60 t.Run("GetChannelCounts", func(t *testing.T) { testChannelStoreGetChannelCounts(t, ss) }) 61 t.Run("GetMembersForUser", func(t *testing.T) { testChannelStoreGetMembersForUser(t, ss) }) 62 t.Run("GetMembersForUserWithPagination", func(t *testing.T) { testChannelStoreGetMembersForUserWithPagination(t, ss) }) 63 t.Run("UpdateLastViewedAt", func(t *testing.T) { testChannelStoreUpdateLastViewedAt(t, ss) }) 64 t.Run("IncrementMentionCount", func(t *testing.T) { testChannelStoreIncrementMentionCount(t, ss) }) 65 t.Run("UpdateChannelMember", func(t *testing.T) { testUpdateChannelMember(t, ss) }) 66 t.Run("GetMember", func(t *testing.T) { testGetMember(t, ss) }) 67 t.Run("GetMemberForPost", func(t *testing.T) { testChannelStoreGetMemberForPost(t, ss) }) 68 t.Run("GetMemberCount", func(t *testing.T) { testGetMemberCount(t, ss) }) 69 t.Run("SearchMore", func(t *testing.T) { testChannelStoreSearchMore(t, ss) }) 70 t.Run("SearchInTeam", func(t *testing.T) { testChannelStoreSearchInTeam(t, ss) }) 71 t.Run("SearchAllChannels", func(t *testing.T) { testChannelStoreSearchAllChannels(t, ss) }) 72 t.Run("AutocompleteInTeamForSearch", func(t *testing.T) { testChannelStoreAutocompleteInTeamForSearch(t, ss, s) }) 73 t.Run("GetMembersByIds", func(t *testing.T) { testChannelStoreGetMembersByIds(t, ss) }) 74 t.Run("AnalyticsDeletedTypeCount", func(t *testing.T) { testChannelStoreAnalyticsDeletedTypeCount(t, ss) }) 75 t.Run("GetPinnedPosts", func(t *testing.T) { testChannelStoreGetPinnedPosts(t, ss) }) 76 t.Run("MaxChannelsPerTeam", func(t *testing.T) { testChannelStoreMaxChannelsPerTeam(t, ss) }) 77 t.Run("GetChannelsByScheme", func(t *testing.T) { testChannelStoreGetChannelsByScheme(t, ss) }) 78 t.Run("MigrateChannelMembers", func(t *testing.T) { testChannelStoreMigrateChannelMembers(t, ss) }) 79 t.Run("ResetAllChannelSchemes", func(t *testing.T) { testResetAllChannelSchemes(t, ss) }) 80 t.Run("ClearAllCustomRoleAssignments", func(t *testing.T) { testChannelStoreClearAllCustomRoleAssignments(t, ss) }) 81 t.Run("MaterializedPublicChannels", func(t *testing.T) { testMaterializedPublicChannels(t, ss, s) }) 82 t.Run("GetAllChannelsForExportAfter", func(t *testing.T) { testChannelStoreGetAllChannelsForExportAfter(t, ss) }) 83 t.Run("GetChannelMembersForExport", func(t *testing.T) { testChannelStoreGetChannelMembersForExport(t, ss) }) 84 t.Run("RemoveAllDeactivatedMembers", func(t *testing.T) { testChannelStoreRemoveAllDeactivatedMembers(t, ss) }) 85 t.Run("ExportAllDirectChannels", func(t *testing.T) { testChannelStoreExportAllDirectChannels(t, ss, s) }) 86 t.Run("ExportAllDirectChannelsExcludePrivateAndPublic", func(t *testing.T) { testChannelStoreExportAllDirectChannelsExcludePrivateAndPublic(t, ss, s) }) 87 t.Run("ExportAllDirectChannelsDeletedChannel", func(t *testing.T) { testChannelStoreExportAllDirectChannelsDeletedChannel(t, ss, s) }) 88 t.Run("GetChannelsBatchForIndexing", func(t *testing.T) { testChannelStoreGetChannelsBatchForIndexing(t, ss) }) 89 } 90 91 func testChannelStoreSave(t *testing.T, ss store.Store) { 92 teamId := model.NewId() 93 94 o1 := model.Channel{} 95 o1.TeamId = teamId 96 o1.DisplayName = "Name" 97 o1.Name = "zz" + model.NewId() + "b" 98 o1.Type = model.CHANNEL_OPEN 99 100 if err := (<-ss.Channel().Save(&o1, -1)).Err; err != nil { 101 t.Fatal("couldn't save item", err) 102 } 103 104 if err := (<-ss.Channel().Save(&o1, -1)).Err; err == nil { 105 t.Fatal("shouldn't be able to update from save") 106 } 107 108 o1.Id = "" 109 if err := (<-ss.Channel().Save(&o1, -1)).Err; err == nil { 110 t.Fatal("should be unique name") 111 } 112 113 o1.Id = "" 114 o1.Name = "zz" + model.NewId() + "b" 115 o1.Type = model.CHANNEL_DIRECT 116 if err := (<-ss.Channel().Save(&o1, -1)).Err; err == nil { 117 t.Fatal("Should not be able to save direct channel") 118 } 119 } 120 121 func testChannelStoreSaveDirectChannel(t *testing.T, ss store.Store, s SqlSupplier) { 122 teamId := model.NewId() 123 124 o1 := model.Channel{} 125 o1.TeamId = teamId 126 o1.DisplayName = "Name" 127 o1.Name = "zz" + model.NewId() + "b" 128 o1.Type = model.CHANNEL_DIRECT 129 130 u1 := &model.User{} 131 u1.Email = MakeEmail() 132 u1.Nickname = model.NewId() 133 store.Must(ss.User().Save(u1)) 134 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u1.Id}, -1)) 135 136 u2 := &model.User{} 137 u2.Email = MakeEmail() 138 u2.Nickname = model.NewId() 139 store.Must(ss.User().Save(u2)) 140 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u2.Id}, -1)) 141 142 m1 := model.ChannelMember{} 143 m1.ChannelId = o1.Id 144 m1.UserId = u1.Id 145 m1.NotifyProps = model.GetDefaultChannelNotifyProps() 146 147 m2 := model.ChannelMember{} 148 m2.ChannelId = o1.Id 149 m2.UserId = u2.Id 150 m2.NotifyProps = model.GetDefaultChannelNotifyProps() 151 152 if err := (<-ss.Channel().SaveDirectChannel(&o1, &m1, &m2)).Err; err != nil { 153 t.Fatal("couldn't save direct channel", err) 154 } 155 156 members := (<-ss.Channel().GetMembers(o1.Id, 0, 100)).Data.(*model.ChannelMembers) 157 if len(*members) != 2 { 158 t.Fatal("should have saved 2 members") 159 } 160 161 if err := (<-ss.Channel().SaveDirectChannel(&o1, &m1, &m2)).Err; err == nil { 162 t.Fatal("shouldn't be able to update from save") 163 } 164 165 // Attempt to save a direct channel that already exists 166 o1a := model.Channel{ 167 TeamId: o1.TeamId, 168 DisplayName: o1.DisplayName, 169 Name: o1.Name, 170 Type: o1.Type, 171 } 172 173 if result := <-ss.Channel().SaveDirectChannel(&o1a, &m1, &m2); result.Err == nil { 174 t.Fatal("should've failed to save a duplicate direct channel") 175 } else if result.Err.Id != store.CHANNEL_EXISTS_ERROR { 176 t.Fatal("should've returned CHANNEL_EXISTS_ERROR") 177 } else if returned := result.Data.(*model.Channel); returned.Id != o1.Id { 178 t.Fatal("should've returned original channel when saving a duplicate direct channel") 179 } 180 181 // Attempt to save a non-direct channel 182 o1.Id = "" 183 o1.Name = "zz" + model.NewId() + "b" 184 o1.Type = model.CHANNEL_OPEN 185 if err := (<-ss.Channel().SaveDirectChannel(&o1, &m1, &m2)).Err; err == nil { 186 t.Fatal("Should not be able to save non-direct channel") 187 } 188 189 // Save yourself Direct Message 190 o1.Id = "" 191 o1.DisplayName = "Myself" 192 o1.Name = "zz" + model.NewId() + "b" 193 o1.Type = model.CHANNEL_DIRECT 194 if err := (<-ss.Channel().SaveDirectChannel(&o1, &m1, &m1)).Err; err != nil { 195 t.Fatal("couldn't save direct channel", err) 196 } 197 198 members = (<-ss.Channel().GetMembers(o1.Id, 0, 100)).Data.(*model.ChannelMembers) 199 if len(*members) != 1 { 200 t.Fatal("should have saved just 1 member") 201 } 202 203 // Manually truncate Channels table until testlib can handle cleanups 204 s.GetMaster().Exec("TRUNCATE Channels") 205 } 206 207 func testChannelStoreCreateDirectChannel(t *testing.T, ss store.Store) { 208 u1 := &model.User{} 209 u1.Email = MakeEmail() 210 u1.Nickname = model.NewId() 211 store.Must(ss.User().Save(u1)) 212 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u1.Id}, -1)) 213 214 u2 := &model.User{} 215 u2.Email = MakeEmail() 216 u2.Nickname = model.NewId() 217 store.Must(ss.User().Save(u2)) 218 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u2.Id}, -1)) 219 220 res := <-ss.Channel().CreateDirectChannel(u1.Id, u2.Id) 221 if res.Err != nil { 222 t.Fatal("couldn't create direct channel", res.Err) 223 } 224 c1 := res.Data.(*model.Channel) 225 defer func() { 226 <-ss.Channel().PermanentDeleteMembersByChannel(c1.Id) 227 <-ss.Channel().PermanentDelete(c1.Id) 228 }() 229 230 members := (<-ss.Channel().GetMembers(c1.Id, 0, 100)).Data.(*model.ChannelMembers) 231 if len(*members) != 2 { 232 t.Fatal("should have saved 2 members") 233 } 234 } 235 236 func testChannelStoreUpdate(t *testing.T, ss store.Store) { 237 o1 := model.Channel{} 238 o1.TeamId = model.NewId() 239 o1.DisplayName = "Name" 240 o1.Name = "zz" + model.NewId() + "b" 241 o1.Type = model.CHANNEL_OPEN 242 store.Must(ss.Channel().Save(&o1, -1)) 243 244 o2 := model.Channel{} 245 o2.TeamId = o1.TeamId 246 o2.DisplayName = "Name" 247 o2.Name = "zz" + model.NewId() + "b" 248 o2.Type = model.CHANNEL_OPEN 249 store.Must(ss.Channel().Save(&o2, -1)) 250 251 time.Sleep(100 * time.Millisecond) 252 253 if err := (<-ss.Channel().Update(&o1)).Err; err != nil { 254 t.Fatal(err) 255 } 256 257 o1.DeleteAt = 100 258 if err := (<-ss.Channel().Update(&o1)).Err; err == nil { 259 t.Fatal("Update should have failed because channel is archived") 260 } 261 262 o1.DeleteAt = 0 263 o1.Id = "missing" 264 if err := (<-ss.Channel().Update(&o1)).Err; err == nil { 265 t.Fatal("Update should have failed because of missing key") 266 } 267 268 o1.Id = model.NewId() 269 if err := (<-ss.Channel().Update(&o1)).Err; err == nil { 270 t.Fatal("Update should have faile because id change") 271 } 272 273 o2.Name = o1.Name 274 if err := (<-ss.Channel().Update(&o2)).Err; err == nil { 275 t.Fatal("Update should have failed because of existing name") 276 } 277 } 278 279 func testGetChannelUnread(t *testing.T, ss store.Store) { 280 teamId1 := model.NewId() 281 teamId2 := model.NewId() 282 283 uid := model.NewId() 284 m1 := &model.TeamMember{TeamId: teamId1, UserId: uid} 285 m2 := &model.TeamMember{TeamId: teamId2, UserId: uid} 286 store.Must(ss.Team().SaveMember(m1, -1)) 287 store.Must(ss.Team().SaveMember(m2, -1)) 288 notifyPropsModel := model.GetDefaultChannelNotifyProps() 289 290 // Setup Channel 1 291 c1 := &model.Channel{TeamId: m1.TeamId, Name: model.NewId(), DisplayName: "Downtown", Type: model.CHANNEL_OPEN, TotalMsgCount: 100} 292 store.Must(ss.Channel().Save(c1, -1)) 293 cm1 := &model.ChannelMember{ChannelId: c1.Id, UserId: m1.UserId, NotifyProps: notifyPropsModel, MsgCount: 90} 294 store.Must(ss.Channel().SaveMember(cm1)) 295 296 // Setup Channel 2 297 c2 := &model.Channel{TeamId: m2.TeamId, Name: model.NewId(), DisplayName: "Cultural", Type: model.CHANNEL_OPEN, TotalMsgCount: 100} 298 store.Must(ss.Channel().Save(c2, -1)) 299 cm2 := &model.ChannelMember{ChannelId: c2.Id, UserId: m2.UserId, NotifyProps: notifyPropsModel, MsgCount: 90, MentionCount: 5} 300 store.Must(ss.Channel().SaveMember(cm2)) 301 302 // Check for Channel 1 303 if resp := <-ss.Channel().GetChannelUnread(c1.Id, uid); resp.Err != nil { 304 t.Fatal(resp.Err) 305 } else { 306 ch := resp.Data.(*model.ChannelUnread) 307 if c1.Id != ch.ChannelId { 308 t.Fatal("wrong channel id") 309 } 310 311 if teamId1 != ch.TeamId { 312 t.Fatal("wrong team id for channel 1") 313 } 314 315 if ch.NotifyProps == nil { 316 t.Fatal("wrong props for channel 1") 317 } 318 319 if ch.MentionCount != 0 { 320 t.Fatal("wrong MentionCount for channel 1") 321 } 322 323 if ch.MsgCount != 10 { 324 t.Fatal("wrong MsgCount for channel 1") 325 } 326 } 327 328 // Check for Channel 2 329 if resp2 := <-ss.Channel().GetChannelUnread(c2.Id, uid); resp2.Err != nil { 330 t.Fatal(resp2.Err) 331 } else { 332 ch2 := resp2.Data.(*model.ChannelUnread) 333 if c2.Id != ch2.ChannelId { 334 t.Fatal("wrong channel id") 335 } 336 337 if teamId2 != ch2.TeamId { 338 t.Fatal("wrong team id") 339 } 340 341 if ch2.MentionCount != 5 { 342 t.Fatal("wrong MentionCount for channel 2") 343 } 344 345 if ch2.MsgCount != 10 { 346 t.Fatal("wrong MsgCount for channel 2") 347 } 348 } 349 } 350 351 func testChannelStoreGet(t *testing.T, ss store.Store, s SqlSupplier) { 352 o1 := model.Channel{} 353 o1.TeamId = model.NewId() 354 o1.DisplayName = "Name" 355 o1.Name = "zz" + model.NewId() + "b" 356 o1.Type = model.CHANNEL_OPEN 357 store.Must(ss.Channel().Save(&o1, -1)) 358 359 if r1 := <-ss.Channel().Get(o1.Id, false); r1.Err != nil { 360 t.Fatal(r1.Err) 361 } else { 362 if r1.Data.(*model.Channel).ToJson() != o1.ToJson() { 363 t.Fatal("invalid returned channel") 364 } 365 } 366 367 if err := (<-ss.Channel().Get("", false)).Err; err == nil { 368 t.Fatal("Missing id should have failed") 369 } 370 371 u1 := &model.User{} 372 u1.Email = MakeEmail() 373 u1.Nickname = model.NewId() 374 store.Must(ss.User().Save(u1)) 375 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u1.Id}, -1)) 376 377 u2 := model.User{} 378 u2.Email = MakeEmail() 379 u2.Nickname = model.NewId() 380 store.Must(ss.User().Save(&u2)) 381 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u2.Id}, -1)) 382 383 o2 := model.Channel{} 384 o2.TeamId = model.NewId() 385 o2.DisplayName = "Direct Name" 386 o2.Name = "zz" + model.NewId() + "b" 387 o2.Type = model.CHANNEL_DIRECT 388 389 m1 := model.ChannelMember{} 390 m1.ChannelId = o2.Id 391 m1.UserId = u1.Id 392 m1.NotifyProps = model.GetDefaultChannelNotifyProps() 393 394 m2 := model.ChannelMember{} 395 m2.ChannelId = o2.Id 396 m2.UserId = u2.Id 397 m2.NotifyProps = model.GetDefaultChannelNotifyProps() 398 399 store.Must(ss.Channel().SaveDirectChannel(&o2, &m1, &m2)) 400 401 if r2 := <-ss.Channel().Get(o2.Id, false); r2.Err != nil { 402 t.Fatal(r2.Err) 403 } else { 404 if r2.Data.(*model.Channel).ToJson() != o2.ToJson() { 405 t.Fatal("invalid returned channel") 406 } 407 } 408 409 if r4 := <-ss.Channel().Get(o2.Id, true); r4.Err != nil { 410 t.Fatal(r4.Err) 411 } else { 412 if r4.Data.(*model.Channel).ToJson() != o2.ToJson() { 413 t.Fatal("invalid returned channel") 414 } 415 } 416 417 if r3 := <-ss.Channel().GetAll(o1.TeamId); r3.Err != nil { 418 t.Fatal(r3.Err) 419 } else { 420 channels := r3.Data.([]*model.Channel) 421 if len(channels) == 0 { 422 t.Fatal("too little") 423 } 424 } 425 426 if r3 := <-ss.Channel().GetTeamChannels(o1.TeamId); r3.Err != nil { 427 t.Fatal(r3.Err) 428 } else { 429 channels := r3.Data.(*model.ChannelList) 430 if len(*channels) == 0 { 431 t.Fatal("too little") 432 } 433 } 434 // Manually truncate Channels table until testlib can handle cleanups 435 s.GetMaster().Exec("TRUNCATE Channels") 436 } 437 438 func testChannelStoreGetChannelsByIds(t *testing.T, ss store.Store) { 439 o1 := model.Channel{} 440 o1.TeamId = model.NewId() 441 o1.DisplayName = "Name" 442 o1.Name = "aa" + model.NewId() + "b" 443 o1.Type = model.CHANNEL_OPEN 444 store.Must(ss.Channel().Save(&o1, -1)) 445 446 u1 := &model.User{} 447 u1.Email = MakeEmail() 448 u1.Nickname = model.NewId() 449 store.Must(ss.User().Save(u1)) 450 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u1.Id}, -1)) 451 452 u2 := model.User{} 453 u2.Email = MakeEmail() 454 u2.Nickname = model.NewId() 455 store.Must(ss.User().Save(&u2)) 456 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u2.Id}, -1)) 457 458 o2 := model.Channel{} 459 o2.TeamId = model.NewId() 460 o2.DisplayName = "Direct Name" 461 o2.Name = "bb" + model.NewId() + "b" 462 o2.Type = model.CHANNEL_DIRECT 463 464 m1 := model.ChannelMember{} 465 m1.ChannelId = o2.Id 466 m1.UserId = u1.Id 467 m1.NotifyProps = model.GetDefaultChannelNotifyProps() 468 469 m2 := model.ChannelMember{} 470 m2.ChannelId = o2.Id 471 m2.UserId = u2.Id 472 m2.NotifyProps = model.GetDefaultChannelNotifyProps() 473 474 store.Must(ss.Channel().SaveDirectChannel(&o2, &m1, &m2)) 475 476 if r1 := <-ss.Channel().GetChannelsByIds([]string{o1.Id, o2.Id}); r1.Err != nil { 477 t.Fatal(r1.Err) 478 } else { 479 cl := r1.Data.([]*model.Channel) 480 if len(cl) != 2 { 481 t.Fatal("invalid returned channels, expected 2 and got " + strconv.Itoa(len(cl))) 482 } 483 if cl[0].ToJson() != o1.ToJson() { 484 t.Fatal("invalid returned channel") 485 } 486 if cl[1].ToJson() != o2.ToJson() { 487 t.Fatal("invalid returned channel") 488 } 489 } 490 491 nonexistentId := "abcd1234" 492 if r2 := <-ss.Channel().GetChannelsByIds([]string{o1.Id, nonexistentId}); r2.Err != nil { 493 t.Fatal(r2.Err) 494 } else { 495 cl := r2.Data.([]*model.Channel) 496 if len(cl) != 1 { 497 t.Fatal("invalid returned channels, expected 1 and got " + strconv.Itoa(len(cl))) 498 } 499 if cl[0].ToJson() != o1.ToJson() { 500 t.Fatal("invalid returned channel") 501 } 502 } 503 } 504 505 func testChannelStoreGetForPost(t *testing.T, ss store.Store) { 506 o1 := store.Must(ss.Channel().Save(&model.Channel{ 507 TeamId: model.NewId(), 508 DisplayName: "Name", 509 Name: "zz" + model.NewId() + "b", 510 Type: model.CHANNEL_OPEN, 511 }, -1)).(*model.Channel) 512 513 p1 := store.Must(ss.Post().Save(&model.Post{ 514 UserId: model.NewId(), 515 ChannelId: o1.Id, 516 Message: "test", 517 })).(*model.Post) 518 519 if r1 := <-ss.Channel().GetForPost(p1.Id); r1.Err != nil { 520 t.Fatal(r1.Err) 521 } else if r1.Data.(*model.Channel).Id != o1.Id { 522 t.Fatal("incorrect channel returned") 523 } 524 } 525 526 func testChannelStoreRestore(t *testing.T, ss store.Store) { 527 o1 := model.Channel{} 528 o1.TeamId = model.NewId() 529 o1.DisplayName = "Channel1" 530 o1.Name = "zz" + model.NewId() + "b" 531 o1.Type = model.CHANNEL_OPEN 532 store.Must(ss.Channel().Save(&o1, -1)) 533 534 if r := <-ss.Channel().Delete(o1.Id, model.GetMillis()); r.Err != nil { 535 t.Fatal(r.Err) 536 } 537 538 if r := <-ss.Channel().Get(o1.Id, false); r.Data.(*model.Channel).DeleteAt == 0 { 539 t.Fatal("should have been deleted") 540 } 541 542 if r := <-ss.Channel().Restore(o1.Id, model.GetMillis()); r.Err != nil { 543 t.Fatal(r.Err) 544 } 545 546 if r := <-ss.Channel().Get(o1.Id, false); r.Data.(*model.Channel).DeleteAt != 0 { 547 t.Fatal("should have been restored") 548 } 549 550 } 551 552 func testChannelStoreDelete(t *testing.T, ss store.Store) { 553 o1 := model.Channel{} 554 o1.TeamId = model.NewId() 555 o1.DisplayName = "Channel1" 556 o1.Name = "zz" + model.NewId() + "b" 557 o1.Type = model.CHANNEL_OPEN 558 store.Must(ss.Channel().Save(&o1, -1)) 559 560 o2 := model.Channel{} 561 o2.TeamId = o1.TeamId 562 o2.DisplayName = "Channel2" 563 o2.Name = "zz" + model.NewId() + "b" 564 o2.Type = model.CHANNEL_OPEN 565 store.Must(ss.Channel().Save(&o2, -1)) 566 567 o3 := model.Channel{} 568 o3.TeamId = o1.TeamId 569 o3.DisplayName = "Channel3" 570 o3.Name = "zz" + model.NewId() + "b" 571 o3.Type = model.CHANNEL_OPEN 572 store.Must(ss.Channel().Save(&o3, -1)) 573 574 o4 := model.Channel{} 575 o4.TeamId = o1.TeamId 576 o4.DisplayName = "Channel4" 577 o4.Name = "zz" + model.NewId() + "b" 578 o4.Type = model.CHANNEL_OPEN 579 store.Must(ss.Channel().Save(&o4, -1)) 580 581 m1 := model.ChannelMember{} 582 m1.ChannelId = o1.Id 583 m1.UserId = model.NewId() 584 m1.NotifyProps = model.GetDefaultChannelNotifyProps() 585 store.Must(ss.Channel().SaveMember(&m1)) 586 587 m2 := model.ChannelMember{} 588 m2.ChannelId = o2.Id 589 m2.UserId = m1.UserId 590 m2.NotifyProps = model.GetDefaultChannelNotifyProps() 591 store.Must(ss.Channel().SaveMember(&m2)) 592 593 if r := <-ss.Channel().Delete(o1.Id, model.GetMillis()); r.Err != nil { 594 t.Fatal(r.Err) 595 } 596 597 if r := <-ss.Channel().Get(o1.Id, false); r.Data.(*model.Channel).DeleteAt == 0 { 598 t.Fatal("should have been deleted") 599 } 600 601 if r := <-ss.Channel().Delete(o3.Id, model.GetMillis()); r.Err != nil { 602 t.Fatal(r.Err) 603 } 604 605 cresult := <-ss.Channel().GetChannels(o1.TeamId, m1.UserId, false) 606 require.Nil(t, cresult.Err) 607 list := cresult.Data.(*model.ChannelList) 608 609 if len(*list) != 1 { 610 t.Fatal("invalid number of channels") 611 } 612 613 cresult = <-ss.Channel().GetMoreChannels(o1.TeamId, m1.UserId, 0, 100) 614 require.Nil(t, cresult.Err) 615 list = cresult.Data.(*model.ChannelList) 616 617 if len(*list) != 1 { 618 t.Fatal("invalid number of channels") 619 } 620 621 cresult = <-ss.Channel().PermanentDelete(o2.Id) 622 require.Nil(t, cresult.Err) 623 624 cresult = <-ss.Channel().GetChannels(o1.TeamId, m1.UserId, false) 625 if assert.NotNil(t, cresult.Err) { 626 require.Equal(t, "store.sql_channel.get_channels.not_found.app_error", cresult.Err.Id) 627 } else { 628 require.Equal(t, &model.ChannelList{}, cresult.Data.(*model.ChannelList)) 629 } 630 631 if r := <-ss.Channel().PermanentDeleteByTeam(o1.TeamId); r.Err != nil { 632 t.Fatal(r.Err) 633 } 634 } 635 636 func testChannelStoreGetByName(t *testing.T, ss store.Store) { 637 o1 := model.Channel{} 638 o1.TeamId = model.NewId() 639 o1.DisplayName = "Name" 640 o1.Name = "zz" + model.NewId() + "b" 641 o1.Type = model.CHANNEL_OPEN 642 store.Must(ss.Channel().Save(&o1, -1)) 643 644 result := <-ss.Channel().GetByName(o1.TeamId, o1.Name, true) 645 require.Nil(t, result.Err) 646 require.Equal(t, o1.ToJson(), result.Data.(*model.Channel).ToJson(), "invalid returned channel") 647 648 channelID := result.Data.(*model.Channel).Id 649 650 result = <-ss.Channel().GetByName(o1.TeamId, "", true) 651 require.NotNil(t, result.Err, "Missing id should have failed") 652 653 result = <-ss.Channel().GetByName(o1.TeamId, o1.Name, false) 654 require.Nil(t, result.Err) 655 require.Equal(t, o1.ToJson(), result.Data.(*model.Channel).ToJson(), "invalid returned channel") 656 657 result = <-ss.Channel().GetByName(o1.TeamId, "", false) 658 require.NotNil(t, result.Err, "Missing id should have failed") 659 660 store.Must(ss.Channel().Delete(channelID, model.GetMillis())) 661 result = <-ss.Channel().GetByName(o1.TeamId, o1.Name, false) 662 require.NotNil(t, result.Err, "Deleted channel should not be returned by GetByName()") 663 } 664 665 func testChannelStoreGetByNames(t *testing.T, ss store.Store) { 666 o1 := model.Channel{ 667 TeamId: model.NewId(), 668 DisplayName: "Name", 669 Name: "zz" + model.NewId() + "b", 670 Type: model.CHANNEL_OPEN, 671 } 672 store.Must(ss.Channel().Save(&o1, -1)) 673 674 o2 := model.Channel{ 675 TeamId: o1.TeamId, 676 DisplayName: "Name", 677 Name: "zz" + model.NewId() + "b", 678 Type: model.CHANNEL_OPEN, 679 } 680 store.Must(ss.Channel().Save(&o2, -1)) 681 682 for index, tc := range []struct { 683 TeamId string 684 Names []string 685 ExpectedIds []string 686 }{ 687 {o1.TeamId, []string{o1.Name}, []string{o1.Id}}, 688 {o1.TeamId, []string{o1.Name, o2.Name}, []string{o1.Id, o2.Id}}, 689 {o1.TeamId, nil, nil}, 690 {o1.TeamId, []string{"foo"}, nil}, 691 {o1.TeamId, []string{o1.Name, "foo", o2.Name, o2.Name}, []string{o1.Id, o2.Id}}, 692 {"", []string{o1.Name, "foo", o2.Name, o2.Name}, []string{o1.Id, o2.Id}}, 693 {"asd", []string{o1.Name, "foo", o2.Name, o2.Name}, nil}, 694 } { 695 r := <-ss.Channel().GetByNames(tc.TeamId, tc.Names, true) 696 require.Nil(t, r.Err) 697 channels := r.Data.([]*model.Channel) 698 var ids []string 699 for _, channel := range channels { 700 ids = append(ids, channel.Id) 701 } 702 sort.Strings(ids) 703 sort.Strings(tc.ExpectedIds) 704 assert.Equal(t, tc.ExpectedIds, ids, "tc %v", index) 705 } 706 707 store.Must(ss.Channel().Delete(o1.Id, model.GetMillis())) 708 store.Must(ss.Channel().Delete(o2.Id, model.GetMillis())) 709 710 r := <-ss.Channel().GetByNames(o1.TeamId, []string{o1.Name}, false) 711 require.Nil(t, r.Err) 712 channels := r.Data.([]*model.Channel) 713 assert.Len(t, channels, 0) 714 } 715 716 func testChannelStoreGetDeletedByName(t *testing.T, ss store.Store) { 717 o1 := model.Channel{} 718 o1.TeamId = model.NewId() 719 o1.DisplayName = "Name" 720 o1.Name = "zz" + model.NewId() + "b" 721 o1.Type = model.CHANNEL_OPEN 722 store.Must(ss.Channel().Save(&o1, -1)) 723 now := model.GetMillis() 724 store.Must(ss.Channel().Delete(o1.Id, now)) 725 o1.DeleteAt = now 726 o1.UpdateAt = now 727 728 if r1 := <-ss.Channel().GetDeletedByName(o1.TeamId, o1.Name); r1.Err != nil { 729 t.Fatal(r1.Err) 730 } else { 731 if r1.Data.(*model.Channel).ToJson() != o1.ToJson() { 732 t.Fatal("invalid returned channel") 733 } 734 } 735 736 if err := (<-ss.Channel().GetDeletedByName(o1.TeamId, "")).Err; err == nil { 737 t.Fatal("Missing id should have failed") 738 } 739 } 740 741 func testChannelStoreGetDeleted(t *testing.T, ss store.Store) { 742 o1 := model.Channel{} 743 o1.TeamId = model.NewId() 744 o1.DisplayName = "Channel1" 745 o1.Name = "zz" + model.NewId() + "b" 746 o1.Type = model.CHANNEL_OPEN 747 store.Must(ss.Channel().Save(&o1, -1)) 748 store.Must(ss.Channel().Delete(o1.Id, model.GetMillis())) 749 750 cresult := <-ss.Channel().GetDeleted(o1.TeamId, 0, 100) 751 if cresult.Err != nil { 752 t.Fatal(cresult.Err) 753 } 754 list := cresult.Data.(*model.ChannelList) 755 756 if len(*list) != 1 { 757 t.Fatal("wrong list") 758 } 759 760 if (*list)[0].Name != o1.Name { 761 t.Fatal("missing channel") 762 } 763 764 o2 := model.Channel{} 765 o2.TeamId = o1.TeamId 766 o2.DisplayName = "Channel2" 767 o2.Name = "zz" + model.NewId() + "b" 768 o2.Type = model.CHANNEL_OPEN 769 store.Must(ss.Channel().Save(&o2, -1)) 770 771 cresult = <-ss.Channel().GetDeleted(o1.TeamId, 0, 100) 772 if cresult.Err != nil { 773 t.Fatal(cresult.Err) 774 } 775 list = cresult.Data.(*model.ChannelList) 776 777 if len(*list) != 1 { 778 t.Fatal("wrong list") 779 } 780 781 o3 := model.Channel{} 782 o3.TeamId = o1.TeamId 783 o3.DisplayName = "Channel3" 784 o3.Name = "zz" + model.NewId() + "b" 785 o3.Type = model.CHANNEL_OPEN 786 store.Must(ss.Channel().Save(&o3, -1)) 787 store.Must(ss.Channel().SetDeleteAt(o3.Id, model.GetMillis(), model.GetMillis())) 788 789 cresult = <-ss.Channel().GetDeleted(o1.TeamId, 0, 100) 790 if cresult.Err != nil { 791 t.Fatal(cresult.Err) 792 } 793 list = cresult.Data.(*model.ChannelList) 794 795 if len(*list) != 2 { 796 t.Fatal("wrong list length") 797 } 798 799 cresult = <-ss.Channel().GetDeleted(o1.TeamId, 0, 1) 800 if cresult.Err != nil { 801 t.Fatal(cresult.Err) 802 } 803 list = cresult.Data.(*model.ChannelList) 804 805 if len(*list) != 1 { 806 t.Fatal("wrong list length") 807 } 808 809 cresult = <-ss.Channel().GetDeleted(o1.TeamId, 1, 1) 810 if cresult.Err != nil { 811 t.Fatal(cresult.Err) 812 } 813 list = cresult.Data.(*model.ChannelList) 814 815 if len(*list) != 1 { 816 t.Fatal("wrong list length") 817 } 818 819 } 820 821 func testChannelMemberStore(t *testing.T, ss store.Store) { 822 c1 := model.Channel{} 823 c1.TeamId = model.NewId() 824 c1.DisplayName = "NameName" 825 c1.Name = "zz" + model.NewId() + "b" 826 c1.Type = model.CHANNEL_OPEN 827 c1 = *store.Must(ss.Channel().Save(&c1, -1)).(*model.Channel) 828 829 c1t1 := (<-ss.Channel().Get(c1.Id, false)).Data.(*model.Channel) 830 assert.EqualValues(t, 0, c1t1.ExtraUpdateAt, "ExtraUpdateAt should be 0") 831 832 u1 := model.User{} 833 u1.Email = MakeEmail() 834 u1.Nickname = model.NewId() 835 store.Must(ss.User().Save(&u1)) 836 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u1.Id}, -1)) 837 838 u2 := model.User{} 839 u2.Email = MakeEmail() 840 u2.Nickname = model.NewId() 841 store.Must(ss.User().Save(&u2)) 842 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u2.Id}, -1)) 843 844 o1 := model.ChannelMember{} 845 o1.ChannelId = c1.Id 846 o1.UserId = u1.Id 847 o1.NotifyProps = model.GetDefaultChannelNotifyProps() 848 store.Must(ss.Channel().SaveMember(&o1)) 849 850 o2 := model.ChannelMember{} 851 o2.ChannelId = c1.Id 852 o2.UserId = u2.Id 853 o2.NotifyProps = model.GetDefaultChannelNotifyProps() 854 store.Must(ss.Channel().SaveMember(&o2)) 855 856 c1t2 := (<-ss.Channel().Get(c1.Id, false)).Data.(*model.Channel) 857 assert.EqualValues(t, 0, c1t2.ExtraUpdateAt, "ExtraUpdateAt should be 0") 858 859 count := (<-ss.Channel().GetMemberCount(o1.ChannelId, true)).Data.(int64) 860 if count != 2 { 861 t.Fatal("should have saved 2 members") 862 } 863 864 count = (<-ss.Channel().GetMemberCount(o1.ChannelId, true)).Data.(int64) 865 if count != 2 { 866 t.Fatal("should have saved 2 members") 867 } 868 869 if ss.Channel().GetMemberCountFromCache(o1.ChannelId) != 2 { 870 t.Fatal("should have saved 2 members") 871 } 872 873 if ss.Channel().GetMemberCountFromCache("junk") != 0 { 874 t.Fatal("should have saved 0 members") 875 } 876 877 count = (<-ss.Channel().GetMemberCount(o1.ChannelId, false)).Data.(int64) 878 if count != 2 { 879 t.Fatal("should have saved 2 members") 880 } 881 882 store.Must(ss.Channel().RemoveMember(o2.ChannelId, o2.UserId)) 883 884 count = (<-ss.Channel().GetMemberCount(o1.ChannelId, false)).Data.(int64) 885 if count != 1 { 886 t.Fatal("should have removed 1 member") 887 } 888 889 c1t3 := (<-ss.Channel().Get(c1.Id, false)).Data.(*model.Channel) 890 assert.EqualValues(t, 0, c1t3.ExtraUpdateAt, "ExtraUpdateAt should be 0") 891 892 member := (<-ss.Channel().GetMember(o1.ChannelId, o1.UserId)).Data.(*model.ChannelMember) 893 if member.ChannelId != o1.ChannelId { 894 t.Fatal("should have go member") 895 } 896 897 if err := (<-ss.Channel().SaveMember(&o1)).Err; err == nil { 898 t.Fatal("Should have been a duplicate") 899 } 900 901 c1t4 := (<-ss.Channel().Get(c1.Id, false)).Data.(*model.Channel) 902 assert.EqualValues(t, 0, c1t4.ExtraUpdateAt, "ExtraUpdateAt should be 0") 903 } 904 905 func testChannelDeleteMemberStore(t *testing.T, ss store.Store) { 906 c1 := model.Channel{} 907 c1.TeamId = model.NewId() 908 c1.DisplayName = "NameName" 909 c1.Name = "zz" + model.NewId() + "b" 910 c1.Type = model.CHANNEL_OPEN 911 c1 = *store.Must(ss.Channel().Save(&c1, -1)).(*model.Channel) 912 913 c1t1 := (<-ss.Channel().Get(c1.Id, false)).Data.(*model.Channel) 914 assert.EqualValues(t, 0, c1t1.ExtraUpdateAt, "ExtraUpdateAt should be 0") 915 916 u1 := model.User{} 917 u1.Email = MakeEmail() 918 u1.Nickname = model.NewId() 919 store.Must(ss.User().Save(&u1)) 920 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u1.Id}, -1)) 921 922 u2 := model.User{} 923 u2.Email = MakeEmail() 924 u2.Nickname = model.NewId() 925 store.Must(ss.User().Save(&u2)) 926 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u2.Id}, -1)) 927 928 o1 := model.ChannelMember{} 929 o1.ChannelId = c1.Id 930 o1.UserId = u1.Id 931 o1.NotifyProps = model.GetDefaultChannelNotifyProps() 932 store.Must(ss.Channel().SaveMember(&o1)) 933 934 o2 := model.ChannelMember{} 935 o2.ChannelId = c1.Id 936 o2.UserId = u2.Id 937 o2.NotifyProps = model.GetDefaultChannelNotifyProps() 938 store.Must(ss.Channel().SaveMember(&o2)) 939 940 c1t2 := (<-ss.Channel().Get(c1.Id, false)).Data.(*model.Channel) 941 assert.EqualValues(t, 0, c1t2.ExtraUpdateAt, "ExtraUpdateAt should be 0") 942 943 count := (<-ss.Channel().GetMemberCount(o1.ChannelId, false)).Data.(int64) 944 if count != 2 { 945 t.Fatal("should have saved 2 members") 946 } 947 948 store.Must(ss.Channel().PermanentDeleteMembersByUser(o2.UserId)) 949 950 count = (<-ss.Channel().GetMemberCount(o1.ChannelId, false)).Data.(int64) 951 if count != 1 { 952 t.Fatal("should have removed 1 member") 953 } 954 955 if r1 := <-ss.Channel().PermanentDeleteMembersByChannel(o1.ChannelId); r1.Err != nil { 956 t.Fatal(r1.Err) 957 } 958 959 count = (<-ss.Channel().GetMemberCount(o1.ChannelId, false)).Data.(int64) 960 if count != 0 { 961 t.Fatal("should have removed all members") 962 } 963 } 964 965 func testChannelStoreGetChannels(t *testing.T, ss store.Store) { 966 o2 := model.Channel{} 967 o2.TeamId = model.NewId() 968 o2.DisplayName = "Channel2" 969 o2.Name = "zz" + model.NewId() + "b" 970 o2.Type = model.CHANNEL_OPEN 971 store.Must(ss.Channel().Save(&o2, -1)) 972 973 o1 := model.Channel{} 974 o1.TeamId = model.NewId() 975 o1.DisplayName = "Channel1" 976 o1.Name = "zz" + model.NewId() + "b" 977 o1.Type = model.CHANNEL_OPEN 978 store.Must(ss.Channel().Save(&o1, -1)) 979 980 m1 := model.ChannelMember{} 981 m1.ChannelId = o1.Id 982 m1.UserId = model.NewId() 983 m1.NotifyProps = model.GetDefaultChannelNotifyProps() 984 store.Must(ss.Channel().SaveMember(&m1)) 985 986 m2 := model.ChannelMember{} 987 m2.ChannelId = o1.Id 988 m2.UserId = model.NewId() 989 m2.NotifyProps = model.GetDefaultChannelNotifyProps() 990 store.Must(ss.Channel().SaveMember(&m2)) 991 992 m3 := model.ChannelMember{} 993 m3.ChannelId = o2.Id 994 m3.UserId = model.NewId() 995 m3.NotifyProps = model.GetDefaultChannelNotifyProps() 996 store.Must(ss.Channel().SaveMember(&m3)) 997 998 cresult := <-ss.Channel().GetChannels(o1.TeamId, m1.UserId, false) 999 list := cresult.Data.(*model.ChannelList) 1000 1001 if (*list)[0].Id != o1.Id { 1002 t.Fatal("missing channel") 1003 } 1004 1005 acresult := <-ss.Channel().GetAllChannelMembersForUser(m1.UserId, false, false) 1006 ids := acresult.Data.(map[string]string) 1007 if _, ok := ids[o1.Id]; !ok { 1008 t.Fatal("missing channel") 1009 } 1010 1011 acresult2 := <-ss.Channel().GetAllChannelMembersForUser(m1.UserId, true, false) 1012 ids2 := acresult2.Data.(map[string]string) 1013 if _, ok := ids2[o1.Id]; !ok { 1014 t.Fatal("missing channel") 1015 } 1016 1017 acresult3 := <-ss.Channel().GetAllChannelMembersForUser(m1.UserId, true, false) 1018 ids3 := acresult3.Data.(map[string]string) 1019 if _, ok := ids3[o1.Id]; !ok { 1020 t.Fatal("missing channel") 1021 } 1022 1023 if !ss.Channel().IsUserInChannelUseCache(m1.UserId, o1.Id) { 1024 t.Fatal("missing channel") 1025 } 1026 1027 if ss.Channel().IsUserInChannelUseCache(m1.UserId, o2.Id) { 1028 t.Fatal("missing channel") 1029 } 1030 1031 if ss.Channel().IsUserInChannelUseCache(m1.UserId, "blahblah") { 1032 t.Fatal("missing channel") 1033 } 1034 1035 if ss.Channel().IsUserInChannelUseCache("blahblah", "blahblah") { 1036 t.Fatal("missing channel") 1037 } 1038 1039 ss.Channel().InvalidateAllChannelMembersForUser(m1.UserId) 1040 } 1041 1042 func testChannelStoreGetAllChannels(t *testing.T, ss store.Store, s SqlSupplier) { 1043 cleanupChannels(t, ss) 1044 1045 t1 := model.Team{} 1046 t1.DisplayName = "Name" 1047 t1.Name = model.NewId() 1048 t1.Email = MakeEmail() 1049 t1.Type = model.TEAM_OPEN 1050 store.Must(ss.Team().Save(&t1)) 1051 1052 t2 := model.Team{} 1053 t2.DisplayName = "Name2" 1054 t2.Name = model.NewId() 1055 t2.Email = MakeEmail() 1056 t2.Type = model.TEAM_OPEN 1057 store.Must(ss.Team().Save(&t2)) 1058 1059 c1 := model.Channel{} 1060 c1.TeamId = t1.Id 1061 c1.DisplayName = "Channel1" + model.NewId() 1062 c1.Name = "zz" + model.NewId() + "b" 1063 c1.Type = model.CHANNEL_OPEN 1064 store.Must(ss.Channel().Save(&c1, -1)) 1065 1066 c2 := model.Channel{} 1067 c2.TeamId = t1.Id 1068 c2.DisplayName = "Channel2" + model.NewId() 1069 c2.Name = "zz" + model.NewId() + "b" 1070 c2.Type = model.CHANNEL_OPEN 1071 store.Must(ss.Channel().Save(&c2, -1)) 1072 c2.DeleteAt = model.GetMillis() 1073 c2.UpdateAt = c2.DeleteAt 1074 store.Must(ss.Channel().Delete(c2.Id, c2.DeleteAt)) 1075 1076 c3 := model.Channel{} 1077 c3.TeamId = t2.Id 1078 c3.DisplayName = "Channel3" + model.NewId() 1079 c3.Name = "zz" + model.NewId() + "b" 1080 c3.Type = model.CHANNEL_PRIVATE 1081 store.Must(ss.Channel().Save(&c3, -1)) 1082 1083 store.Must(ss.Channel().CreateDirectChannel(model.NewId(), model.NewId())) 1084 1085 userIds := []string{model.NewId(), model.NewId(), model.NewId()} 1086 1087 c5 := model.Channel{} 1088 c5.Name = model.GetGroupNameFromUserIds(userIds) 1089 c5.DisplayName = "GroupChannel" + model.NewId() 1090 c5.Name = "zz" + model.NewId() + "b" 1091 c5.Type = model.CHANNEL_GROUP 1092 store.Must(ss.Channel().Save(&c5, -1)) 1093 1094 cresult := <-ss.Channel().GetAllChannels(0, 10, false) 1095 list := cresult.Data.(*model.ChannelListWithTeamData) 1096 assert.Len(t, *list, 2) 1097 assert.Equal(t, (*list)[0].Id, c1.Id) 1098 assert.Equal(t, (*list)[0].TeamDisplayName, "Name") 1099 assert.Equal(t, (*list)[1].Id, c3.Id) 1100 assert.Equal(t, (*list)[1].TeamDisplayName, "Name2") 1101 1102 cresult = <-ss.Channel().GetAllChannels(0, 10, true) 1103 list = cresult.Data.(*model.ChannelListWithTeamData) 1104 assert.Len(t, *list, 3) 1105 assert.Equal(t, (*list)[0].Id, c1.Id) 1106 assert.Equal(t, (*list)[0].TeamDisplayName, "Name") 1107 assert.Equal(t, (*list)[1].Id, c2.Id) 1108 assert.Equal(t, (*list)[2].Id, c3.Id) 1109 1110 cresult = <-ss.Channel().GetAllChannels(0, 1, true) 1111 list = cresult.Data.(*model.ChannelListWithTeamData) 1112 assert.Len(t, *list, 1) 1113 assert.Equal(t, (*list)[0].Id, c1.Id) 1114 assert.Equal(t, (*list)[0].TeamDisplayName, "Name") 1115 1116 // Manually truncate Channels table until testlib can handle cleanups 1117 s.GetMaster().Exec("TRUNCATE Channels") 1118 } 1119 1120 func testChannelStoreGetMoreChannels(t *testing.T, ss store.Store) { 1121 teamId := model.NewId() 1122 otherTeamId := model.NewId() 1123 userId := model.NewId() 1124 otherUserId1 := model.NewId() 1125 otherUserId2 := model.NewId() 1126 1127 // o1 is a channel on the team to which the user (and the other user 1) belongs 1128 o1 := model.Channel{ 1129 TeamId: teamId, 1130 DisplayName: "Channel1", 1131 Name: "zz" + model.NewId() + "b", 1132 Type: model.CHANNEL_OPEN, 1133 } 1134 store.Must(ss.Channel().Save(&o1, -1)) 1135 1136 store.Must(ss.Channel().SaveMember(&model.ChannelMember{ 1137 ChannelId: o1.Id, 1138 UserId: userId, 1139 NotifyProps: model.GetDefaultChannelNotifyProps(), 1140 })) 1141 1142 store.Must(ss.Channel().SaveMember(&model.ChannelMember{ 1143 ChannelId: o1.Id, 1144 UserId: otherUserId1, 1145 NotifyProps: model.GetDefaultChannelNotifyProps(), 1146 })) 1147 1148 // o2 is a channel on the other team to which the user belongs 1149 o2 := model.Channel{ 1150 TeamId: otherTeamId, 1151 DisplayName: "Channel2", 1152 Name: "zz" + model.NewId() + "b", 1153 Type: model.CHANNEL_OPEN, 1154 } 1155 store.Must(ss.Channel().Save(&o2, -1)) 1156 1157 store.Must(ss.Channel().SaveMember(&model.ChannelMember{ 1158 ChannelId: o2.Id, 1159 UserId: otherUserId2, 1160 NotifyProps: model.GetDefaultChannelNotifyProps(), 1161 })) 1162 1163 // o3 is a channel on the team to which the user does not belong, and thus should show up 1164 // in "more channels" 1165 o3 := model.Channel{ 1166 TeamId: teamId, 1167 DisplayName: "ChannelA", 1168 Name: "zz" + model.NewId() + "b", 1169 Type: model.CHANNEL_OPEN, 1170 } 1171 store.Must(ss.Channel().Save(&o3, -1)) 1172 1173 // o4 is a private channel on the team to which the user does not belong 1174 o4 := model.Channel{ 1175 TeamId: teamId, 1176 DisplayName: "ChannelB", 1177 Name: "zz" + model.NewId() + "b", 1178 Type: model.CHANNEL_PRIVATE, 1179 } 1180 store.Must(ss.Channel().Save(&o4, -1)) 1181 1182 // o5 is another private channel on the team to which the user does belong 1183 o5 := model.Channel{ 1184 TeamId: teamId, 1185 DisplayName: "ChannelC", 1186 Name: "zz" + model.NewId() + "b", 1187 Type: model.CHANNEL_PRIVATE, 1188 } 1189 store.Must(ss.Channel().Save(&o5, -1)) 1190 1191 store.Must(ss.Channel().SaveMember(&model.ChannelMember{ 1192 ChannelId: o5.Id, 1193 UserId: userId, 1194 NotifyProps: model.GetDefaultChannelNotifyProps(), 1195 })) 1196 1197 t.Run("only o3 listed in more channels", func(t *testing.T) { 1198 result := <-ss.Channel().GetMoreChannels(teamId, userId, 0, 100) 1199 require.Nil(t, result.Err) 1200 require.Equal(t, &model.ChannelList{&o3}, result.Data.(*model.ChannelList)) 1201 }) 1202 1203 // o6 is another channel on the team to which the user does not belong, and would thus 1204 // start showing up in "more channels". 1205 o6 := model.Channel{ 1206 TeamId: teamId, 1207 DisplayName: "ChannelD", 1208 Name: "zz" + model.NewId() + "b", 1209 Type: model.CHANNEL_OPEN, 1210 } 1211 store.Must(ss.Channel().Save(&o6, -1)) 1212 1213 // o7 is another channel on the team to which the user does not belong, but is deleted, 1214 // and thus would not start showing up in "more channels" 1215 o7 := model.Channel{ 1216 TeamId: teamId, 1217 DisplayName: "ChannelD", 1218 Name: "zz" + model.NewId() + "b", 1219 Type: model.CHANNEL_OPEN, 1220 } 1221 store.Must(ss.Channel().Save(&o7, -1)) 1222 store.Must(ss.Channel().Delete(o7.Id, model.GetMillis())) 1223 1224 t.Run("both o3 and o6 listed in more channels", func(t *testing.T) { 1225 result := <-ss.Channel().GetMoreChannels(teamId, userId, 0, 100) 1226 require.Nil(t, result.Err) 1227 require.Equal(t, &model.ChannelList{&o3, &o6}, result.Data.(*model.ChannelList)) 1228 }) 1229 1230 t.Run("only o3 listed in more channels with offset 0, limit 1", func(t *testing.T) { 1231 result := <-ss.Channel().GetMoreChannels(teamId, userId, 0, 1) 1232 require.Nil(t, result.Err) 1233 require.Equal(t, &model.ChannelList{&o3}, result.Data.(*model.ChannelList)) 1234 }) 1235 1236 t.Run("only o6 listed in more channels with offset 1, limit 1", func(t *testing.T) { 1237 result := <-ss.Channel().GetMoreChannels(teamId, userId, 1, 1) 1238 require.Nil(t, result.Err) 1239 require.Equal(t, &model.ChannelList{&o6}, result.Data.(*model.ChannelList)) 1240 }) 1241 1242 t.Run("verify analytics for open channels", func(t *testing.T) { 1243 result := <-ss.Channel().AnalyticsTypeCount(teamId, model.CHANNEL_OPEN) 1244 require.Nil(t, result.Err) 1245 require.EqualValues(t, 4, result.Data.(int64)) 1246 }) 1247 1248 t.Run("verify analytics for private channels", func(t *testing.T) { 1249 result := <-ss.Channel().AnalyticsTypeCount(teamId, model.CHANNEL_PRIVATE) 1250 require.Nil(t, result.Err) 1251 require.EqualValues(t, 2, result.Data.(int64)) 1252 }) 1253 } 1254 1255 func testChannelStoreGetPublicChannelsForTeam(t *testing.T, ss store.Store) { 1256 teamId := model.NewId() 1257 1258 // o1 is a public channel on the team 1259 o1 := model.Channel{ 1260 TeamId: teamId, 1261 DisplayName: "OpenChannel1Team1", 1262 Name: "zz" + model.NewId() + "b", 1263 Type: model.CHANNEL_OPEN, 1264 } 1265 store.Must(ss.Channel().Save(&o1, -1)) 1266 1267 // o2 is a public channel on another team 1268 o2 := model.Channel{ 1269 TeamId: model.NewId(), 1270 DisplayName: "OpenChannel1Team2", 1271 Name: "zz" + model.NewId() + "b", 1272 Type: model.CHANNEL_OPEN, 1273 } 1274 store.Must(ss.Channel().Save(&o2, -1)) 1275 1276 // o3 is a private channel on the team 1277 o3 := model.Channel{ 1278 TeamId: teamId, 1279 DisplayName: "PrivateChannel1Team1", 1280 Name: "zz" + model.NewId() + "b", 1281 Type: model.CHANNEL_PRIVATE, 1282 } 1283 store.Must(ss.Channel().Save(&o3, -1)) 1284 1285 t.Run("only o1 initially listed in public channels", func(t *testing.T) { 1286 result := <-ss.Channel().GetPublicChannelsForTeam(teamId, 0, 100) 1287 require.Nil(t, result.Err) 1288 require.Equal(t, &model.ChannelList{&o1}, result.Data.(*model.ChannelList)) 1289 }) 1290 1291 // o4 is another public channel on the team 1292 o4 := model.Channel{ 1293 TeamId: teamId, 1294 DisplayName: "OpenChannel2Team1", 1295 Name: "zz" + model.NewId() + "b", 1296 Type: model.CHANNEL_OPEN, 1297 } 1298 store.Must(ss.Channel().Save(&o4, -1)) 1299 1300 // o5 is another public, but deleted channel on the team 1301 o5 := model.Channel{ 1302 TeamId: teamId, 1303 DisplayName: "OpenChannel3Team1", 1304 Name: "zz" + model.NewId() + "b", 1305 Type: model.CHANNEL_OPEN, 1306 } 1307 store.Must(ss.Channel().Save(&o5, -1)) 1308 store.Must(ss.Channel().Delete(o5.Id, model.GetMillis())) 1309 1310 t.Run("both o1 and o4 listed in public channels", func(t *testing.T) { 1311 cresult := <-ss.Channel().GetPublicChannelsForTeam(teamId, 0, 100) 1312 require.Nil(t, cresult.Err) 1313 require.Equal(t, &model.ChannelList{&o1, &o4}, cresult.Data.(*model.ChannelList)) 1314 }) 1315 1316 t.Run("only o1 listed in public channels with offset 0, limit 1", func(t *testing.T) { 1317 result := <-ss.Channel().GetPublicChannelsForTeam(teamId, 0, 1) 1318 require.Nil(t, result.Err) 1319 require.Equal(t, &model.ChannelList{&o1}, result.Data.(*model.ChannelList)) 1320 }) 1321 1322 t.Run("only o4 listed in public channels with offset 1, limit 1", func(t *testing.T) { 1323 result := <-ss.Channel().GetPublicChannelsForTeam(teamId, 1, 1) 1324 require.Nil(t, result.Err) 1325 require.Equal(t, &model.ChannelList{&o4}, result.Data.(*model.ChannelList)) 1326 }) 1327 1328 t.Run("verify analytics for open channels", func(t *testing.T) { 1329 result := <-ss.Channel().AnalyticsTypeCount(teamId, model.CHANNEL_OPEN) 1330 require.Nil(t, result.Err) 1331 require.EqualValues(t, 3, result.Data.(int64)) 1332 }) 1333 1334 t.Run("verify analytics for private channels", func(t *testing.T) { 1335 result := <-ss.Channel().AnalyticsTypeCount(teamId, model.CHANNEL_PRIVATE) 1336 require.Nil(t, result.Err) 1337 require.EqualValues(t, 1, result.Data.(int64)) 1338 }) 1339 } 1340 1341 func testChannelStoreGetPublicChannelsByIdsForTeam(t *testing.T, ss store.Store) { 1342 teamId := model.NewId() 1343 1344 // oc1 is a public channel on the team 1345 oc1 := model.Channel{ 1346 TeamId: teamId, 1347 DisplayName: "OpenChannel1Team1", 1348 Name: "zz" + model.NewId() + "b", 1349 Type: model.CHANNEL_OPEN, 1350 } 1351 store.Must(ss.Channel().Save(&oc1, -1)) 1352 1353 // oc2 is a public channel on another team 1354 oc2 := model.Channel{ 1355 TeamId: model.NewId(), 1356 DisplayName: "OpenChannel2TeamOther", 1357 Name: "zz" + model.NewId() + "b", 1358 Type: model.CHANNEL_OPEN, 1359 } 1360 store.Must(ss.Channel().Save(&oc2, -1)) 1361 1362 // pc3 is a private channel on the team 1363 pc3 := model.Channel{ 1364 TeamId: teamId, 1365 DisplayName: "PrivateChannel3Team1", 1366 Name: "zz" + model.NewId() + "b", 1367 Type: model.CHANNEL_PRIVATE, 1368 } 1369 store.Must(ss.Channel().Save(&pc3, -1)) 1370 1371 t.Run("oc1 by itself should be found as a public channel in the team", func(t *testing.T) { 1372 result := <-ss.Channel().GetPublicChannelsByIdsForTeam(teamId, []string{oc1.Id}) 1373 require.Nil(t, result.Err) 1374 require.Equal(t, &model.ChannelList{&oc1}, result.Data.(*model.ChannelList)) 1375 }) 1376 1377 t.Run("only oc1, among others, should be found as a public channel in the team", func(t *testing.T) { 1378 result := <-ss.Channel().GetPublicChannelsByIdsForTeam(teamId, []string{oc1.Id, oc2.Id, model.NewId(), pc3.Id}) 1379 require.Nil(t, result.Err) 1380 require.Equal(t, &model.ChannelList{&oc1}, result.Data.(*model.ChannelList)) 1381 }) 1382 1383 // oc4 is another public channel on the team 1384 oc4 := model.Channel{ 1385 TeamId: teamId, 1386 DisplayName: "OpenChannel4Team1", 1387 Name: "zz" + model.NewId() + "b", 1388 Type: model.CHANNEL_OPEN, 1389 } 1390 store.Must(ss.Channel().Save(&oc4, -1)) 1391 1392 // oc4 is another public, but deleted channel on the team 1393 oc5 := model.Channel{ 1394 TeamId: teamId, 1395 DisplayName: "OpenChannel4Team1", 1396 Name: "zz" + model.NewId() + "b", 1397 Type: model.CHANNEL_OPEN, 1398 } 1399 store.Must(ss.Channel().Save(&oc5, -1)) 1400 store.Must(ss.Channel().Delete(oc5.Id, model.GetMillis())) 1401 1402 t.Run("only oc1 and oc4, among others, should be found as a public channel in the team", func(t *testing.T) { 1403 result := <-ss.Channel().GetPublicChannelsByIdsForTeam(teamId, []string{oc1.Id, oc2.Id, model.NewId(), pc3.Id, oc4.Id}) 1404 require.Nil(t, result.Err) 1405 require.Equal(t, &model.ChannelList{&oc1, &oc4}, result.Data.(*model.ChannelList)) 1406 }) 1407 1408 t.Run("random channel id should not be found as a public channel in the team", func(t *testing.T) { 1409 result := <-ss.Channel().GetPublicChannelsByIdsForTeam(teamId, []string{model.NewId()}) 1410 require.NotNil(t, result.Err) 1411 require.Equal(t, result.Err.Id, "store.sql_channel.get_channels_by_ids.not_found.app_error") 1412 }) 1413 } 1414 1415 func testChannelStoreGetChannelCounts(t *testing.T, ss store.Store) { 1416 o2 := model.Channel{} 1417 o2.TeamId = model.NewId() 1418 o2.DisplayName = "Channel2" 1419 o2.Name = "zz" + model.NewId() + "b" 1420 o2.Type = model.CHANNEL_OPEN 1421 store.Must(ss.Channel().Save(&o2, -1)) 1422 1423 o1 := model.Channel{} 1424 o1.TeamId = model.NewId() 1425 o1.DisplayName = "Channel1" 1426 o1.Name = "zz" + model.NewId() + "b" 1427 o1.Type = model.CHANNEL_OPEN 1428 store.Must(ss.Channel().Save(&o1, -1)) 1429 1430 m1 := model.ChannelMember{} 1431 m1.ChannelId = o1.Id 1432 m1.UserId = model.NewId() 1433 m1.NotifyProps = model.GetDefaultChannelNotifyProps() 1434 store.Must(ss.Channel().SaveMember(&m1)) 1435 1436 m2 := model.ChannelMember{} 1437 m2.ChannelId = o1.Id 1438 m2.UserId = model.NewId() 1439 m2.NotifyProps = model.GetDefaultChannelNotifyProps() 1440 store.Must(ss.Channel().SaveMember(&m2)) 1441 1442 m3 := model.ChannelMember{} 1443 m3.ChannelId = o2.Id 1444 m3.UserId = model.NewId() 1445 m3.NotifyProps = model.GetDefaultChannelNotifyProps() 1446 store.Must(ss.Channel().SaveMember(&m3)) 1447 1448 cresult := <-ss.Channel().GetChannelCounts(o1.TeamId, m1.UserId) 1449 counts := cresult.Data.(*model.ChannelCounts) 1450 1451 if len(counts.Counts) != 1 { 1452 t.Fatal("wrong number of counts") 1453 } 1454 1455 if len(counts.UpdateTimes) != 1 { 1456 t.Fatal("wrong number of update times") 1457 } 1458 } 1459 1460 func testChannelStoreGetMembersForUser(t *testing.T, ss store.Store) { 1461 t1 := model.Team{} 1462 t1.DisplayName = "Name" 1463 t1.Name = model.NewId() 1464 t1.Email = MakeEmail() 1465 t1.Type = model.TEAM_OPEN 1466 store.Must(ss.Team().Save(&t1)) 1467 1468 o1 := model.Channel{} 1469 o1.TeamId = t1.Id 1470 o1.DisplayName = "Channel1" 1471 o1.Name = "zz" + model.NewId() + "b" 1472 o1.Type = model.CHANNEL_OPEN 1473 store.Must(ss.Channel().Save(&o1, -1)) 1474 1475 o2 := model.Channel{} 1476 o2.TeamId = o1.TeamId 1477 o2.DisplayName = "Channel2" 1478 o2.Name = "zz" + model.NewId() + "b" 1479 o2.Type = model.CHANNEL_OPEN 1480 store.Must(ss.Channel().Save(&o2, -1)) 1481 1482 m1 := model.ChannelMember{} 1483 m1.ChannelId = o1.Id 1484 m1.UserId = model.NewId() 1485 m1.NotifyProps = model.GetDefaultChannelNotifyProps() 1486 store.Must(ss.Channel().SaveMember(&m1)) 1487 1488 m2 := model.ChannelMember{} 1489 m2.ChannelId = o2.Id 1490 m2.UserId = m1.UserId 1491 m2.NotifyProps = model.GetDefaultChannelNotifyProps() 1492 store.Must(ss.Channel().SaveMember(&m2)) 1493 1494 cresult := <-ss.Channel().GetMembersForUser(o1.TeamId, m1.UserId) 1495 members := cresult.Data.(*model.ChannelMembers) 1496 1497 // no unread messages 1498 if len(*members) != 2 { 1499 t.Fatal("wrong number of members") 1500 } 1501 } 1502 1503 func testChannelStoreGetMembersForUserWithPagination(t *testing.T, ss store.Store) { 1504 t1 := model.Team{} 1505 t1.DisplayName = "Name" 1506 t1.Name = model.NewId() 1507 t1.Email = MakeEmail() 1508 t1.Type = model.TEAM_OPEN 1509 store.Must(ss.Team().Save(&t1)) 1510 1511 o1 := model.Channel{} 1512 o1.TeamId = t1.Id 1513 o1.DisplayName = "Channel1" 1514 o1.Name = "zz" + model.NewId() + "b" 1515 o1.Type = model.CHANNEL_OPEN 1516 store.Must(ss.Channel().Save(&o1, -1)) 1517 1518 o2 := model.Channel{} 1519 o2.TeamId = o1.TeamId 1520 o2.DisplayName = "Channel2" 1521 o2.Name = "zz" + model.NewId() + "b" 1522 o2.Type = model.CHANNEL_OPEN 1523 store.Must(ss.Channel().Save(&o2, -1)) 1524 1525 m1 := model.ChannelMember{} 1526 m1.ChannelId = o1.Id 1527 m1.UserId = model.NewId() 1528 m1.NotifyProps = model.GetDefaultChannelNotifyProps() 1529 store.Must(ss.Channel().SaveMember(&m1)) 1530 1531 m2 := model.ChannelMember{} 1532 m2.ChannelId = o2.Id 1533 m2.UserId = m1.UserId 1534 m2.NotifyProps = model.GetDefaultChannelNotifyProps() 1535 store.Must(ss.Channel().SaveMember(&m2)) 1536 1537 cresult := <-ss.Channel().GetMembersForUserWithPagination(o1.TeamId, m1.UserId, 0, 1) 1538 members := cresult.Data.(*model.ChannelMembers) 1539 1540 assert.Len(t, *members, 1) 1541 1542 cresult = <-ss.Channel().GetMembersForUserWithPagination(o1.TeamId, m1.UserId, 1, 1) 1543 members = cresult.Data.(*model.ChannelMembers) 1544 assert.Len(t, *members, 1) 1545 } 1546 1547 func testChannelStoreUpdateLastViewedAt(t *testing.T, ss store.Store) { 1548 o1 := model.Channel{} 1549 o1.TeamId = model.NewId() 1550 o1.DisplayName = "Channel1" 1551 o1.Name = "zz" + model.NewId() + "b" 1552 o1.Type = model.CHANNEL_OPEN 1553 o1.TotalMsgCount = 25 1554 o1.LastPostAt = 12345 1555 store.Must(ss.Channel().Save(&o1, -1)) 1556 1557 m1 := model.ChannelMember{} 1558 m1.ChannelId = o1.Id 1559 m1.UserId = model.NewId() 1560 m1.NotifyProps = model.GetDefaultChannelNotifyProps() 1561 store.Must(ss.Channel().SaveMember(&m1)) 1562 1563 o2 := model.Channel{} 1564 o2.TeamId = model.NewId() 1565 o2.DisplayName = "Channel1" 1566 o2.Name = "zz" + model.NewId() + "c" 1567 o2.Type = model.CHANNEL_OPEN 1568 o2.TotalMsgCount = 26 1569 o2.LastPostAt = 123456 1570 store.Must(ss.Channel().Save(&o2, -1)) 1571 1572 m2 := model.ChannelMember{} 1573 m2.ChannelId = o2.Id 1574 m2.UserId = m1.UserId 1575 m2.NotifyProps = model.GetDefaultChannelNotifyProps() 1576 store.Must(ss.Channel().SaveMember(&m2)) 1577 1578 if result := <-ss.Channel().UpdateLastViewedAt([]string{m1.ChannelId}, m1.UserId); result.Err != nil { 1579 t.Fatal("failed to update", result.Err) 1580 } else if result.Data.(map[string]int64)[o1.Id] != o1.LastPostAt { 1581 t.Fatal("last viewed at time incorrect") 1582 } 1583 1584 if result := <-ss.Channel().UpdateLastViewedAt([]string{m1.ChannelId, m2.ChannelId}, m1.UserId); result.Err != nil { 1585 t.Fatal("failed to update", result.Err) 1586 } else if result.Data.(map[string]int64)[o2.Id] != o2.LastPostAt { 1587 t.Fatal("last viewed at time incorrect") 1588 } 1589 1590 rm1 := store.Must(ss.Channel().GetMember(m1.ChannelId, m1.UserId)).(*model.ChannelMember) 1591 assert.Equal(t, rm1.LastViewedAt, o1.LastPostAt) 1592 assert.Equal(t, rm1.LastUpdateAt, o1.LastPostAt) 1593 assert.Equal(t, rm1.MsgCount, o1.TotalMsgCount) 1594 1595 rm2 := store.Must(ss.Channel().GetMember(m2.ChannelId, m2.UserId)).(*model.ChannelMember) 1596 assert.Equal(t, rm2.LastViewedAt, o2.LastPostAt) 1597 assert.Equal(t, rm2.LastUpdateAt, o2.LastPostAt) 1598 assert.Equal(t, rm2.MsgCount, o2.TotalMsgCount) 1599 1600 if result := <-ss.Channel().UpdateLastViewedAt([]string{m1.ChannelId}, "missing id"); result.Err != nil { 1601 t.Fatal("failed to update") 1602 } 1603 } 1604 1605 func testChannelStoreIncrementMentionCount(t *testing.T, ss store.Store) { 1606 o1 := model.Channel{} 1607 o1.TeamId = model.NewId() 1608 o1.DisplayName = "Channel1" 1609 o1.Name = "zz" + model.NewId() + "b" 1610 o1.Type = model.CHANNEL_OPEN 1611 o1.TotalMsgCount = 25 1612 store.Must(ss.Channel().Save(&o1, -1)) 1613 1614 m1 := model.ChannelMember{} 1615 m1.ChannelId = o1.Id 1616 m1.UserId = model.NewId() 1617 m1.NotifyProps = model.GetDefaultChannelNotifyProps() 1618 store.Must(ss.Channel().SaveMember(&m1)) 1619 1620 err := (<-ss.Channel().IncrementMentionCount(m1.ChannelId, m1.UserId)).Err 1621 if err != nil { 1622 t.Fatal("failed to update") 1623 } 1624 1625 err = (<-ss.Channel().IncrementMentionCount(m1.ChannelId, "missing id")).Err 1626 if err != nil { 1627 t.Fatal("failed to update") 1628 } 1629 1630 err = (<-ss.Channel().IncrementMentionCount("missing id", m1.UserId)).Err 1631 if err != nil { 1632 t.Fatal("failed to update") 1633 } 1634 1635 err = (<-ss.Channel().IncrementMentionCount("missing id", "missing id")).Err 1636 if err != nil { 1637 t.Fatal("failed to update") 1638 } 1639 } 1640 1641 func testUpdateChannelMember(t *testing.T, ss store.Store) { 1642 userId := model.NewId() 1643 1644 c1 := &model.Channel{ 1645 TeamId: model.NewId(), 1646 DisplayName: model.NewId(), 1647 Name: model.NewId(), 1648 Type: model.CHANNEL_OPEN, 1649 } 1650 store.Must(ss.Channel().Save(c1, -1)) 1651 1652 m1 := &model.ChannelMember{ 1653 ChannelId: c1.Id, 1654 UserId: userId, 1655 NotifyProps: model.GetDefaultChannelNotifyProps(), 1656 } 1657 store.Must(ss.Channel().SaveMember(m1)) 1658 1659 m1.NotifyProps["test"] = "sometext" 1660 if result := <-ss.Channel().UpdateMember(m1); result.Err != nil { 1661 t.Fatal(result.Err) 1662 } 1663 1664 m1.UserId = "" 1665 if result := <-ss.Channel().UpdateMember(m1); result.Err == nil { 1666 t.Fatal("bad user id - should fail") 1667 } 1668 } 1669 1670 func testGetMember(t *testing.T, ss store.Store) { 1671 userId := model.NewId() 1672 1673 c1 := &model.Channel{ 1674 TeamId: model.NewId(), 1675 DisplayName: model.NewId(), 1676 Name: model.NewId(), 1677 Type: model.CHANNEL_OPEN, 1678 } 1679 store.Must(ss.Channel().Save(c1, -1)) 1680 1681 c2 := &model.Channel{ 1682 TeamId: c1.TeamId, 1683 DisplayName: model.NewId(), 1684 Name: model.NewId(), 1685 Type: model.CHANNEL_OPEN, 1686 } 1687 store.Must(ss.Channel().Save(c2, -1)) 1688 1689 m1 := &model.ChannelMember{ 1690 ChannelId: c1.Id, 1691 UserId: userId, 1692 NotifyProps: model.GetDefaultChannelNotifyProps(), 1693 } 1694 store.Must(ss.Channel().SaveMember(m1)) 1695 1696 m2 := &model.ChannelMember{ 1697 ChannelId: c2.Id, 1698 UserId: userId, 1699 NotifyProps: model.GetDefaultChannelNotifyProps(), 1700 } 1701 store.Must(ss.Channel().SaveMember(m2)) 1702 1703 if result := <-ss.Channel().GetMember(model.NewId(), userId); result.Err == nil { 1704 t.Fatal("should've failed to get member for non-existent channel") 1705 } 1706 1707 if result := <-ss.Channel().GetMember(c1.Id, model.NewId()); result.Err == nil { 1708 t.Fatal("should've failed to get member for non-existent user") 1709 } 1710 1711 if result := <-ss.Channel().GetMember(c1.Id, userId); result.Err != nil { 1712 t.Fatal("shouldn't have errored when getting member", result.Err) 1713 } else if member := result.Data.(*model.ChannelMember); member.ChannelId != c1.Id { 1714 t.Fatal("should've gotten member of channel 1") 1715 } else if member.UserId != userId { 1716 t.Fatal("should've gotten member for user") 1717 } 1718 1719 if result := <-ss.Channel().GetMember(c2.Id, userId); result.Err != nil { 1720 t.Fatal("shouldn't have errored when getting member", result.Err) 1721 } else if member := result.Data.(*model.ChannelMember); member.ChannelId != c2.Id { 1722 t.Fatal("should've gotten member of channel 2") 1723 } else if member.UserId != userId { 1724 t.Fatal("should've gotten member for user") 1725 } 1726 1727 if result := <-ss.Channel().GetAllChannelMembersNotifyPropsForChannel(c2.Id, false); result.Err != nil { 1728 t.Fatal(result.Err) 1729 } else { 1730 props := result.Data.(map[string]model.StringMap) 1731 if len(props) == 0 { 1732 t.Fatal("should not be empty") 1733 } 1734 } 1735 1736 if result := <-ss.Channel().GetAllChannelMembersNotifyPropsForChannel(c2.Id, true); result.Err != nil { 1737 t.Fatal(result.Err) 1738 } else { 1739 props := result.Data.(map[string]model.StringMap) 1740 if len(props) == 0 { 1741 t.Fatal("should not be empty") 1742 } 1743 } 1744 1745 ss.Channel().InvalidateCacheForChannelMembersNotifyProps(c2.Id) 1746 } 1747 1748 func testChannelStoreGetMemberForPost(t *testing.T, ss store.Store) { 1749 o1 := store.Must(ss.Channel().Save(&model.Channel{ 1750 TeamId: model.NewId(), 1751 DisplayName: "Name", 1752 Name: "zz" + model.NewId() + "b", 1753 Type: model.CHANNEL_OPEN, 1754 }, -1)).(*model.Channel) 1755 1756 m1 := store.Must(ss.Channel().SaveMember(&model.ChannelMember{ 1757 ChannelId: o1.Id, 1758 UserId: model.NewId(), 1759 NotifyProps: model.GetDefaultChannelNotifyProps(), 1760 })).(*model.ChannelMember) 1761 1762 p1 := store.Must(ss.Post().Save(&model.Post{ 1763 UserId: model.NewId(), 1764 ChannelId: o1.Id, 1765 Message: "test", 1766 })).(*model.Post) 1767 1768 if r1 := <-ss.Channel().GetMemberForPost(p1.Id, m1.UserId); r1.Err != nil { 1769 t.Fatal(r1.Err) 1770 } else if r1.Data.(*model.ChannelMember).ToJson() != m1.ToJson() { 1771 t.Fatal("invalid returned channel member") 1772 } 1773 1774 if r2 := <-ss.Channel().GetMemberForPost(p1.Id, model.NewId()); r2.Err == nil { 1775 t.Fatal("shouldn't have returned a member") 1776 } 1777 } 1778 1779 func testGetMemberCount(t *testing.T, ss store.Store) { 1780 teamId := model.NewId() 1781 1782 c1 := model.Channel{ 1783 TeamId: teamId, 1784 DisplayName: "Channel1", 1785 Name: "zz" + model.NewId() + "b", 1786 Type: model.CHANNEL_OPEN, 1787 } 1788 store.Must(ss.Channel().Save(&c1, -1)) 1789 1790 c2 := model.Channel{ 1791 TeamId: teamId, 1792 DisplayName: "Channel2", 1793 Name: "zz" + model.NewId() + "b", 1794 Type: model.CHANNEL_OPEN, 1795 } 1796 store.Must(ss.Channel().Save(&c2, -1)) 1797 1798 u1 := &model.User{ 1799 Email: MakeEmail(), 1800 DeleteAt: 0, 1801 } 1802 store.Must(ss.User().Save(u1)) 1803 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u1.Id}, -1)) 1804 1805 m1 := model.ChannelMember{ 1806 ChannelId: c1.Id, 1807 UserId: u1.Id, 1808 NotifyProps: model.GetDefaultChannelNotifyProps(), 1809 } 1810 store.Must(ss.Channel().SaveMember(&m1)) 1811 1812 if result := <-ss.Channel().GetMemberCount(c1.Id, false); result.Err != nil { 1813 t.Fatalf("failed to get member count: %v", result.Err) 1814 } else if result.Data.(int64) != 1 { 1815 t.Fatalf("got incorrect member count %v", result.Data) 1816 } 1817 1818 u2 := model.User{ 1819 Email: MakeEmail(), 1820 DeleteAt: 0, 1821 } 1822 store.Must(ss.User().Save(&u2)) 1823 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u2.Id}, -1)) 1824 1825 m2 := model.ChannelMember{ 1826 ChannelId: c1.Id, 1827 UserId: u2.Id, 1828 NotifyProps: model.GetDefaultChannelNotifyProps(), 1829 } 1830 store.Must(ss.Channel().SaveMember(&m2)) 1831 1832 if result := <-ss.Channel().GetMemberCount(c1.Id, false); result.Err != nil { 1833 t.Fatalf("failed to get member count: %v", result.Err) 1834 } else if result.Data.(int64) != 2 { 1835 t.Fatalf("got incorrect member count %v", result.Data) 1836 } 1837 1838 // make sure members of other channels aren't counted 1839 u3 := model.User{ 1840 Email: MakeEmail(), 1841 DeleteAt: 0, 1842 } 1843 store.Must(ss.User().Save(&u3)) 1844 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u3.Id}, -1)) 1845 1846 m3 := model.ChannelMember{ 1847 ChannelId: c2.Id, 1848 UserId: u3.Id, 1849 NotifyProps: model.GetDefaultChannelNotifyProps(), 1850 } 1851 store.Must(ss.Channel().SaveMember(&m3)) 1852 1853 if result := <-ss.Channel().GetMemberCount(c1.Id, false); result.Err != nil { 1854 t.Fatalf("failed to get member count: %v", result.Err) 1855 } else if result.Data.(int64) != 2 { 1856 t.Fatalf("got incorrect member count %v", result.Data) 1857 } 1858 1859 // make sure inactive users aren't counted 1860 u4 := &model.User{ 1861 Email: MakeEmail(), 1862 DeleteAt: 10000, 1863 } 1864 store.Must(ss.User().Save(u4)) 1865 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u4.Id}, -1)) 1866 1867 m4 := model.ChannelMember{ 1868 ChannelId: c1.Id, 1869 UserId: u4.Id, 1870 NotifyProps: model.GetDefaultChannelNotifyProps(), 1871 } 1872 store.Must(ss.Channel().SaveMember(&m4)) 1873 1874 if result := <-ss.Channel().GetMemberCount(c1.Id, false); result.Err != nil { 1875 t.Fatalf("failed to get member count: %v", result.Err) 1876 } else if result.Data.(int64) != 2 { 1877 t.Fatalf("got incorrect member count %v", result.Data) 1878 } 1879 } 1880 1881 func testChannelStoreSearchMore(t *testing.T, ss store.Store) { 1882 teamId := model.NewId() 1883 otherTeamId := model.NewId() 1884 1885 o1 := model.Channel{ 1886 TeamId: teamId, 1887 DisplayName: "ChannelA", 1888 Name: "zz" + model.NewId() + "b", 1889 Type: model.CHANNEL_OPEN, 1890 } 1891 store.Must(ss.Channel().Save(&o1, -1)) 1892 1893 m1 := model.ChannelMember{ 1894 ChannelId: o1.Id, 1895 UserId: model.NewId(), 1896 NotifyProps: model.GetDefaultChannelNotifyProps(), 1897 } 1898 store.Must(ss.Channel().SaveMember(&m1)) 1899 1900 m2 := model.ChannelMember{ 1901 ChannelId: o1.Id, 1902 UserId: model.NewId(), 1903 NotifyProps: model.GetDefaultChannelNotifyProps(), 1904 } 1905 store.Must(ss.Channel().SaveMember(&m2)) 1906 1907 o2 := model.Channel{ 1908 TeamId: otherTeamId, 1909 DisplayName: "Channel2", 1910 Name: "zz" + model.NewId() + "b", 1911 Type: model.CHANNEL_OPEN, 1912 } 1913 store.Must(ss.Channel().Save(&o2, -1)) 1914 1915 m3 := model.ChannelMember{ 1916 ChannelId: o2.Id, 1917 UserId: model.NewId(), 1918 NotifyProps: model.GetDefaultChannelNotifyProps(), 1919 } 1920 store.Must(ss.Channel().SaveMember(&m3)) 1921 1922 o3 := model.Channel{ 1923 TeamId: teamId, 1924 DisplayName: "ChannelA", 1925 Name: "zz" + model.NewId() + "b", 1926 Type: model.CHANNEL_OPEN, 1927 } 1928 store.Must(ss.Channel().Save(&o3, -1)) 1929 1930 o4 := model.Channel{ 1931 TeamId: teamId, 1932 DisplayName: "ChannelB", 1933 Name: "zz" + model.NewId() + "b", 1934 Type: model.CHANNEL_PRIVATE, 1935 } 1936 store.Must(ss.Channel().Save(&o4, -1)) 1937 1938 o5 := model.Channel{ 1939 TeamId: teamId, 1940 DisplayName: "ChannelC", 1941 Name: "zz" + model.NewId() + "b", 1942 Type: model.CHANNEL_PRIVATE, 1943 } 1944 store.Must(ss.Channel().Save(&o5, -1)) 1945 1946 o6 := model.Channel{ 1947 TeamId: teamId, 1948 DisplayName: "Off-Topic", 1949 Name: "off-topic", 1950 Type: model.CHANNEL_OPEN, 1951 } 1952 store.Must(ss.Channel().Save(&o6, -1)) 1953 1954 o7 := model.Channel{ 1955 TeamId: teamId, 1956 DisplayName: "Off-Set", 1957 Name: "off-set", 1958 Type: model.CHANNEL_OPEN, 1959 } 1960 store.Must(ss.Channel().Save(&o7, -1)) 1961 1962 o8 := model.Channel{ 1963 TeamId: teamId, 1964 DisplayName: "Off-Limit", 1965 Name: "off-limit", 1966 Type: model.CHANNEL_PRIVATE, 1967 } 1968 store.Must(ss.Channel().Save(&o8, -1)) 1969 1970 o9 := model.Channel{ 1971 TeamId: teamId, 1972 DisplayName: "Channel With Purpose", 1973 Purpose: "This can now be searchable!", 1974 Name: "with-purpose", 1975 Type: model.CHANNEL_OPEN, 1976 } 1977 store.Must(ss.Channel().Save(&o9, -1)) 1978 1979 o10 := model.Channel{ 1980 TeamId: teamId, 1981 DisplayName: "ChannelA", 1982 Name: "channel-a-deleted", 1983 Type: model.CHANNEL_OPEN, 1984 } 1985 store.Must(ss.Channel().Save(&o10, -1)) 1986 o10.DeleteAt = model.GetMillis() 1987 o10.UpdateAt = o10.DeleteAt 1988 store.Must(ss.Channel().Delete(o10.Id, o10.DeleteAt)) 1989 1990 t.Run("three public channels matching 'ChannelA', but already a member of one and one deleted", func(t *testing.T) { 1991 result := <-ss.Channel().SearchMore(m1.UserId, teamId, "ChannelA") 1992 require.Nil(t, result.Err) 1993 require.Equal(t, &model.ChannelList{&o3}, result.Data.(*model.ChannelList)) 1994 }) 1995 1996 t.Run("one public channels, but already a member", func(t *testing.T) { 1997 result := <-ss.Channel().SearchMore(m1.UserId, teamId, o4.Name) 1998 require.Nil(t, result.Err) 1999 require.Equal(t, &model.ChannelList{}, result.Data.(*model.ChannelList)) 2000 }) 2001 2002 t.Run("three matching channels, but only two public", func(t *testing.T) { 2003 result := <-ss.Channel().SearchMore(m1.UserId, teamId, "off-") 2004 require.Nil(t, result.Err) 2005 require.Equal(t, &model.ChannelList{&o7, &o6}, result.Data.(*model.ChannelList)) 2006 }) 2007 2008 t.Run("one channel matching 'off-topic'", func(t *testing.T) { 2009 result := <-ss.Channel().SearchMore(m1.UserId, teamId, "off-topic") 2010 require.Nil(t, result.Err) 2011 require.Equal(t, &model.ChannelList{&o6}, result.Data.(*model.ChannelList)) 2012 }) 2013 2014 t.Run("search purpose", func(t *testing.T) { 2015 result := <-ss.Channel().SearchMore(m1.UserId, teamId, "now searchable") 2016 require.Nil(t, result.Err) 2017 require.Equal(t, &model.ChannelList{&o9}, result.Data.(*model.ChannelList)) 2018 }) 2019 } 2020 2021 type ByChannelDisplayName model.ChannelList 2022 2023 func (s ByChannelDisplayName) Len() int { return len(s) } 2024 func (s ByChannelDisplayName) Swap(i, j int) { 2025 s[i], s[j] = s[j], s[i] 2026 } 2027 func (s ByChannelDisplayName) Less(i, j int) bool { 2028 if s[i].DisplayName != s[j].DisplayName { 2029 return s[i].DisplayName < s[j].DisplayName 2030 } 2031 2032 return s[i].Id < s[j].Id 2033 } 2034 2035 func testChannelStoreSearchInTeam(t *testing.T, ss store.Store) { 2036 teamId := model.NewId() 2037 otherTeamId := model.NewId() 2038 2039 o1 := model.Channel{ 2040 TeamId: teamId, 2041 DisplayName: "ChannelA", 2042 Name: "zz" + model.NewId() + "b", 2043 Type: model.CHANNEL_OPEN, 2044 } 2045 store.Must(ss.Channel().Save(&o1, -1)) 2046 2047 o2 := model.Channel{ 2048 TeamId: otherTeamId, 2049 DisplayName: "ChannelA", 2050 Name: "zz" + model.NewId() + "b", 2051 Type: model.CHANNEL_OPEN, 2052 } 2053 store.Must(ss.Channel().Save(&o2, -1)) 2054 2055 m1 := model.ChannelMember{ 2056 ChannelId: o1.Id, 2057 UserId: model.NewId(), 2058 NotifyProps: model.GetDefaultChannelNotifyProps(), 2059 } 2060 store.Must(ss.Channel().SaveMember(&m1)) 2061 2062 m2 := model.ChannelMember{ 2063 ChannelId: o1.Id, 2064 UserId: model.NewId(), 2065 NotifyProps: model.GetDefaultChannelNotifyProps(), 2066 } 2067 store.Must(ss.Channel().SaveMember(&m2)) 2068 2069 m3 := model.ChannelMember{ 2070 ChannelId: o2.Id, 2071 UserId: model.NewId(), 2072 NotifyProps: model.GetDefaultChannelNotifyProps(), 2073 } 2074 store.Must(ss.Channel().SaveMember(&m3)) 2075 2076 o3 := model.Channel{ 2077 TeamId: teamId, 2078 DisplayName: "ChannelA (alternate)", 2079 Name: "zz" + model.NewId() + "b", 2080 Type: model.CHANNEL_OPEN, 2081 } 2082 store.Must(ss.Channel().Save(&o3, -1)) 2083 2084 o4 := model.Channel{ 2085 TeamId: teamId, 2086 DisplayName: "Channel B", 2087 Name: "zz" + model.NewId() + "b", 2088 Type: model.CHANNEL_PRIVATE, 2089 } 2090 store.Must(ss.Channel().Save(&o4, -1)) 2091 2092 o5 := model.Channel{ 2093 TeamId: teamId, 2094 DisplayName: "Channel C", 2095 Name: "zz" + model.NewId() + "b", 2096 Type: model.CHANNEL_PRIVATE, 2097 } 2098 store.Must(ss.Channel().Save(&o5, -1)) 2099 2100 o6 := model.Channel{ 2101 TeamId: teamId, 2102 DisplayName: "Off-Topic", 2103 Name: "off-topic", 2104 Type: model.CHANNEL_OPEN, 2105 } 2106 store.Must(ss.Channel().Save(&o6, -1)) 2107 2108 o7 := model.Channel{ 2109 TeamId: teamId, 2110 DisplayName: "Off-Set", 2111 Name: "off-set", 2112 Type: model.CHANNEL_OPEN, 2113 } 2114 store.Must(ss.Channel().Save(&o7, -1)) 2115 2116 o8 := model.Channel{ 2117 TeamId: teamId, 2118 DisplayName: "Off-Limit", 2119 Name: "off-limit", 2120 Type: model.CHANNEL_PRIVATE, 2121 } 2122 store.Must(ss.Channel().Save(&o8, -1)) 2123 2124 o9 := model.Channel{ 2125 TeamId: teamId, 2126 DisplayName: "Town Square", 2127 Name: "town-square", 2128 Type: model.CHANNEL_OPEN, 2129 } 2130 store.Must(ss.Channel().Save(&o9, -1)) 2131 2132 o10 := model.Channel{ 2133 TeamId: teamId, 2134 DisplayName: "The", 2135 Name: "the", 2136 Type: model.CHANNEL_OPEN, 2137 } 2138 store.Must(ss.Channel().Save(&o10, -1)) 2139 2140 o11 := model.Channel{ 2141 TeamId: teamId, 2142 DisplayName: "Native Mobile Apps", 2143 Name: "native-mobile-apps", 2144 Type: model.CHANNEL_OPEN, 2145 } 2146 store.Must(ss.Channel().Save(&o11, -1)) 2147 2148 o12 := model.Channel{ 2149 TeamId: teamId, 2150 DisplayName: "ChannelZ", 2151 Purpose: "This can now be searchable!", 2152 Name: "with-purpose", 2153 Type: model.CHANNEL_OPEN, 2154 } 2155 store.Must(ss.Channel().Save(&o12, -1)) 2156 2157 o13 := model.Channel{ 2158 TeamId: teamId, 2159 DisplayName: "ChannelA (deleted)", 2160 Name: model.NewId(), 2161 Type: model.CHANNEL_OPEN, 2162 } 2163 store.Must(ss.Channel().Save(&o13, -1)) 2164 o13.DeleteAt = model.GetMillis() 2165 o13.UpdateAt = o13.DeleteAt 2166 store.Must(ss.Channel().Delete(o13.Id, o13.DeleteAt)) 2167 2168 testCases := []struct { 2169 Description string 2170 TeamId string 2171 Term string 2172 IncludeDeleted bool 2173 ExpectedResults *model.ChannelList 2174 }{ 2175 {"ChannelA", teamId, "ChannelA", false, &model.ChannelList{&o1, &o3}}, 2176 {"ChannelA, include deleted", teamId, "ChannelA", true, &model.ChannelList{&o1, &o3, &o13}}, 2177 {"ChannelA, other team", otherTeamId, "ChannelA", false, &model.ChannelList{&o2}}, 2178 {"empty string", teamId, "", false, &model.ChannelList{&o1, &o3, &o12, &o11, &o7, &o6, &o10, &o9}}, 2179 {"no matches", teamId, "blargh", false, &model.ChannelList{}}, 2180 {"prefix", teamId, "off-", false, &model.ChannelList{&o7, &o6}}, 2181 {"full match with dash", teamId, "off-topic", false, &model.ChannelList{&o6}}, 2182 {"town square", teamId, "town square", false, &model.ChannelList{&o9}}, 2183 {"the in name", teamId, "the", false, &model.ChannelList{&o10}}, 2184 {"Mobile", teamId, "Mobile", false, &model.ChannelList{&o11}}, 2185 {"search purpose", teamId, "now searchable", false, &model.ChannelList{&o12}}, 2186 {"pipe ignored", teamId, "town square |", false, &model.ChannelList{&o9}}, 2187 } 2188 2189 for name, search := range map[string]func(teamId string, term string, includeDeleted bool) store.StoreChannel{ 2190 "AutocompleteInTeam": ss.Channel().AutocompleteInTeam, 2191 "SearchInTeam": ss.Channel().SearchInTeam, 2192 } { 2193 for _, testCase := range testCases { 2194 t.Run(testCase.Description, func(t *testing.T) { 2195 result := <-search(testCase.TeamId, testCase.Term, testCase.IncludeDeleted) 2196 require.Nil(t, result.Err) 2197 2198 channels := result.Data.(*model.ChannelList) 2199 2200 // AutoCompleteInTeam doesn't currently sort its output results. 2201 if name == "AutocompleteInTeam" { 2202 sort.Sort(ByChannelDisplayName(*channels)) 2203 } 2204 2205 require.Equal(t, testCase.ExpectedResults, channels) 2206 }) 2207 } 2208 } 2209 } 2210 2211 func testChannelStoreSearchAllChannels(t *testing.T, ss store.Store) { 2212 cleanupChannels(t, ss) 2213 2214 t1 := model.Team{} 2215 t1.DisplayName = "Name" 2216 t1.Name = model.NewId() 2217 t1.Email = MakeEmail() 2218 t1.Type = model.TEAM_OPEN 2219 store.Must(ss.Team().Save(&t1)) 2220 2221 t2 := model.Team{} 2222 t2.DisplayName = "Name2" 2223 t2.Name = model.NewId() 2224 t2.Email = MakeEmail() 2225 t2.Type = model.TEAM_OPEN 2226 store.Must(ss.Team().Save(&t2)) 2227 2228 o1 := model.Channel{ 2229 TeamId: t1.Id, 2230 DisplayName: "ChannelA", 2231 Name: "zz" + model.NewId() + "b", 2232 Type: model.CHANNEL_OPEN, 2233 } 2234 store.Must(ss.Channel().Save(&o1, -1)) 2235 2236 o2 := model.Channel{ 2237 TeamId: t2.Id, 2238 DisplayName: "ChannelA", 2239 Name: "zz" + model.NewId() + "b", 2240 Type: model.CHANNEL_OPEN, 2241 } 2242 store.Must(ss.Channel().Save(&o2, -1)) 2243 2244 m1 := model.ChannelMember{ 2245 ChannelId: o1.Id, 2246 UserId: model.NewId(), 2247 NotifyProps: model.GetDefaultChannelNotifyProps(), 2248 } 2249 store.Must(ss.Channel().SaveMember(&m1)) 2250 2251 m2 := model.ChannelMember{ 2252 ChannelId: o1.Id, 2253 UserId: model.NewId(), 2254 NotifyProps: model.GetDefaultChannelNotifyProps(), 2255 } 2256 store.Must(ss.Channel().SaveMember(&m2)) 2257 2258 m3 := model.ChannelMember{ 2259 ChannelId: o2.Id, 2260 UserId: model.NewId(), 2261 NotifyProps: model.GetDefaultChannelNotifyProps(), 2262 } 2263 store.Must(ss.Channel().SaveMember(&m3)) 2264 2265 o3 := model.Channel{ 2266 TeamId: t1.Id, 2267 DisplayName: "ChannelA (alternate)", 2268 Name: "zz" + model.NewId() + "b", 2269 Type: model.CHANNEL_OPEN, 2270 } 2271 store.Must(ss.Channel().Save(&o3, -1)) 2272 2273 o4 := model.Channel{ 2274 TeamId: t1.Id, 2275 DisplayName: "ChannelB", 2276 Name: "zz" + model.NewId() + "b", 2277 Type: model.CHANNEL_PRIVATE, 2278 } 2279 store.Must(ss.Channel().Save(&o4, -1)) 2280 2281 o5 := model.Channel{ 2282 TeamId: t1.Id, 2283 DisplayName: "ChannelC", 2284 Name: "zz" + model.NewId() + "b", 2285 Type: model.CHANNEL_PRIVATE, 2286 } 2287 store.Must(ss.Channel().Save(&o5, -1)) 2288 2289 o6 := model.Channel{ 2290 TeamId: t1.Id, 2291 DisplayName: "Off-Topic", 2292 Name: "off-topic", 2293 Type: model.CHANNEL_OPEN, 2294 } 2295 store.Must(ss.Channel().Save(&o6, -1)) 2296 2297 o7 := model.Channel{ 2298 TeamId: t1.Id, 2299 DisplayName: "Off-Set", 2300 Name: "off-set", 2301 Type: model.CHANNEL_OPEN, 2302 } 2303 store.Must(ss.Channel().Save(&o7, -1)) 2304 2305 o8 := model.Channel{ 2306 TeamId: t1.Id, 2307 DisplayName: "Off-Limit", 2308 Name: "off-limit", 2309 Type: model.CHANNEL_PRIVATE, 2310 } 2311 store.Must(ss.Channel().Save(&o8, -1)) 2312 2313 o9 := model.Channel{ 2314 TeamId: t1.Id, 2315 DisplayName: "Town Square", 2316 Name: "town-square", 2317 Type: model.CHANNEL_OPEN, 2318 } 2319 store.Must(ss.Channel().Save(&o9, -1)) 2320 2321 o10 := model.Channel{ 2322 TeamId: t1.Id, 2323 DisplayName: "The", 2324 Name: "the", 2325 Type: model.CHANNEL_OPEN, 2326 } 2327 store.Must(ss.Channel().Save(&o10, -1)) 2328 2329 o11 := model.Channel{ 2330 TeamId: t1.Id, 2331 DisplayName: "Native Mobile Apps", 2332 Name: "native-mobile-apps", 2333 Type: model.CHANNEL_OPEN, 2334 } 2335 store.Must(ss.Channel().Save(&o11, -1)) 2336 2337 o12 := model.Channel{ 2338 TeamId: t1.Id, 2339 DisplayName: "ChannelZ", 2340 Purpose: "This can now be searchable!", 2341 Name: "with-purpose", 2342 Type: model.CHANNEL_OPEN, 2343 } 2344 store.Must(ss.Channel().Save(&o12, -1)) 2345 2346 o13 := model.Channel{ 2347 TeamId: t1.Id, 2348 DisplayName: "ChannelA (deleted)", 2349 Name: model.NewId(), 2350 Type: model.CHANNEL_OPEN, 2351 } 2352 store.Must(ss.Channel().Save(&o13, -1)) 2353 o13.DeleteAt = model.GetMillis() 2354 o13.UpdateAt = o13.DeleteAt 2355 store.Must(ss.Channel().Delete(o13.Id, o13.DeleteAt)) 2356 2357 testCases := []struct { 2358 Description string 2359 Term string 2360 IncludeDeleted bool 2361 ExpectedResults *model.ChannelList 2362 }{ 2363 {"ChannelA", "ChannelA", false, &model.ChannelList{&o1, &o2, &o3}}, 2364 {"ChannelA, include deleted", "ChannelA", true, &model.ChannelList{&o1, &o2, &o3, &o13}}, 2365 {"empty string", "", false, &model.ChannelList{&o1, &o2, &o3, &o4, &o5, &o12, &o11, &o8, &o7, &o6, &o10, &o9}}, 2366 {"no matches", "blargh", false, &model.ChannelList{}}, 2367 {"prefix", "off-", false, &model.ChannelList{&o8, &o7, &o6}}, 2368 {"full match with dash", "off-topic", false, &model.ChannelList{&o6}}, 2369 {"town square", "town square", false, &model.ChannelList{&o9}}, 2370 {"the in name", "the", false, &model.ChannelList{&o10}}, 2371 {"Mobile", "Mobile", false, &model.ChannelList{&o11}}, 2372 {"search purpose", "now searchable", false, &model.ChannelList{&o12}}, 2373 {"pipe ignored", "town square |", false, &model.ChannelList{&o9}}, 2374 } 2375 2376 for _, testCase := range testCases { 2377 t.Run(testCase.Description, func(t *testing.T) { 2378 result := <-ss.Channel().SearchAllChannels(testCase.Term, testCase.IncludeDeleted) 2379 require.Nil(t, result.Err) 2380 channels := result.Data.(*model.ChannelListWithTeamData) 2381 require.Equal(t, len(*channels), len(*testCase.ExpectedResults)) 2382 for i, expected := range *testCase.ExpectedResults { 2383 require.Equal(t, (*channels)[i].Id, expected.Id) 2384 } 2385 }) 2386 } 2387 } 2388 2389 func testChannelStoreAutocompleteInTeamForSearch(t *testing.T, ss store.Store, s SqlSupplier) { 2390 u1 := &model.User{} 2391 u1.Email = MakeEmail() 2392 u1.Username = "user1" + model.NewId() 2393 u1.Nickname = model.NewId() 2394 store.Must(ss.User().Save(u1)) 2395 2396 u2 := &model.User{} 2397 u2.Email = MakeEmail() 2398 u2.Username = "user2" + model.NewId() 2399 u2.Nickname = model.NewId() 2400 store.Must(ss.User().Save(u2)) 2401 2402 u3 := &model.User{} 2403 u3.Email = MakeEmail() 2404 u3.Username = "user3" + model.NewId() 2405 u3.Nickname = model.NewId() 2406 store.Must(ss.User().Save(u3)) 2407 2408 u4 := &model.User{} 2409 u4.Email = MakeEmail() 2410 u4.Username = "user4" + model.NewId() 2411 u4.Nickname = model.NewId() 2412 store.Must(ss.User().Save(u4)) 2413 2414 o1 := model.Channel{} 2415 o1.TeamId = model.NewId() 2416 o1.DisplayName = "ChannelA" 2417 o1.Name = "zz" + model.NewId() + "b" 2418 o1.Type = model.CHANNEL_OPEN 2419 store.Must(ss.Channel().Save(&o1, -1)) 2420 2421 m1 := model.ChannelMember{} 2422 m1.ChannelId = o1.Id 2423 m1.UserId = u1.Id 2424 m1.NotifyProps = model.GetDefaultChannelNotifyProps() 2425 store.Must(ss.Channel().SaveMember(&m1)) 2426 2427 o2 := model.Channel{} 2428 o2.TeamId = model.NewId() 2429 o2.DisplayName = "Channel2" 2430 o2.Name = "zz" + model.NewId() + "b" 2431 o2.Type = model.CHANNEL_OPEN 2432 store.Must(ss.Channel().Save(&o2, -1)) 2433 2434 m2 := model.ChannelMember{} 2435 m2.ChannelId = o2.Id 2436 m2.UserId = m1.UserId 2437 m2.NotifyProps = model.GetDefaultChannelNotifyProps() 2438 store.Must(ss.Channel().SaveMember(&m2)) 2439 2440 o3 := model.Channel{} 2441 o3.TeamId = o1.TeamId 2442 o3.DisplayName = "ChannelA" 2443 o3.Name = "zz" + model.NewId() + "b" 2444 o3.Type = model.CHANNEL_OPEN 2445 store.Must(ss.Channel().Save(&o3, -1)) 2446 2447 m3 := model.ChannelMember{} 2448 m3.ChannelId = o3.Id 2449 m3.UserId = m1.UserId 2450 m3.NotifyProps = model.GetDefaultChannelNotifyProps() 2451 store.Must(ss.Channel().SaveMember(&m3)) 2452 2453 store.Must(ss.Channel().SetDeleteAt(o3.Id, 100, 100)) 2454 2455 o4 := model.Channel{} 2456 o4.TeamId = o1.TeamId 2457 o4.DisplayName = "ChannelA" 2458 o4.Name = "zz" + model.NewId() + "b" 2459 o4.Type = model.CHANNEL_PRIVATE 2460 store.Must(ss.Channel().Save(&o4, -1)) 2461 2462 m4 := model.ChannelMember{} 2463 m4.ChannelId = o4.Id 2464 m4.UserId = m1.UserId 2465 m4.NotifyProps = model.GetDefaultChannelNotifyProps() 2466 store.Must(ss.Channel().SaveMember(&m4)) 2467 2468 o5 := model.Channel{} 2469 o5.TeamId = o1.TeamId 2470 o5.DisplayName = "ChannelC" 2471 o5.Name = "zz" + model.NewId() + "b" 2472 o5.Type = model.CHANNEL_PRIVATE 2473 store.Must(ss.Channel().Save(&o5, -1)) 2474 2475 store.Must(ss.Channel().CreateDirectChannel(u1.Id, u2.Id)) 2476 store.Must(ss.Channel().CreateDirectChannel(u2.Id, u3.Id)) 2477 2478 tt := []struct { 2479 name string 2480 term string 2481 includeDeleted bool 2482 expectedMatches int 2483 }{ 2484 {"Empty search (list all)", "", false, 4}, 2485 {"Narrow search", "ChannelA", false, 2}, 2486 {"Wide search", "Cha", false, 3}, 2487 {"Direct messages", "user", false, 1}, 2488 {"Wide search with archived channels", "Cha", true, 4}, 2489 {"Narrow with archived channels", "ChannelA", true, 3}, 2490 {"Direct messages with archived channels", "user", true, 1}, 2491 {"Search without results", "blarg", true, 0}, 2492 } 2493 2494 for _, tc := range tt { 2495 t.Run(tc.name, func(t *testing.T) { 2496 result := <-ss.Channel().AutocompleteInTeamForSearch(o1.TeamId, m1.UserId, "ChannelA", false) 2497 require.Nil(t, result.Err) 2498 channels := result.Data.(*model.ChannelList) 2499 require.Len(t, *channels, 2) 2500 }) 2501 } 2502 2503 // Manually truncate Channels table until testlib can handle cleanups 2504 s.GetMaster().Exec("TRUNCATE Channels") 2505 } 2506 2507 func testChannelStoreGetMembersByIds(t *testing.T, ss store.Store) { 2508 o1 := model.Channel{} 2509 o1.TeamId = model.NewId() 2510 o1.DisplayName = "ChannelA" 2511 o1.Name = "zz" + model.NewId() + "b" 2512 o1.Type = model.CHANNEL_OPEN 2513 store.Must(ss.Channel().Save(&o1, -1)) 2514 2515 m1 := &model.ChannelMember{ChannelId: o1.Id, UserId: model.NewId(), NotifyProps: model.GetDefaultChannelNotifyProps()} 2516 store.Must(ss.Channel().SaveMember(m1)) 2517 2518 if r := <-ss.Channel().GetMembersByIds(m1.ChannelId, []string{m1.UserId}); r.Err != nil { 2519 t.Fatal(r.Err) 2520 } else { 2521 rm1 := (*r.Data.(*model.ChannelMembers))[0] 2522 2523 if rm1.ChannelId != m1.ChannelId { 2524 t.Fatal("bad team id") 2525 } 2526 2527 if rm1.UserId != m1.UserId { 2528 t.Fatal("bad user id") 2529 } 2530 } 2531 2532 m2 := &model.ChannelMember{ChannelId: o1.Id, UserId: model.NewId(), NotifyProps: model.GetDefaultChannelNotifyProps()} 2533 store.Must(ss.Channel().SaveMember(m2)) 2534 2535 if r := <-ss.Channel().GetMembersByIds(m1.ChannelId, []string{m1.UserId, m2.UserId, model.NewId()}); r.Err != nil { 2536 t.Fatal(r.Err) 2537 } else { 2538 rm := (*r.Data.(*model.ChannelMembers)) 2539 2540 if len(rm) != 2 { 2541 t.Fatal("return wrong number of results") 2542 } 2543 } 2544 2545 if r := <-ss.Channel().GetMembersByIds(m1.ChannelId, []string{}); r.Err == nil { 2546 t.Fatal("empty user ids - should have failed") 2547 } 2548 } 2549 2550 func testChannelStoreAnalyticsDeletedTypeCount(t *testing.T, ss store.Store) { 2551 o1 := model.Channel{} 2552 o1.TeamId = model.NewId() 2553 o1.DisplayName = "ChannelA" 2554 o1.Name = "zz" + model.NewId() + "b" 2555 o1.Type = model.CHANNEL_OPEN 2556 store.Must(ss.Channel().Save(&o1, -1)) 2557 2558 o2 := model.Channel{} 2559 o2.TeamId = model.NewId() 2560 o2.DisplayName = "Channel2" 2561 o2.Name = "zz" + model.NewId() + "b" 2562 o2.Type = model.CHANNEL_OPEN 2563 store.Must(ss.Channel().Save(&o2, -1)) 2564 2565 p3 := model.Channel{} 2566 p3.TeamId = model.NewId() 2567 p3.DisplayName = "Channel3" 2568 p3.Name = "zz" + model.NewId() + "b" 2569 p3.Type = model.CHANNEL_PRIVATE 2570 store.Must(ss.Channel().Save(&p3, -1)) 2571 2572 u1 := &model.User{} 2573 u1.Email = MakeEmail() 2574 u1.Nickname = model.NewId() 2575 store.Must(ss.User().Save(u1)) 2576 2577 u2 := &model.User{} 2578 u2.Email = MakeEmail() 2579 u2.Nickname = model.NewId() 2580 store.Must(ss.User().Save(u2)) 2581 2582 var d4 *model.Channel 2583 if result := <-ss.Channel().CreateDirectChannel(u1.Id, u2.Id); result.Err != nil { 2584 t.Fatalf(result.Err.Error()) 2585 } else { 2586 d4 = result.Data.(*model.Channel) 2587 } 2588 defer func() { 2589 <-ss.Channel().PermanentDeleteMembersByChannel(d4.Id) 2590 <-ss.Channel().PermanentDelete(d4.Id) 2591 }() 2592 2593 var openStartCount int64 2594 if result := <-ss.Channel().AnalyticsDeletedTypeCount("", "O"); result.Err != nil { 2595 t.Fatal(result.Err.Error()) 2596 } else { 2597 openStartCount = result.Data.(int64) 2598 } 2599 2600 var privateStartCount int64 2601 if result := <-ss.Channel().AnalyticsDeletedTypeCount("", "P"); result.Err != nil { 2602 t.Fatal(result.Err.Error()) 2603 } else { 2604 privateStartCount = result.Data.(int64) 2605 } 2606 2607 var directStartCount int64 2608 if result := <-ss.Channel().AnalyticsDeletedTypeCount("", "D"); result.Err != nil { 2609 t.Fatal(result.Err.Error()) 2610 } else { 2611 directStartCount = result.Data.(int64) 2612 } 2613 2614 store.Must(ss.Channel().Delete(o1.Id, model.GetMillis())) 2615 store.Must(ss.Channel().Delete(o2.Id, model.GetMillis())) 2616 store.Must(ss.Channel().Delete(p3.Id, model.GetMillis())) 2617 store.Must(ss.Channel().Delete(d4.Id, model.GetMillis())) 2618 2619 if result := <-ss.Channel().AnalyticsDeletedTypeCount("", "O"); result.Err != nil { 2620 t.Fatal(result.Err.Error()) 2621 } else { 2622 if result.Data.(int64) != openStartCount+2 { 2623 t.Fatalf("Wrong open channel deleted count.") 2624 } 2625 } 2626 2627 if result := <-ss.Channel().AnalyticsDeletedTypeCount("", "P"); result.Err != nil { 2628 t.Fatal(result.Err.Error()) 2629 } else { 2630 if result.Data.(int64) != privateStartCount+1 { 2631 t.Fatalf("Wrong private channel deleted count.") 2632 } 2633 } 2634 2635 if result := <-ss.Channel().AnalyticsDeletedTypeCount("", "D"); result.Err != nil { 2636 t.Fatal(result.Err.Error()) 2637 } else { 2638 if result.Data.(int64) != directStartCount+1 { 2639 t.Fatalf("Wrong direct channel deleted count.") 2640 } 2641 } 2642 } 2643 2644 func testChannelStoreGetPinnedPosts(t *testing.T, ss store.Store) { 2645 o1 := store.Must(ss.Channel().Save(&model.Channel{ 2646 TeamId: model.NewId(), 2647 DisplayName: "Name", 2648 Name: "zz" + model.NewId() + "b", 2649 Type: model.CHANNEL_OPEN, 2650 }, -1)).(*model.Channel) 2651 2652 p1 := store.Must(ss.Post().Save(&model.Post{ 2653 UserId: model.NewId(), 2654 ChannelId: o1.Id, 2655 Message: "test", 2656 IsPinned: true, 2657 })).(*model.Post) 2658 2659 if r1 := <-ss.Channel().GetPinnedPosts(o1.Id); r1.Err != nil { 2660 t.Fatal(r1.Err) 2661 } else if r1.Data.(*model.PostList).Posts[p1.Id] == nil { 2662 t.Fatal("didn't return relevant pinned posts") 2663 } 2664 2665 o2 := store.Must(ss.Channel().Save(&model.Channel{ 2666 TeamId: model.NewId(), 2667 DisplayName: "Name", 2668 Name: "zz" + model.NewId() + "b", 2669 Type: model.CHANNEL_OPEN, 2670 }, -1)).(*model.Channel) 2671 2672 store.Must(ss.Post().Save(&model.Post{ 2673 UserId: model.NewId(), 2674 ChannelId: o2.Id, 2675 Message: "test", 2676 })) 2677 2678 if r2 := <-ss.Channel().GetPinnedPosts(o2.Id); r2.Err != nil { 2679 t.Fatal(r2.Err) 2680 } else if len(r2.Data.(*model.PostList).Posts) != 0 { 2681 t.Fatal("wasn't supposed to return posts") 2682 } 2683 } 2684 2685 func testChannelStoreMaxChannelsPerTeam(t *testing.T, ss store.Store) { 2686 channel := &model.Channel{ 2687 TeamId: model.NewId(), 2688 DisplayName: "Channel", 2689 Name: model.NewId(), 2690 Type: model.CHANNEL_OPEN, 2691 } 2692 result := <-ss.Channel().Save(channel, 0) 2693 assert.NotEqual(t, nil, result.Err) 2694 assert.Equal(t, result.Err.Id, "store.sql_channel.save_channel.limit.app_error") 2695 2696 channel.Id = "" 2697 result = <-ss.Channel().Save(channel, 1) 2698 assert.Nil(t, result.Err) 2699 } 2700 2701 func testChannelStoreGetChannelsByScheme(t *testing.T, ss store.Store) { 2702 // Create some schemes. 2703 s1 := &model.Scheme{ 2704 DisplayName: model.NewId(), 2705 Name: model.NewId(), 2706 Description: model.NewId(), 2707 Scope: model.SCHEME_SCOPE_CHANNEL, 2708 } 2709 2710 s2 := &model.Scheme{ 2711 DisplayName: model.NewId(), 2712 Name: model.NewId(), 2713 Description: model.NewId(), 2714 Scope: model.SCHEME_SCOPE_CHANNEL, 2715 } 2716 2717 s1 = (<-ss.Scheme().Save(s1)).Data.(*model.Scheme) 2718 s2 = (<-ss.Scheme().Save(s2)).Data.(*model.Scheme) 2719 2720 // Create and save some teams. 2721 c1 := &model.Channel{ 2722 TeamId: model.NewId(), 2723 DisplayName: "Name", 2724 Name: model.NewId(), 2725 Type: model.CHANNEL_OPEN, 2726 SchemeId: &s1.Id, 2727 } 2728 2729 c2 := &model.Channel{ 2730 TeamId: model.NewId(), 2731 DisplayName: "Name", 2732 Name: model.NewId(), 2733 Type: model.CHANNEL_OPEN, 2734 SchemeId: &s1.Id, 2735 } 2736 2737 c3 := &model.Channel{ 2738 TeamId: model.NewId(), 2739 DisplayName: "Name", 2740 Name: model.NewId(), 2741 Type: model.CHANNEL_OPEN, 2742 } 2743 2744 _ = (<-ss.Channel().Save(c1, 100)).Data.(*model.Channel) 2745 _ = (<-ss.Channel().Save(c2, 100)).Data.(*model.Channel) 2746 _ = (<-ss.Channel().Save(c3, 100)).Data.(*model.Channel) 2747 2748 // Get the channels by a valid Scheme ID. 2749 res1 := <-ss.Channel().GetChannelsByScheme(s1.Id, 0, 100) 2750 assert.Nil(t, res1.Err) 2751 d1 := res1.Data.(model.ChannelList) 2752 assert.Len(t, d1, 2) 2753 2754 // Get the channels by a valid Scheme ID where there aren't any matching Channel. 2755 res2 := <-ss.Channel().GetChannelsByScheme(s2.Id, 0, 100) 2756 assert.Nil(t, res2.Err) 2757 d2 := res2.Data.(model.ChannelList) 2758 assert.Len(t, d2, 0) 2759 2760 // Get the channels by an invalid Scheme ID. 2761 res3 := <-ss.Channel().GetChannelsByScheme(model.NewId(), 0, 100) 2762 assert.Nil(t, res3.Err) 2763 d3 := res3.Data.(model.ChannelList) 2764 assert.Len(t, d3, 0) 2765 } 2766 2767 func testChannelStoreMigrateChannelMembers(t *testing.T, ss store.Store) { 2768 s1 := model.NewId() 2769 c1 := &model.Channel{ 2770 TeamId: model.NewId(), 2771 DisplayName: "Name", 2772 Name: model.NewId(), 2773 Type: model.CHANNEL_OPEN, 2774 SchemeId: &s1, 2775 } 2776 c1 = (<-ss.Channel().Save(c1, 100)).Data.(*model.Channel) 2777 2778 cm1 := &model.ChannelMember{ 2779 ChannelId: c1.Id, 2780 UserId: model.NewId(), 2781 ExplicitRoles: "channel_admin channel_user", 2782 NotifyProps: model.GetDefaultChannelNotifyProps(), 2783 } 2784 cm2 := &model.ChannelMember{ 2785 ChannelId: c1.Id, 2786 UserId: model.NewId(), 2787 ExplicitRoles: "channel_user", 2788 NotifyProps: model.GetDefaultChannelNotifyProps(), 2789 } 2790 cm3 := &model.ChannelMember{ 2791 ChannelId: c1.Id, 2792 UserId: model.NewId(), 2793 ExplicitRoles: "something_else", 2794 NotifyProps: model.GetDefaultChannelNotifyProps(), 2795 } 2796 2797 cm1 = (<-ss.Channel().SaveMember(cm1)).Data.(*model.ChannelMember) 2798 cm2 = (<-ss.Channel().SaveMember(cm2)).Data.(*model.ChannelMember) 2799 cm3 = (<-ss.Channel().SaveMember(cm3)).Data.(*model.ChannelMember) 2800 2801 lastDoneChannelId := strings.Repeat("0", 26) 2802 lastDoneUserId := strings.Repeat("0", 26) 2803 2804 for { 2805 res := <-ss.Channel().MigrateChannelMembers(lastDoneChannelId, lastDoneUserId) 2806 if assert.Nil(t, res.Err) { 2807 if res.Data == nil { 2808 break 2809 } 2810 data := res.Data.(map[string]string) 2811 lastDoneChannelId = data["ChannelId"] 2812 lastDoneUserId = data["UserId"] 2813 } 2814 } 2815 2816 ss.Channel().ClearCaches() 2817 2818 res1 := <-ss.Channel().GetMember(cm1.ChannelId, cm1.UserId) 2819 assert.Nil(t, res1.Err) 2820 cm1b := res1.Data.(*model.ChannelMember) 2821 assert.Equal(t, "", cm1b.ExplicitRoles) 2822 assert.True(t, cm1b.SchemeUser) 2823 assert.True(t, cm1b.SchemeAdmin) 2824 2825 res2 := <-ss.Channel().GetMember(cm2.ChannelId, cm2.UserId) 2826 assert.Nil(t, res2.Err) 2827 cm2b := res2.Data.(*model.ChannelMember) 2828 assert.Equal(t, "", cm2b.ExplicitRoles) 2829 assert.True(t, cm2b.SchemeUser) 2830 assert.False(t, cm2b.SchemeAdmin) 2831 2832 res3 := <-ss.Channel().GetMember(cm3.ChannelId, cm3.UserId) 2833 assert.Nil(t, res3.Err) 2834 cm3b := res3.Data.(*model.ChannelMember) 2835 assert.Equal(t, "something_else", cm3b.ExplicitRoles) 2836 assert.False(t, cm3b.SchemeUser) 2837 assert.False(t, cm3b.SchemeAdmin) 2838 } 2839 2840 func testResetAllChannelSchemes(t *testing.T, ss store.Store) { 2841 s1 := &model.Scheme{ 2842 Name: model.NewId(), 2843 DisplayName: model.NewId(), 2844 Description: model.NewId(), 2845 Scope: model.SCHEME_SCOPE_CHANNEL, 2846 } 2847 s1 = (<-ss.Scheme().Save(s1)).Data.(*model.Scheme) 2848 2849 c1 := &model.Channel{ 2850 TeamId: model.NewId(), 2851 DisplayName: "Name", 2852 Name: model.NewId(), 2853 Type: model.CHANNEL_OPEN, 2854 SchemeId: &s1.Id, 2855 } 2856 2857 c2 := &model.Channel{ 2858 TeamId: model.NewId(), 2859 DisplayName: "Name", 2860 Name: model.NewId(), 2861 Type: model.CHANNEL_OPEN, 2862 SchemeId: &s1.Id, 2863 } 2864 2865 c1 = (<-ss.Channel().Save(c1, 100)).Data.(*model.Channel) 2866 c2 = (<-ss.Channel().Save(c2, 100)).Data.(*model.Channel) 2867 2868 assert.Equal(t, s1.Id, *c1.SchemeId) 2869 assert.Equal(t, s1.Id, *c2.SchemeId) 2870 2871 res := <-ss.Channel().ResetAllChannelSchemes() 2872 assert.Nil(t, res.Err) 2873 2874 c1 = (<-ss.Channel().Get(c1.Id, true)).Data.(*model.Channel) 2875 c2 = (<-ss.Channel().Get(c2.Id, true)).Data.(*model.Channel) 2876 2877 assert.Equal(t, "", *c1.SchemeId) 2878 assert.Equal(t, "", *c2.SchemeId) 2879 } 2880 2881 func testChannelStoreClearAllCustomRoleAssignments(t *testing.T, ss store.Store) { 2882 c := &model.Channel{ 2883 TeamId: model.NewId(), 2884 DisplayName: "Name", 2885 Name: model.NewId(), 2886 Type: model.CHANNEL_OPEN, 2887 } 2888 2889 c = (<-ss.Channel().Save(c, 100)).Data.(*model.Channel) 2890 2891 m1 := &model.ChannelMember{ 2892 ChannelId: c.Id, 2893 UserId: model.NewId(), 2894 NotifyProps: model.GetDefaultChannelNotifyProps(), 2895 ExplicitRoles: "channel_user channel_admin system_user_access_token", 2896 } 2897 m2 := &model.ChannelMember{ 2898 ChannelId: c.Id, 2899 UserId: model.NewId(), 2900 NotifyProps: model.GetDefaultChannelNotifyProps(), 2901 ExplicitRoles: "channel_user custom_role channel_admin another_custom_role", 2902 } 2903 m3 := &model.ChannelMember{ 2904 ChannelId: c.Id, 2905 UserId: model.NewId(), 2906 NotifyProps: model.GetDefaultChannelNotifyProps(), 2907 ExplicitRoles: "channel_user", 2908 } 2909 m4 := &model.ChannelMember{ 2910 ChannelId: c.Id, 2911 UserId: model.NewId(), 2912 NotifyProps: model.GetDefaultChannelNotifyProps(), 2913 ExplicitRoles: "custom_only", 2914 } 2915 2916 store.Must(ss.Channel().SaveMember(m1)) 2917 store.Must(ss.Channel().SaveMember(m2)) 2918 store.Must(ss.Channel().SaveMember(m3)) 2919 store.Must(ss.Channel().SaveMember(m4)) 2920 2921 require.Nil(t, (<-ss.Channel().ClearAllCustomRoleAssignments()).Err) 2922 2923 r1 := <-ss.Channel().GetMember(m1.ChannelId, m1.UserId) 2924 require.Nil(t, r1.Err) 2925 assert.Equal(t, m1.ExplicitRoles, r1.Data.(*model.ChannelMember).Roles) 2926 2927 r2 := <-ss.Channel().GetMember(m2.ChannelId, m2.UserId) 2928 require.Nil(t, r2.Err) 2929 assert.Equal(t, "channel_user channel_admin", r2.Data.(*model.ChannelMember).Roles) 2930 2931 r3 := <-ss.Channel().GetMember(m3.ChannelId, m3.UserId) 2932 require.Nil(t, r3.Err) 2933 assert.Equal(t, m3.ExplicitRoles, r3.Data.(*model.ChannelMember).Roles) 2934 2935 r4 := <-ss.Channel().GetMember(m4.ChannelId, m4.UserId) 2936 require.Nil(t, r4.Err) 2937 assert.Equal(t, "", r4.Data.(*model.ChannelMember).Roles) 2938 } 2939 2940 // testMaterializedPublicChannels tests edge cases involving the triggers and stored procedures 2941 // that materialize the PublicChannels table. 2942 func testMaterializedPublicChannels(t *testing.T, ss store.Store, s SqlSupplier) { 2943 teamId := model.NewId() 2944 2945 // o1 is a public channel on the team 2946 o1 := model.Channel{ 2947 TeamId: teamId, 2948 DisplayName: "Open Channel", 2949 Name: model.NewId(), 2950 Type: model.CHANNEL_OPEN, 2951 } 2952 store.Must(ss.Channel().Save(&o1, -1)) 2953 2954 // o2 is another public channel on the team 2955 o2 := model.Channel{ 2956 TeamId: teamId, 2957 DisplayName: "Open Channel 2", 2958 Name: model.NewId(), 2959 Type: model.CHANNEL_OPEN, 2960 } 2961 store.Must(ss.Channel().Save(&o2, -1)) 2962 2963 t.Run("o1 and o2 initially listed in public channels", func(t *testing.T) { 2964 result := <-ss.Channel().SearchInTeam(teamId, "", true) 2965 require.Nil(t, result.Err) 2966 require.Equal(t, &model.ChannelList{&o1, &o2}, result.Data.(*model.ChannelList)) 2967 }) 2968 2969 o1.DeleteAt = model.GetMillis() 2970 o1.UpdateAt = model.GetMillis() 2971 store.Must(ss.Channel().Delete(o1.Id, o1.DeleteAt)) 2972 2973 t.Run("o1 still listed in public channels when marked as deleted", func(t *testing.T) { 2974 result := <-ss.Channel().SearchInTeam(teamId, "", true) 2975 require.Nil(t, result.Err) 2976 require.Equal(t, &model.ChannelList{&o1, &o2}, result.Data.(*model.ChannelList)) 2977 }) 2978 2979 <-ss.Channel().PermanentDelete(o1.Id) 2980 2981 t.Run("o1 no longer listed in public channels when permanently deleted", func(t *testing.T) { 2982 result := <-ss.Channel().SearchInTeam(teamId, "", true) 2983 require.Nil(t, result.Err) 2984 require.Equal(t, &model.ChannelList{&o2}, result.Data.(*model.ChannelList)) 2985 }) 2986 2987 o2.Type = model.CHANNEL_PRIVATE 2988 require.Nil(t, (<-ss.Channel().Update(&o2)).Err) 2989 2990 t.Run("o2 no longer listed since now private", func(t *testing.T) { 2991 result := <-ss.Channel().SearchInTeam(teamId, "", true) 2992 require.Nil(t, result.Err) 2993 require.Equal(t, &model.ChannelList{}, result.Data.(*model.ChannelList)) 2994 }) 2995 2996 o2.Type = model.CHANNEL_OPEN 2997 require.Nil(t, (<-ss.Channel().Update(&o2)).Err) 2998 2999 t.Run("o2 listed once again since now public", func(t *testing.T) { 3000 result := <-ss.Channel().SearchInTeam(teamId, "", true) 3001 require.Nil(t, result.Err) 3002 require.Equal(t, &model.ChannelList{&o2}, result.Data.(*model.ChannelList)) 3003 }) 3004 3005 // o3 is a public channel on the team that already existed in the PublicChannels table. 3006 o3 := model.Channel{ 3007 Id: model.NewId(), 3008 TeamId: teamId, 3009 DisplayName: "Open Channel 3", 3010 Name: model.NewId(), 3011 Type: model.CHANNEL_OPEN, 3012 } 3013 3014 _, err := s.GetMaster().ExecNoTimeout(` 3015 INSERT INTO 3016 PublicChannels(Id, DeleteAt, TeamId, DisplayName, Name, Header, Purpose) 3017 VALUES 3018 (:Id, :DeleteAt, :TeamId, :DisplayName, :Name, :Header, :Purpose); 3019 `, map[string]interface{}{ 3020 "Id": o3.Id, 3021 "DeleteAt": o3.DeleteAt, 3022 "TeamId": o3.TeamId, 3023 "DisplayName": o3.DisplayName, 3024 "Name": o3.Name, 3025 "Header": o3.Header, 3026 "Purpose": o3.Purpose, 3027 }) 3028 require.Nil(t, err) 3029 3030 o3.DisplayName = "Open Channel 3 - Modified" 3031 3032 _, err = s.GetMaster().ExecNoTimeout(` 3033 INSERT INTO 3034 Channels(Id, CreateAt, UpdateAt, DeleteAt, TeamId, Type, DisplayName, Name, Header, Purpose, LastPostAt, TotalMsgCount, ExtraUpdateAt, CreatorId) 3035 VALUES 3036 (:Id, :CreateAt, :UpdateAt, :DeleteAt, :TeamId, :Type, :DisplayName, :Name, :Header, :Purpose, :LastPostAt, :TotalMsgCount, :ExtraUpdateAt, :CreatorId); 3037 `, map[string]interface{}{ 3038 "Id": o3.Id, 3039 "CreateAt": o3.CreateAt, 3040 "UpdateAt": o3.UpdateAt, 3041 "DeleteAt": o3.DeleteAt, 3042 "TeamId": o3.TeamId, 3043 "Type": o3.Type, 3044 "DisplayName": o3.DisplayName, 3045 "Name": o3.Name, 3046 "Header": o3.Header, 3047 "Purpose": o3.Purpose, 3048 "LastPostAt": o3.LastPostAt, 3049 "TotalMsgCount": o3.TotalMsgCount, 3050 "ExtraUpdateAt": o3.ExtraUpdateAt, 3051 "CreatorId": o3.CreatorId, 3052 }) 3053 require.Nil(t, err) 3054 3055 t.Run("verify o3 INSERT converted to UPDATE", func(t *testing.T) { 3056 result := <-ss.Channel().SearchInTeam(teamId, "", true) 3057 require.Nil(t, result.Err) 3058 require.Equal(t, &model.ChannelList{&o2, &o3}, result.Data.(*model.ChannelList)) 3059 }) 3060 3061 // o4 is a public channel on the team that existed in the Channels table but was omitted from the PublicChannels table. 3062 o4 := model.Channel{ 3063 TeamId: teamId, 3064 DisplayName: "Open Channel 4", 3065 Name: model.NewId(), 3066 Type: model.CHANNEL_OPEN, 3067 } 3068 3069 store.Must(ss.Channel().Save(&o4, -1)) 3070 3071 _, err = s.GetMaster().ExecNoTimeout(` 3072 DELETE FROM 3073 PublicChannels 3074 WHERE 3075 Id = :Id 3076 `, map[string]interface{}{ 3077 "Id": o4.Id, 3078 }) 3079 require.Nil(t, err) 3080 3081 o4.DisplayName += " - Modified" 3082 require.Nil(t, (<-ss.Channel().Update(&o4)).Err) 3083 3084 t.Run("verify o4 UPDATE converted to INSERT", func(t *testing.T) { 3085 result := <-ss.Channel().SearchInTeam(teamId, "", true) 3086 require.Nil(t, result.Err) 3087 require.Equal(t, &model.ChannelList{&o2, &o3, &o4}, result.Data.(*model.ChannelList)) 3088 }) 3089 } 3090 3091 func testChannelStoreGetAllChannelsForExportAfter(t *testing.T, ss store.Store) { 3092 t1 := model.Team{} 3093 t1.DisplayName = "Name" 3094 t1.Name = model.NewId() 3095 t1.Email = MakeEmail() 3096 t1.Type = model.TEAM_OPEN 3097 store.Must(ss.Team().Save(&t1)) 3098 3099 c1 := model.Channel{} 3100 c1.TeamId = t1.Id 3101 c1.DisplayName = "Channel1" 3102 c1.Name = "zz" + model.NewId() + "b" 3103 c1.Type = model.CHANNEL_OPEN 3104 store.Must(ss.Channel().Save(&c1, -1)) 3105 3106 r1 := <-ss.Channel().GetAllChannelsForExportAfter(10000, strings.Repeat("0", 26)) 3107 assert.Nil(t, r1.Err) 3108 d1 := r1.Data.([]*model.ChannelForExport) 3109 3110 found := false 3111 for _, c := range d1 { 3112 if c.Id == c1.Id { 3113 found = true 3114 assert.Equal(t, t1.Id, c.TeamId) 3115 assert.Nil(t, c.SchemeId) 3116 assert.Equal(t, t1.Name, c.TeamName) 3117 } 3118 } 3119 assert.True(t, found) 3120 } 3121 3122 func testChannelStoreGetChannelMembersForExport(t *testing.T, ss store.Store) { 3123 t1 := model.Team{} 3124 t1.DisplayName = "Name" 3125 t1.Name = model.NewId() 3126 t1.Email = MakeEmail() 3127 t1.Type = model.TEAM_OPEN 3128 store.Must(ss.Team().Save(&t1)) 3129 3130 c1 := model.Channel{} 3131 c1.TeamId = t1.Id 3132 c1.DisplayName = "Channel1" 3133 c1.Name = "zz" + model.NewId() + "b" 3134 c1.Type = model.CHANNEL_OPEN 3135 store.Must(ss.Channel().Save(&c1, -1)) 3136 3137 c2 := model.Channel{} 3138 c2.TeamId = model.NewId() 3139 c2.DisplayName = "Channel2" 3140 c2.Name = "zz" + model.NewId() + "b" 3141 c2.Type = model.CHANNEL_OPEN 3142 store.Must(ss.Channel().Save(&c2, -1)) 3143 3144 u1 := model.User{} 3145 u1.Email = MakeEmail() 3146 u1.Nickname = model.NewId() 3147 store.Must(ss.User().Save(&u1)) 3148 3149 m1 := model.ChannelMember{} 3150 m1.ChannelId = c1.Id 3151 m1.UserId = u1.Id 3152 m1.NotifyProps = model.GetDefaultChannelNotifyProps() 3153 store.Must(ss.Channel().SaveMember(&m1)) 3154 3155 m2 := model.ChannelMember{} 3156 m2.ChannelId = c2.Id 3157 m2.UserId = u1.Id 3158 m2.NotifyProps = model.GetDefaultChannelNotifyProps() 3159 store.Must(ss.Channel().SaveMember(&m2)) 3160 3161 r1 := <-ss.Channel().GetChannelMembersForExport(u1.Id, t1.Id) 3162 assert.Nil(t, r1.Err) 3163 3164 d1 := r1.Data.([]*model.ChannelMemberForExport) 3165 assert.Len(t, d1, 1) 3166 3167 cmfe1 := d1[0] 3168 assert.Equal(t, c1.Name, cmfe1.ChannelName) 3169 assert.Equal(t, c1.Id, cmfe1.ChannelId) 3170 assert.Equal(t, u1.Id, cmfe1.UserId) 3171 } 3172 3173 func testChannelStoreRemoveAllDeactivatedMembers(t *testing.T, ss store.Store) { 3174 // Set up all the objects needed in the store. 3175 t1 := model.Team{} 3176 t1.DisplayName = "Name" 3177 t1.Name = model.NewId() 3178 t1.Email = MakeEmail() 3179 t1.Type = model.TEAM_OPEN 3180 store.Must(ss.Team().Save(&t1)) 3181 3182 c1 := model.Channel{} 3183 c1.TeamId = t1.Id 3184 c1.DisplayName = "Channel1" 3185 c1.Name = "zz" + model.NewId() + "b" 3186 c1.Type = model.CHANNEL_OPEN 3187 store.Must(ss.Channel().Save(&c1, -1)) 3188 3189 u1 := model.User{} 3190 u1.Email = MakeEmail() 3191 u1.Nickname = model.NewId() 3192 store.Must(ss.User().Save(&u1)) 3193 3194 u2 := model.User{} 3195 u2.Email = MakeEmail() 3196 u2.Nickname = model.NewId() 3197 store.Must(ss.User().Save(&u2)) 3198 3199 u3 := model.User{} 3200 u3.Email = MakeEmail() 3201 u3.Nickname = model.NewId() 3202 store.Must(ss.User().Save(&u3)) 3203 3204 m1 := model.ChannelMember{} 3205 m1.ChannelId = c1.Id 3206 m1.UserId = u1.Id 3207 m1.NotifyProps = model.GetDefaultChannelNotifyProps() 3208 store.Must(ss.Channel().SaveMember(&m1)) 3209 3210 m2 := model.ChannelMember{} 3211 m2.ChannelId = c1.Id 3212 m2.UserId = u2.Id 3213 m2.NotifyProps = model.GetDefaultChannelNotifyProps() 3214 store.Must(ss.Channel().SaveMember(&m2)) 3215 3216 m3 := model.ChannelMember{} 3217 m3.ChannelId = c1.Id 3218 m3.UserId = u3.Id 3219 m3.NotifyProps = model.GetDefaultChannelNotifyProps() 3220 store.Must(ss.Channel().SaveMember(&m3)) 3221 3222 // Get all the channel members. Check there are 3. 3223 r1 := <-ss.Channel().GetMembers(c1.Id, 0, 1000) 3224 assert.Nil(t, r1.Err) 3225 d1 := r1.Data.(*model.ChannelMembers) 3226 assert.Len(t, *d1, 3) 3227 3228 // Deactivate users 1 & 2. 3229 u1.DeleteAt = model.GetMillis() 3230 u2.DeleteAt = model.GetMillis() 3231 require.Nil(t, (<-ss.User().Update(&u1, true)).Err) 3232 require.Nil(t, (<-ss.User().Update(&u2, true)).Err) 3233 3234 // Remove all deactivated users from the channel. 3235 assert.Nil(t, (<-ss.Channel().RemoveAllDeactivatedMembers(c1.Id)).Err) 3236 3237 // Get all the channel members. Check there is now only 1: m3. 3238 r2 := <-ss.Channel().GetMembers(c1.Id, 0, 1000) 3239 assert.Nil(t, r1.Err) 3240 d2 := r2.Data.(*model.ChannelMembers) 3241 assert.Len(t, *d2, 1) 3242 assert.Equal(t, (*d2)[0].UserId, u3.Id) 3243 } 3244 3245 func testChannelStoreExportAllDirectChannels(t *testing.T, ss store.Store, s SqlSupplier) { 3246 teamId := model.NewId() 3247 3248 o1 := model.Channel{} 3249 o1.TeamId = teamId 3250 o1.DisplayName = "Name" + model.NewId() 3251 o1.Name = "zz" + model.NewId() + "b" 3252 o1.Type = model.CHANNEL_DIRECT 3253 3254 userIds := []string{model.NewId(), model.NewId(), model.NewId()} 3255 3256 o2 := model.Channel{} 3257 o2.Name = model.GetGroupNameFromUserIds(userIds) 3258 o2.DisplayName = "GroupChannel" + model.NewId() 3259 o2.Name = "zz" + model.NewId() + "b" 3260 o2.Type = model.CHANNEL_GROUP 3261 store.Must(ss.Channel().Save(&o2, -1)) 3262 3263 u1 := &model.User{} 3264 u1.Email = MakeEmail() 3265 u1.Nickname = model.NewId() 3266 store.Must(ss.User().Save(u1)) 3267 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u1.Id}, -1)) 3268 3269 u2 := &model.User{} 3270 u2.Email = MakeEmail() 3271 u2.Nickname = model.NewId() 3272 store.Must(ss.User().Save(u2)) 3273 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u2.Id}, -1)) 3274 3275 m1 := model.ChannelMember{} 3276 m1.ChannelId = o1.Id 3277 m1.UserId = u1.Id 3278 m1.NotifyProps = model.GetDefaultChannelNotifyProps() 3279 3280 m2 := model.ChannelMember{} 3281 m2.ChannelId = o1.Id 3282 m2.UserId = u2.Id 3283 m2.NotifyProps = model.GetDefaultChannelNotifyProps() 3284 3285 <-ss.Channel().SaveDirectChannel(&o1, &m1, &m2) 3286 3287 r1 := <-ss.Channel().GetAllDirectChannelsForExportAfter(10000, strings.Repeat("0", 26)) 3288 assert.Nil(t, r1.Err) 3289 d1 := r1.Data.([]*model.DirectChannelForExport) 3290 3291 assert.Equal(t, 2, len(d1)) 3292 assert.ElementsMatch(t, []string{o1.DisplayName, o2.DisplayName}, []string{d1[0].DisplayName, d1[1].DisplayName}) 3293 3294 // Manually truncate Channels table until testlib can handle cleanups 3295 s.GetMaster().Exec("TRUNCATE Channels") 3296 } 3297 3298 func testChannelStoreExportAllDirectChannelsExcludePrivateAndPublic(t *testing.T, ss store.Store, s SqlSupplier) { 3299 teamId := model.NewId() 3300 3301 o1 := model.Channel{} 3302 o1.TeamId = teamId 3303 o1.DisplayName = "The Direct Channel" + model.NewId() 3304 o1.Name = "zz" + model.NewId() + "b" 3305 o1.Type = model.CHANNEL_DIRECT 3306 3307 o2 := model.Channel{} 3308 o2.TeamId = teamId 3309 o2.DisplayName = "Channel2" + model.NewId() 3310 o2.Name = "zz" + model.NewId() + "b" 3311 o2.Type = model.CHANNEL_OPEN 3312 store.Must(ss.Channel().Save(&o2, -1)) 3313 3314 o3 := model.Channel{} 3315 o3.TeamId = teamId 3316 o3.DisplayName = "Channel3" + model.NewId() 3317 o3.Name = "zz" + model.NewId() + "b" 3318 o3.Type = model.CHANNEL_PRIVATE 3319 store.Must(ss.Channel().Save(&o3, -1)) 3320 3321 u1 := &model.User{} 3322 u1.Email = MakeEmail() 3323 u1.Nickname = model.NewId() 3324 store.Must(ss.User().Save(u1)) 3325 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u1.Id}, -1)) 3326 3327 u2 := &model.User{} 3328 u2.Email = MakeEmail() 3329 u2.Nickname = model.NewId() 3330 store.Must(ss.User().Save(u2)) 3331 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u2.Id}, -1)) 3332 3333 m1 := model.ChannelMember{} 3334 m1.ChannelId = o1.Id 3335 m1.UserId = u1.Id 3336 m1.NotifyProps = model.GetDefaultChannelNotifyProps() 3337 3338 m2 := model.ChannelMember{} 3339 m2.ChannelId = o1.Id 3340 m2.UserId = u2.Id 3341 m2.NotifyProps = model.GetDefaultChannelNotifyProps() 3342 3343 <-ss.Channel().SaveDirectChannel(&o1, &m1, &m2) 3344 3345 r1 := <-ss.Channel().GetAllDirectChannelsForExportAfter(10000, strings.Repeat("0", 26)) 3346 assert.Nil(t, r1.Err) 3347 d1 := r1.Data.([]*model.DirectChannelForExport) 3348 assert.Equal(t, 1, len(d1)) 3349 assert.Equal(t, o1.DisplayName, d1[0].DisplayName) 3350 3351 // Manually truncate Channels table until testlib can handle cleanups 3352 s.GetMaster().Exec("TRUNCATE Channels") 3353 } 3354 3355 func testChannelStoreExportAllDirectChannelsDeletedChannel(t *testing.T, ss store.Store, s SqlSupplier) { 3356 teamId := model.NewId() 3357 3358 o1 := model.Channel{} 3359 o1.TeamId = teamId 3360 o1.DisplayName = "Different Name" + model.NewId() 3361 o1.Name = "zz" + model.NewId() + "b" 3362 o1.Type = model.CHANNEL_DIRECT 3363 3364 u1 := &model.User{} 3365 u1.Email = MakeEmail() 3366 u1.Nickname = model.NewId() 3367 store.Must(ss.User().Save(u1)) 3368 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u1.Id}, -1)) 3369 3370 u2 := &model.User{} 3371 u2.Email = MakeEmail() 3372 u2.Nickname = model.NewId() 3373 store.Must(ss.User().Save(u2)) 3374 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u2.Id}, -1)) 3375 3376 m1 := model.ChannelMember{} 3377 m1.ChannelId = o1.Id 3378 m1.UserId = u1.Id 3379 m1.NotifyProps = model.GetDefaultChannelNotifyProps() 3380 3381 m2 := model.ChannelMember{} 3382 m2.ChannelId = o1.Id 3383 m2.UserId = u2.Id 3384 m2.NotifyProps = model.GetDefaultChannelNotifyProps() 3385 3386 result := <-ss.Channel().SaveDirectChannel(&o1, &m1, &m2) 3387 3388 o1.DeleteAt = 1 3389 result = <-ss.Channel().SetDeleteAt(o1.Id, 1, 1) 3390 assert.Nil(t, result.Err) 3391 3392 r1 := <-ss.Channel().GetAllDirectChannelsForExportAfter(10000, strings.Repeat("0", 26)) 3393 assert.Nil(t, r1.Err) 3394 d1 := r1.Data.([]*model.DirectChannelForExport) 3395 3396 assert.Equal(t, 0, len(d1)) 3397 3398 // Manually truncate Channels table until testlib can handle cleanups 3399 s.GetMaster().Exec("TRUNCATE Channels") 3400 } 3401 3402 func testChannelStoreGetChannelsBatchForIndexing(t *testing.T, ss store.Store) { 3403 // Set up all the objects needed 3404 c1 := &model.Channel{} 3405 c1.DisplayName = "Channel1" 3406 c1.Name = "zz" + model.NewId() + "b" 3407 c1.Type = model.CHANNEL_OPEN 3408 store.Must(ss.Channel().Save(c1, -1)) 3409 3410 time.Sleep(10 * time.Millisecond) 3411 3412 c2 := &model.Channel{} 3413 c2.DisplayName = "Channel2" 3414 c2.Name = "zz" + model.NewId() + "b" 3415 c2.Type = model.CHANNEL_OPEN 3416 store.Must(ss.Channel().Save(c2, -1)) 3417 3418 time.Sleep(10 * time.Millisecond) 3419 startTime := c2.CreateAt 3420 3421 c3 := &model.Channel{} 3422 c3.DisplayName = "Channel3" 3423 c3.Name = "zz" + model.NewId() + "b" 3424 c3.Type = model.CHANNEL_OPEN 3425 store.Must(ss.Channel().Save(c3, -1)) 3426 3427 c4 := &model.Channel{} 3428 c4.DisplayName = "Channel4" 3429 c4.Name = "zz" + model.NewId() + "b" 3430 c4.Type = model.CHANNEL_PRIVATE 3431 store.Must(ss.Channel().Save(c4, -1)) 3432 3433 c5 := &model.Channel{} 3434 c5.DisplayName = "Channel5" 3435 c5.Name = "zz" + model.NewId() + "b" 3436 c5.Type = model.CHANNEL_OPEN 3437 store.Must(ss.Channel().Save(c5, -1)) 3438 3439 time.Sleep(10 * time.Millisecond) 3440 3441 c6 := &model.Channel{} 3442 c6.DisplayName = "Channel6" 3443 c6.Name = "zz" + model.NewId() + "b" 3444 c6.Type = model.CHANNEL_OPEN 3445 store.Must(ss.Channel().Save(c6, -1)) 3446 3447 endTime := c6.CreateAt 3448 3449 // First and last channel should be outside the range 3450 res1 := <-ss.Channel().GetChannelsBatchForIndexing(startTime, endTime, 1000) 3451 assert.Nil(t, res1.Err) 3452 assert.ElementsMatch(t, []*model.Channel{c2, c3, c5}, res1.Data) 3453 3454 // Update the endTime, last channel should be in 3455 endTime = model.GetMillis() 3456 res2 := <-ss.Channel().GetChannelsBatchForIndexing(startTime, endTime, 1000) 3457 assert.Nil(t, res2.Err) 3458 assert.ElementsMatch(t, []*model.Channel{c2, c3, c5, c6}, res2.Data) 3459 3460 // Testing the limit 3461 res3 := <-ss.Channel().GetChannelsBatchForIndexing(startTime, endTime, 2) 3462 assert.Nil(t, res3.Err) 3463 assert.ElementsMatch(t, []*model.Channel{c2, c3}, res3.Data) 3464 }