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