github.com/levb/mattermost-server@v5.3.1+incompatible/store/storetest/channel_store.go (about)

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