github.com/wgh-/mattermost-server@v4.8.0-rc2+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 "testing" 9 "time" 10 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/require" 13 14 "github.com/mattermost/mattermost-server/model" 15 "github.com/mattermost/mattermost-server/store" 16 ) 17 18 func TestChannelStore(t *testing.T, ss store.Store) { 19 t.Run("Save", func(t *testing.T) { testChannelStoreSave(t, ss) }) 20 t.Run("SaveDirectChannel", func(t *testing.T) { testChannelStoreSaveDirectChannel(t, ss) }) 21 t.Run("CreateDirectChannel", func(t *testing.T) { testChannelStoreCreateDirectChannel(t, ss) }) 22 t.Run("Update", func(t *testing.T) { testChannelStoreUpdate(t, ss) }) 23 t.Run("GetChannelUnread", func(t *testing.T) { testGetChannelUnread(t, ss) }) 24 t.Run("Get", func(t *testing.T) { testChannelStoreGet(t, ss) }) 25 t.Run("GetForPost", func(t *testing.T) { testChannelStoreGetForPost(t, ss) }) 26 t.Run("Restore", func(t *testing.T) { testChannelStoreRestore(t, ss) }) 27 t.Run("Delete", func(t *testing.T) { testChannelStoreDelete(t, ss) }) 28 t.Run("GetByName", func(t *testing.T) { testChannelStoreGetByName(t, ss) }) 29 t.Run("GetByNames", func(t *testing.T) { testChannelStoreGetByNames(t, ss) }) 30 t.Run("GetDeletedByName", func(t *testing.T) { testChannelStoreGetDeletedByName(t, ss) }) 31 t.Run("GetDeleted", func(t *testing.T) { testChannelStoreGetDeleted(t, ss) }) 32 t.Run("ChannelMemberStore", func(t *testing.T) { testChannelMemberStore(t, ss) }) 33 t.Run("ChannelDeleteMemberStore", func(t *testing.T) { testChannelDeleteMemberStore(t, ss) }) 34 t.Run("GetChannels", func(t *testing.T) { testChannelStoreGetChannels(t, ss) }) 35 t.Run("GetMoreChannels", func(t *testing.T) { testChannelStoreGetMoreChannels(t, ss) }) 36 t.Run("GetPublicChannelsForTeam", func(t *testing.T) { testChannelStoreGetPublicChannelsForTeam(t, ss) }) 37 t.Run("GetPublicChannelsByIdsForTeam", func(t *testing.T) { testChannelStoreGetPublicChannelsByIdsForTeam(t, ss) }) 38 t.Run("GetChannelCounts", func(t *testing.T) { testChannelStoreGetChannelCounts(t, ss) }) 39 t.Run("GetMembersForUser", func(t *testing.T) { testChannelStoreGetMembersForUser(t, ss) }) 40 t.Run("UpdateLastViewedAt", func(t *testing.T) { testChannelStoreUpdateLastViewedAt(t, ss) }) 41 t.Run("IncrementMentionCount", func(t *testing.T) { testChannelStoreIncrementMentionCount(t, ss) }) 42 t.Run("UpdateChannelMember", func(t *testing.T) { testUpdateChannelMember(t, ss) }) 43 t.Run("GetMember", func(t *testing.T) { testGetMember(t, ss) }) 44 t.Run("GetMemberForPost", func(t *testing.T) { testChannelStoreGetMemberForPost(t, ss) }) 45 t.Run("GetMemberCount", func(t *testing.T) { testGetMemberCount(t, ss) }) 46 t.Run("UpdateExtrasByUser", func(t *testing.T) { testUpdateExtrasByUser(t, ss) }) 47 t.Run("SearchMore", func(t *testing.T) { testChannelStoreSearchMore(t, ss) }) 48 t.Run("SearchInTeam", func(t *testing.T) { testChannelStoreSearchInTeam(t, ss) }) 49 t.Run("GetMembersByIds", func(t *testing.T) { testChannelStoreGetMembersByIds(t, ss) }) 50 t.Run("AnalyticsDeletedTypeCount", func(t *testing.T) { testChannelStoreAnalyticsDeletedTypeCount(t, ss) }) 51 t.Run("GetPinnedPosts", func(t *testing.T) { testChannelStoreGetPinnedPosts(t, ss) }) 52 t.Run("MaxChannelsPerTeam", func(t *testing.T) { testChannelStoreMaxChannelsPerTeam(t, ss) }) 53 } 54 55 func testChannelStoreSave(t *testing.T, ss store.Store) { 56 teamId := model.NewId() 57 58 o1 := model.Channel{} 59 o1.TeamId = teamId 60 o1.DisplayName = "Name" 61 o1.Name = "zz" + model.NewId() + "b" 62 o1.Type = model.CHANNEL_OPEN 63 64 if err := (<-ss.Channel().Save(&o1, -1)).Err; err != nil { 65 t.Fatal("couldn't save item", err) 66 } 67 68 if err := (<-ss.Channel().Save(&o1, -1)).Err; err == nil { 69 t.Fatal("shouldn't be able to update from save") 70 } 71 72 o1.Id = "" 73 if err := (<-ss.Channel().Save(&o1, -1)).Err; err == nil { 74 t.Fatal("should be unique name") 75 } 76 77 o1.Id = "" 78 o1.Name = "zz" + model.NewId() + "b" 79 o1.Type = model.CHANNEL_DIRECT 80 if err := (<-ss.Channel().Save(&o1, -1)).Err; err == nil { 81 t.Fatal("Should not be able to save direct channel") 82 } 83 } 84 85 func testChannelStoreSaveDirectChannel(t *testing.T, ss store.Store) { 86 teamId := model.NewId() 87 88 o1 := model.Channel{} 89 o1.TeamId = teamId 90 o1.DisplayName = "Name" 91 o1.Name = "zz" + model.NewId() + "b" 92 o1.Type = model.CHANNEL_DIRECT 93 94 u1 := &model.User{} 95 u1.Email = model.NewId() 96 u1.Nickname = model.NewId() 97 store.Must(ss.User().Save(u1)) 98 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u1.Id}, -1)) 99 100 u2 := &model.User{} 101 u2.Email = model.NewId() 102 u2.Nickname = model.NewId() 103 store.Must(ss.User().Save(u2)) 104 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u2.Id}, -1)) 105 106 m1 := model.ChannelMember{} 107 m1.ChannelId = o1.Id 108 m1.UserId = u1.Id 109 m1.NotifyProps = model.GetDefaultChannelNotifyProps() 110 111 m2 := model.ChannelMember{} 112 m2.ChannelId = o1.Id 113 m2.UserId = u2.Id 114 m2.NotifyProps = model.GetDefaultChannelNotifyProps() 115 116 if err := (<-ss.Channel().SaveDirectChannel(&o1, &m1, &m2)).Err; err != nil { 117 t.Fatal("couldn't save direct channel", err) 118 } 119 120 members := (<-ss.Channel().GetMembers(o1.Id, 0, 100)).Data.(*model.ChannelMembers) 121 if len(*members) != 2 { 122 t.Fatal("should have saved 2 members") 123 } 124 125 if err := (<-ss.Channel().SaveDirectChannel(&o1, &m1, &m2)).Err; err == nil { 126 t.Fatal("shouldn't be able to update from save") 127 } 128 129 // Attempt to save a direct channel that already exists 130 o1a := model.Channel{ 131 TeamId: o1.TeamId, 132 DisplayName: o1.DisplayName, 133 Name: o1.Name, 134 Type: o1.Type, 135 } 136 137 if result := <-ss.Channel().SaveDirectChannel(&o1a, &m1, &m2); result.Err == nil { 138 t.Fatal("should've failed to save a duplicate direct channel") 139 } else if result.Err.Id != store.CHANNEL_EXISTS_ERROR { 140 t.Fatal("should've returned CHANNEL_EXISTS_ERROR") 141 } else if returned := result.Data.(*model.Channel); returned.Id != o1.Id { 142 t.Fatal("should've returned original channel when saving a duplicate direct channel") 143 } 144 145 // Attempt to save a non-direct channel 146 o1.Id = "" 147 o1.Name = "zz" + model.NewId() + "b" 148 o1.Type = model.CHANNEL_OPEN 149 if err := (<-ss.Channel().SaveDirectChannel(&o1, &m1, &m2)).Err; err == nil { 150 t.Fatal("Should not be able to save non-direct channel") 151 } 152 153 // Save yourself Direct Message 154 o1.Id = "" 155 o1.DisplayName = "Myself" 156 o1.Name = "zz" + model.NewId() + "b" 157 o1.Type = model.CHANNEL_DIRECT 158 if err := (<-ss.Channel().SaveDirectChannel(&o1, &m1, &m1)).Err; err != nil { 159 t.Fatal("couldn't save direct channel", err) 160 } 161 162 members = (<-ss.Channel().GetMembers(o1.Id, 0, 100)).Data.(*model.ChannelMembers) 163 if len(*members) != 1 { 164 t.Fatal("should have saved just 1 member") 165 } 166 167 } 168 169 func testChannelStoreCreateDirectChannel(t *testing.T, ss store.Store) { 170 u1 := &model.User{} 171 u1.Email = model.NewId() 172 u1.Nickname = model.NewId() 173 store.Must(ss.User().Save(u1)) 174 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u1.Id}, -1)) 175 176 u2 := &model.User{} 177 u2.Email = model.NewId() 178 u2.Nickname = model.NewId() 179 store.Must(ss.User().Save(u2)) 180 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u2.Id}, -1)) 181 182 res := <-ss.Channel().CreateDirectChannel(u1.Id, u2.Id) 183 if res.Err != nil { 184 t.Fatal("couldn't create direct channel", res.Err) 185 } 186 187 c1 := res.Data.(*model.Channel) 188 189 members := (<-ss.Channel().GetMembers(c1.Id, 0, 100)).Data.(*model.ChannelMembers) 190 if len(*members) != 2 { 191 t.Fatal("should have saved 2 members") 192 } 193 } 194 195 func testChannelStoreUpdate(t *testing.T, ss store.Store) { 196 o1 := model.Channel{} 197 o1.TeamId = model.NewId() 198 o1.DisplayName = "Name" 199 o1.Name = "zz" + model.NewId() + "b" 200 o1.Type = model.CHANNEL_OPEN 201 store.Must(ss.Channel().Save(&o1, -1)) 202 203 o2 := model.Channel{} 204 o2.TeamId = o1.TeamId 205 o2.DisplayName = "Name" 206 o2.Name = "zz" + model.NewId() + "b" 207 o2.Type = model.CHANNEL_OPEN 208 store.Must(ss.Channel().Save(&o2, -1)) 209 210 time.Sleep(100 * time.Millisecond) 211 212 if err := (<-ss.Channel().Update(&o1)).Err; err != nil { 213 t.Fatal(err) 214 } 215 216 o1.Id = "missing" 217 if err := (<-ss.Channel().Update(&o1)).Err; err == nil { 218 t.Fatal("Update should have failed because of missing key") 219 } 220 221 o1.Id = model.NewId() 222 if err := (<-ss.Channel().Update(&o1)).Err; err == nil { 223 t.Fatal("Update should have faile because id change") 224 } 225 226 o2.Name = o1.Name 227 if err := (<-ss.Channel().Update(&o2)).Err; err == nil { 228 t.Fatal("Update should have failed because of existing name") 229 } 230 } 231 232 func testGetChannelUnread(t *testing.T, ss store.Store) { 233 teamId1 := model.NewId() 234 teamId2 := model.NewId() 235 236 uid := model.NewId() 237 m1 := &model.TeamMember{TeamId: teamId1, UserId: uid} 238 m2 := &model.TeamMember{TeamId: teamId2, UserId: uid} 239 store.Must(ss.Team().SaveMember(m1, -1)) 240 store.Must(ss.Team().SaveMember(m2, -1)) 241 notifyPropsModel := model.GetDefaultChannelNotifyProps() 242 243 // Setup Channel 1 244 c1 := &model.Channel{TeamId: m1.TeamId, Name: model.NewId(), DisplayName: "Downtown", Type: model.CHANNEL_OPEN, TotalMsgCount: 100} 245 store.Must(ss.Channel().Save(c1, -1)) 246 cm1 := &model.ChannelMember{ChannelId: c1.Id, UserId: m1.UserId, NotifyProps: notifyPropsModel, MsgCount: 90} 247 store.Must(ss.Channel().SaveMember(cm1)) 248 249 // Setup Channel 2 250 c2 := &model.Channel{TeamId: m2.TeamId, Name: model.NewId(), DisplayName: "Cultural", Type: model.CHANNEL_OPEN, TotalMsgCount: 100} 251 store.Must(ss.Channel().Save(c2, -1)) 252 cm2 := &model.ChannelMember{ChannelId: c2.Id, UserId: m2.UserId, NotifyProps: notifyPropsModel, MsgCount: 90, MentionCount: 5} 253 store.Must(ss.Channel().SaveMember(cm2)) 254 255 // Check for Channel 1 256 if resp := <-ss.Channel().GetChannelUnread(c1.Id, uid); resp.Err != nil { 257 t.Fatal(resp.Err) 258 } else { 259 ch := resp.Data.(*model.ChannelUnread) 260 if c1.Id != ch.ChannelId { 261 t.Fatal("wrong channel id") 262 } 263 264 if teamId1 != ch.TeamId { 265 t.Fatal("wrong team id for channel 1") 266 } 267 268 if ch.NotifyProps == nil { 269 t.Fatal("wrong props for channel 1") 270 } 271 272 if ch.MentionCount != 0 { 273 t.Fatal("wrong MentionCount for channel 1") 274 } 275 276 if ch.MsgCount != 10 { 277 t.Fatal("wrong MsgCount for channel 1") 278 } 279 } 280 281 // Check for Channel 2 282 if resp2 := <-ss.Channel().GetChannelUnread(c2.Id, uid); resp2.Err != nil { 283 t.Fatal(resp2.Err) 284 } else { 285 ch2 := resp2.Data.(*model.ChannelUnread) 286 if c2.Id != ch2.ChannelId { 287 t.Fatal("wrong channel id") 288 } 289 290 if teamId2 != ch2.TeamId { 291 t.Fatal("wrong team id") 292 } 293 294 if ch2.MentionCount != 5 { 295 t.Fatal("wrong MentionCount for channel 2") 296 } 297 298 if ch2.MsgCount != 10 { 299 t.Fatal("wrong MsgCount for channel 2") 300 } 301 } 302 } 303 304 func testChannelStoreGet(t *testing.T, ss store.Store) { 305 o1 := model.Channel{} 306 o1.TeamId = model.NewId() 307 o1.DisplayName = "Name" 308 o1.Name = "zz" + model.NewId() + "b" 309 o1.Type = model.CHANNEL_OPEN 310 store.Must(ss.Channel().Save(&o1, -1)) 311 312 if r1 := <-ss.Channel().Get(o1.Id, false); r1.Err != nil { 313 t.Fatal(r1.Err) 314 } else { 315 if r1.Data.(*model.Channel).ToJson() != o1.ToJson() { 316 t.Fatal("invalid returned channel") 317 } 318 } 319 320 if err := (<-ss.Channel().Get("", false)).Err; err == nil { 321 t.Fatal("Missing id should have failed") 322 } 323 324 u1 := &model.User{} 325 u1.Email = model.NewId() 326 u1.Nickname = model.NewId() 327 store.Must(ss.User().Save(u1)) 328 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u1.Id}, -1)) 329 330 u2 := model.User{} 331 u2.Email = model.NewId() 332 u2.Nickname = model.NewId() 333 store.Must(ss.User().Save(&u2)) 334 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u2.Id}, -1)) 335 336 o2 := model.Channel{} 337 o2.TeamId = model.NewId() 338 o2.DisplayName = "Direct Name" 339 o2.Name = "zz" + model.NewId() + "b" 340 o2.Type = model.CHANNEL_DIRECT 341 342 m1 := model.ChannelMember{} 343 m1.ChannelId = o2.Id 344 m1.UserId = u1.Id 345 m1.NotifyProps = model.GetDefaultChannelNotifyProps() 346 347 m2 := model.ChannelMember{} 348 m2.ChannelId = o2.Id 349 m2.UserId = u2.Id 350 m2.NotifyProps = model.GetDefaultChannelNotifyProps() 351 352 store.Must(ss.Channel().SaveDirectChannel(&o2, &m1, &m2)) 353 354 if r2 := <-ss.Channel().Get(o2.Id, false); r2.Err != nil { 355 t.Fatal(r2.Err) 356 } else { 357 if r2.Data.(*model.Channel).ToJson() != o2.ToJson() { 358 t.Fatal("invalid returned channel") 359 } 360 } 361 362 if r4 := <-ss.Channel().Get(o2.Id, true); r4.Err != nil { 363 t.Fatal(r4.Err) 364 } else { 365 if r4.Data.(*model.Channel).ToJson() != o2.ToJson() { 366 t.Fatal("invalid returned channel") 367 } 368 } 369 370 if r3 := <-ss.Channel().GetAll(o1.TeamId); r3.Err != nil { 371 t.Fatal(r3.Err) 372 } else { 373 channels := r3.Data.([]*model.Channel) 374 if len(channels) == 0 { 375 t.Fatal("too little") 376 } 377 } 378 379 if r3 := <-ss.Channel().GetTeamChannels(o1.TeamId); r3.Err != nil { 380 t.Fatal(r3.Err) 381 } else { 382 channels := r3.Data.(*model.ChannelList) 383 if len(*channels) == 0 { 384 t.Fatal("too little") 385 } 386 } 387 } 388 389 func testChannelStoreGetForPost(t *testing.T, ss store.Store) { 390 o1 := store.Must(ss.Channel().Save(&model.Channel{ 391 TeamId: model.NewId(), 392 DisplayName: "Name", 393 Name: "zz" + model.NewId() + "b", 394 Type: model.CHANNEL_OPEN, 395 }, -1)).(*model.Channel) 396 397 p1 := store.Must(ss.Post().Save(&model.Post{ 398 UserId: model.NewId(), 399 ChannelId: o1.Id, 400 Message: "test", 401 })).(*model.Post) 402 403 if r1 := <-ss.Channel().GetForPost(p1.Id); r1.Err != nil { 404 t.Fatal(r1.Err) 405 } else if r1.Data.(*model.Channel).Id != o1.Id { 406 t.Fatal("incorrect channel returned") 407 } 408 } 409 410 func testChannelStoreRestore(t *testing.T, ss store.Store) { 411 o1 := model.Channel{} 412 o1.TeamId = model.NewId() 413 o1.DisplayName = "Channel1" 414 o1.Name = "zz" + model.NewId() + "b" 415 o1.Type = model.CHANNEL_OPEN 416 store.Must(ss.Channel().Save(&o1, -1)) 417 418 if r := <-ss.Channel().Delete(o1.Id, model.GetMillis()); r.Err != nil { 419 t.Fatal(r.Err) 420 } 421 422 if r := <-ss.Channel().Get(o1.Id, false); r.Data.(*model.Channel).DeleteAt == 0 { 423 t.Fatal("should have been deleted") 424 } 425 426 if r := <-ss.Channel().Restore(o1.Id, model.GetMillis()); r.Err != nil { 427 t.Fatal(r.Err) 428 } 429 430 if r := <-ss.Channel().Get(o1.Id, false); r.Data.(*model.Channel).DeleteAt != 0 { 431 t.Fatal("should have been restored") 432 } 433 434 } 435 436 func testChannelStoreDelete(t *testing.T, ss store.Store) { 437 o1 := model.Channel{} 438 o1.TeamId = model.NewId() 439 o1.DisplayName = "Channel1" 440 o1.Name = "zz" + model.NewId() + "b" 441 o1.Type = model.CHANNEL_OPEN 442 store.Must(ss.Channel().Save(&o1, -1)) 443 444 o2 := model.Channel{} 445 o2.TeamId = o1.TeamId 446 o2.DisplayName = "Channel2" 447 o2.Name = "zz" + model.NewId() + "b" 448 o2.Type = model.CHANNEL_OPEN 449 store.Must(ss.Channel().Save(&o2, -1)) 450 451 o3 := model.Channel{} 452 o3.TeamId = o1.TeamId 453 o3.DisplayName = "Channel3" 454 o3.Name = "zz" + model.NewId() + "b" 455 o3.Type = model.CHANNEL_OPEN 456 store.Must(ss.Channel().Save(&o3, -1)) 457 458 o4 := model.Channel{} 459 o4.TeamId = o1.TeamId 460 o4.DisplayName = "Channel4" 461 o4.Name = "zz" + model.NewId() + "b" 462 o4.Type = model.CHANNEL_OPEN 463 store.Must(ss.Channel().Save(&o4, -1)) 464 465 m1 := model.ChannelMember{} 466 m1.ChannelId = o1.Id 467 m1.UserId = model.NewId() 468 m1.NotifyProps = model.GetDefaultChannelNotifyProps() 469 store.Must(ss.Channel().SaveMember(&m1)) 470 471 m2 := model.ChannelMember{} 472 m2.ChannelId = o2.Id 473 m2.UserId = m1.UserId 474 m2.NotifyProps = model.GetDefaultChannelNotifyProps() 475 store.Must(ss.Channel().SaveMember(&m2)) 476 477 if r := <-ss.Channel().Delete(o1.Id, model.GetMillis()); r.Err != nil { 478 t.Fatal(r.Err) 479 } 480 481 if r := <-ss.Channel().Get(o1.Id, false); r.Data.(*model.Channel).DeleteAt == 0 { 482 t.Fatal("should have been deleted") 483 } 484 485 if r := <-ss.Channel().Delete(o3.Id, model.GetMillis()); r.Err != nil { 486 t.Fatal(r.Err) 487 } 488 489 cresult := <-ss.Channel().GetChannels(o1.TeamId, m1.UserId) 490 list := cresult.Data.(*model.ChannelList) 491 492 if len(*list) != 1 { 493 t.Fatal("invalid number of channels") 494 } 495 496 cresult = <-ss.Channel().GetMoreChannels(o1.TeamId, m1.UserId, 0, 100) 497 list = cresult.Data.(*model.ChannelList) 498 499 if len(*list) != 1 { 500 t.Fatal("invalid number of channels") 501 } 502 503 <-ss.Channel().PermanentDelete(o2.Id) 504 505 cresult = <-ss.Channel().GetChannels(o1.TeamId, m1.UserId) 506 t.Log(cresult.Err) 507 if cresult.Err.Id != "store.sql_channel.get_channels.not_found.app_error" { 508 t.Fatal("no channels should be found") 509 } 510 511 if r := <-ss.Channel().PermanentDeleteByTeam(o1.TeamId); r.Err != nil { 512 t.Fatal(r.Err) 513 } 514 } 515 516 func testChannelStoreGetByName(t *testing.T, ss store.Store) { 517 o1 := model.Channel{} 518 o1.TeamId = model.NewId() 519 o1.DisplayName = "Name" 520 o1.Name = "zz" + model.NewId() + "b" 521 o1.Type = model.CHANNEL_OPEN 522 store.Must(ss.Channel().Save(&o1, -1)) 523 524 r1 := <-ss.Channel().GetByName(o1.TeamId, o1.Name, true) 525 if r1.Err != nil { 526 t.Fatal(r1.Err) 527 } else { 528 if r1.Data.(*model.Channel).ToJson() != o1.ToJson() { 529 t.Fatal("invalid returned channel") 530 } 531 } 532 533 if err := (<-ss.Channel().GetByName(o1.TeamId, "", true)).Err; err == nil { 534 t.Fatal("Missing id should have failed") 535 } 536 537 if r1 := <-ss.Channel().GetByName(o1.TeamId, o1.Name, false); r1.Err != nil { 538 t.Fatal(r1.Err) 539 } else { 540 if r1.Data.(*model.Channel).ToJson() != o1.ToJson() { 541 t.Fatal("invalid returned channel") 542 } 543 } 544 545 if err := (<-ss.Channel().GetByName(o1.TeamId, "", false)).Err; err == nil { 546 t.Fatal("Missing id should have failed") 547 } 548 549 store.Must(ss.Channel().Delete(r1.Data.(*model.Channel).Id, model.GetMillis())) 550 551 if err := (<-ss.Channel().GetByName(o1.TeamId, "", false)).Err; err == nil { 552 t.Fatal("Deleted channel should not be returned by GetByName()") 553 } 554 } 555 556 func testChannelStoreGetByNames(t *testing.T, ss store.Store) { 557 o1 := model.Channel{ 558 TeamId: model.NewId(), 559 DisplayName: "Name", 560 Name: "zz" + model.NewId() + "b", 561 Type: model.CHANNEL_OPEN, 562 } 563 store.Must(ss.Channel().Save(&o1, -1)) 564 565 o2 := model.Channel{ 566 TeamId: o1.TeamId, 567 DisplayName: "Name", 568 Name: "zz" + model.NewId() + "b", 569 Type: model.CHANNEL_OPEN, 570 } 571 store.Must(ss.Channel().Save(&o2, -1)) 572 573 for index, tc := range []struct { 574 TeamId string 575 Names []string 576 ExpectedIds []string 577 }{ 578 {o1.TeamId, []string{o1.Name}, []string{o1.Id}}, 579 {o1.TeamId, []string{o1.Name, o2.Name}, []string{o1.Id, o2.Id}}, 580 {o1.TeamId, nil, nil}, 581 {o1.TeamId, []string{"foo"}, nil}, 582 {o1.TeamId, []string{o1.Name, "foo", o2.Name, o2.Name}, []string{o1.Id, o2.Id}}, 583 {"", []string{o1.Name, "foo", o2.Name, o2.Name}, []string{o1.Id, o2.Id}}, 584 {"asd", []string{o1.Name, "foo", o2.Name, o2.Name}, nil}, 585 } { 586 r := <-ss.Channel().GetByNames(tc.TeamId, tc.Names, true) 587 require.Nil(t, r.Err) 588 channels := r.Data.([]*model.Channel) 589 var ids []string 590 for _, channel := range channels { 591 ids = append(ids, channel.Id) 592 } 593 sort.Strings(ids) 594 sort.Strings(tc.ExpectedIds) 595 assert.Equal(t, tc.ExpectedIds, ids, "tc %v", index) 596 } 597 598 store.Must(ss.Channel().Delete(o1.Id, model.GetMillis())) 599 store.Must(ss.Channel().Delete(o2.Id, model.GetMillis())) 600 601 r := <-ss.Channel().GetByNames(o1.TeamId, []string{o1.Name}, false) 602 require.Nil(t, r.Err) 603 channels := r.Data.([]*model.Channel) 604 assert.Len(t, channels, 0) 605 } 606 607 func testChannelStoreGetDeletedByName(t *testing.T, ss store.Store) { 608 o1 := model.Channel{} 609 o1.TeamId = model.NewId() 610 o1.DisplayName = "Name" 611 o1.Name = "zz" + model.NewId() + "b" 612 o1.Type = model.CHANNEL_OPEN 613 o1.DeleteAt = model.GetMillis() 614 store.Must(ss.Channel().Save(&o1, -1)) 615 616 if r1 := <-ss.Channel().GetDeletedByName(o1.TeamId, o1.Name); r1.Err != nil { 617 t.Fatal(r1.Err) 618 } else { 619 if r1.Data.(*model.Channel).ToJson() != o1.ToJson() { 620 t.Fatal("invalid returned channel") 621 } 622 } 623 624 if err := (<-ss.Channel().GetDeletedByName(o1.TeamId, "")).Err; err == nil { 625 t.Fatal("Missing id should have failed") 626 } 627 } 628 629 func testChannelStoreGetDeleted(t *testing.T, ss store.Store) { 630 o1 := model.Channel{} 631 o1.TeamId = model.NewId() 632 o1.DisplayName = "Channel1" 633 o1.Name = "zz" + model.NewId() + "b" 634 o1.Type = model.CHANNEL_OPEN 635 o1.DeleteAt = model.GetMillis() 636 store.Must(ss.Channel().Save(&o1, -1)) 637 638 cresult := <-ss.Channel().GetDeleted(o1.TeamId, 0, 100) 639 if cresult.Err != nil { 640 t.Fatal(cresult.Err) 641 } 642 list := cresult.Data.(*model.ChannelList) 643 644 if len(*list) != 1 { 645 t.Fatal("wrong list") 646 } 647 648 if (*list)[0].Name != o1.Name { 649 t.Fatal("missing channel") 650 } 651 652 o2 := model.Channel{} 653 o2.TeamId = o1.TeamId 654 o2.DisplayName = "Channel2" 655 o2.Name = "zz" + model.NewId() + "b" 656 o2.Type = model.CHANNEL_OPEN 657 store.Must(ss.Channel().Save(&o2, -1)) 658 659 cresult = <-ss.Channel().GetDeleted(o1.TeamId, 0, 100) 660 if cresult.Err != nil { 661 t.Fatal(cresult.Err) 662 } 663 list = cresult.Data.(*model.ChannelList) 664 665 if len(*list) != 1 { 666 t.Fatal("wrong list") 667 } 668 669 o3 := model.Channel{} 670 o3.TeamId = o1.TeamId 671 o3.DisplayName = "Channel3" 672 o3.Name = "zz" + model.NewId() + "b" 673 o3.Type = model.CHANNEL_OPEN 674 o3.DeleteAt = model.GetMillis() 675 store.Must(ss.Channel().Save(&o3, -1)) 676 677 cresult = <-ss.Channel().GetDeleted(o1.TeamId, 0, 100) 678 if cresult.Err != nil { 679 t.Fatal(cresult.Err) 680 } 681 list = cresult.Data.(*model.ChannelList) 682 683 if len(*list) != 2 { 684 t.Fatal("wrong list length") 685 } 686 687 cresult = <-ss.Channel().GetDeleted(o1.TeamId, 0, 1) 688 if cresult.Err != nil { 689 t.Fatal(cresult.Err) 690 } 691 list = cresult.Data.(*model.ChannelList) 692 693 if len(*list) != 1 { 694 t.Fatal("wrong list length") 695 } 696 697 cresult = <-ss.Channel().GetDeleted(o1.TeamId, 1, 1) 698 if cresult.Err != nil { 699 t.Fatal(cresult.Err) 700 } 701 list = cresult.Data.(*model.ChannelList) 702 703 if len(*list) != 1 { 704 t.Fatal("wrong list length") 705 } 706 707 } 708 709 func testChannelMemberStore(t *testing.T, ss store.Store) { 710 c1 := model.Channel{} 711 c1.TeamId = model.NewId() 712 c1.DisplayName = "NameName" 713 c1.Name = "zz" + model.NewId() + "b" 714 c1.Type = model.CHANNEL_OPEN 715 c1 = *store.Must(ss.Channel().Save(&c1, -1)).(*model.Channel) 716 717 c1t1 := (<-ss.Channel().Get(c1.Id, false)).Data.(*model.Channel) 718 t1 := c1t1.ExtraUpdateAt 719 720 u1 := model.User{} 721 u1.Email = model.NewId() 722 u1.Nickname = model.NewId() 723 store.Must(ss.User().Save(&u1)) 724 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u1.Id}, -1)) 725 726 u2 := model.User{} 727 u2.Email = model.NewId() 728 u2.Nickname = model.NewId() 729 store.Must(ss.User().Save(&u2)) 730 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u2.Id}, -1)) 731 732 o1 := model.ChannelMember{} 733 o1.ChannelId = c1.Id 734 o1.UserId = u1.Id 735 o1.NotifyProps = model.GetDefaultChannelNotifyProps() 736 store.Must(ss.Channel().SaveMember(&o1)) 737 738 o2 := model.ChannelMember{} 739 o2.ChannelId = c1.Id 740 o2.UserId = u2.Id 741 o2.NotifyProps = model.GetDefaultChannelNotifyProps() 742 store.Must(ss.Channel().SaveMember(&o2)) 743 744 c1t2 := (<-ss.Channel().Get(c1.Id, false)).Data.(*model.Channel) 745 t2 := c1t2.ExtraUpdateAt 746 747 if t2 <= t1 { 748 t.Fatal("Member update time incorrect") 749 } 750 751 count := (<-ss.Channel().GetMemberCount(o1.ChannelId, true)).Data.(int64) 752 if count != 2 { 753 t.Fatal("should have saved 2 members") 754 } 755 756 count = (<-ss.Channel().GetMemberCount(o1.ChannelId, true)).Data.(int64) 757 if count != 2 { 758 t.Fatal("should have saved 2 members") 759 } 760 761 if ss.Channel().GetMemberCountFromCache(o1.ChannelId) != 2 { 762 t.Fatal("should have saved 2 members") 763 } 764 765 if ss.Channel().GetMemberCountFromCache("junk") != 0 { 766 t.Fatal("should have saved 0 members") 767 } 768 769 count = (<-ss.Channel().GetMemberCount(o1.ChannelId, false)).Data.(int64) 770 if count != 2 { 771 t.Fatal("should have saved 2 members") 772 } 773 774 store.Must(ss.Channel().RemoveMember(o2.ChannelId, o2.UserId)) 775 776 count = (<-ss.Channel().GetMemberCount(o1.ChannelId, false)).Data.(int64) 777 if count != 1 { 778 t.Fatal("should have removed 1 member") 779 } 780 781 c1t3 := (<-ss.Channel().Get(c1.Id, false)).Data.(*model.Channel) 782 t3 := c1t3.ExtraUpdateAt 783 784 if t3 <= t2 || t3 <= t1 { 785 t.Fatal("Member update time incorrect on delete") 786 } 787 788 member := (<-ss.Channel().GetMember(o1.ChannelId, o1.UserId)).Data.(*model.ChannelMember) 789 if member.ChannelId != o1.ChannelId { 790 t.Fatal("should have go member") 791 } 792 793 if err := (<-ss.Channel().SaveMember(&o1)).Err; err == nil { 794 t.Fatal("Should have been a duplicate") 795 } 796 797 c1t4 := (<-ss.Channel().Get(c1.Id, false)).Data.(*model.Channel) 798 t4 := c1t4.ExtraUpdateAt 799 if t4 != t3 { 800 t.Fatal("Should not update time upon failure") 801 } 802 } 803 804 func testChannelDeleteMemberStore(t *testing.T, ss store.Store) { 805 c1 := model.Channel{} 806 c1.TeamId = model.NewId() 807 c1.DisplayName = "NameName" 808 c1.Name = "zz" + model.NewId() + "b" 809 c1.Type = model.CHANNEL_OPEN 810 c1 = *store.Must(ss.Channel().Save(&c1, -1)).(*model.Channel) 811 812 c1t1 := (<-ss.Channel().Get(c1.Id, false)).Data.(*model.Channel) 813 t1 := c1t1.ExtraUpdateAt 814 815 u1 := model.User{} 816 u1.Email = model.NewId() 817 u1.Nickname = model.NewId() 818 store.Must(ss.User().Save(&u1)) 819 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u1.Id}, -1)) 820 821 u2 := model.User{} 822 u2.Email = model.NewId() 823 u2.Nickname = model.NewId() 824 store.Must(ss.User().Save(&u2)) 825 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: model.NewId(), UserId: u2.Id}, -1)) 826 827 o1 := model.ChannelMember{} 828 o1.ChannelId = c1.Id 829 o1.UserId = u1.Id 830 o1.NotifyProps = model.GetDefaultChannelNotifyProps() 831 store.Must(ss.Channel().SaveMember(&o1)) 832 833 o2 := model.ChannelMember{} 834 o2.ChannelId = c1.Id 835 o2.UserId = u2.Id 836 o2.NotifyProps = model.GetDefaultChannelNotifyProps() 837 store.Must(ss.Channel().SaveMember(&o2)) 838 839 c1t2 := (<-ss.Channel().Get(c1.Id, false)).Data.(*model.Channel) 840 t2 := c1t2.ExtraUpdateAt 841 842 if t2 <= t1 { 843 t.Fatal("Member update time incorrect") 844 } 845 846 count := (<-ss.Channel().GetMemberCount(o1.ChannelId, false)).Data.(int64) 847 if count != 2 { 848 t.Fatal("should have saved 2 members") 849 } 850 851 store.Must(ss.Channel().PermanentDeleteMembersByUser(o2.UserId)) 852 853 count = (<-ss.Channel().GetMemberCount(o1.ChannelId, false)).Data.(int64) 854 if count != 1 { 855 t.Fatal("should have removed 1 member") 856 } 857 858 if r1 := <-ss.Channel().PermanentDeleteMembersByChannel(o1.ChannelId); r1.Err != nil { 859 t.Fatal(r1.Err) 860 } 861 862 count = (<-ss.Channel().GetMemberCount(o1.ChannelId, false)).Data.(int64) 863 if count != 0 { 864 t.Fatal("should have removed all members") 865 } 866 } 867 868 func testChannelStoreGetChannels(t *testing.T, ss store.Store) { 869 o2 := model.Channel{} 870 o2.TeamId = model.NewId() 871 o2.DisplayName = "Channel2" 872 o2.Name = "zz" + model.NewId() + "b" 873 o2.Type = model.CHANNEL_OPEN 874 store.Must(ss.Channel().Save(&o2, -1)) 875 876 o1 := model.Channel{} 877 o1.TeamId = model.NewId() 878 o1.DisplayName = "Channel1" 879 o1.Name = "zz" + model.NewId() + "b" 880 o1.Type = model.CHANNEL_OPEN 881 store.Must(ss.Channel().Save(&o1, -1)) 882 883 m1 := model.ChannelMember{} 884 m1.ChannelId = o1.Id 885 m1.UserId = model.NewId() 886 m1.NotifyProps = model.GetDefaultChannelNotifyProps() 887 store.Must(ss.Channel().SaveMember(&m1)) 888 889 m2 := model.ChannelMember{} 890 m2.ChannelId = o1.Id 891 m2.UserId = model.NewId() 892 m2.NotifyProps = model.GetDefaultChannelNotifyProps() 893 store.Must(ss.Channel().SaveMember(&m2)) 894 895 m3 := model.ChannelMember{} 896 m3.ChannelId = o2.Id 897 m3.UserId = model.NewId() 898 m3.NotifyProps = model.GetDefaultChannelNotifyProps() 899 store.Must(ss.Channel().SaveMember(&m3)) 900 901 cresult := <-ss.Channel().GetChannels(o1.TeamId, m1.UserId) 902 list := cresult.Data.(*model.ChannelList) 903 904 if (*list)[0].Id != o1.Id { 905 t.Fatal("missing channel") 906 } 907 908 acresult := <-ss.Channel().GetAllChannelMembersForUser(m1.UserId, false) 909 ids := acresult.Data.(map[string]string) 910 if _, ok := ids[o1.Id]; !ok { 911 t.Fatal("missing channel") 912 } 913 914 acresult2 := <-ss.Channel().GetAllChannelMembersForUser(m1.UserId, true) 915 ids2 := acresult2.Data.(map[string]string) 916 if _, ok := ids2[o1.Id]; !ok { 917 t.Fatal("missing channel") 918 } 919 920 acresult3 := <-ss.Channel().GetAllChannelMembersForUser(m1.UserId, true) 921 ids3 := acresult3.Data.(map[string]string) 922 if _, ok := ids3[o1.Id]; !ok { 923 t.Fatal("missing channel") 924 } 925 926 if !ss.Channel().IsUserInChannelUseCache(m1.UserId, o1.Id) { 927 t.Fatal("missing channel") 928 } 929 930 if ss.Channel().IsUserInChannelUseCache(m1.UserId, o2.Id) { 931 t.Fatal("missing channel") 932 } 933 934 if ss.Channel().IsUserInChannelUseCache(m1.UserId, "blahblah") { 935 t.Fatal("missing channel") 936 } 937 938 if ss.Channel().IsUserInChannelUseCache("blahblah", "blahblah") { 939 t.Fatal("missing channel") 940 } 941 942 ss.Channel().InvalidateAllChannelMembersForUser(m1.UserId) 943 } 944 945 func testChannelStoreGetMoreChannels(t *testing.T, ss store.Store) { 946 o1 := model.Channel{} 947 o1.TeamId = model.NewId() 948 o1.DisplayName = "Channel1" 949 o1.Name = "zz" + model.NewId() + "b" 950 o1.Type = model.CHANNEL_OPEN 951 store.Must(ss.Channel().Save(&o1, -1)) 952 953 o2 := model.Channel{} 954 o2.TeamId = model.NewId() 955 o2.DisplayName = "Channel2" 956 o2.Name = "zz" + model.NewId() + "b" 957 o2.Type = model.CHANNEL_OPEN 958 store.Must(ss.Channel().Save(&o2, -1)) 959 960 m1 := model.ChannelMember{} 961 m1.ChannelId = o1.Id 962 m1.UserId = model.NewId() 963 m1.NotifyProps = model.GetDefaultChannelNotifyProps() 964 store.Must(ss.Channel().SaveMember(&m1)) 965 966 m2 := model.ChannelMember{} 967 m2.ChannelId = o1.Id 968 m2.UserId = model.NewId() 969 m2.NotifyProps = model.GetDefaultChannelNotifyProps() 970 store.Must(ss.Channel().SaveMember(&m2)) 971 972 m3 := model.ChannelMember{} 973 m3.ChannelId = o2.Id 974 m3.UserId = model.NewId() 975 m3.NotifyProps = model.GetDefaultChannelNotifyProps() 976 store.Must(ss.Channel().SaveMember(&m3)) 977 978 o3 := model.Channel{} 979 o3.TeamId = o1.TeamId 980 o3.DisplayName = "ChannelA" 981 o3.Name = "zz" + model.NewId() + "b" 982 o3.Type = model.CHANNEL_OPEN 983 store.Must(ss.Channel().Save(&o3, -1)) 984 985 o4 := model.Channel{} 986 o4.TeamId = o1.TeamId 987 o4.DisplayName = "ChannelB" 988 o4.Name = "zz" + model.NewId() + "b" 989 o4.Type = model.CHANNEL_PRIVATE 990 store.Must(ss.Channel().Save(&o4, -1)) 991 992 o5 := model.Channel{} 993 o5.TeamId = o1.TeamId 994 o5.DisplayName = "ChannelC" 995 o5.Name = "zz" + model.NewId() + "b" 996 o5.Type = model.CHANNEL_PRIVATE 997 store.Must(ss.Channel().Save(&o5, -1)) 998 999 cresult := <-ss.Channel().GetMoreChannels(o1.TeamId, m1.UserId, 0, 100) 1000 if cresult.Err != nil { 1001 t.Fatal(cresult.Err) 1002 } 1003 list := cresult.Data.(*model.ChannelList) 1004 1005 if len(*list) != 1 { 1006 t.Fatal("wrong list") 1007 } 1008 1009 if (*list)[0].Name != o3.Name { 1010 t.Fatal("missing channel") 1011 } 1012 1013 o6 := model.Channel{} 1014 o6.TeamId = o1.TeamId 1015 o6.DisplayName = "ChannelA" 1016 o6.Name = "zz" + model.NewId() + "b" 1017 o6.Type = model.CHANNEL_OPEN 1018 store.Must(ss.Channel().Save(&o6, -1)) 1019 1020 cresult = <-ss.Channel().GetMoreChannels(o1.TeamId, m1.UserId, 0, 100) 1021 list = cresult.Data.(*model.ChannelList) 1022 1023 if len(*list) != 2 { 1024 t.Fatal("wrong list length") 1025 } 1026 1027 cresult = <-ss.Channel().GetMoreChannels(o1.TeamId, m1.UserId, 0, 1) 1028 list = cresult.Data.(*model.ChannelList) 1029 1030 if len(*list) != 1 { 1031 t.Fatal("wrong list length") 1032 } 1033 1034 cresult = <-ss.Channel().GetMoreChannels(o1.TeamId, m1.UserId, 1, 1) 1035 list = cresult.Data.(*model.ChannelList) 1036 1037 if len(*list) != 1 { 1038 t.Fatal("wrong list length") 1039 } 1040 1041 if r1 := <-ss.Channel().AnalyticsTypeCount(o1.TeamId, model.CHANNEL_OPEN); r1.Err != nil { 1042 t.Fatal(r1.Err) 1043 } else { 1044 if r1.Data.(int64) != 3 { 1045 t.Log(r1.Data) 1046 t.Fatal("wrong value") 1047 } 1048 } 1049 1050 if r1 := <-ss.Channel().AnalyticsTypeCount(o1.TeamId, model.CHANNEL_PRIVATE); r1.Err != nil { 1051 t.Fatal(r1.Err) 1052 } else { 1053 if r1.Data.(int64) != 2 { 1054 t.Log(r1.Data) 1055 t.Fatal("wrong value") 1056 } 1057 } 1058 } 1059 1060 func testChannelStoreGetPublicChannelsForTeam(t *testing.T, ss store.Store) { 1061 o1 := model.Channel{} 1062 o1.TeamId = model.NewId() 1063 o1.DisplayName = "OpenChannel1Team1" 1064 o1.Name = "zz" + model.NewId() + "b" 1065 o1.Type = model.CHANNEL_OPEN 1066 store.Must(ss.Channel().Save(&o1, -1)) 1067 1068 o2 := model.Channel{} 1069 o2.TeamId = model.NewId() 1070 o2.DisplayName = "OpenChannel1Team2" 1071 o2.Name = "zz" + model.NewId() + "b" 1072 o2.Type = model.CHANNEL_OPEN 1073 store.Must(ss.Channel().Save(&o2, -1)) 1074 1075 o3 := model.Channel{} 1076 o3.TeamId = o1.TeamId 1077 o3.DisplayName = "PrivateChannel1Team1" 1078 o3.Name = "zz" + model.NewId() + "b" 1079 o3.Type = model.CHANNEL_PRIVATE 1080 store.Must(ss.Channel().Save(&o3, -1)) 1081 1082 cresult := <-ss.Channel().GetPublicChannelsForTeam(o1.TeamId, 0, 100) 1083 if cresult.Err != nil { 1084 t.Fatal(cresult.Err) 1085 } 1086 list := cresult.Data.(*model.ChannelList) 1087 1088 if len(*list) != 1 { 1089 t.Fatal("wrong list") 1090 } 1091 1092 if (*list)[0].Name != o1.Name { 1093 t.Fatal("missing channel") 1094 } 1095 1096 o4 := model.Channel{} 1097 o4.TeamId = o1.TeamId 1098 o4.DisplayName = "OpenChannel2Team1" 1099 o4.Name = "zz" + model.NewId() + "b" 1100 o4.Type = model.CHANNEL_OPEN 1101 store.Must(ss.Channel().Save(&o4, -1)) 1102 1103 cresult = <-ss.Channel().GetPublicChannelsForTeam(o1.TeamId, 0, 100) 1104 list = cresult.Data.(*model.ChannelList) 1105 1106 if len(*list) != 2 { 1107 t.Fatal("wrong list length") 1108 } 1109 1110 cresult = <-ss.Channel().GetPublicChannelsForTeam(o1.TeamId, 0, 1) 1111 list = cresult.Data.(*model.ChannelList) 1112 1113 if len(*list) != 1 { 1114 t.Fatal("wrong list length") 1115 } 1116 1117 cresult = <-ss.Channel().GetPublicChannelsForTeam(o1.TeamId, 1, 1) 1118 list = cresult.Data.(*model.ChannelList) 1119 1120 if len(*list) != 1 { 1121 t.Fatal("wrong list length") 1122 } 1123 1124 if r1 := <-ss.Channel().AnalyticsTypeCount(o1.TeamId, model.CHANNEL_OPEN); r1.Err != nil { 1125 t.Fatal(r1.Err) 1126 } else { 1127 if r1.Data.(int64) != 2 { 1128 t.Log(r1.Data) 1129 t.Fatal("wrong value") 1130 } 1131 } 1132 1133 if r1 := <-ss.Channel().AnalyticsTypeCount(o1.TeamId, model.CHANNEL_PRIVATE); r1.Err != nil { 1134 t.Fatal(r1.Err) 1135 } else { 1136 if r1.Data.(int64) != 1 { 1137 t.Log(r1.Data) 1138 t.Fatal("wrong value") 1139 } 1140 } 1141 } 1142 1143 func testChannelStoreGetPublicChannelsByIdsForTeam(t *testing.T, ss store.Store) { 1144 teamId1 := model.NewId() 1145 1146 oc1 := model.Channel{} 1147 oc1.TeamId = teamId1 1148 oc1.DisplayName = "OpenChannel1Team1" 1149 oc1.Name = "zz" + model.NewId() + "b" 1150 oc1.Type = model.CHANNEL_OPEN 1151 store.Must(ss.Channel().Save(&oc1, -1)) 1152 1153 oc2 := model.Channel{} 1154 oc2.TeamId = model.NewId() 1155 oc2.DisplayName = "OpenChannel2TeamOther" 1156 oc2.Name = "zz" + model.NewId() + "b" 1157 oc2.Type = model.CHANNEL_OPEN 1158 store.Must(ss.Channel().Save(&oc2, -1)) 1159 1160 pc3 := model.Channel{} 1161 pc3.TeamId = teamId1 1162 pc3.DisplayName = "PrivateChannel3Team1" 1163 pc3.Name = "zz" + model.NewId() + "b" 1164 pc3.Type = model.CHANNEL_PRIVATE 1165 store.Must(ss.Channel().Save(&pc3, -1)) 1166 1167 cids := []string{oc1.Id} 1168 cresult := <-ss.Channel().GetPublicChannelsByIdsForTeam(teamId1, cids) 1169 list := cresult.Data.(*model.ChannelList) 1170 1171 if len(*list) != 1 { 1172 t.Fatal("should return 1 channel") 1173 } 1174 1175 if (*list)[0].Id != oc1.Id { 1176 t.Fatal("missing channel") 1177 } 1178 1179 cids = append(cids, oc2.Id) 1180 cids = append(cids, model.NewId()) 1181 cids = append(cids, pc3.Id) 1182 cresult = <-ss.Channel().GetPublicChannelsByIdsForTeam(teamId1, cids) 1183 list = cresult.Data.(*model.ChannelList) 1184 1185 if len(*list) != 1 { 1186 t.Fatal("should return 1 channel") 1187 } 1188 1189 oc4 := model.Channel{} 1190 oc4.TeamId = teamId1 1191 oc4.DisplayName = "OpenChannel4Team1" 1192 oc4.Name = "zz" + model.NewId() + "b" 1193 oc4.Type = model.CHANNEL_OPEN 1194 store.Must(ss.Channel().Save(&oc4, -1)) 1195 1196 cids = append(cids, oc4.Id) 1197 cresult = <-ss.Channel().GetPublicChannelsByIdsForTeam(teamId1, cids) 1198 list = cresult.Data.(*model.ChannelList) 1199 1200 if len(*list) != 2 { 1201 t.Fatal("should return 2 channels") 1202 } 1203 1204 if (*list)[0].Id != oc1.Id { 1205 t.Fatal("missing channel") 1206 } 1207 1208 if (*list)[1].Id != oc4.Id { 1209 t.Fatal("missing channel") 1210 } 1211 1212 cids = cids[:0] 1213 cids = append(cids, model.NewId()) 1214 cresult = <-ss.Channel().GetPublicChannelsByIdsForTeam(teamId1, cids) 1215 list = cresult.Data.(*model.ChannelList) 1216 1217 if len(*list) != 0 { 1218 t.Fatal("should not return a channel") 1219 } 1220 } 1221 1222 func testChannelStoreGetChannelCounts(t *testing.T, ss store.Store) { 1223 o2 := model.Channel{} 1224 o2.TeamId = model.NewId() 1225 o2.DisplayName = "Channel2" 1226 o2.Name = "zz" + model.NewId() + "b" 1227 o2.Type = model.CHANNEL_OPEN 1228 store.Must(ss.Channel().Save(&o2, -1)) 1229 1230 o1 := model.Channel{} 1231 o1.TeamId = model.NewId() 1232 o1.DisplayName = "Channel1" 1233 o1.Name = "zz" + model.NewId() + "b" 1234 o1.Type = model.CHANNEL_OPEN 1235 store.Must(ss.Channel().Save(&o1, -1)) 1236 1237 m1 := model.ChannelMember{} 1238 m1.ChannelId = o1.Id 1239 m1.UserId = model.NewId() 1240 m1.NotifyProps = model.GetDefaultChannelNotifyProps() 1241 store.Must(ss.Channel().SaveMember(&m1)) 1242 1243 m2 := model.ChannelMember{} 1244 m2.ChannelId = o1.Id 1245 m2.UserId = model.NewId() 1246 m2.NotifyProps = model.GetDefaultChannelNotifyProps() 1247 store.Must(ss.Channel().SaveMember(&m2)) 1248 1249 m3 := model.ChannelMember{} 1250 m3.ChannelId = o2.Id 1251 m3.UserId = model.NewId() 1252 m3.NotifyProps = model.GetDefaultChannelNotifyProps() 1253 store.Must(ss.Channel().SaveMember(&m3)) 1254 1255 cresult := <-ss.Channel().GetChannelCounts(o1.TeamId, m1.UserId) 1256 counts := cresult.Data.(*model.ChannelCounts) 1257 1258 if len(counts.Counts) != 1 { 1259 t.Fatal("wrong number of counts") 1260 } 1261 1262 if len(counts.UpdateTimes) != 1 { 1263 t.Fatal("wrong number of update times") 1264 } 1265 } 1266 1267 func testChannelStoreGetMembersForUser(t *testing.T, ss store.Store) { 1268 t1 := model.Team{} 1269 t1.DisplayName = "Name" 1270 t1.Name = model.NewId() 1271 t1.Email = model.NewId() + "@nowhere.com" 1272 t1.Type = model.TEAM_OPEN 1273 store.Must(ss.Team().Save(&t1)) 1274 1275 o1 := model.Channel{} 1276 o1.TeamId = t1.Id 1277 o1.DisplayName = "Channel1" 1278 o1.Name = "zz" + model.NewId() + "b" 1279 o1.Type = model.CHANNEL_OPEN 1280 store.Must(ss.Channel().Save(&o1, -1)) 1281 1282 o2 := model.Channel{} 1283 o2.TeamId = o1.TeamId 1284 o2.DisplayName = "Channel2" 1285 o2.Name = "zz" + model.NewId() + "b" 1286 o2.Type = model.CHANNEL_OPEN 1287 store.Must(ss.Channel().Save(&o2, -1)) 1288 1289 m1 := model.ChannelMember{} 1290 m1.ChannelId = o1.Id 1291 m1.UserId = model.NewId() 1292 m1.NotifyProps = model.GetDefaultChannelNotifyProps() 1293 store.Must(ss.Channel().SaveMember(&m1)) 1294 1295 m2 := model.ChannelMember{} 1296 m2.ChannelId = o2.Id 1297 m2.UserId = m1.UserId 1298 m2.NotifyProps = model.GetDefaultChannelNotifyProps() 1299 store.Must(ss.Channel().SaveMember(&m2)) 1300 1301 cresult := <-ss.Channel().GetMembersForUser(o1.TeamId, m1.UserId) 1302 members := cresult.Data.(*model.ChannelMembers) 1303 1304 // no unread messages 1305 if len(*members) != 2 { 1306 t.Fatal("wrong number of members") 1307 } 1308 } 1309 1310 func testChannelStoreUpdateLastViewedAt(t *testing.T, ss store.Store) { 1311 o1 := model.Channel{} 1312 o1.TeamId = model.NewId() 1313 o1.DisplayName = "Channel1" 1314 o1.Name = "zz" + model.NewId() + "b" 1315 o1.Type = model.CHANNEL_OPEN 1316 o1.TotalMsgCount = 25 1317 o1.LastPostAt = 12345 1318 store.Must(ss.Channel().Save(&o1, -1)) 1319 1320 m1 := model.ChannelMember{} 1321 m1.ChannelId = o1.Id 1322 m1.UserId = model.NewId() 1323 m1.NotifyProps = model.GetDefaultChannelNotifyProps() 1324 store.Must(ss.Channel().SaveMember(&m1)) 1325 1326 o2 := model.Channel{} 1327 o2.TeamId = model.NewId() 1328 o2.DisplayName = "Channel1" 1329 o2.Name = "zz" + model.NewId() + "c" 1330 o2.Type = model.CHANNEL_OPEN 1331 o2.TotalMsgCount = 26 1332 o2.LastPostAt = 123456 1333 store.Must(ss.Channel().Save(&o2, -1)) 1334 1335 m2 := model.ChannelMember{} 1336 m2.ChannelId = o2.Id 1337 m2.UserId = m1.UserId 1338 m2.NotifyProps = model.GetDefaultChannelNotifyProps() 1339 store.Must(ss.Channel().SaveMember(&m2)) 1340 1341 if result := <-ss.Channel().UpdateLastViewedAt([]string{m1.ChannelId}, m1.UserId); result.Err != nil { 1342 t.Fatal("failed to update", result.Err) 1343 } else if result.Data.(map[string]int64)[o1.Id] != o1.LastPostAt { 1344 t.Fatal("last viewed at time incorrect") 1345 } 1346 1347 if result := <-ss.Channel().UpdateLastViewedAt([]string{m1.ChannelId, m2.ChannelId}, m1.UserId); result.Err != nil { 1348 t.Fatal("failed to update", result.Err) 1349 } else if result.Data.(map[string]int64)[o2.Id] != o2.LastPostAt { 1350 t.Fatal("last viewed at time incorrect") 1351 } 1352 1353 rm1 := store.Must(ss.Channel().GetMember(m1.ChannelId, m1.UserId)).(*model.ChannelMember) 1354 assert.Equal(t, rm1.LastViewedAt, o1.LastPostAt) 1355 assert.Equal(t, rm1.LastUpdateAt, o1.LastPostAt) 1356 assert.Equal(t, rm1.MsgCount, o1.TotalMsgCount) 1357 1358 rm2 := store.Must(ss.Channel().GetMember(m2.ChannelId, m2.UserId)).(*model.ChannelMember) 1359 assert.Equal(t, rm2.LastViewedAt, o2.LastPostAt) 1360 assert.Equal(t, rm2.LastUpdateAt, o2.LastPostAt) 1361 assert.Equal(t, rm2.MsgCount, o2.TotalMsgCount) 1362 1363 if result := <-ss.Channel().UpdateLastViewedAt([]string{m1.ChannelId}, "missing id"); result.Err != nil { 1364 t.Fatal("failed to update") 1365 } 1366 } 1367 1368 func testChannelStoreIncrementMentionCount(t *testing.T, ss store.Store) { 1369 o1 := model.Channel{} 1370 o1.TeamId = model.NewId() 1371 o1.DisplayName = "Channel1" 1372 o1.Name = "zz" + model.NewId() + "b" 1373 o1.Type = model.CHANNEL_OPEN 1374 o1.TotalMsgCount = 25 1375 store.Must(ss.Channel().Save(&o1, -1)) 1376 1377 m1 := model.ChannelMember{} 1378 m1.ChannelId = o1.Id 1379 m1.UserId = model.NewId() 1380 m1.NotifyProps = model.GetDefaultChannelNotifyProps() 1381 store.Must(ss.Channel().SaveMember(&m1)) 1382 1383 err := (<-ss.Channel().IncrementMentionCount(m1.ChannelId, m1.UserId)).Err 1384 if err != nil { 1385 t.Fatal("failed to update") 1386 } 1387 1388 err = (<-ss.Channel().IncrementMentionCount(m1.ChannelId, "missing id")).Err 1389 if err != nil { 1390 t.Fatal("failed to update") 1391 } 1392 1393 err = (<-ss.Channel().IncrementMentionCount("missing id", m1.UserId)).Err 1394 if err != nil { 1395 t.Fatal("failed to update") 1396 } 1397 1398 err = (<-ss.Channel().IncrementMentionCount("missing id", "missing id")).Err 1399 if err != nil { 1400 t.Fatal("failed to update") 1401 } 1402 } 1403 1404 func testUpdateChannelMember(t *testing.T, ss store.Store) { 1405 userId := model.NewId() 1406 1407 c1 := &model.Channel{ 1408 TeamId: model.NewId(), 1409 DisplayName: model.NewId(), 1410 Name: model.NewId(), 1411 Type: model.CHANNEL_OPEN, 1412 } 1413 store.Must(ss.Channel().Save(c1, -1)) 1414 1415 m1 := &model.ChannelMember{ 1416 ChannelId: c1.Id, 1417 UserId: userId, 1418 NotifyProps: model.GetDefaultChannelNotifyProps(), 1419 } 1420 store.Must(ss.Channel().SaveMember(m1)) 1421 1422 m1.NotifyProps["test"] = "sometext" 1423 if result := <-ss.Channel().UpdateMember(m1); result.Err != nil { 1424 t.Fatal(result.Err) 1425 } 1426 1427 m1.UserId = "" 1428 if result := <-ss.Channel().UpdateMember(m1); result.Err == nil { 1429 t.Fatal("bad user id - should fail") 1430 } 1431 } 1432 1433 func testGetMember(t *testing.T, ss store.Store) { 1434 userId := model.NewId() 1435 1436 c1 := &model.Channel{ 1437 TeamId: model.NewId(), 1438 DisplayName: model.NewId(), 1439 Name: model.NewId(), 1440 Type: model.CHANNEL_OPEN, 1441 } 1442 store.Must(ss.Channel().Save(c1, -1)) 1443 1444 c2 := &model.Channel{ 1445 TeamId: c1.TeamId, 1446 DisplayName: model.NewId(), 1447 Name: model.NewId(), 1448 Type: model.CHANNEL_OPEN, 1449 } 1450 store.Must(ss.Channel().Save(c2, -1)) 1451 1452 m1 := &model.ChannelMember{ 1453 ChannelId: c1.Id, 1454 UserId: userId, 1455 NotifyProps: model.GetDefaultChannelNotifyProps(), 1456 } 1457 store.Must(ss.Channel().SaveMember(m1)) 1458 1459 m2 := &model.ChannelMember{ 1460 ChannelId: c2.Id, 1461 UserId: userId, 1462 NotifyProps: model.GetDefaultChannelNotifyProps(), 1463 } 1464 store.Must(ss.Channel().SaveMember(m2)) 1465 1466 if result := <-ss.Channel().GetMember(model.NewId(), userId); result.Err == nil { 1467 t.Fatal("should've failed to get member for non-existant channel") 1468 } 1469 1470 if result := <-ss.Channel().GetMember(c1.Id, model.NewId()); result.Err == nil { 1471 t.Fatal("should've failed to get member for non-existant user") 1472 } 1473 1474 if result := <-ss.Channel().GetMember(c1.Id, userId); result.Err != nil { 1475 t.Fatal("shouldn't have errored when getting member", result.Err) 1476 } else if member := result.Data.(*model.ChannelMember); member.ChannelId != c1.Id { 1477 t.Fatal("should've gotten member of channel 1") 1478 } else if member.UserId != userId { 1479 t.Fatal("should've gotten member for user") 1480 } 1481 1482 if result := <-ss.Channel().GetMember(c2.Id, userId); result.Err != nil { 1483 t.Fatal("shouldn't have errored when getting member", result.Err) 1484 } else if member := result.Data.(*model.ChannelMember); member.ChannelId != c2.Id { 1485 t.Fatal("should've gotten member of channel 2") 1486 } else if member.UserId != userId { 1487 t.Fatal("should've gotten member for user") 1488 } 1489 1490 if result := <-ss.Channel().GetAllChannelMembersNotifyPropsForChannel(c2.Id, false); result.Err != nil { 1491 t.Fatal(result.Err) 1492 } else { 1493 props := result.Data.(map[string]model.StringMap) 1494 if len(props) == 0 { 1495 t.Fatal("should not be empty") 1496 } 1497 } 1498 1499 if result := <-ss.Channel().GetAllChannelMembersNotifyPropsForChannel(c2.Id, true); result.Err != nil { 1500 t.Fatal(result.Err) 1501 } else { 1502 props := result.Data.(map[string]model.StringMap) 1503 if len(props) == 0 { 1504 t.Fatal("should not be empty") 1505 } 1506 } 1507 1508 ss.Channel().InvalidateCacheForChannelMembersNotifyProps(c2.Id) 1509 } 1510 1511 func testChannelStoreGetMemberForPost(t *testing.T, ss store.Store) { 1512 o1 := store.Must(ss.Channel().Save(&model.Channel{ 1513 TeamId: model.NewId(), 1514 DisplayName: "Name", 1515 Name: "zz" + model.NewId() + "b", 1516 Type: model.CHANNEL_OPEN, 1517 }, -1)).(*model.Channel) 1518 1519 m1 := store.Must(ss.Channel().SaveMember(&model.ChannelMember{ 1520 ChannelId: o1.Id, 1521 UserId: model.NewId(), 1522 NotifyProps: model.GetDefaultChannelNotifyProps(), 1523 })).(*model.ChannelMember) 1524 1525 p1 := store.Must(ss.Post().Save(&model.Post{ 1526 UserId: model.NewId(), 1527 ChannelId: o1.Id, 1528 Message: "test", 1529 })).(*model.Post) 1530 1531 if r1 := <-ss.Channel().GetMemberForPost(p1.Id, m1.UserId); r1.Err != nil { 1532 t.Fatal(r1.Err) 1533 } else if r1.Data.(*model.ChannelMember).ToJson() != m1.ToJson() { 1534 t.Fatal("invalid returned channel member") 1535 } 1536 1537 if r2 := <-ss.Channel().GetMemberForPost(p1.Id, model.NewId()); r2.Err == nil { 1538 t.Fatal("shouldn't have returned a member") 1539 } 1540 } 1541 1542 func testGetMemberCount(t *testing.T, ss store.Store) { 1543 teamId := model.NewId() 1544 1545 c1 := model.Channel{ 1546 TeamId: teamId, 1547 DisplayName: "Channel1", 1548 Name: "zz" + model.NewId() + "b", 1549 Type: model.CHANNEL_OPEN, 1550 } 1551 store.Must(ss.Channel().Save(&c1, -1)) 1552 1553 c2 := model.Channel{ 1554 TeamId: teamId, 1555 DisplayName: "Channel2", 1556 Name: "zz" + model.NewId() + "b", 1557 Type: model.CHANNEL_OPEN, 1558 } 1559 store.Must(ss.Channel().Save(&c2, -1)) 1560 1561 u1 := &model.User{ 1562 Email: model.NewId(), 1563 DeleteAt: 0, 1564 } 1565 store.Must(ss.User().Save(u1)) 1566 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u1.Id}, -1)) 1567 1568 m1 := model.ChannelMember{ 1569 ChannelId: c1.Id, 1570 UserId: u1.Id, 1571 NotifyProps: model.GetDefaultChannelNotifyProps(), 1572 } 1573 store.Must(ss.Channel().SaveMember(&m1)) 1574 1575 if result := <-ss.Channel().GetMemberCount(c1.Id, false); result.Err != nil { 1576 t.Fatalf("failed to get member count: %v", result.Err) 1577 } else if result.Data.(int64) != 1 { 1578 t.Fatalf("got incorrect member count %v", result.Data) 1579 } 1580 1581 u2 := model.User{ 1582 Email: model.NewId(), 1583 DeleteAt: 0, 1584 } 1585 store.Must(ss.User().Save(&u2)) 1586 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u2.Id}, -1)) 1587 1588 m2 := model.ChannelMember{ 1589 ChannelId: c1.Id, 1590 UserId: u2.Id, 1591 NotifyProps: model.GetDefaultChannelNotifyProps(), 1592 } 1593 store.Must(ss.Channel().SaveMember(&m2)) 1594 1595 if result := <-ss.Channel().GetMemberCount(c1.Id, false); result.Err != nil { 1596 t.Fatalf("failed to get member count: %v", result.Err) 1597 } else if result.Data.(int64) != 2 { 1598 t.Fatalf("got incorrect member count %v", result.Data) 1599 } 1600 1601 // make sure members of other channels aren't counted 1602 u3 := model.User{ 1603 Email: model.NewId(), 1604 DeleteAt: 0, 1605 } 1606 store.Must(ss.User().Save(&u3)) 1607 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u3.Id}, -1)) 1608 1609 m3 := model.ChannelMember{ 1610 ChannelId: c2.Id, 1611 UserId: u3.Id, 1612 NotifyProps: model.GetDefaultChannelNotifyProps(), 1613 } 1614 store.Must(ss.Channel().SaveMember(&m3)) 1615 1616 if result := <-ss.Channel().GetMemberCount(c1.Id, false); result.Err != nil { 1617 t.Fatalf("failed to get member count: %v", result.Err) 1618 } else if result.Data.(int64) != 2 { 1619 t.Fatalf("got incorrect member count %v", result.Data) 1620 } 1621 1622 // make sure inactive users aren't counted 1623 u4 := &model.User{ 1624 Email: model.NewId(), 1625 DeleteAt: 10000, 1626 } 1627 store.Must(ss.User().Save(u4)) 1628 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u4.Id}, -1)) 1629 1630 m4 := model.ChannelMember{ 1631 ChannelId: c1.Id, 1632 UserId: u4.Id, 1633 NotifyProps: model.GetDefaultChannelNotifyProps(), 1634 } 1635 store.Must(ss.Channel().SaveMember(&m4)) 1636 1637 if result := <-ss.Channel().GetMemberCount(c1.Id, false); result.Err != nil { 1638 t.Fatalf("failed to get member count: %v", result.Err) 1639 } else if result.Data.(int64) != 2 { 1640 t.Fatalf("got incorrect member count %v", result.Data) 1641 } 1642 } 1643 1644 func testUpdateExtrasByUser(t *testing.T, ss store.Store) { 1645 teamId := model.NewId() 1646 1647 c1 := model.Channel{ 1648 TeamId: teamId, 1649 DisplayName: "Channel1", 1650 Name: "zz" + model.NewId() + "b", 1651 Type: model.CHANNEL_OPEN, 1652 } 1653 store.Must(ss.Channel().Save(&c1, -1)) 1654 1655 c2 := model.Channel{ 1656 TeamId: teamId, 1657 DisplayName: "Channel2", 1658 Name: "zz" + model.NewId() + "b", 1659 Type: model.CHANNEL_OPEN, 1660 } 1661 store.Must(ss.Channel().Save(&c2, -1)) 1662 1663 u1 := &model.User{ 1664 Email: model.NewId(), 1665 DeleteAt: 0, 1666 } 1667 store.Must(ss.User().Save(u1)) 1668 store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: u1.Id}, -1)) 1669 1670 m1 := model.ChannelMember{ 1671 ChannelId: c1.Id, 1672 UserId: u1.Id, 1673 NotifyProps: model.GetDefaultChannelNotifyProps(), 1674 } 1675 store.Must(ss.Channel().SaveMember(&m1)) 1676 1677 u1.DeleteAt = model.GetMillis() 1678 store.Must(ss.User().Update(u1, true)) 1679 1680 if result := <-ss.Channel().ExtraUpdateByUser(u1.Id, u1.DeleteAt); result.Err != nil { 1681 t.Fatalf("failed to update extras by user: %v", result.Err) 1682 } 1683 1684 u1.DeleteAt = 0 1685 store.Must(ss.User().Update(u1, true)) 1686 1687 if result := <-ss.Channel().ExtraUpdateByUser(u1.Id, u1.DeleteAt); result.Err != nil { 1688 t.Fatalf("failed to update extras by user: %v", result.Err) 1689 } 1690 } 1691 1692 func testChannelStoreSearchMore(t *testing.T, ss store.Store) { 1693 o1 := model.Channel{} 1694 o1.TeamId = model.NewId() 1695 o1.DisplayName = "ChannelA" 1696 o1.Name = "zz" + model.NewId() + "b" 1697 o1.Type = model.CHANNEL_OPEN 1698 store.Must(ss.Channel().Save(&o1, -1)) 1699 1700 o2 := model.Channel{} 1701 o2.TeamId = model.NewId() 1702 o2.DisplayName = "Channel2" 1703 o2.Name = "zz" + model.NewId() + "b" 1704 o2.Type = model.CHANNEL_OPEN 1705 store.Must(ss.Channel().Save(&o2, -1)) 1706 1707 m1 := model.ChannelMember{} 1708 m1.ChannelId = o1.Id 1709 m1.UserId = model.NewId() 1710 m1.NotifyProps = model.GetDefaultChannelNotifyProps() 1711 store.Must(ss.Channel().SaveMember(&m1)) 1712 1713 m2 := model.ChannelMember{} 1714 m2.ChannelId = o1.Id 1715 m2.UserId = model.NewId() 1716 m2.NotifyProps = model.GetDefaultChannelNotifyProps() 1717 store.Must(ss.Channel().SaveMember(&m2)) 1718 1719 m3 := model.ChannelMember{} 1720 m3.ChannelId = o2.Id 1721 m3.UserId = model.NewId() 1722 m3.NotifyProps = model.GetDefaultChannelNotifyProps() 1723 store.Must(ss.Channel().SaveMember(&m3)) 1724 1725 o3 := model.Channel{} 1726 o3.TeamId = o1.TeamId 1727 o3.DisplayName = "ChannelA" 1728 o3.Name = "zz" + model.NewId() + "b" 1729 o3.Type = model.CHANNEL_OPEN 1730 store.Must(ss.Channel().Save(&o3, -1)) 1731 1732 o4 := model.Channel{} 1733 o4.TeamId = o1.TeamId 1734 o4.DisplayName = "ChannelB" 1735 o4.Name = "zz" + model.NewId() + "b" 1736 o4.Type = model.CHANNEL_PRIVATE 1737 store.Must(ss.Channel().Save(&o4, -1)) 1738 1739 o5 := model.Channel{} 1740 o5.TeamId = o1.TeamId 1741 o5.DisplayName = "ChannelC" 1742 o5.Name = "zz" + model.NewId() + "b" 1743 o5.Type = model.CHANNEL_PRIVATE 1744 store.Must(ss.Channel().Save(&o5, -1)) 1745 1746 o6 := model.Channel{} 1747 o6.TeamId = o1.TeamId 1748 o6.DisplayName = "Off-Topic" 1749 o6.Name = "off-topic" 1750 o6.Type = model.CHANNEL_OPEN 1751 store.Must(ss.Channel().Save(&o6, -1)) 1752 1753 o7 := model.Channel{} 1754 o7.TeamId = o1.TeamId 1755 o7.DisplayName = "Off-Set" 1756 o7.Name = "off-set" 1757 o7.Type = model.CHANNEL_OPEN 1758 store.Must(ss.Channel().Save(&o7, -1)) 1759 1760 o8 := model.Channel{} 1761 o8.TeamId = o1.TeamId 1762 o8.DisplayName = "Off-Limit" 1763 o8.Name = "off-limit" 1764 o8.Type = model.CHANNEL_PRIVATE 1765 store.Must(ss.Channel().Save(&o8, -1)) 1766 1767 if result := <-ss.Channel().SearchMore(m1.UserId, o1.TeamId, "ChannelA"); result.Err != nil { 1768 t.Fatal(result.Err) 1769 } else { 1770 channels := result.Data.(*model.ChannelList) 1771 if len(*channels) == 0 { 1772 t.Fatal("should not be empty") 1773 } 1774 1775 if (*channels)[0].Name != o3.Name { 1776 t.Fatal("wrong channel returned") 1777 } 1778 } 1779 1780 if result := <-ss.Channel().SearchMore(m1.UserId, o1.TeamId, o4.Name); result.Err != nil { 1781 t.Fatal(result.Err) 1782 } else { 1783 channels := result.Data.(*model.ChannelList) 1784 if len(*channels) != 0 { 1785 t.Fatal("should be empty") 1786 } 1787 } 1788 1789 if result := <-ss.Channel().SearchMore(m1.UserId, o1.TeamId, o3.Name); result.Err != nil { 1790 t.Fatal(result.Err) 1791 } else { 1792 channels := result.Data.(*model.ChannelList) 1793 if len(*channels) == 0 { 1794 t.Fatal("should not be empty") 1795 } 1796 1797 if (*channels)[0].Name != o3.Name { 1798 t.Fatal("wrong channel returned") 1799 } 1800 } 1801 1802 if result := <-ss.Channel().SearchMore(m1.UserId, o1.TeamId, "off-"); result.Err != nil { 1803 t.Fatal(result.Err) 1804 } else { 1805 channels := result.Data.(*model.ChannelList) 1806 if len(*channels) != 2 { 1807 t.Fatal("should return 2 channels, not including private channel") 1808 } 1809 1810 if (*channels)[0].Name != o7.Name { 1811 t.Fatal("wrong channel returned") 1812 } 1813 1814 if (*channels)[1].Name != o6.Name { 1815 t.Fatal("wrong channel returned") 1816 } 1817 } 1818 1819 if result := <-ss.Channel().SearchMore(m1.UserId, o1.TeamId, "off-topic"); result.Err != nil { 1820 t.Fatal(result.Err) 1821 } else { 1822 channels := result.Data.(*model.ChannelList) 1823 if len(*channels) != 1 { 1824 t.Fatal("should return 1 channel") 1825 } 1826 1827 if (*channels)[0].Name != o6.Name { 1828 t.Fatal("wrong channel returned") 1829 } 1830 } 1831 1832 /* 1833 // Disabling this check as it will fail on PostgreSQL as we have "liberalised" channel matching to deal with 1834 // Full-Text Stemming Limitations. 1835 if result := <-ss.Channel().SearchMore(m1.UserId, o1.TeamId, "off-topics"); result.Err != nil { 1836 t.Fatal(result.Err) 1837 } else { 1838 channels := result.Data.(*model.ChannelList) 1839 if len(*channels) != 0 { 1840 t.Logf("%v\n", *channels) 1841 t.Fatal("should be empty") 1842 } 1843 } 1844 */ 1845 } 1846 1847 func testChannelStoreSearchInTeam(t *testing.T, ss store.Store) { 1848 o1 := model.Channel{} 1849 o1.TeamId = model.NewId() 1850 o1.DisplayName = "ChannelA" 1851 o1.Name = "zz" + model.NewId() + "b" 1852 o1.Type = model.CHANNEL_OPEN 1853 store.Must(ss.Channel().Save(&o1, -1)) 1854 1855 o2 := model.Channel{} 1856 o2.TeamId = model.NewId() 1857 o2.DisplayName = "Channel2" 1858 o2.Name = "zz" + model.NewId() + "b" 1859 o2.Type = model.CHANNEL_OPEN 1860 store.Must(ss.Channel().Save(&o2, -1)) 1861 1862 m1 := model.ChannelMember{} 1863 m1.ChannelId = o1.Id 1864 m1.UserId = model.NewId() 1865 m1.NotifyProps = model.GetDefaultChannelNotifyProps() 1866 store.Must(ss.Channel().SaveMember(&m1)) 1867 1868 m2 := model.ChannelMember{} 1869 m2.ChannelId = o1.Id 1870 m2.UserId = model.NewId() 1871 m2.NotifyProps = model.GetDefaultChannelNotifyProps() 1872 store.Must(ss.Channel().SaveMember(&m2)) 1873 1874 m3 := model.ChannelMember{} 1875 m3.ChannelId = o2.Id 1876 m3.UserId = model.NewId() 1877 m3.NotifyProps = model.GetDefaultChannelNotifyProps() 1878 store.Must(ss.Channel().SaveMember(&m3)) 1879 1880 o3 := model.Channel{} 1881 o3.TeamId = o1.TeamId 1882 o3.DisplayName = "ChannelA" 1883 o3.Name = "zz" + model.NewId() + "b" 1884 o3.Type = model.CHANNEL_OPEN 1885 store.Must(ss.Channel().Save(&o3, -1)) 1886 1887 o4 := model.Channel{} 1888 o4.TeamId = o1.TeamId 1889 o4.DisplayName = "ChannelB" 1890 o4.Name = "zz" + model.NewId() + "b" 1891 o4.Type = model.CHANNEL_PRIVATE 1892 store.Must(ss.Channel().Save(&o4, -1)) 1893 1894 o5 := model.Channel{} 1895 o5.TeamId = o1.TeamId 1896 o5.DisplayName = "ChannelC" 1897 o5.Name = "zz" + model.NewId() + "b" 1898 o5.Type = model.CHANNEL_PRIVATE 1899 store.Must(ss.Channel().Save(&o5, -1)) 1900 1901 o6 := model.Channel{} 1902 o6.TeamId = o1.TeamId 1903 o6.DisplayName = "Off-Topic" 1904 o6.Name = "off-topic" 1905 o6.Type = model.CHANNEL_OPEN 1906 store.Must(ss.Channel().Save(&o6, -1)) 1907 1908 o7 := model.Channel{} 1909 o7.TeamId = o1.TeamId 1910 o7.DisplayName = "Off-Set" 1911 o7.Name = "off-set" 1912 o7.Type = model.CHANNEL_OPEN 1913 store.Must(ss.Channel().Save(&o7, -1)) 1914 1915 o8 := model.Channel{} 1916 o8.TeamId = o1.TeamId 1917 o8.DisplayName = "Off-Limit" 1918 o8.Name = "off-limit" 1919 o8.Type = model.CHANNEL_PRIVATE 1920 store.Must(ss.Channel().Save(&o8, -1)) 1921 1922 o9 := model.Channel{} 1923 o9.TeamId = o1.TeamId 1924 o9.DisplayName = "Town Square" 1925 o9.Name = "town-square" 1926 o9.Type = model.CHANNEL_OPEN 1927 store.Must(ss.Channel().Save(&o9, -1)) 1928 1929 o10 := model.Channel{} 1930 o10.TeamId = o1.TeamId 1931 o10.DisplayName = "The" 1932 o10.Name = "the" 1933 o10.Type = model.CHANNEL_OPEN 1934 store.Must(ss.Channel().Save(&o10, -1)) 1935 1936 o11 := model.Channel{} 1937 o11.TeamId = o1.TeamId 1938 o11.DisplayName = "Native Mobile Apps" 1939 o11.Name = "native-mobile-apps" 1940 o11.Type = model.CHANNEL_OPEN 1941 store.Must(ss.Channel().Save(&o11, -1)) 1942 1943 for name, search := range map[string]func(teamId string, term string) store.StoreChannel{ 1944 "AutocompleteInTeam": ss.Channel().AutocompleteInTeam, 1945 "SearchInTeam": ss.Channel().SearchInTeam, 1946 } { 1947 t.Run(name, func(t *testing.T) { 1948 if result := <-search(o1.TeamId, "ChannelA"); result.Err != nil { 1949 t.Fatal(result.Err) 1950 } else { 1951 channels := result.Data.(*model.ChannelList) 1952 if len(*channels) != 2 { 1953 t.Fatal("wrong length") 1954 } 1955 } 1956 1957 if result := <-search(o1.TeamId, ""); result.Err != nil { 1958 t.Fatal(result.Err) 1959 } else { 1960 channels := result.Data.(*model.ChannelList) 1961 if len(*channels) == 0 { 1962 t.Fatal("should not be empty") 1963 } 1964 } 1965 1966 if result := <-search(o1.TeamId, "blargh"); result.Err != nil { 1967 t.Fatal(result.Err) 1968 } else { 1969 channels := result.Data.(*model.ChannelList) 1970 if len(*channels) != 0 { 1971 t.Fatal("should be empty") 1972 } 1973 } 1974 1975 if result := <-search(o1.TeamId, "off-"); result.Err != nil { 1976 t.Fatal(result.Err) 1977 } else { 1978 channels := result.Data.(*model.ChannelList) 1979 if len(*channels) != 2 { 1980 t.Fatal("should return 2 channels, not including private channel") 1981 } 1982 1983 if (*channels)[0].Name != o7.Name { 1984 t.Fatal("wrong channel returned") 1985 } 1986 1987 if (*channels)[1].Name != o6.Name { 1988 t.Fatal("wrong channel returned") 1989 } 1990 } 1991 1992 if result := <-search(o1.TeamId, "off-topic"); result.Err != nil { 1993 t.Fatal(result.Err) 1994 } else { 1995 channels := result.Data.(*model.ChannelList) 1996 if len(*channels) != 1 { 1997 t.Fatal("should return 1 channel") 1998 } 1999 2000 if (*channels)[0].Name != o6.Name { 2001 t.Fatal("wrong channel returned") 2002 } 2003 } 2004 2005 if result := <-search(o1.TeamId, "town square"); result.Err != nil { 2006 t.Fatal(result.Err) 2007 } else { 2008 channels := result.Data.(*model.ChannelList) 2009 if len(*channels) != 1 { 2010 t.Fatal("should return 1 channel") 2011 } 2012 2013 if (*channels)[0].Name != o9.Name { 2014 t.Fatal("wrong channel returned") 2015 } 2016 } 2017 2018 if result := <-search(o1.TeamId, "the"); result.Err != nil { 2019 t.Fatal(result.Err) 2020 } else { 2021 channels := result.Data.(*model.ChannelList) 2022 t.Log(channels.ToJson()) 2023 if len(*channels) != 1 { 2024 t.Fatal("should return 1 channel") 2025 } 2026 2027 if (*channels)[0].Name != o10.Name { 2028 t.Fatal("wrong channel returned") 2029 } 2030 } 2031 2032 if result := <-search(o1.TeamId, "Mobile"); result.Err != nil { 2033 t.Fatal(result.Err) 2034 } else { 2035 channels := result.Data.(*model.ChannelList) 2036 t.Log(channels.ToJson()) 2037 if len(*channels) != 1 { 2038 t.Fatal("should return 1 channel") 2039 } 2040 2041 if (*channels)[0].Name != o11.Name { 2042 t.Fatal("wrong channel returned") 2043 } 2044 } 2045 }) 2046 } 2047 } 2048 2049 func testChannelStoreGetMembersByIds(t *testing.T, ss store.Store) { 2050 o1 := model.Channel{} 2051 o1.TeamId = model.NewId() 2052 o1.DisplayName = "ChannelA" 2053 o1.Name = "zz" + model.NewId() + "b" 2054 o1.Type = model.CHANNEL_OPEN 2055 store.Must(ss.Channel().Save(&o1, -1)) 2056 2057 m1 := &model.ChannelMember{ChannelId: o1.Id, UserId: model.NewId(), NotifyProps: model.GetDefaultChannelNotifyProps()} 2058 store.Must(ss.Channel().SaveMember(m1)) 2059 2060 if r := <-ss.Channel().GetMembersByIds(m1.ChannelId, []string{m1.UserId}); r.Err != nil { 2061 t.Fatal(r.Err) 2062 } else { 2063 rm1 := (*r.Data.(*model.ChannelMembers))[0] 2064 2065 if rm1.ChannelId != m1.ChannelId { 2066 t.Fatal("bad team id") 2067 } 2068 2069 if rm1.UserId != m1.UserId { 2070 t.Fatal("bad user id") 2071 } 2072 } 2073 2074 m2 := &model.ChannelMember{ChannelId: o1.Id, UserId: model.NewId(), NotifyProps: model.GetDefaultChannelNotifyProps()} 2075 store.Must(ss.Channel().SaveMember(m2)) 2076 2077 if r := <-ss.Channel().GetMembersByIds(m1.ChannelId, []string{m1.UserId, m2.UserId, model.NewId()}); r.Err != nil { 2078 t.Fatal(r.Err) 2079 } else { 2080 rm := (*r.Data.(*model.ChannelMembers)) 2081 2082 if len(rm) != 2 { 2083 t.Fatal("return wrong number of results") 2084 } 2085 } 2086 2087 if r := <-ss.Channel().GetMembersByIds(m1.ChannelId, []string{}); r.Err == nil { 2088 t.Fatal("empty user ids - should have failed") 2089 } 2090 } 2091 2092 func testChannelStoreAnalyticsDeletedTypeCount(t *testing.T, ss store.Store) { 2093 o1 := model.Channel{} 2094 o1.TeamId = model.NewId() 2095 o1.DisplayName = "ChannelA" 2096 o1.Name = "zz" + model.NewId() + "b" 2097 o1.Type = model.CHANNEL_OPEN 2098 store.Must(ss.Channel().Save(&o1, -1)) 2099 2100 o2 := model.Channel{} 2101 o2.TeamId = model.NewId() 2102 o2.DisplayName = "Channel2" 2103 o2.Name = "zz" + model.NewId() + "b" 2104 o2.Type = model.CHANNEL_OPEN 2105 store.Must(ss.Channel().Save(&o2, -1)) 2106 2107 p3 := model.Channel{} 2108 p3.TeamId = model.NewId() 2109 p3.DisplayName = "Channel3" 2110 p3.Name = "zz" + model.NewId() + "b" 2111 p3.Type = model.CHANNEL_PRIVATE 2112 store.Must(ss.Channel().Save(&p3, -1)) 2113 2114 u1 := &model.User{} 2115 u1.Email = model.NewId() 2116 u1.Nickname = model.NewId() 2117 store.Must(ss.User().Save(u1)) 2118 2119 u2 := &model.User{} 2120 u2.Email = model.NewId() 2121 u2.Nickname = model.NewId() 2122 store.Must(ss.User().Save(u2)) 2123 2124 var d4 *model.Channel 2125 if result := <-ss.Channel().CreateDirectChannel(u1.Id, u2.Id); result.Err != nil { 2126 t.Fatalf(result.Err.Error()) 2127 } else { 2128 d4 = result.Data.(*model.Channel) 2129 } 2130 2131 var openStartCount int64 2132 if result := <-ss.Channel().AnalyticsDeletedTypeCount("", "O"); result.Err != nil { 2133 t.Fatal(result.Err.Error()) 2134 } else { 2135 openStartCount = result.Data.(int64) 2136 } 2137 2138 var privateStartCount int64 2139 if result := <-ss.Channel().AnalyticsDeletedTypeCount("", "P"); result.Err != nil { 2140 t.Fatal(result.Err.Error()) 2141 } else { 2142 privateStartCount = result.Data.(int64) 2143 } 2144 2145 var directStartCount int64 2146 if result := <-ss.Channel().AnalyticsDeletedTypeCount("", "D"); result.Err != nil { 2147 t.Fatal(result.Err.Error()) 2148 } else { 2149 directStartCount = result.Data.(int64) 2150 } 2151 2152 store.Must(ss.Channel().Delete(o1.Id, model.GetMillis())) 2153 store.Must(ss.Channel().Delete(o2.Id, model.GetMillis())) 2154 store.Must(ss.Channel().Delete(p3.Id, model.GetMillis())) 2155 store.Must(ss.Channel().Delete(d4.Id, model.GetMillis())) 2156 2157 if result := <-ss.Channel().AnalyticsDeletedTypeCount("", "O"); result.Err != nil { 2158 t.Fatal(result.Err.Error()) 2159 } else { 2160 if result.Data.(int64) != openStartCount+2 { 2161 t.Fatalf("Wrong open channel deleted count.") 2162 } 2163 } 2164 2165 if result := <-ss.Channel().AnalyticsDeletedTypeCount("", "P"); result.Err != nil { 2166 t.Fatal(result.Err.Error()) 2167 } else { 2168 if result.Data.(int64) != privateStartCount+1 { 2169 t.Fatalf("Wrong private channel deleted count.") 2170 } 2171 } 2172 2173 if result := <-ss.Channel().AnalyticsDeletedTypeCount("", "D"); result.Err != nil { 2174 t.Fatal(result.Err.Error()) 2175 } else { 2176 if result.Data.(int64) != directStartCount+1 { 2177 t.Fatalf("Wrong direct channel deleted count.") 2178 } 2179 } 2180 } 2181 2182 func testChannelStoreGetPinnedPosts(t *testing.T, ss store.Store) { 2183 o1 := store.Must(ss.Channel().Save(&model.Channel{ 2184 TeamId: model.NewId(), 2185 DisplayName: "Name", 2186 Name: "zz" + model.NewId() + "b", 2187 Type: model.CHANNEL_OPEN, 2188 }, -1)).(*model.Channel) 2189 2190 p1 := store.Must(ss.Post().Save(&model.Post{ 2191 UserId: model.NewId(), 2192 ChannelId: o1.Id, 2193 Message: "test", 2194 IsPinned: true, 2195 })).(*model.Post) 2196 2197 if r1 := <-ss.Channel().GetPinnedPosts(o1.Id); r1.Err != nil { 2198 t.Fatal(r1.Err) 2199 } else if r1.Data.(*model.PostList).Posts[p1.Id] == nil { 2200 t.Fatal("didn't return relevant pinned posts") 2201 } 2202 2203 o2 := store.Must(ss.Channel().Save(&model.Channel{ 2204 TeamId: model.NewId(), 2205 DisplayName: "Name", 2206 Name: "zz" + model.NewId() + "b", 2207 Type: model.CHANNEL_OPEN, 2208 }, -1)).(*model.Channel) 2209 2210 store.Must(ss.Post().Save(&model.Post{ 2211 UserId: model.NewId(), 2212 ChannelId: o2.Id, 2213 Message: "test", 2214 })) 2215 2216 if r2 := <-ss.Channel().GetPinnedPosts(o2.Id); r2.Err != nil { 2217 t.Fatal(r2.Err) 2218 } else if len(r2.Data.(*model.PostList).Posts) != 0 { 2219 t.Fatal("wasn't supposed to return posts") 2220 } 2221 } 2222 2223 func testChannelStoreMaxChannelsPerTeam(t *testing.T, ss store.Store) { 2224 channel := &model.Channel{ 2225 TeamId: model.NewId(), 2226 DisplayName: "Channel", 2227 Name: model.NewId(), 2228 Type: model.CHANNEL_OPEN, 2229 } 2230 result := <-ss.Channel().Save(channel, 0) 2231 assert.NotEqual(t, nil, result.Err) 2232 assert.Equal(t, result.Err.Id, "store.sql_channel.save_channel.limit.app_error") 2233 2234 channel.Id = "" 2235 result = <-ss.Channel().Save(channel, 1) 2236 assert.Nil(t, result.Err) 2237 }