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