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