github.com/mad-app/mattermost-server@v5.11.1+incompatible/store/storetest/team_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 TestTeamStore(t *testing.T, ss store.Store) {
    19  	createDefaultRoles(t, ss)
    20  
    21  	t.Run("Save", func(t *testing.T) { testTeamStoreSave(t, ss) })
    22  	t.Run("Update", func(t *testing.T) { testTeamStoreUpdate(t, ss) })
    23  	t.Run("UpdateDisplayName", func(t *testing.T) { testTeamStoreUpdateDisplayName(t, ss) })
    24  	t.Run("Get", func(t *testing.T) { testTeamStoreGet(t, ss) })
    25  	t.Run("GetByName", func(t *testing.T) { testTeamStoreGetByName(t, ss) })
    26  	t.Run("SearchByName", func(t *testing.T) { testTeamStoreSearchByName(t, ss) })
    27  	t.Run("SearchAll", func(t *testing.T) { testTeamStoreSearchAll(t, ss) })
    28  	t.Run("SearchOpen", func(t *testing.T) { testTeamStoreSearchOpen(t, ss) })
    29  	t.Run("SearchPrivate", func(t *testing.T) { testTeamStoreSearchPrivate(t, ss) })
    30  	t.Run("GetByIniviteId", func(t *testing.T) { testTeamStoreGetByIniviteId(t, ss) })
    31  	t.Run("ByUserId", func(t *testing.T) { testTeamStoreByUserId(t, ss) })
    32  	t.Run("GetAllTeamListing", func(t *testing.T) { testGetAllTeamListing(t, ss) })
    33  	t.Run("GetAllTeamPageListing", func(t *testing.T) { testGetAllTeamPageListing(t, ss) })
    34  	t.Run("GetAllPrivateTeamListing", func(t *testing.T) { testGetAllPrivateTeamListing(t, ss) })
    35  	t.Run("GetAllPrivateTeamPageListing", func(t *testing.T) { testGetAllPrivateTeamPageListing(t, ss) })
    36  	t.Run("Delete", func(t *testing.T) { testDelete(t, ss) })
    37  	t.Run("TeamCount", func(t *testing.T) { testTeamCount(t, ss) })
    38  	t.Run("TeamMembers", func(t *testing.T) { testTeamMembers(t, ss) })
    39  	t.Run("SaveTeamMemberMaxMembers", func(t *testing.T) { testSaveTeamMemberMaxMembers(t, ss) })
    40  	t.Run("GetTeamMember", func(t *testing.T) { testGetTeamMember(t, ss) })
    41  	t.Run("GetTeamMembersByIds", func(t *testing.T) { testGetTeamMembersByIds(t, ss) })
    42  	t.Run("MemberCount", func(t *testing.T) { testTeamStoreMemberCount(t, ss) })
    43  	t.Run("GetChannelUnreadsForAllTeams", func(t *testing.T) { testGetChannelUnreadsForAllTeams(t, ss) })
    44  	t.Run("GetChannelUnreadsForTeam", func(t *testing.T) { testGetChannelUnreadsForTeam(t, ss) })
    45  	t.Run("UpdateLastTeamIconUpdate", func(t *testing.T) { testUpdateLastTeamIconUpdate(t, ss) })
    46  	t.Run("GetTeamsByScheme", func(t *testing.T) { testGetTeamsByScheme(t, ss) })
    47  	t.Run("MigrateTeamMembers", func(t *testing.T) { testTeamStoreMigrateTeamMembers(t, ss) })
    48  	t.Run("ResetAllTeamSchemes", func(t *testing.T) { testResetAllTeamSchemes(t, ss) })
    49  	t.Run("ClearAllCustomRoleAssignments", func(t *testing.T) { testTeamStoreClearAllCustomRoleAssignments(t, ss) })
    50  	t.Run("AnalyticsGetTeamCountForScheme", func(t *testing.T) { testTeamStoreAnalyticsGetTeamCountForScheme(t, ss) })
    51  	t.Run("GetAllForExportAfter", func(t *testing.T) { testTeamStoreGetAllForExportAfter(t, ss) })
    52  	t.Run("GetTeamMembersForExport", func(t *testing.T) { testTeamStoreGetTeamMembersForExport(t, ss) })
    53  	t.Run("GetTeamsForUserWithPagination", func(t *testing.T) { testTeamMembersWithPagination(t, ss) })
    54  }
    55  
    56  func testTeamStoreSave(t *testing.T, ss store.Store) {
    57  	o1 := model.Team{}
    58  	o1.DisplayName = "DisplayName"
    59  	o1.Name = "z-z-z" + model.NewId() + "b"
    60  	o1.Email = MakeEmail()
    61  	o1.Type = model.TEAM_OPEN
    62  
    63  	if err := (<-ss.Team().Save(&o1)).Err; err != nil {
    64  		t.Fatal("couldn't save item", err)
    65  	}
    66  
    67  	if err := (<-ss.Team().Save(&o1)).Err; err == nil {
    68  		t.Fatal("shouldn't be able to update from save")
    69  	}
    70  
    71  	o1.Id = ""
    72  	if err := (<-ss.Team().Save(&o1)).Err; err == nil {
    73  		t.Fatal("should be unique domain")
    74  	}
    75  }
    76  
    77  func testTeamStoreUpdate(t *testing.T, ss store.Store) {
    78  	o1 := model.Team{}
    79  	o1.DisplayName = "DisplayName"
    80  	o1.Name = "z-z-z" + model.NewId() + "b"
    81  	o1.Email = MakeEmail()
    82  	o1.Type = model.TEAM_OPEN
    83  	if err := (<-ss.Team().Save(&o1)).Err; err != nil {
    84  		t.Fatal(err)
    85  	}
    86  
    87  	time.Sleep(100 * time.Millisecond)
    88  
    89  	if err := (<-ss.Team().Update(&o1)).Err; err != nil {
    90  		t.Fatal(err)
    91  	}
    92  
    93  	o1.Id = "missing"
    94  	if err := (<-ss.Team().Update(&o1)).Err; err == nil {
    95  		t.Fatal("Update should have failed because of missing key")
    96  	}
    97  
    98  	o1.Id = model.NewId()
    99  	if err := (<-ss.Team().Update(&o1)).Err; err == nil {
   100  		t.Fatal("Update should have faile because id change")
   101  	}
   102  }
   103  
   104  func testTeamStoreUpdateDisplayName(t *testing.T, ss store.Store) {
   105  	o1 := &model.Team{}
   106  	o1.DisplayName = "Display Name"
   107  	o1.Name = "z-z-z" + model.NewId() + "b"
   108  	o1.Email = MakeEmail()
   109  	o1.Type = model.TEAM_OPEN
   110  	o1 = (<-ss.Team().Save(o1)).Data.(*model.Team)
   111  
   112  	newDisplayName := "NewDisplayName"
   113  
   114  	if err := (<-ss.Team().UpdateDisplayName(newDisplayName, o1.Id)).Err; err != nil {
   115  		t.Fatal(err)
   116  	}
   117  
   118  	ro1 := (<-ss.Team().Get(o1.Id)).Data.(*model.Team)
   119  	if ro1.DisplayName != newDisplayName {
   120  		t.Fatal("DisplayName not updated")
   121  	}
   122  }
   123  
   124  func testTeamStoreGet(t *testing.T, ss store.Store) {
   125  	o1 := model.Team{}
   126  	o1.DisplayName = "DisplayName"
   127  	o1.Name = "z-z-z" + model.NewId() + "b"
   128  	o1.Email = MakeEmail()
   129  	o1.Type = model.TEAM_OPEN
   130  	store.Must(ss.Team().Save(&o1))
   131  
   132  	if r1 := <-ss.Team().Get(o1.Id); r1.Err != nil {
   133  		t.Fatal(r1.Err)
   134  	} else {
   135  		if r1.Data.(*model.Team).ToJson() != o1.ToJson() {
   136  			t.Fatal("invalid returned team")
   137  		}
   138  	}
   139  
   140  	if err := (<-ss.Team().Get("")).Err; err == nil {
   141  		t.Fatal("Missing id should have failed")
   142  	}
   143  }
   144  
   145  func testTeamStoreGetByName(t *testing.T, ss store.Store) {
   146  	o1 := model.Team{}
   147  	o1.DisplayName = "DisplayName"
   148  	o1.Name = "z-z-z" + model.NewId() + "b"
   149  	o1.Email = MakeEmail()
   150  	o1.Type = model.TEAM_OPEN
   151  
   152  	if err := (<-ss.Team().Save(&o1)).Err; err != nil {
   153  		t.Fatal(err)
   154  	}
   155  
   156  	if r1 := <-ss.Team().GetByName(o1.Name); r1.Err != nil {
   157  		t.Fatal(r1.Err)
   158  	} else {
   159  		if r1.Data.(*model.Team).ToJson() != o1.ToJson() {
   160  			t.Fatal("invalid returned team")
   161  		}
   162  	}
   163  
   164  	if err := (<-ss.Team().GetByName("")).Err; err == nil {
   165  		t.Fatal("Missing id should have failed")
   166  	}
   167  }
   168  
   169  func testTeamStoreSearchByName(t *testing.T, ss store.Store) {
   170  	o1 := model.Team{}
   171  	o1.DisplayName = "DisplayName"
   172  	var name = "zzz" + model.NewId()
   173  	o1.Name = name + "b"
   174  	o1.Email = MakeEmail()
   175  	o1.Type = model.TEAM_OPEN
   176  
   177  	if err := (<-ss.Team().Save(&o1)).Err; err != nil {
   178  		t.Fatal(err)
   179  	}
   180  
   181  	if r1 := <-ss.Team().SearchByName(name); r1.Err != nil {
   182  		t.Fatal(r1.Err)
   183  	} else {
   184  		if r1.Data.([]*model.Team)[0].ToJson() != o1.ToJson() {
   185  			t.Fatal("invalid returned team")
   186  		}
   187  	}
   188  }
   189  
   190  func testTeamStoreSearchAll(t *testing.T, ss store.Store) {
   191  	o := model.Team{}
   192  	o.DisplayName = "ADisplayName" + model.NewId()
   193  	o.Name = "zzzzzz-" + model.NewId() + "a"
   194  	o.Email = MakeEmail()
   195  	o.Type = model.TEAM_OPEN
   196  	o.AllowOpenInvite = true
   197  
   198  	require.Nil(t, (<-ss.Team().Save(&o)).Err)
   199  
   200  	p := model.Team{}
   201  	p.DisplayName = "ADisplayName" + model.NewId()
   202  	p.Name = "zzzzzz-" + model.NewId() + "a"
   203  	p.Email = MakeEmail()
   204  	p.Type = model.TEAM_OPEN
   205  	p.AllowOpenInvite = false
   206  
   207  	require.Nil(t, (<-ss.Team().Save(&p)).Err)
   208  
   209  	testCases := []struct {
   210  		Name            string
   211  		Term            string
   212  		ExpectedLenth   int
   213  		ExpectedFirstId string
   214  	}{
   215  		{
   216  			"Search for open team name",
   217  			o.Name,
   218  			1,
   219  			o.Id,
   220  		},
   221  		{
   222  			"Search for open team displayName",
   223  			o.DisplayName,
   224  			1,
   225  			o.Id,
   226  		},
   227  		{
   228  			"Search for open team without results",
   229  			"junk",
   230  			0,
   231  			"",
   232  		},
   233  		{
   234  			"Search for private team",
   235  			p.DisplayName,
   236  			1,
   237  			p.Id,
   238  		},
   239  		{
   240  			"Search for both teams",
   241  			"zzzzzz",
   242  			2,
   243  			"",
   244  		},
   245  	}
   246  
   247  	for _, tc := range testCases {
   248  		t.Run(tc.Name, func(t *testing.T) {
   249  			r1 := <-ss.Team().SearchAll(tc.Term)
   250  			require.Nil(t, r1.Err)
   251  			results := r1.Data.([]*model.Team)
   252  			require.Equal(t, tc.ExpectedLenth, len(results))
   253  			if tc.ExpectedFirstId != "" {
   254  				assert.Equal(t, tc.ExpectedFirstId, results[0].Id)
   255  			}
   256  		})
   257  	}
   258  }
   259  
   260  func testTeamStoreSearchOpen(t *testing.T, ss store.Store) {
   261  	o := model.Team{}
   262  	o.DisplayName = "ADisplayName" + model.NewId()
   263  	o.Name = "zz" + model.NewId() + "a"
   264  	o.Email = MakeEmail()
   265  	o.Type = model.TEAM_OPEN
   266  	o.AllowOpenInvite = true
   267  
   268  	require.Nil(t, (<-ss.Team().Save(&o)).Err)
   269  
   270  	p := model.Team{}
   271  	p.DisplayName = "ADisplayName" + model.NewId()
   272  	p.Name = "zz" + model.NewId() + "a"
   273  	p.Email = MakeEmail()
   274  	p.Type = model.TEAM_OPEN
   275  	p.AllowOpenInvite = false
   276  
   277  	require.Nil(t, (<-ss.Team().Save(&p)).Err)
   278  
   279  	testCases := []struct {
   280  		Name            string
   281  		Term            string
   282  		ExpectedLenth   int
   283  		ExpectedFirstId string
   284  	}{
   285  		{
   286  			"Search for open team name",
   287  			o.Name,
   288  			1,
   289  			o.Id,
   290  		},
   291  		{
   292  			"Search for open team displayName",
   293  			o.DisplayName,
   294  			1,
   295  			o.Id,
   296  		},
   297  		{
   298  			"Search for open team without results",
   299  			"junk",
   300  			0,
   301  			"",
   302  		},
   303  		{
   304  			"Search for a private team (expected no results)",
   305  			p.DisplayName,
   306  			0,
   307  			"",
   308  		},
   309  	}
   310  
   311  	for _, tc := range testCases {
   312  		t.Run(tc.Name, func(t *testing.T) {
   313  			r1 := <-ss.Team().SearchOpen(tc.Term)
   314  			require.Nil(t, r1.Err)
   315  			results := r1.Data.([]*model.Team)
   316  			require.Equal(t, tc.ExpectedLenth, len(results))
   317  			if tc.ExpectedFirstId != "" {
   318  				assert.Equal(t, tc.ExpectedFirstId, results[0].Id)
   319  			}
   320  		})
   321  	}
   322  }
   323  
   324  func testTeamStoreSearchPrivate(t *testing.T, ss store.Store) {
   325  	o := model.Team{}
   326  	o.DisplayName = "ADisplayName" + model.NewId()
   327  	o.Name = "zz" + model.NewId() + "a"
   328  	o.Email = MakeEmail()
   329  	o.Type = model.TEAM_OPEN
   330  	o.AllowOpenInvite = true
   331  
   332  	require.Nil(t, (<-ss.Team().Save(&o)).Err)
   333  
   334  	p := model.Team{}
   335  	p.DisplayName = "ADisplayName" + model.NewId()
   336  	p.Name = "zz" + model.NewId() + "a"
   337  	p.Email = MakeEmail()
   338  	p.Type = model.TEAM_OPEN
   339  	p.AllowOpenInvite = false
   340  
   341  	require.Nil(t, (<-ss.Team().Save(&p)).Err)
   342  
   343  	testCases := []struct {
   344  		Name            string
   345  		Term            string
   346  		ExpectedLenth   int
   347  		ExpectedFirstId string
   348  	}{
   349  		{
   350  			"Search for private team name",
   351  			p.Name,
   352  			1,
   353  			p.Id,
   354  		},
   355  		{
   356  			"Search for private team displayName",
   357  			p.DisplayName,
   358  			1,
   359  			p.Id,
   360  		},
   361  		{
   362  			"Search for private team without results",
   363  			"junk",
   364  			0,
   365  			"",
   366  		},
   367  		{
   368  			"Search for a open team (expected no results)",
   369  			o.DisplayName,
   370  			0,
   371  			"",
   372  		},
   373  	}
   374  
   375  	for _, tc := range testCases {
   376  		t.Run(tc.Name, func(t *testing.T) {
   377  			r1 := <-ss.Team().SearchPrivate(tc.Term)
   378  			require.Nil(t, r1.Err)
   379  			results := r1.Data.([]*model.Team)
   380  			require.Equal(t, tc.ExpectedLenth, len(results))
   381  			if tc.ExpectedFirstId != "" {
   382  				assert.Equal(t, tc.ExpectedFirstId, results[0].Id)
   383  			}
   384  		})
   385  	}
   386  }
   387  
   388  func testTeamStoreGetByIniviteId(t *testing.T, ss store.Store) {
   389  	o1 := model.Team{}
   390  	o1.DisplayName = "DisplayName"
   391  	o1.Name = "z-z-z" + model.NewId() + "b"
   392  	o1.Email = MakeEmail()
   393  	o1.Type = model.TEAM_OPEN
   394  	o1.InviteId = model.NewId()
   395  
   396  	save1 := <-ss.Team().Save(&o1)
   397  
   398  	if err := save1.Err; err != nil {
   399  		t.Fatal(err)
   400  	}
   401  
   402  	o2 := model.Team{}
   403  	o2.DisplayName = "DisplayName"
   404  	o2.Name = "zz" + model.NewId() + "b"
   405  	o2.Email = MakeEmail()
   406  	o2.Type = model.TEAM_OPEN
   407  
   408  	if r1 := <-ss.Team().GetByInviteId(save1.Data.(*model.Team).InviteId); r1.Err != nil {
   409  		t.Fatal(r1.Err)
   410  	} else {
   411  		if r1.Data.(*model.Team).ToJson() != o1.ToJson() {
   412  			t.Fatal("invalid returned team")
   413  		}
   414  	}
   415  
   416  	if err := (<-ss.Team().GetByInviteId("")).Err; err == nil {
   417  		t.Fatal("Missing id should have failed")
   418  	}
   419  }
   420  
   421  func testTeamStoreByUserId(t *testing.T, ss store.Store) {
   422  	o1 := &model.Team{}
   423  	o1.DisplayName = "DisplayName"
   424  	o1.Name = "z-z-z" + model.NewId() + "b"
   425  	o1.Email = MakeEmail()
   426  	o1.Type = model.TEAM_OPEN
   427  	o1.InviteId = model.NewId()
   428  	o1 = store.Must(ss.Team().Save(o1)).(*model.Team)
   429  
   430  	m1 := &model.TeamMember{TeamId: o1.Id, UserId: model.NewId()}
   431  	store.Must(ss.Team().SaveMember(m1, -1))
   432  
   433  	if r1 := <-ss.Team().GetTeamsByUserId(m1.UserId); r1.Err != nil {
   434  		t.Fatal(r1.Err)
   435  	} else {
   436  		teams := r1.Data.([]*model.Team)
   437  		if len(teams) == 0 {
   438  			t.Fatal("Should return a team")
   439  		}
   440  
   441  		if teams[0].Id != o1.Id {
   442  			t.Fatal("should be a member")
   443  		}
   444  
   445  	}
   446  }
   447  
   448  func testGetAllTeamListing(t *testing.T, ss store.Store) {
   449  	o1 := model.Team{}
   450  	o1.DisplayName = "DisplayName"
   451  	o1.Name = "z-z-z" + model.NewId() + "b"
   452  	o1.Email = MakeEmail()
   453  	o1.Type = model.TEAM_OPEN
   454  	o1.AllowOpenInvite = true
   455  	store.Must(ss.Team().Save(&o1))
   456  
   457  	o2 := model.Team{}
   458  	o2.DisplayName = "DisplayName"
   459  	o2.Name = "zz" + model.NewId() + "b"
   460  	o2.Email = MakeEmail()
   461  	o2.Type = model.TEAM_OPEN
   462  	store.Must(ss.Team().Save(&o2))
   463  
   464  	o3 := model.Team{}
   465  	o3.DisplayName = "DisplayName"
   466  	o3.Name = "z-z-z" + model.NewId() + "b"
   467  	o3.Email = MakeEmail()
   468  	o3.Type = model.TEAM_INVITE
   469  	o3.AllowOpenInvite = true
   470  	store.Must(ss.Team().Save(&o3))
   471  
   472  	o4 := model.Team{}
   473  	o4.DisplayName = "DisplayName"
   474  	o4.Name = "zz" + model.NewId() + "b"
   475  	o4.Email = MakeEmail()
   476  	o4.Type = model.TEAM_INVITE
   477  	store.Must(ss.Team().Save(&o4))
   478  
   479  	if r1 := <-ss.Team().GetAllTeamListing(); r1.Err != nil {
   480  		t.Fatal(r1.Err)
   481  	} else {
   482  		teams := r1.Data.([]*model.Team)
   483  
   484  		for _, team := range teams {
   485  			if !team.AllowOpenInvite {
   486  				t.Fatal("should have returned team with AllowOpenInvite as true")
   487  			}
   488  		}
   489  
   490  		if len(teams) == 0 {
   491  			t.Fatal("failed team listing")
   492  		}
   493  	}
   494  }
   495  
   496  func testGetAllTeamPageListing(t *testing.T, ss store.Store) {
   497  	o1 := model.Team{}
   498  	o1.DisplayName = "DisplayName"
   499  	o1.Name = "z-z-z" + model.NewId() + "b"
   500  	o1.Email = MakeEmail()
   501  	o1.Type = model.TEAM_OPEN
   502  	o1.AllowOpenInvite = true
   503  	store.Must(ss.Team().Save(&o1))
   504  
   505  	o2 := model.Team{}
   506  	o2.DisplayName = "DisplayName"
   507  	o2.Name = "zz" + model.NewId() + "b"
   508  	o2.Email = MakeEmail()
   509  	o2.Type = model.TEAM_OPEN
   510  	o2.AllowOpenInvite = false
   511  	store.Must(ss.Team().Save(&o2))
   512  
   513  	o3 := model.Team{}
   514  	o3.DisplayName = "DisplayName"
   515  	o3.Name = "z-z-z" + model.NewId() + "b"
   516  	o3.Email = MakeEmail()
   517  	o3.Type = model.TEAM_INVITE
   518  	o3.AllowOpenInvite = true
   519  	store.Must(ss.Team().Save(&o3))
   520  
   521  	o4 := model.Team{}
   522  	o4.DisplayName = "DisplayName"
   523  	o4.Name = "zz" + model.NewId() + "b"
   524  	o4.Email = MakeEmail()
   525  	o4.Type = model.TEAM_INVITE
   526  	o4.AllowOpenInvite = false
   527  	store.Must(ss.Team().Save(&o4))
   528  
   529  	if r1 := <-ss.Team().GetAllTeamPageListing(0, 10); r1.Err != nil {
   530  		t.Fatal(r1.Err)
   531  	} else {
   532  		teams := r1.Data.([]*model.Team)
   533  
   534  		for _, team := range teams {
   535  			if !team.AllowOpenInvite {
   536  				t.Fatal("should have returned team with AllowOpenInvite as true")
   537  			}
   538  		}
   539  
   540  		if len(teams) > 10 {
   541  			t.Fatal("should have returned max of 10 teams")
   542  		}
   543  	}
   544  
   545  	o5 := model.Team{}
   546  	o5.DisplayName = "DisplayName"
   547  	o5.Name = "z-z-z" + model.NewId() + "b"
   548  	o5.Email = MakeEmail()
   549  	o5.Type = model.TEAM_OPEN
   550  	o5.AllowOpenInvite = true
   551  	store.Must(ss.Team().Save(&o5))
   552  
   553  	if r1 := <-ss.Team().GetAllTeamPageListing(0, 4); r1.Err != nil {
   554  		t.Fatal(r1.Err)
   555  	} else {
   556  		teams := r1.Data.([]*model.Team)
   557  
   558  		for _, team := range teams {
   559  			if !team.AllowOpenInvite {
   560  				t.Fatal("should have returned team with AllowOpenInvite as true")
   561  			}
   562  		}
   563  
   564  		if len(teams) > 4 {
   565  			t.Fatal("should have returned max of 4 teams")
   566  		}
   567  	}
   568  
   569  	if r1 := <-ss.Team().GetAllTeamPageListing(1, 1); r1.Err != nil {
   570  		t.Fatal(r1.Err)
   571  	} else {
   572  		teams := r1.Data.([]*model.Team)
   573  
   574  		for _, team := range teams {
   575  			if !team.AllowOpenInvite {
   576  				t.Fatal("should have returned team with AllowOpenInvite as true")
   577  			}
   578  		}
   579  
   580  		if len(teams) > 1 {
   581  			t.Fatal("should have returned max of 1 team")
   582  		}
   583  	}
   584  }
   585  
   586  func testGetAllPrivateTeamListing(t *testing.T, ss store.Store) {
   587  	o1 := model.Team{}
   588  	o1.DisplayName = "DisplayName"
   589  	o1.Name = "z-z-z" + model.NewId() + "b"
   590  	o1.Email = MakeEmail()
   591  	o1.Type = model.TEAM_OPEN
   592  	o1.AllowOpenInvite = true
   593  	store.Must(ss.Team().Save(&o1))
   594  
   595  	o2 := model.Team{}
   596  	o2.DisplayName = "DisplayName"
   597  	o2.Name = "zz" + model.NewId() + "b"
   598  	o2.Email = MakeEmail()
   599  	o2.Type = model.TEAM_OPEN
   600  	store.Must(ss.Team().Save(&o2))
   601  
   602  	o3 := model.Team{}
   603  	o3.DisplayName = "DisplayName"
   604  	o3.Name = "z-z-z" + model.NewId() + "b"
   605  	o3.Email = MakeEmail()
   606  	o3.Type = model.TEAM_INVITE
   607  	o3.AllowOpenInvite = true
   608  	store.Must(ss.Team().Save(&o3))
   609  
   610  	o4 := model.Team{}
   611  	o4.DisplayName = "DisplayName"
   612  	o4.Name = "zz" + model.NewId() + "b"
   613  	o4.Email = MakeEmail()
   614  	o4.Type = model.TEAM_INVITE
   615  	store.Must(ss.Team().Save(&o4))
   616  
   617  	if r1 := <-ss.Team().GetAllPrivateTeamListing(); r1.Err != nil {
   618  		t.Fatal(r1.Err)
   619  	} else {
   620  		teams := r1.Data.([]*model.Team)
   621  
   622  		for _, team := range teams {
   623  			if team.AllowOpenInvite {
   624  				t.Fatal("should have returned team with AllowOpenInvite as false")
   625  			}
   626  		}
   627  
   628  		if len(teams) == 0 {
   629  			t.Fatal("failed team listing")
   630  		}
   631  	}
   632  }
   633  
   634  func testGetAllPrivateTeamPageListing(t *testing.T, ss store.Store) {
   635  	o1 := model.Team{}
   636  	o1.DisplayName = "DisplayName"
   637  	o1.Name = "z-z-z" + model.NewId() + "b"
   638  	o1.Email = MakeEmail()
   639  	o1.Type = model.TEAM_OPEN
   640  	o1.AllowOpenInvite = true
   641  	store.Must(ss.Team().Save(&o1))
   642  
   643  	o2 := model.Team{}
   644  	o2.DisplayName = "DisplayName"
   645  	o2.Name = "zz" + model.NewId() + "b"
   646  	o2.Email = MakeEmail()
   647  	o2.Type = model.TEAM_OPEN
   648  	o2.AllowOpenInvite = false
   649  	store.Must(ss.Team().Save(&o2))
   650  
   651  	o3 := model.Team{}
   652  	o3.DisplayName = "DisplayName"
   653  	o3.Name = "z-z-z" + model.NewId() + "b"
   654  	o3.Email = MakeEmail()
   655  	o3.Type = model.TEAM_INVITE
   656  	o3.AllowOpenInvite = true
   657  	store.Must(ss.Team().Save(&o3))
   658  
   659  	o4 := model.Team{}
   660  	o4.DisplayName = "DisplayName"
   661  	o4.Name = "zz" + model.NewId() + "b"
   662  	o4.Email = MakeEmail()
   663  	o4.Type = model.TEAM_INVITE
   664  	o4.AllowOpenInvite = false
   665  	store.Must(ss.Team().Save(&o4))
   666  
   667  	if r1 := <-ss.Team().GetAllPrivateTeamPageListing(0, 10); r1.Err != nil {
   668  		t.Fatal(r1.Err)
   669  	} else {
   670  		teams := r1.Data.([]*model.Team)
   671  
   672  		for _, team := range teams {
   673  			if team.AllowOpenInvite {
   674  				t.Fatal("should have returned team with AllowOpenInvite as false")
   675  			}
   676  		}
   677  
   678  		if len(teams) > 10 {
   679  			t.Fatal("should have returned max of 10 teams")
   680  		}
   681  	}
   682  
   683  	o5 := model.Team{}
   684  	o5.DisplayName = "DisplayName"
   685  	o5.Name = "z-z-z" + model.NewId() + "b"
   686  	o5.Email = MakeEmail()
   687  	o5.Type = model.TEAM_OPEN
   688  	o5.AllowOpenInvite = true
   689  	store.Must(ss.Team().Save(&o5))
   690  
   691  	if r1 := <-ss.Team().GetAllPrivateTeamPageListing(0, 4); r1.Err != nil {
   692  		t.Fatal(r1.Err)
   693  	} else {
   694  		teams := r1.Data.([]*model.Team)
   695  
   696  		for _, team := range teams {
   697  			if team.AllowOpenInvite {
   698  				t.Fatal("should have returned team with AllowOpenInvite as false")
   699  			}
   700  		}
   701  
   702  		if len(teams) > 4 {
   703  			t.Fatal("should have returned max of 4 teams")
   704  		}
   705  	}
   706  
   707  	if r1 := <-ss.Team().GetAllPrivateTeamPageListing(1, 1); r1.Err != nil {
   708  		t.Fatal(r1.Err)
   709  	} else {
   710  		teams := r1.Data.([]*model.Team)
   711  
   712  		for _, team := range teams {
   713  			if team.AllowOpenInvite {
   714  				t.Fatal("should have returned team with AllowOpenInvite as false")
   715  			}
   716  		}
   717  
   718  		if len(teams) > 1 {
   719  			t.Fatal("should have returned max of 1 team")
   720  		}
   721  	}
   722  }
   723  
   724  func testDelete(t *testing.T, ss store.Store) {
   725  	o1 := model.Team{}
   726  	o1.DisplayName = "DisplayName"
   727  	o1.Name = "z-z-z" + model.NewId() + "b"
   728  	o1.Email = MakeEmail()
   729  	o1.Type = model.TEAM_OPEN
   730  	o1.AllowOpenInvite = true
   731  	store.Must(ss.Team().Save(&o1))
   732  
   733  	o2 := model.Team{}
   734  	o2.DisplayName = "DisplayName"
   735  	o2.Name = "zz" + model.NewId() + "b"
   736  	o2.Email = MakeEmail()
   737  	o2.Type = model.TEAM_OPEN
   738  	store.Must(ss.Team().Save(&o2))
   739  
   740  	if r1 := <-ss.Team().PermanentDelete(o1.Id); r1.Err != nil {
   741  		t.Fatal(r1.Err)
   742  	}
   743  }
   744  
   745  func testTeamCount(t *testing.T, ss store.Store) {
   746  	o1 := model.Team{}
   747  	o1.DisplayName = "DisplayName"
   748  	o1.Name = "z-z-z" + model.NewId() + "b"
   749  	o1.Email = MakeEmail()
   750  	o1.Type = model.TEAM_OPEN
   751  	o1.AllowOpenInvite = true
   752  	store.Must(ss.Team().Save(&o1))
   753  
   754  	if r1 := <-ss.Team().AnalyticsTeamCount(); r1.Err != nil {
   755  		t.Fatal(r1.Err)
   756  	} else {
   757  		if r1.Data.(int64) == 0 {
   758  			t.Fatal("should be at least 1 team")
   759  		}
   760  	}
   761  }
   762  
   763  func testTeamMembers(t *testing.T, ss store.Store) {
   764  	teamId1 := model.NewId()
   765  	teamId2 := model.NewId()
   766  
   767  	m1 := &model.TeamMember{TeamId: teamId1, UserId: model.NewId()}
   768  	m2 := &model.TeamMember{TeamId: teamId1, UserId: model.NewId()}
   769  	m3 := &model.TeamMember{TeamId: teamId2, UserId: model.NewId()}
   770  
   771  	if r1 := <-ss.Team().SaveMember(m1, -1); r1.Err != nil {
   772  		t.Fatal(r1.Err)
   773  	}
   774  
   775  	store.Must(ss.Team().SaveMember(m2, -1))
   776  	store.Must(ss.Team().SaveMember(m3, -1))
   777  
   778  	if r1 := <-ss.Team().GetMembers(teamId1, 0, 100); r1.Err != nil {
   779  		t.Fatal(r1.Err)
   780  	} else {
   781  		ms := r1.Data.([]*model.TeamMember)
   782  		require.Len(t, ms, 2)
   783  	}
   784  
   785  	if r1 := <-ss.Team().GetMembers(teamId2, 0, 100); r1.Err != nil {
   786  		t.Fatal(r1.Err)
   787  	} else {
   788  		ms := r1.Data.([]*model.TeamMember)
   789  
   790  		require.Len(t, ms, 1)
   791  		require.Equal(t, m3.UserId, ms[0].UserId)
   792  	}
   793  
   794  	if r1 := <-ss.Team().GetTeamsForUser(m1.UserId); r1.Err != nil {
   795  		t.Fatal(r1.Err)
   796  	} else {
   797  		ms := r1.Data.([]*model.TeamMember)
   798  
   799  		require.Len(t, ms, 1)
   800  		require.Equal(t, m1.TeamId, ms[0].TeamId)
   801  	}
   802  
   803  	if r1 := <-ss.Team().RemoveMember(teamId1, m1.UserId); r1.Err != nil {
   804  		t.Fatal(r1.Err)
   805  	}
   806  
   807  	if r1 := <-ss.Team().GetMembers(teamId1, 0, 100); r1.Err != nil {
   808  		t.Fatal(r1.Err)
   809  	} else {
   810  		ms := r1.Data.([]*model.TeamMember)
   811  
   812  		require.Len(t, ms, 1)
   813  		require.Equal(t, m2.UserId, ms[0].UserId)
   814  	}
   815  
   816  	store.Must(ss.Team().SaveMember(m1, -1))
   817  
   818  	if r1 := <-ss.Team().RemoveAllMembersByTeam(teamId1); r1.Err != nil {
   819  		t.Fatal(r1.Err)
   820  	}
   821  
   822  	if r1 := <-ss.Team().GetMembers(teamId1, 0, 100); r1.Err != nil {
   823  		t.Fatal(r1.Err)
   824  	} else {
   825  		ms := r1.Data.([]*model.TeamMember)
   826  
   827  		require.Len(t, ms, 0)
   828  	}
   829  
   830  	uid := model.NewId()
   831  	m4 := &model.TeamMember{TeamId: teamId1, UserId: uid}
   832  	m5 := &model.TeamMember{TeamId: teamId2, UserId: uid}
   833  	store.Must(ss.Team().SaveMember(m4, -1))
   834  	store.Must(ss.Team().SaveMember(m5, -1))
   835  
   836  	if r1 := <-ss.Team().GetTeamsForUser(uid); r1.Err != nil {
   837  		t.Fatal(r1.Err)
   838  	} else {
   839  		ms := r1.Data.([]*model.TeamMember)
   840  
   841  		require.Len(t, ms, 2)
   842  	}
   843  
   844  	if r1 := <-ss.Team().RemoveAllMembersByUser(uid); r1.Err != nil {
   845  		t.Fatal(r1.Err)
   846  	}
   847  
   848  	if r1 := <-ss.Team().GetTeamsForUser(m1.UserId); r1.Err != nil {
   849  		t.Fatal(r1.Err)
   850  	} else {
   851  		ms := r1.Data.([]*model.TeamMember)
   852  
   853  		require.Len(t, ms, 0)
   854  	}
   855  }
   856  
   857  func testTeamMembersWithPagination(t *testing.T, ss store.Store) {
   858  	teamId1 := model.NewId()
   859  	teamId2 := model.NewId()
   860  
   861  	m1 := &model.TeamMember{TeamId: teamId1, UserId: model.NewId()}
   862  	m2 := &model.TeamMember{TeamId: teamId1, UserId: model.NewId()}
   863  	m3 := &model.TeamMember{TeamId: teamId2, UserId: model.NewId()}
   864  
   865  	r1 := <-ss.Team().SaveMember(m1, -1)
   866  	require.Nil(t, r1.Err)
   867  
   868  	store.Must(ss.Team().SaveMember(m2, -1))
   869  	store.Must(ss.Team().SaveMember(m3, -1))
   870  
   871  	r1 = <-ss.Team().GetTeamsForUserWithPagination(m1.UserId, 0, 1)
   872  	require.Nil(t, r1.Err)
   873  	ms := r1.Data.([]*model.TeamMember)
   874  
   875  	require.Len(t, ms, 1)
   876  	require.Equal(t, m1.TeamId, ms[0].TeamId)
   877  
   878  	r1 = <-ss.Team().RemoveMember(teamId1, m1.UserId)
   879  	require.Nil(t, r1.Err)
   880  
   881  	r1 = <-ss.Team().GetMembers(teamId1, 0, 100)
   882  	require.Nil(t, r1.Err)
   883  
   884  	ms = r1.Data.([]*model.TeamMember)
   885  	require.Len(t, ms, 1)
   886  	require.Equal(t, m2.UserId, ms[0].UserId)
   887  
   888  	store.Must(ss.Team().SaveMember(m1, -1))
   889  
   890  	r1 = <-ss.Team().RemoveAllMembersByTeam(teamId1)
   891  	require.Nil(t, r1.Err)
   892  
   893  	uid := model.NewId()
   894  	m4 := &model.TeamMember{TeamId: teamId1, UserId: uid}
   895  	m5 := &model.TeamMember{TeamId: teamId2, UserId: uid}
   896  	store.Must(ss.Team().SaveMember(m4, -1))
   897  	store.Must(ss.Team().SaveMember(m5, -1))
   898  
   899  	r1 = <-ss.Team().GetTeamsForUserWithPagination(uid, 0, 1)
   900  	require.Nil(t, r1.Err)
   901  	ms = r1.Data.([]*model.TeamMember)
   902  	require.Len(t, ms, 1)
   903  
   904  	r1 = <-ss.Team().RemoveAllMembersByUser(uid)
   905  	require.Nil(t, r1.Err)
   906  
   907  	r1 = <-ss.Team().GetTeamsForUserWithPagination(uid, 1, 1)
   908  	require.Nil(t, r1.Err)
   909  	ms = r1.Data.([]*model.TeamMember)
   910  	require.Len(t, ms, 0)
   911  }
   912  
   913  func testSaveTeamMemberMaxMembers(t *testing.T, ss store.Store) {
   914  	maxUsersPerTeam := 5
   915  
   916  	team := store.Must(ss.Team().Save(&model.Team{
   917  		DisplayName: "DisplayName",
   918  		Name:        "z-z-z" + model.NewId() + "b",
   919  		Type:        model.TEAM_OPEN,
   920  	})).(*model.Team)
   921  	defer func() {
   922  		<-ss.Team().PermanentDelete(team.Id)
   923  	}()
   924  
   925  	userIds := make([]string, maxUsersPerTeam)
   926  
   927  	for i := 0; i < maxUsersPerTeam; i++ {
   928  		userIds[i] = store.Must(ss.User().Save(&model.User{
   929  			Username: model.NewId(),
   930  			Email:    MakeEmail(),
   931  		})).(*model.User).Id
   932  
   933  		defer func(userId string) {
   934  			<-ss.User().PermanentDelete(userId)
   935  		}(userIds[i])
   936  
   937  		store.Must(ss.Team().SaveMember(&model.TeamMember{
   938  			TeamId: team.Id,
   939  			UserId: userIds[i],
   940  		}, maxUsersPerTeam))
   941  
   942  		defer func(userId string) {
   943  			<-ss.Team().RemoveMember(team.Id, userId)
   944  		}(userIds[i])
   945  	}
   946  
   947  	if result := <-ss.Team().GetTotalMemberCount(team.Id); result.Err != nil {
   948  		t.Fatal(result.Err)
   949  	} else if count := result.Data.(int64); int(count) != maxUsersPerTeam {
   950  		t.Fatalf("should start with 5 team members, had %v instead", count)
   951  	}
   952  
   953  	newUserId := store.Must(ss.User().Save(&model.User{
   954  		Username: model.NewId(),
   955  		Email:    MakeEmail(),
   956  	})).(*model.User).Id
   957  	defer func() {
   958  		<-ss.User().PermanentDelete(newUserId)
   959  	}()
   960  
   961  	if result := <-ss.Team().SaveMember(&model.TeamMember{
   962  		TeamId: team.Id,
   963  		UserId: newUserId,
   964  	}, maxUsersPerTeam); result.Err == nil {
   965  		t.Fatal("shouldn't be able to save member when at maximum members per team")
   966  	}
   967  
   968  	if result := <-ss.Team().GetTotalMemberCount(team.Id); result.Err != nil {
   969  		t.Fatal(result.Err)
   970  	} else if count := result.Data.(int64); int(count) != maxUsersPerTeam {
   971  		t.Fatalf("should still have 5 team members, had %v instead", count)
   972  	}
   973  
   974  	// Leaving the team from the UI sets DeleteAt instead of using TeamStore.RemoveMember
   975  	store.Must(ss.Team().UpdateMember(&model.TeamMember{
   976  		TeamId:   team.Id,
   977  		UserId:   userIds[0],
   978  		DeleteAt: 1234,
   979  	}))
   980  
   981  	if result := <-ss.Team().GetTotalMemberCount(team.Id); result.Err != nil {
   982  		t.Fatal(result.Err)
   983  	} else if count := result.Data.(int64); int(count) != maxUsersPerTeam-1 {
   984  		t.Fatalf("should now only have 4 team members, had %v instead", count)
   985  	}
   986  
   987  	if result := <-ss.Team().SaveMember(&model.TeamMember{TeamId: team.Id, UserId: newUserId}, maxUsersPerTeam); result.Err != nil {
   988  		t.Fatal("should've been able to save new member after deleting one", result.Err)
   989  	} else {
   990  		defer func(userId string) {
   991  			<-ss.Team().RemoveMember(team.Id, userId)
   992  		}(newUserId)
   993  	}
   994  
   995  	if result := <-ss.Team().GetTotalMemberCount(team.Id); result.Err != nil {
   996  		t.Fatal(result.Err)
   997  	} else if count := result.Data.(int64); int(count) != maxUsersPerTeam {
   998  		t.Fatalf("should have 5 team members again, had %v instead", count)
   999  	}
  1000  
  1001  	// Deactivating a user should make them stop counting against max members
  1002  	user2 := store.Must(ss.User().Get(userIds[1])).(*model.User)
  1003  	user2.DeleteAt = 1234
  1004  	store.Must(ss.User().Update(user2, true))
  1005  
  1006  	newUserId2 := store.Must(ss.User().Save(&model.User{
  1007  		Username: model.NewId(),
  1008  		Email:    MakeEmail(),
  1009  	})).(*model.User).Id
  1010  	if result := <-ss.Team().SaveMember(&model.TeamMember{TeamId: team.Id, UserId: newUserId2}, maxUsersPerTeam); result.Err != nil {
  1011  		t.Fatal("should've been able to save new member after deleting one", result.Err)
  1012  	} else {
  1013  		defer func(userId string) {
  1014  			<-ss.Team().RemoveMember(team.Id, userId)
  1015  		}(newUserId2)
  1016  	}
  1017  }
  1018  
  1019  func testGetTeamMember(t *testing.T, ss store.Store) {
  1020  	teamId1 := model.NewId()
  1021  
  1022  	m1 := &model.TeamMember{TeamId: teamId1, UserId: model.NewId()}
  1023  	store.Must(ss.Team().SaveMember(m1, -1))
  1024  
  1025  	if r := <-ss.Team().GetMember(m1.TeamId, m1.UserId); r.Err != nil {
  1026  		t.Fatal(r.Err)
  1027  	} else {
  1028  		rm1 := r.Data.(*model.TeamMember)
  1029  
  1030  		if rm1.TeamId != m1.TeamId {
  1031  			t.Fatal("bad team id")
  1032  		}
  1033  
  1034  		if rm1.UserId != m1.UserId {
  1035  			t.Fatal("bad user id")
  1036  		}
  1037  	}
  1038  
  1039  	if r := <-ss.Team().GetMember(m1.TeamId, ""); r.Err == nil {
  1040  		t.Fatal("empty user id - should have failed")
  1041  	}
  1042  
  1043  	if r := <-ss.Team().GetMember("", m1.UserId); r.Err == nil {
  1044  		t.Fatal("empty team id - should have failed")
  1045  	}
  1046  
  1047  	// Test with a custom team scheme.
  1048  	s2 := &model.Scheme{
  1049  		Name:        model.NewId(),
  1050  		DisplayName: model.NewId(),
  1051  		Description: model.NewId(),
  1052  		Scope:       model.SCHEME_SCOPE_TEAM,
  1053  	}
  1054  	s2 = (<-ss.Scheme().Save(s2)).Data.(*model.Scheme)
  1055  	t.Log(s2)
  1056  
  1057  	t2 := store.Must(ss.Team().Save(&model.Team{
  1058  		DisplayName: "DisplayName",
  1059  		Name:        "z-z-z" + model.NewId() + "b",
  1060  		Type:        model.TEAM_OPEN,
  1061  		SchemeId:    &s2.Id,
  1062  	})).(*model.Team)
  1063  
  1064  	defer func() {
  1065  		<-ss.Team().PermanentDelete(t2.Id)
  1066  	}()
  1067  
  1068  	m2 := &model.TeamMember{TeamId: t2.Id, UserId: model.NewId(), SchemeUser: true}
  1069  	store.Must(ss.Team().SaveMember(m2, -1))
  1070  
  1071  	r2 := <-ss.Team().GetMember(m2.TeamId, m2.UserId)
  1072  	require.Nil(t, r2.Err)
  1073  	m3 := r2.Data.(*model.TeamMember)
  1074  	t.Log(m3)
  1075  
  1076  	assert.Equal(t, s2.DefaultTeamUserRole, m3.Roles)
  1077  }
  1078  
  1079  func testGetTeamMembersByIds(t *testing.T, ss store.Store) {
  1080  	teamId1 := model.NewId()
  1081  
  1082  	m1 := &model.TeamMember{TeamId: teamId1, UserId: model.NewId()}
  1083  	store.Must(ss.Team().SaveMember(m1, -1))
  1084  
  1085  	if r := <-ss.Team().GetMembersByIds(m1.TeamId, []string{m1.UserId}); r.Err != nil {
  1086  		t.Fatal(r.Err)
  1087  	} else {
  1088  		rm1 := r.Data.([]*model.TeamMember)[0]
  1089  
  1090  		if rm1.TeamId != m1.TeamId {
  1091  			t.Fatal("bad team id")
  1092  		}
  1093  
  1094  		if rm1.UserId != m1.UserId {
  1095  			t.Fatal("bad user id")
  1096  		}
  1097  	}
  1098  
  1099  	m2 := &model.TeamMember{TeamId: teamId1, UserId: model.NewId()}
  1100  	store.Must(ss.Team().SaveMember(m2, -1))
  1101  
  1102  	if r := <-ss.Team().GetMembersByIds(m1.TeamId, []string{m1.UserId, m2.UserId, model.NewId()}); r.Err != nil {
  1103  		t.Fatal(r.Err)
  1104  	} else {
  1105  		rm := r.Data.([]*model.TeamMember)
  1106  
  1107  		if len(rm) != 2 {
  1108  			t.Fatal("return wrong number of results")
  1109  		}
  1110  	}
  1111  
  1112  	if r := <-ss.Team().GetMembersByIds(m1.TeamId, []string{}); r.Err == nil {
  1113  		t.Fatal("empty user ids - should have failed")
  1114  	}
  1115  }
  1116  
  1117  func testTeamStoreMemberCount(t *testing.T, ss store.Store) {
  1118  	u1 := &model.User{}
  1119  	u1.Email = MakeEmail()
  1120  	store.Must(ss.User().Save(u1))
  1121  
  1122  	u2 := &model.User{}
  1123  	u2.Email = MakeEmail()
  1124  	u2.DeleteAt = 1
  1125  	store.Must(ss.User().Save(u2))
  1126  
  1127  	teamId1 := model.NewId()
  1128  	m1 := &model.TeamMember{TeamId: teamId1, UserId: u1.Id}
  1129  	store.Must(ss.Team().SaveMember(m1, -1))
  1130  
  1131  	m2 := &model.TeamMember{TeamId: teamId1, UserId: u2.Id}
  1132  	store.Must(ss.Team().SaveMember(m2, -1))
  1133  
  1134  	if result := <-ss.Team().GetTotalMemberCount(teamId1); result.Err != nil {
  1135  		t.Fatal(result.Err)
  1136  	} else {
  1137  		if result.Data.(int64) != 2 {
  1138  			t.Fatal("wrong count")
  1139  		}
  1140  	}
  1141  
  1142  	if result := <-ss.Team().GetActiveMemberCount(teamId1); result.Err != nil {
  1143  		t.Fatal(result.Err)
  1144  	} else {
  1145  		if result.Data.(int64) != 1 {
  1146  			t.Fatal("wrong count")
  1147  		}
  1148  	}
  1149  
  1150  	m3 := &model.TeamMember{TeamId: teamId1, UserId: model.NewId()}
  1151  	store.Must(ss.Team().SaveMember(m3, -1))
  1152  
  1153  	if result := <-ss.Team().GetTotalMemberCount(teamId1); result.Err != nil {
  1154  		t.Fatal(result.Err)
  1155  	} else {
  1156  		if result.Data.(int64) != 2 {
  1157  			t.Fatal("wrong count")
  1158  		}
  1159  	}
  1160  
  1161  	if result := <-ss.Team().GetActiveMemberCount(teamId1); result.Err != nil {
  1162  		t.Fatal(result.Err)
  1163  	} else {
  1164  		if result.Data.(int64) != 1 {
  1165  			t.Fatal("wrong count")
  1166  		}
  1167  	}
  1168  }
  1169  
  1170  func testGetChannelUnreadsForAllTeams(t *testing.T, ss store.Store) {
  1171  	teamId1 := model.NewId()
  1172  	teamId2 := model.NewId()
  1173  
  1174  	uid := model.NewId()
  1175  	m1 := &model.TeamMember{TeamId: teamId1, UserId: uid}
  1176  	m2 := &model.TeamMember{TeamId: teamId2, UserId: uid}
  1177  	store.Must(ss.Team().SaveMember(m1, -1))
  1178  	store.Must(ss.Team().SaveMember(m2, -1))
  1179  
  1180  	c1 := &model.Channel{TeamId: m1.TeamId, Name: model.NewId(), DisplayName: "Town Square", Type: model.CHANNEL_OPEN, TotalMsgCount: 100}
  1181  	store.Must(ss.Channel().Save(c1, -1))
  1182  	c2 := &model.Channel{TeamId: m2.TeamId, Name: model.NewId(), DisplayName: "Town Square", Type: model.CHANNEL_OPEN, TotalMsgCount: 100}
  1183  	store.Must(ss.Channel().Save(c2, -1))
  1184  
  1185  	cm1 := &model.ChannelMember{ChannelId: c1.Id, UserId: m1.UserId, NotifyProps: model.GetDefaultChannelNotifyProps(), MsgCount: 90}
  1186  	store.Must(ss.Channel().SaveMember(cm1))
  1187  	cm2 := &model.ChannelMember{ChannelId: c2.Id, UserId: m2.UserId, NotifyProps: model.GetDefaultChannelNotifyProps(), MsgCount: 90}
  1188  	store.Must(ss.Channel().SaveMember(cm2))
  1189  
  1190  	if r1 := <-ss.Team().GetChannelUnreadsForAllTeams("", uid); r1.Err != nil {
  1191  		t.Fatal(r1.Err)
  1192  	} else {
  1193  		ms := r1.Data.([]*model.ChannelUnread)
  1194  		membersMap := make(map[string]bool)
  1195  		for i := range ms {
  1196  			id := ms[i].TeamId
  1197  			if _, ok := membersMap[id]; !ok {
  1198  				membersMap[id] = true
  1199  			}
  1200  		}
  1201  		if len(membersMap) != 2 {
  1202  			t.Fatal("Should be the unreads for all the teams")
  1203  		}
  1204  
  1205  		if ms[0].MsgCount != 10 {
  1206  			t.Fatal("subtraction failed")
  1207  		}
  1208  	}
  1209  
  1210  	if r2 := <-ss.Team().GetChannelUnreadsForAllTeams(teamId1, uid); r2.Err != nil {
  1211  		t.Fatal(r2.Err)
  1212  	} else {
  1213  		ms := r2.Data.([]*model.ChannelUnread)
  1214  		membersMap := make(map[string]bool)
  1215  		for i := range ms {
  1216  			id := ms[i].TeamId
  1217  			if _, ok := membersMap[id]; !ok {
  1218  				membersMap[id] = true
  1219  			}
  1220  		}
  1221  
  1222  		if len(membersMap) != 1 {
  1223  			t.Fatal("Should be the unreads for just one team")
  1224  		}
  1225  
  1226  		if ms[0].MsgCount != 10 {
  1227  			t.Fatal("subtraction failed")
  1228  		}
  1229  	}
  1230  
  1231  	if r1 := <-ss.Team().RemoveAllMembersByUser(uid); r1.Err != nil {
  1232  		t.Fatal(r1.Err)
  1233  	}
  1234  }
  1235  
  1236  func testGetChannelUnreadsForTeam(t *testing.T, ss store.Store) {
  1237  	teamId1 := model.NewId()
  1238  
  1239  	uid := model.NewId()
  1240  	m1 := &model.TeamMember{TeamId: teamId1, UserId: uid}
  1241  	store.Must(ss.Team().SaveMember(m1, -1))
  1242  
  1243  	c1 := &model.Channel{TeamId: m1.TeamId, Name: model.NewId(), DisplayName: "Town Square", Type: model.CHANNEL_OPEN, TotalMsgCount: 100}
  1244  	store.Must(ss.Channel().Save(c1, -1))
  1245  	c2 := &model.Channel{TeamId: m1.TeamId, Name: model.NewId(), DisplayName: "Town Square", Type: model.CHANNEL_OPEN, TotalMsgCount: 100}
  1246  	store.Must(ss.Channel().Save(c2, -1))
  1247  
  1248  	cm1 := &model.ChannelMember{ChannelId: c1.Id, UserId: m1.UserId, NotifyProps: model.GetDefaultChannelNotifyProps(), MsgCount: 90}
  1249  	store.Must(ss.Channel().SaveMember(cm1))
  1250  	cm2 := &model.ChannelMember{ChannelId: c2.Id, UserId: m1.UserId, NotifyProps: model.GetDefaultChannelNotifyProps(), MsgCount: 90}
  1251  	store.Must(ss.Channel().SaveMember(cm2))
  1252  
  1253  	if r1 := <-ss.Team().GetChannelUnreadsForTeam(m1.TeamId, m1.UserId); r1.Err != nil {
  1254  		t.Fatal(r1.Err)
  1255  	} else {
  1256  		ms := r1.Data.([]*model.ChannelUnread)
  1257  		if len(ms) != 2 {
  1258  			t.Fatal("wrong length")
  1259  		}
  1260  
  1261  		if ms[0].MsgCount != 10 {
  1262  			t.Fatal("subtraction failed")
  1263  		}
  1264  	}
  1265  }
  1266  
  1267  func testUpdateLastTeamIconUpdate(t *testing.T, ss store.Store) {
  1268  
  1269  	// team icon initially updated a second ago
  1270  	lastTeamIconUpdateInitial := model.GetMillis() - 1000
  1271  
  1272  	o1 := &model.Team{}
  1273  	o1.DisplayName = "Display Name"
  1274  	o1.Name = "z-z-z" + model.NewId() + "b"
  1275  	o1.Email = MakeEmail()
  1276  	o1.Type = model.TEAM_OPEN
  1277  	o1.LastTeamIconUpdate = lastTeamIconUpdateInitial
  1278  	o1 = (<-ss.Team().Save(o1)).Data.(*model.Team)
  1279  
  1280  	curTime := model.GetMillis()
  1281  
  1282  	if err := (<-ss.Team().UpdateLastTeamIconUpdate(o1.Id, curTime)).Err; err != nil {
  1283  		t.Fatal(err)
  1284  	}
  1285  
  1286  	ro1 := (<-ss.Team().Get(o1.Id)).Data.(*model.Team)
  1287  	if ro1.LastTeamIconUpdate <= lastTeamIconUpdateInitial {
  1288  		t.Fatal("LastTeamIconUpdate not updated")
  1289  	}
  1290  }
  1291  
  1292  func testGetTeamsByScheme(t *testing.T, ss store.Store) {
  1293  	// Create some schemes.
  1294  	s1 := &model.Scheme{
  1295  		DisplayName: model.NewId(),
  1296  		Name:        model.NewId(),
  1297  		Description: model.NewId(),
  1298  		Scope:       model.SCHEME_SCOPE_TEAM,
  1299  	}
  1300  
  1301  	s2 := &model.Scheme{
  1302  		DisplayName: model.NewId(),
  1303  		Name:        model.NewId(),
  1304  		Description: model.NewId(),
  1305  		Scope:       model.SCHEME_SCOPE_TEAM,
  1306  	}
  1307  
  1308  	s1 = (<-ss.Scheme().Save(s1)).Data.(*model.Scheme)
  1309  	s2 = (<-ss.Scheme().Save(s2)).Data.(*model.Scheme)
  1310  
  1311  	// Create and save some teams.
  1312  	t1 := &model.Team{
  1313  		Name:        model.NewId(),
  1314  		DisplayName: model.NewId(),
  1315  		Email:       MakeEmail(),
  1316  		Type:        model.TEAM_OPEN,
  1317  		SchemeId:    &s1.Id,
  1318  	}
  1319  
  1320  	t2 := &model.Team{
  1321  		Name:        model.NewId(),
  1322  		DisplayName: model.NewId(),
  1323  		Email:       MakeEmail(),
  1324  		Type:        model.TEAM_OPEN,
  1325  		SchemeId:    &s1.Id,
  1326  	}
  1327  
  1328  	t3 := &model.Team{
  1329  		Name:        model.NewId(),
  1330  		DisplayName: model.NewId(),
  1331  		Email:       MakeEmail(),
  1332  		Type:        model.TEAM_OPEN,
  1333  	}
  1334  
  1335  	_ = (<-ss.Team().Save(t1)).Data.(*model.Team)
  1336  	_ = (<-ss.Team().Save(t2)).Data.(*model.Team)
  1337  	_ = (<-ss.Team().Save(t3)).Data.(*model.Team)
  1338  
  1339  	// Get the teams by a valid Scheme ID.
  1340  	res1 := <-ss.Team().GetTeamsByScheme(s1.Id, 0, 100)
  1341  	assert.Nil(t, res1.Err)
  1342  	d1 := res1.Data.([]*model.Team)
  1343  	assert.Len(t, d1, 2)
  1344  
  1345  	// Get the teams by a valid Scheme ID where there aren't any matching Teams.
  1346  	res2 := <-ss.Team().GetTeamsByScheme(s2.Id, 0, 100)
  1347  	assert.Nil(t, res2.Err)
  1348  	d2 := res2.Data.([]*model.Team)
  1349  	assert.Len(t, d2, 0)
  1350  
  1351  	// Get the teams by an invalid Scheme ID.
  1352  	res3 := <-ss.Team().GetTeamsByScheme(model.NewId(), 0, 100)
  1353  	assert.Nil(t, res3.Err)
  1354  	d3 := res3.Data.([]*model.Team)
  1355  	assert.Len(t, d3, 0)
  1356  }
  1357  
  1358  func testTeamStoreMigrateTeamMembers(t *testing.T, ss store.Store) {
  1359  	s1 := model.NewId()
  1360  	t1 := &model.Team{
  1361  		DisplayName: "Name",
  1362  		Name:        "z-z-z" + model.NewId() + "b",
  1363  		Email:       MakeEmail(),
  1364  		Type:        model.TEAM_OPEN,
  1365  		InviteId:    model.NewId(),
  1366  		SchemeId:    &s1,
  1367  	}
  1368  	t1 = store.Must(ss.Team().Save(t1)).(*model.Team)
  1369  
  1370  	tm1 := &model.TeamMember{
  1371  		TeamId:        t1.Id,
  1372  		UserId:        model.NewId(),
  1373  		ExplicitRoles: "team_admin team_user",
  1374  	}
  1375  	tm2 := &model.TeamMember{
  1376  		TeamId:        t1.Id,
  1377  		UserId:        model.NewId(),
  1378  		ExplicitRoles: "team_user",
  1379  	}
  1380  	tm3 := &model.TeamMember{
  1381  		TeamId:        t1.Id,
  1382  		UserId:        model.NewId(),
  1383  		ExplicitRoles: "something_else",
  1384  	}
  1385  
  1386  	tm1 = (<-ss.Team().SaveMember(tm1, -1)).Data.(*model.TeamMember)
  1387  	tm2 = (<-ss.Team().SaveMember(tm2, -1)).Data.(*model.TeamMember)
  1388  	tm3 = (<-ss.Team().SaveMember(tm3, -1)).Data.(*model.TeamMember)
  1389  
  1390  	lastDoneTeamId := strings.Repeat("0", 26)
  1391  	lastDoneUserId := strings.Repeat("0", 26)
  1392  
  1393  	for {
  1394  		res := <-ss.Team().MigrateTeamMembers(lastDoneTeamId, lastDoneUserId)
  1395  		if assert.Nil(t, res.Err) {
  1396  			if res.Data == nil {
  1397  				break
  1398  			}
  1399  			data := res.Data.(map[string]string)
  1400  			lastDoneTeamId = data["TeamId"]
  1401  			lastDoneUserId = data["UserId"]
  1402  		}
  1403  	}
  1404  
  1405  	res1 := <-ss.Team().GetMember(tm1.TeamId, tm1.UserId)
  1406  	assert.Nil(t, res1.Err)
  1407  	tm1b := res1.Data.(*model.TeamMember)
  1408  	assert.Equal(t, "", tm1b.ExplicitRoles)
  1409  	assert.True(t, tm1b.SchemeUser)
  1410  	assert.True(t, tm1b.SchemeAdmin)
  1411  
  1412  	res2 := <-ss.Team().GetMember(tm2.TeamId, tm2.UserId)
  1413  	assert.Nil(t, res2.Err)
  1414  	tm2b := res2.Data.(*model.TeamMember)
  1415  	assert.Equal(t, "", tm2b.ExplicitRoles)
  1416  	assert.True(t, tm2b.SchemeUser)
  1417  	assert.False(t, tm2b.SchemeAdmin)
  1418  
  1419  	res3 := <-ss.Team().GetMember(tm3.TeamId, tm3.UserId)
  1420  	assert.Nil(t, res3.Err)
  1421  	tm3b := res3.Data.(*model.TeamMember)
  1422  	assert.Equal(t, "something_else", tm3b.ExplicitRoles)
  1423  	assert.False(t, tm3b.SchemeUser)
  1424  	assert.False(t, tm3b.SchemeAdmin)
  1425  }
  1426  
  1427  func testResetAllTeamSchemes(t *testing.T, ss store.Store) {
  1428  	s1 := &model.Scheme{
  1429  		Name:        model.NewId(),
  1430  		DisplayName: model.NewId(),
  1431  		Description: model.NewId(),
  1432  		Scope:       model.SCHEME_SCOPE_TEAM,
  1433  	}
  1434  	s1 = (<-ss.Scheme().Save(s1)).Data.(*model.Scheme)
  1435  
  1436  	t1 := &model.Team{
  1437  		Name:        model.NewId(),
  1438  		DisplayName: model.NewId(),
  1439  		Email:       MakeEmail(),
  1440  		Type:        model.TEAM_OPEN,
  1441  		SchemeId:    &s1.Id,
  1442  	}
  1443  
  1444  	t2 := &model.Team{
  1445  		Name:        model.NewId(),
  1446  		DisplayName: model.NewId(),
  1447  		Email:       MakeEmail(),
  1448  		Type:        model.TEAM_OPEN,
  1449  		SchemeId:    &s1.Id,
  1450  	}
  1451  
  1452  	t1 = (<-ss.Team().Save(t1)).Data.(*model.Team)
  1453  	t2 = (<-ss.Team().Save(t2)).Data.(*model.Team)
  1454  
  1455  	assert.Equal(t, s1.Id, *t1.SchemeId)
  1456  	assert.Equal(t, s1.Id, *t2.SchemeId)
  1457  
  1458  	res := <-ss.Team().ResetAllTeamSchemes()
  1459  	assert.Nil(t, res.Err)
  1460  
  1461  	t1 = (<-ss.Team().Get(t1.Id)).Data.(*model.Team)
  1462  	t2 = (<-ss.Team().Get(t2.Id)).Data.(*model.Team)
  1463  
  1464  	assert.Equal(t, "", *t1.SchemeId)
  1465  	assert.Equal(t, "", *t2.SchemeId)
  1466  }
  1467  
  1468  func testTeamStoreClearAllCustomRoleAssignments(t *testing.T, ss store.Store) {
  1469  	m1 := &model.TeamMember{
  1470  		TeamId:        model.NewId(),
  1471  		UserId:        model.NewId(),
  1472  		ExplicitRoles: "team_user team_admin team_post_all_public",
  1473  	}
  1474  	m2 := &model.TeamMember{
  1475  		TeamId:        model.NewId(),
  1476  		UserId:        model.NewId(),
  1477  		ExplicitRoles: "team_user custom_role team_admin another_custom_role",
  1478  	}
  1479  	m3 := &model.TeamMember{
  1480  		TeamId:        model.NewId(),
  1481  		UserId:        model.NewId(),
  1482  		ExplicitRoles: "team_user",
  1483  	}
  1484  	m4 := &model.TeamMember{
  1485  		TeamId:        model.NewId(),
  1486  		UserId:        model.NewId(),
  1487  		ExplicitRoles: "custom_only",
  1488  	}
  1489  
  1490  	store.Must(ss.Team().SaveMember(m1, -1))
  1491  	store.Must(ss.Team().SaveMember(m2, -1))
  1492  	store.Must(ss.Team().SaveMember(m3, -1))
  1493  	store.Must(ss.Team().SaveMember(m4, -1))
  1494  
  1495  	require.Nil(t, (<-ss.Team().ClearAllCustomRoleAssignments()).Err)
  1496  
  1497  	r1 := <-ss.Team().GetMember(m1.TeamId, m1.UserId)
  1498  	require.Nil(t, r1.Err)
  1499  	assert.Equal(t, m1.ExplicitRoles, r1.Data.(*model.TeamMember).Roles)
  1500  
  1501  	r2 := <-ss.Team().GetMember(m2.TeamId, m2.UserId)
  1502  	require.Nil(t, r2.Err)
  1503  	assert.Equal(t, "team_user team_admin", r2.Data.(*model.TeamMember).Roles)
  1504  
  1505  	r3 := <-ss.Team().GetMember(m3.TeamId, m3.UserId)
  1506  	require.Nil(t, r3.Err)
  1507  	assert.Equal(t, m3.ExplicitRoles, r3.Data.(*model.TeamMember).Roles)
  1508  
  1509  	r4 := <-ss.Team().GetMember(m4.TeamId, m4.UserId)
  1510  	require.Nil(t, r4.Err)
  1511  	assert.Equal(t, "", r4.Data.(*model.TeamMember).Roles)
  1512  }
  1513  
  1514  func testTeamStoreAnalyticsGetTeamCountForScheme(t *testing.T, ss store.Store) {
  1515  	s1 := &model.Scheme{
  1516  		DisplayName: model.NewId(),
  1517  		Name:        model.NewId(),
  1518  		Description: model.NewId(),
  1519  		Scope:       model.SCHEME_SCOPE_TEAM,
  1520  	}
  1521  	s1 = (<-ss.Scheme().Save(s1)).Data.(*model.Scheme)
  1522  
  1523  	count1 := (<-ss.Team().AnalyticsGetTeamCountForScheme(s1.Id)).Data.(int64)
  1524  	assert.Equal(t, int64(0), count1)
  1525  
  1526  	t1 := &model.Team{
  1527  		Name:        model.NewId(),
  1528  		DisplayName: model.NewId(),
  1529  		Email:       MakeEmail(),
  1530  		Type:        model.TEAM_OPEN,
  1531  		SchemeId:    &s1.Id,
  1532  	}
  1533  	_ = (<-ss.Team().Save(t1)).Data.(*model.Team)
  1534  
  1535  	count2 := (<-ss.Team().AnalyticsGetTeamCountForScheme(s1.Id)).Data.(int64)
  1536  	assert.Equal(t, int64(1), count2)
  1537  
  1538  	t2 := &model.Team{
  1539  		Name:        model.NewId(),
  1540  		DisplayName: model.NewId(),
  1541  		Email:       MakeEmail(),
  1542  		Type:        model.TEAM_OPEN,
  1543  		SchemeId:    &s1.Id,
  1544  	}
  1545  	_ = (<-ss.Team().Save(t2)).Data.(*model.Team)
  1546  
  1547  	count3 := (<-ss.Team().AnalyticsGetTeamCountForScheme(s1.Id)).Data.(int64)
  1548  	assert.Equal(t, int64(2), count3)
  1549  
  1550  	t3 := &model.Team{
  1551  		Name:        model.NewId(),
  1552  		DisplayName: model.NewId(),
  1553  		Email:       MakeEmail(),
  1554  		Type:        model.TEAM_OPEN,
  1555  	}
  1556  	_ = (<-ss.Team().Save(t3)).Data.(*model.Team)
  1557  
  1558  	count4 := (<-ss.Team().AnalyticsGetTeamCountForScheme(s1.Id)).Data.(int64)
  1559  	assert.Equal(t, int64(2), count4)
  1560  
  1561  	t4 := &model.Team{
  1562  		Name:        model.NewId(),
  1563  		DisplayName: model.NewId(),
  1564  		Email:       MakeEmail(),
  1565  		Type:        model.TEAM_OPEN,
  1566  		SchemeId:    &s1.Id,
  1567  		DeleteAt:    model.GetMillis(),
  1568  	}
  1569  	_ = (<-ss.Team().Save(t4)).Data.(*model.Team)
  1570  
  1571  	count5 := (<-ss.Team().AnalyticsGetTeamCountForScheme(s1.Id)).Data.(int64)
  1572  	assert.Equal(t, int64(2), count5)
  1573  }
  1574  
  1575  func testTeamStoreGetAllForExportAfter(t *testing.T, ss store.Store) {
  1576  	t1 := model.Team{}
  1577  	t1.DisplayName = "Name"
  1578  	t1.Name = model.NewId()
  1579  	t1.Email = MakeEmail()
  1580  	t1.Type = model.TEAM_OPEN
  1581  	store.Must(ss.Team().Save(&t1))
  1582  
  1583  	r1 := <-ss.Team().GetAllForExportAfter(10000, strings.Repeat("0", 26))
  1584  	assert.Nil(t, r1.Err)
  1585  	d1 := r1.Data.([]*model.TeamForExport)
  1586  
  1587  	found := false
  1588  	for _, team := range d1 {
  1589  		if team.Id == t1.Id {
  1590  			found = true
  1591  			assert.Equal(t, t1.Id, team.Id)
  1592  			assert.Nil(t, team.SchemeId)
  1593  			assert.Equal(t, t1.Name, team.Name)
  1594  		}
  1595  	}
  1596  	assert.True(t, found)
  1597  }
  1598  
  1599  func testTeamStoreGetTeamMembersForExport(t *testing.T, ss store.Store) {
  1600  	t1 := model.Team{}
  1601  	t1.DisplayName = "Name"
  1602  	t1.Name = model.NewId()
  1603  	t1.Email = MakeEmail()
  1604  	t1.Type = model.TEAM_OPEN
  1605  	store.Must(ss.Team().Save(&t1))
  1606  
  1607  	u1 := model.User{}
  1608  	u1.Email = MakeEmail()
  1609  	u1.Nickname = model.NewId()
  1610  	store.Must(ss.User().Save(&u1))
  1611  
  1612  	u2 := model.User{}
  1613  	u2.Email = MakeEmail()
  1614  	u2.Nickname = model.NewId()
  1615  	store.Must(ss.User().Save(&u2))
  1616  
  1617  	m1 := &model.TeamMember{TeamId: t1.Id, UserId: u1.Id}
  1618  	store.Must(ss.Team().SaveMember(m1, -1))
  1619  
  1620  	m2 := &model.TeamMember{TeamId: t1.Id, UserId: u2.Id}
  1621  	store.Must(ss.Team().SaveMember(m2, -1))
  1622  
  1623  	r1 := <-ss.Team().GetTeamMembersForExport(u1.Id)
  1624  	assert.Nil(t, r1.Err)
  1625  
  1626  	d1 := r1.Data.([]*model.TeamMemberForExport)
  1627  	assert.Len(t, d1, 1)
  1628  
  1629  	tmfe1 := d1[0]
  1630  	assert.Equal(t, t1.Id, tmfe1.TeamId)
  1631  	assert.Equal(t, u1.Id, tmfe1.UserId)
  1632  	assert.Equal(t, t1.Name, tmfe1.TeamName)
  1633  }