github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+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  
    13  	"github.com/mattermost/mattermost-server/model"
    14  	"github.com/mattermost/mattermost-server/store"
    15  	"github.com/stretchr/testify/require"
    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("GetByIniviteId", func(t *testing.T) { testTeamStoreGetByIniviteId(t, ss) })
    30  	t.Run("ByUserId", func(t *testing.T) { testTeamStoreByUserId(t, ss) })
    31  	t.Run("GetAllTeamListing", func(t *testing.T) { testGetAllTeamListing(t, ss) })
    32  	t.Run("GetAllTeamPageListing", func(t *testing.T) { testGetAllTeamPageListing(t, ss) })
    33  	t.Run("Delete", func(t *testing.T) { testDelete(t, ss) })
    34  	t.Run("TeamCount", func(t *testing.T) { testTeamCount(t, ss) })
    35  	t.Run("TeamMembers", func(t *testing.T) { testTeamMembers(t, ss) })
    36  	t.Run("SaveTeamMemberMaxMembers", func(t *testing.T) { testSaveTeamMemberMaxMembers(t, ss) })
    37  	t.Run("GetTeamMember", func(t *testing.T) { testGetTeamMember(t, ss) })
    38  	t.Run("GetTeamMembersByIds", func(t *testing.T) { testGetTeamMembersByIds(t, ss) })
    39  	t.Run("MemberCount", func(t *testing.T) { testTeamStoreMemberCount(t, ss) })
    40  	t.Run("GetChannelUnreadsForAllTeams", func(t *testing.T) { testGetChannelUnreadsForAllTeams(t, ss) })
    41  	t.Run("GetChannelUnreadsForTeam", func(t *testing.T) { testGetChannelUnreadsForTeam(t, ss) })
    42  	t.Run("UpdateLastTeamIconUpdate", func(t *testing.T) { testUpdateLastTeamIconUpdate(t, ss) })
    43  	t.Run("GetTeamsByScheme", func(t *testing.T) { testGetTeamsByScheme(t, ss) })
    44  	t.Run("MigrateTeamMembers", func(t *testing.T) { testTeamStoreMigrateTeamMembers(t, ss) })
    45  	t.Run("ResetAllTeamSchemes", func(t *testing.T) { testResetAllTeamSchemes(t, ss) })
    46  }
    47  
    48  func testTeamStoreSave(t *testing.T, ss store.Store) {
    49  	o1 := model.Team{}
    50  	o1.DisplayName = "DisplayName"
    51  	o1.Name = "z-z-z" + model.NewId() + "b"
    52  	o1.Email = model.NewId() + "@nowhere.com"
    53  	o1.Type = model.TEAM_OPEN
    54  
    55  	if err := (<-ss.Team().Save(&o1)).Err; err != nil {
    56  		t.Fatal("couldn't save item", err)
    57  	}
    58  
    59  	if err := (<-ss.Team().Save(&o1)).Err; err == nil {
    60  		t.Fatal("shouldn't be able to update from save")
    61  	}
    62  
    63  	o1.Id = ""
    64  	if err := (<-ss.Team().Save(&o1)).Err; err == nil {
    65  		t.Fatal("should be unique domain")
    66  	}
    67  }
    68  
    69  func testTeamStoreUpdate(t *testing.T, ss store.Store) {
    70  	o1 := model.Team{}
    71  	o1.DisplayName = "DisplayName"
    72  	o1.Name = "z-z-z" + model.NewId() + "b"
    73  	o1.Email = model.NewId() + "@nowhere.com"
    74  	o1.Type = model.TEAM_OPEN
    75  	if err := (<-ss.Team().Save(&o1)).Err; err != nil {
    76  		t.Fatal(err)
    77  	}
    78  
    79  	time.Sleep(100 * time.Millisecond)
    80  
    81  	if err := (<-ss.Team().Update(&o1)).Err; err != nil {
    82  		t.Fatal(err)
    83  	}
    84  
    85  	o1.Id = "missing"
    86  	if err := (<-ss.Team().Update(&o1)).Err; err == nil {
    87  		t.Fatal("Update should have failed because of missing key")
    88  	}
    89  
    90  	o1.Id = model.NewId()
    91  	if err := (<-ss.Team().Update(&o1)).Err; err == nil {
    92  		t.Fatal("Update should have faile because id change")
    93  	}
    94  }
    95  
    96  func testTeamStoreUpdateDisplayName(t *testing.T, ss store.Store) {
    97  	o1 := &model.Team{}
    98  	o1.DisplayName = "Display Name"
    99  	o1.Name = "z-z-z" + model.NewId() + "b"
   100  	o1.Email = model.NewId() + "@nowhere.com"
   101  	o1.Type = model.TEAM_OPEN
   102  	o1 = (<-ss.Team().Save(o1)).Data.(*model.Team)
   103  
   104  	newDisplayName := "NewDisplayName"
   105  
   106  	if err := (<-ss.Team().UpdateDisplayName(newDisplayName, o1.Id)).Err; err != nil {
   107  		t.Fatal(err)
   108  	}
   109  
   110  	ro1 := (<-ss.Team().Get(o1.Id)).Data.(*model.Team)
   111  	if ro1.DisplayName != newDisplayName {
   112  		t.Fatal("DisplayName not updated")
   113  	}
   114  }
   115  
   116  func testTeamStoreGet(t *testing.T, ss store.Store) {
   117  	o1 := model.Team{}
   118  	o1.DisplayName = "DisplayName"
   119  	o1.Name = "z-z-z" + model.NewId() + "b"
   120  	o1.Email = model.NewId() + "@nowhere.com"
   121  	o1.Type = model.TEAM_OPEN
   122  	store.Must(ss.Team().Save(&o1))
   123  
   124  	if r1 := <-ss.Team().Get(o1.Id); r1.Err != nil {
   125  		t.Fatal(r1.Err)
   126  	} else {
   127  		if r1.Data.(*model.Team).ToJson() != o1.ToJson() {
   128  			t.Fatal("invalid returned team")
   129  		}
   130  	}
   131  
   132  	if err := (<-ss.Team().Get("")).Err; err == nil {
   133  		t.Fatal("Missing id should have failed")
   134  	}
   135  }
   136  
   137  func testTeamStoreGetByName(t *testing.T, ss store.Store) {
   138  	o1 := model.Team{}
   139  	o1.DisplayName = "DisplayName"
   140  	o1.Name = "z-z-z" + model.NewId() + "b"
   141  	o1.Email = model.NewId() + "@nowhere.com"
   142  	o1.Type = model.TEAM_OPEN
   143  
   144  	if err := (<-ss.Team().Save(&o1)).Err; err != nil {
   145  		t.Fatal(err)
   146  	}
   147  
   148  	if r1 := <-ss.Team().GetByName(o1.Name); r1.Err != nil {
   149  		t.Fatal(r1.Err)
   150  	} else {
   151  		if r1.Data.(*model.Team).ToJson() != o1.ToJson() {
   152  			t.Fatal("invalid returned team")
   153  		}
   154  	}
   155  
   156  	if err := (<-ss.Team().GetByName("")).Err; err == nil {
   157  		t.Fatal("Missing id should have failed")
   158  	}
   159  }
   160  
   161  func testTeamStoreSearchByName(t *testing.T, ss store.Store) {
   162  	o1 := model.Team{}
   163  	o1.DisplayName = "DisplayName"
   164  	var name = "zzz" + model.NewId()
   165  	o1.Name = name + "b"
   166  	o1.Email = model.NewId() + "@nowhere.com"
   167  	o1.Type = model.TEAM_OPEN
   168  
   169  	if err := (<-ss.Team().Save(&o1)).Err; err != nil {
   170  		t.Fatal(err)
   171  	}
   172  
   173  	if r1 := <-ss.Team().SearchByName(name); r1.Err != nil {
   174  		t.Fatal(r1.Err)
   175  	} else {
   176  		if r1.Data.([]*model.Team)[0].ToJson() != o1.ToJson() {
   177  			t.Fatal("invalid returned team")
   178  		}
   179  	}
   180  }
   181  
   182  func testTeamStoreSearchAll(t *testing.T, ss store.Store) {
   183  	o1 := model.Team{}
   184  	o1.DisplayName = "ADisplayName" + model.NewId()
   185  	o1.Name = "zz" + model.NewId() + "a"
   186  	o1.Email = model.NewId() + "@nowhere.com"
   187  	o1.Type = model.TEAM_OPEN
   188  
   189  	if err := (<-ss.Team().Save(&o1)).Err; err != nil {
   190  		t.Fatal(err)
   191  	}
   192  
   193  	p2 := model.Team{}
   194  	p2.DisplayName = "BDisplayName" + model.NewId()
   195  	p2.Name = "b" + model.NewId() + "b"
   196  	p2.Email = model.NewId() + "@nowhere.com"
   197  	p2.Type = model.TEAM_INVITE
   198  
   199  	if err := (<-ss.Team().Save(&p2)).Err; err != nil {
   200  		t.Fatal(err)
   201  	}
   202  
   203  	r1 := <-ss.Team().SearchAll(o1.Name)
   204  	if r1.Err != nil {
   205  		t.Fatal(r1.Err)
   206  	}
   207  	if len(r1.Data.([]*model.Team)) != 1 {
   208  		t.Fatal("should have returned 1 team")
   209  	}
   210  	if r1.Data.([]*model.Team)[0].ToJson() != o1.ToJson() {
   211  		t.Fatal("invalid returned team")
   212  	}
   213  
   214  	r1 = <-ss.Team().SearchAll(p2.DisplayName)
   215  	if r1.Err != nil {
   216  		t.Fatal(r1.Err)
   217  	}
   218  	if len(r1.Data.([]*model.Team)) != 1 {
   219  		t.Fatal("should have returned 1 team")
   220  	}
   221  	if r1.Data.([]*model.Team)[0].ToJson() != p2.ToJson() {
   222  		t.Fatal("invalid returned team")
   223  	}
   224  
   225  	r1 = <-ss.Team().SearchAll("junk")
   226  	if r1.Err != nil {
   227  		t.Fatal(r1.Err)
   228  	}
   229  	if len(r1.Data.([]*model.Team)) != 0 {
   230  		t.Fatal("should have not returned a team")
   231  	}
   232  }
   233  
   234  func testTeamStoreSearchOpen(t *testing.T, ss store.Store) {
   235  	o1 := model.Team{}
   236  	o1.DisplayName = "ADisplayName" + model.NewId()
   237  	o1.Name = "zz" + model.NewId() + "a"
   238  	o1.Email = model.NewId() + "@nowhere.com"
   239  	o1.Type = model.TEAM_OPEN
   240  	o1.AllowOpenInvite = true
   241  
   242  	if err := (<-ss.Team().Save(&o1)).Err; err != nil {
   243  		t.Fatal(err)
   244  	}
   245  
   246  	o2 := model.Team{}
   247  	o2.DisplayName = "ADisplayName" + model.NewId()
   248  	o2.Name = "zz" + model.NewId() + "a"
   249  	o2.Email = model.NewId() + "@nowhere.com"
   250  	o2.Type = model.TEAM_OPEN
   251  	o2.AllowOpenInvite = false
   252  
   253  	if err := (<-ss.Team().Save(&o2)).Err; err != nil {
   254  		t.Fatal(err)
   255  	}
   256  
   257  	p2 := model.Team{}
   258  	p2.DisplayName = "BDisplayName" + model.NewId()
   259  	p2.Name = "b" + model.NewId() + "b"
   260  	p2.Email = model.NewId() + "@nowhere.com"
   261  	p2.Type = model.TEAM_INVITE
   262  	p2.AllowOpenInvite = true
   263  
   264  	if err := (<-ss.Team().Save(&p2)).Err; err != nil {
   265  		t.Fatal(err)
   266  	}
   267  
   268  	r1 := <-ss.Team().SearchOpen(o1.Name)
   269  	if r1.Err != nil {
   270  		t.Fatal(r1.Err)
   271  	}
   272  	if len(r1.Data.([]*model.Team)) != 1 {
   273  		t.Fatal("should have returned 1 team")
   274  	}
   275  	if r1.Data.([]*model.Team)[0].ToJson() != o1.ToJson() {
   276  		t.Fatal("invalid returned team")
   277  	}
   278  
   279  	r1 = <-ss.Team().SearchOpen(o1.DisplayName)
   280  	if r1.Err != nil {
   281  		t.Fatal(r1.Err)
   282  	}
   283  	if len(r1.Data.([]*model.Team)) != 1 {
   284  		t.Fatal("should have returned 1 team")
   285  	}
   286  	if r1.Data.([]*model.Team)[0].ToJson() != o1.ToJson() {
   287  		t.Fatal("invalid returned team")
   288  	}
   289  
   290  	r1 = <-ss.Team().SearchOpen(p2.Name)
   291  	if r1.Err != nil {
   292  		t.Fatal(r1.Err)
   293  	}
   294  	if len(r1.Data.([]*model.Team)) != 0 {
   295  		t.Fatal("should have not returned a team")
   296  	}
   297  
   298  	r1 = <-ss.Team().SearchOpen(p2.DisplayName)
   299  	if r1.Err != nil {
   300  		t.Fatal(r1.Err)
   301  	}
   302  	if len(r1.Data.([]*model.Team)) != 0 {
   303  		t.Fatal("should have not returned a team")
   304  	}
   305  
   306  	r1 = <-ss.Team().SearchOpen("junk")
   307  	if r1.Err != nil {
   308  		t.Fatal(r1.Err)
   309  	}
   310  	if len(r1.Data.([]*model.Team)) != 0 {
   311  		t.Fatal("should have not returned a team")
   312  	}
   313  
   314  	r1 = <-ss.Team().SearchOpen(o2.DisplayName)
   315  	if r1.Err != nil {
   316  		t.Fatal(r1.Err)
   317  	}
   318  	if len(r1.Data.([]*model.Team)) != 0 {
   319  		t.Fatal("should have not returned a team")
   320  	}
   321  }
   322  
   323  func testTeamStoreGetByIniviteId(t *testing.T, ss store.Store) {
   324  	o1 := model.Team{}
   325  	o1.DisplayName = "DisplayName"
   326  	o1.Name = "z-z-z" + model.NewId() + "b"
   327  	o1.Email = model.NewId() + "@nowhere.com"
   328  	o1.Type = model.TEAM_OPEN
   329  	o1.InviteId = model.NewId()
   330  
   331  	if err := (<-ss.Team().Save(&o1)).Err; err != nil {
   332  		t.Fatal(err)
   333  	}
   334  
   335  	o2 := model.Team{}
   336  	o2.DisplayName = "DisplayName"
   337  	o2.Name = "zz" + model.NewId() + "b"
   338  	o2.Email = model.NewId() + "@nowhere.com"
   339  	o2.Type = model.TEAM_OPEN
   340  
   341  	if err := (<-ss.Team().Save(&o2)).Err; err != nil {
   342  		t.Fatal(err)
   343  	}
   344  
   345  	if r1 := <-ss.Team().GetByInviteId(o1.InviteId); r1.Err != nil {
   346  		t.Fatal(r1.Err)
   347  	} else {
   348  		if r1.Data.(*model.Team).ToJson() != o1.ToJson() {
   349  			t.Fatal("invalid returned team")
   350  		}
   351  	}
   352  
   353  	o2.InviteId = ""
   354  	<-ss.Team().Update(&o2)
   355  
   356  	if r1 := <-ss.Team().GetByInviteId(o2.Id); r1.Err != nil {
   357  		t.Fatal(r1.Err)
   358  	} else {
   359  		if r1.Data.(*model.Team).Id != o2.Id {
   360  			t.Fatal("invalid returned team")
   361  		}
   362  	}
   363  
   364  	if err := (<-ss.Team().GetByInviteId("")).Err; err == nil {
   365  		t.Fatal("Missing id should have failed")
   366  	}
   367  }
   368  
   369  func testTeamStoreByUserId(t *testing.T, ss store.Store) {
   370  	o1 := &model.Team{}
   371  	o1.DisplayName = "DisplayName"
   372  	o1.Name = "z-z-z" + model.NewId() + "b"
   373  	o1.Email = model.NewId() + "@nowhere.com"
   374  	o1.Type = model.TEAM_OPEN
   375  	o1.InviteId = model.NewId()
   376  	o1 = store.Must(ss.Team().Save(o1)).(*model.Team)
   377  
   378  	m1 := &model.TeamMember{TeamId: o1.Id, UserId: model.NewId()}
   379  	store.Must(ss.Team().SaveMember(m1, -1))
   380  
   381  	if r1 := <-ss.Team().GetTeamsByUserId(m1.UserId); r1.Err != nil {
   382  		t.Fatal(r1.Err)
   383  	} else {
   384  		teams := r1.Data.([]*model.Team)
   385  		if len(teams) == 0 {
   386  			t.Fatal("Should return a team")
   387  		}
   388  
   389  		if teams[0].Id != o1.Id {
   390  			t.Fatal("should be a member")
   391  		}
   392  
   393  	}
   394  }
   395  
   396  func testGetAllTeamListing(t *testing.T, ss store.Store) {
   397  	o1 := model.Team{}
   398  	o1.DisplayName = "DisplayName"
   399  	o1.Name = "z-z-z" + model.NewId() + "b"
   400  	o1.Email = model.NewId() + "@nowhere.com"
   401  	o1.Type = model.TEAM_OPEN
   402  	o1.AllowOpenInvite = true
   403  	store.Must(ss.Team().Save(&o1))
   404  
   405  	o2 := model.Team{}
   406  	o2.DisplayName = "DisplayName"
   407  	o2.Name = "zz" + model.NewId() + "b"
   408  	o2.Email = model.NewId() + "@nowhere.com"
   409  	o2.Type = model.TEAM_OPEN
   410  	store.Must(ss.Team().Save(&o2))
   411  
   412  	o3 := model.Team{}
   413  	o3.DisplayName = "DisplayName"
   414  	o3.Name = "z-z-z" + model.NewId() + "b"
   415  	o3.Email = model.NewId() + "@nowhere.com"
   416  	o3.Type = model.TEAM_INVITE
   417  	o3.AllowOpenInvite = true
   418  	store.Must(ss.Team().Save(&o3))
   419  
   420  	o4 := model.Team{}
   421  	o4.DisplayName = "DisplayName"
   422  	o4.Name = "zz" + model.NewId() + "b"
   423  	o4.Email = model.NewId() + "@nowhere.com"
   424  	o4.Type = model.TEAM_INVITE
   425  	store.Must(ss.Team().Save(&o4))
   426  
   427  	if r1 := <-ss.Team().GetAllTeamListing(); r1.Err != nil {
   428  		t.Fatal(r1.Err)
   429  	} else {
   430  		teams := r1.Data.([]*model.Team)
   431  
   432  		for _, team := range teams {
   433  			if !team.AllowOpenInvite {
   434  				t.Fatal("should have returned team with AllowOpenInvite as true")
   435  			}
   436  		}
   437  
   438  		if len(teams) == 0 {
   439  			t.Fatal("failed team listing")
   440  		}
   441  	}
   442  }
   443  
   444  func testGetAllTeamPageListing(t *testing.T, ss store.Store) {
   445  	o1 := model.Team{}
   446  	o1.DisplayName = "DisplayName"
   447  	o1.Name = "z-z-z" + model.NewId() + "b"
   448  	o1.Email = model.NewId() + "@nowhere.com"
   449  	o1.Type = model.TEAM_OPEN
   450  	o1.AllowOpenInvite = true
   451  	store.Must(ss.Team().Save(&o1))
   452  
   453  	o2 := model.Team{}
   454  	o2.DisplayName = "DisplayName"
   455  	o2.Name = "zz" + model.NewId() + "b"
   456  	o2.Email = model.NewId() + "@nowhere.com"
   457  	o2.Type = model.TEAM_OPEN
   458  	o2.AllowOpenInvite = false
   459  	store.Must(ss.Team().Save(&o2))
   460  
   461  	o3 := model.Team{}
   462  	o3.DisplayName = "DisplayName"
   463  	o3.Name = "z-z-z" + model.NewId() + "b"
   464  	o3.Email = model.NewId() + "@nowhere.com"
   465  	o3.Type = model.TEAM_INVITE
   466  	o3.AllowOpenInvite = true
   467  	store.Must(ss.Team().Save(&o3))
   468  
   469  	o4 := model.Team{}
   470  	o4.DisplayName = "DisplayName"
   471  	o4.Name = "zz" + model.NewId() + "b"
   472  	o4.Email = model.NewId() + "@nowhere.com"
   473  	o4.Type = model.TEAM_INVITE
   474  	o4.AllowOpenInvite = false
   475  	store.Must(ss.Team().Save(&o4))
   476  
   477  	if r1 := <-ss.Team().GetAllTeamPageListing(0, 10); r1.Err != nil {
   478  		t.Fatal(r1.Err)
   479  	} else {
   480  		teams := r1.Data.([]*model.Team)
   481  
   482  		for _, team := range teams {
   483  			if !team.AllowOpenInvite {
   484  				t.Fatal("should have returned team with AllowOpenInvite as true")
   485  			}
   486  		}
   487  
   488  		if len(teams) > 10 {
   489  			t.Fatal("should have returned max of 10 teams")
   490  		}
   491  	}
   492  
   493  	o5 := model.Team{}
   494  	o5.DisplayName = "DisplayName"
   495  	o5.Name = "z-z-z" + model.NewId() + "b"
   496  	o5.Email = model.NewId() + "@nowhere.com"
   497  	o5.Type = model.TEAM_OPEN
   498  	o5.AllowOpenInvite = true
   499  	store.Must(ss.Team().Save(&o5))
   500  
   501  	if r1 := <-ss.Team().GetAllTeamPageListing(0, 4); r1.Err != nil {
   502  		t.Fatal(r1.Err)
   503  	} else {
   504  		teams := r1.Data.([]*model.Team)
   505  
   506  		for _, team := range teams {
   507  			if !team.AllowOpenInvite {
   508  				t.Fatal("should have returned team with AllowOpenInvite as true")
   509  			}
   510  		}
   511  
   512  		if len(teams) > 4 {
   513  			t.Fatal("should have returned max of 4 teams")
   514  		}
   515  	}
   516  
   517  	if r1 := <-ss.Team().GetAllTeamPageListing(1, 1); r1.Err != nil {
   518  		t.Fatal(r1.Err)
   519  	} else {
   520  		teams := r1.Data.([]*model.Team)
   521  
   522  		for _, team := range teams {
   523  			if !team.AllowOpenInvite {
   524  				t.Fatal("should have returned team with AllowOpenInvite as true")
   525  			}
   526  		}
   527  
   528  		if len(teams) > 1 {
   529  			t.Fatal("should have returned max of 1 team")
   530  		}
   531  	}
   532  }
   533  
   534  func testDelete(t *testing.T, ss store.Store) {
   535  	o1 := model.Team{}
   536  	o1.DisplayName = "DisplayName"
   537  	o1.Name = "z-z-z" + model.NewId() + "b"
   538  	o1.Email = model.NewId() + "@nowhere.com"
   539  	o1.Type = model.TEAM_OPEN
   540  	o1.AllowOpenInvite = true
   541  	store.Must(ss.Team().Save(&o1))
   542  
   543  	o2 := model.Team{}
   544  	o2.DisplayName = "DisplayName"
   545  	o2.Name = "zz" + model.NewId() + "b"
   546  	o2.Email = model.NewId() + "@nowhere.com"
   547  	o2.Type = model.TEAM_OPEN
   548  	store.Must(ss.Team().Save(&o2))
   549  
   550  	if r1 := <-ss.Team().PermanentDelete(o1.Id); r1.Err != nil {
   551  		t.Fatal(r1.Err)
   552  	}
   553  }
   554  
   555  func testTeamCount(t *testing.T, ss store.Store) {
   556  	o1 := model.Team{}
   557  	o1.DisplayName = "DisplayName"
   558  	o1.Name = "z-z-z" + model.NewId() + "b"
   559  	o1.Email = model.NewId() + "@nowhere.com"
   560  	o1.Type = model.TEAM_OPEN
   561  	o1.AllowOpenInvite = true
   562  	store.Must(ss.Team().Save(&o1))
   563  
   564  	if r1 := <-ss.Team().AnalyticsTeamCount(); r1.Err != nil {
   565  		t.Fatal(r1.Err)
   566  	} else {
   567  		if r1.Data.(int64) == 0 {
   568  			t.Fatal("should be at least 1 team")
   569  		}
   570  	}
   571  }
   572  
   573  func testTeamMembers(t *testing.T, ss store.Store) {
   574  	teamId1 := model.NewId()
   575  	teamId2 := model.NewId()
   576  
   577  	m1 := &model.TeamMember{TeamId: teamId1, UserId: model.NewId()}
   578  	m2 := &model.TeamMember{TeamId: teamId1, UserId: model.NewId()}
   579  	m3 := &model.TeamMember{TeamId: teamId2, UserId: model.NewId()}
   580  
   581  	if r1 := <-ss.Team().SaveMember(m1, -1); r1.Err != nil {
   582  		t.Fatal(r1.Err)
   583  	}
   584  
   585  	store.Must(ss.Team().SaveMember(m2, -1))
   586  	store.Must(ss.Team().SaveMember(m3, -1))
   587  
   588  	if r1 := <-ss.Team().GetMembers(teamId1, 0, 100); r1.Err != nil {
   589  		t.Fatal(r1.Err)
   590  	} else {
   591  		ms := r1.Data.([]*model.TeamMember)
   592  
   593  		if len(ms) != 2 {
   594  			t.Fatal()
   595  		}
   596  	}
   597  
   598  	if r1 := <-ss.Team().GetMembers(teamId2, 0, 100); r1.Err != nil {
   599  		t.Fatal(r1.Err)
   600  	} else {
   601  		ms := r1.Data.([]*model.TeamMember)
   602  
   603  		if len(ms) != 1 {
   604  			t.Fatal()
   605  		}
   606  
   607  		if ms[0].UserId != m3.UserId {
   608  			t.Fatal()
   609  
   610  		}
   611  	}
   612  
   613  	if r1 := <-ss.Team().GetTeamsForUser(m1.UserId); r1.Err != nil {
   614  		t.Fatal(r1.Err)
   615  	} else {
   616  		ms := r1.Data.([]*model.TeamMember)
   617  
   618  		if len(ms) != 1 {
   619  			t.Fatal()
   620  		}
   621  
   622  		if ms[0].TeamId != m1.TeamId {
   623  			t.Fatal()
   624  
   625  		}
   626  	}
   627  
   628  	if r1 := <-ss.Team().RemoveMember(teamId1, m1.UserId); r1.Err != nil {
   629  		t.Fatal(r1.Err)
   630  	}
   631  
   632  	if r1 := <-ss.Team().GetMembers(teamId1, 0, 100); r1.Err != nil {
   633  		t.Fatal(r1.Err)
   634  	} else {
   635  		ms := r1.Data.([]*model.TeamMember)
   636  
   637  		if len(ms) != 1 {
   638  			t.Fatal()
   639  		}
   640  
   641  		if ms[0].UserId != m2.UserId {
   642  			t.Fatal()
   643  
   644  		}
   645  	}
   646  
   647  	store.Must(ss.Team().SaveMember(m1, -1))
   648  
   649  	if r1 := <-ss.Team().RemoveAllMembersByTeam(teamId1); r1.Err != nil {
   650  		t.Fatal(r1.Err)
   651  	}
   652  
   653  	if r1 := <-ss.Team().GetMembers(teamId1, 0, 100); r1.Err != nil {
   654  		t.Fatal(r1.Err)
   655  	} else {
   656  		ms := r1.Data.([]*model.TeamMember)
   657  
   658  		if len(ms) != 0 {
   659  			t.Fatal()
   660  		}
   661  	}
   662  
   663  	uid := model.NewId()
   664  	m4 := &model.TeamMember{TeamId: teamId1, UserId: uid}
   665  	m5 := &model.TeamMember{TeamId: teamId2, UserId: uid}
   666  	store.Must(ss.Team().SaveMember(m4, -1))
   667  	store.Must(ss.Team().SaveMember(m5, -1))
   668  
   669  	if r1 := <-ss.Team().GetTeamsForUser(uid); r1.Err != nil {
   670  		t.Fatal(r1.Err)
   671  	} else {
   672  		ms := r1.Data.([]*model.TeamMember)
   673  
   674  		if len(ms) != 2 {
   675  			t.Fatal()
   676  		}
   677  	}
   678  
   679  	if r1 := <-ss.Team().RemoveAllMembersByUser(uid); r1.Err != nil {
   680  		t.Fatal(r1.Err)
   681  	}
   682  
   683  	if r1 := <-ss.Team().GetTeamsForUser(m1.UserId); r1.Err != nil {
   684  		t.Fatal(r1.Err)
   685  	} else {
   686  		ms := r1.Data.([]*model.TeamMember)
   687  
   688  		if len(ms) != 0 {
   689  			t.Fatal()
   690  		}
   691  	}
   692  }
   693  
   694  func testSaveTeamMemberMaxMembers(t *testing.T, ss store.Store) {
   695  	maxUsersPerTeam := 5
   696  
   697  	team := store.Must(ss.Team().Save(&model.Team{
   698  		DisplayName: "DisplayName",
   699  		Name:        "z-z-z" + model.NewId() + "b",
   700  		Type:        model.TEAM_OPEN,
   701  	})).(*model.Team)
   702  	defer func() {
   703  		<-ss.Team().PermanentDelete(team.Id)
   704  	}()
   705  
   706  	userIds := make([]string, maxUsersPerTeam)
   707  
   708  	for i := 0; i < maxUsersPerTeam; i++ {
   709  		userIds[i] = store.Must(ss.User().Save(&model.User{
   710  			Username: model.NewId(),
   711  			Email:    model.NewId(),
   712  		})).(*model.User).Id
   713  
   714  		defer func(userId string) {
   715  			<-ss.User().PermanentDelete(userId)
   716  		}(userIds[i])
   717  
   718  		store.Must(ss.Team().SaveMember(&model.TeamMember{
   719  			TeamId: team.Id,
   720  			UserId: userIds[i],
   721  		}, maxUsersPerTeam))
   722  
   723  		defer func(userId string) {
   724  			<-ss.Team().RemoveMember(team.Id, userId)
   725  		}(userIds[i])
   726  	}
   727  
   728  	if result := <-ss.Team().GetTotalMemberCount(team.Id); result.Err != nil {
   729  		t.Fatal(result.Err)
   730  	} else if count := result.Data.(int64); int(count) != maxUsersPerTeam {
   731  		t.Fatalf("should start with 5 team members, had %v instead", count)
   732  	}
   733  
   734  	newUserId := store.Must(ss.User().Save(&model.User{
   735  		Username: model.NewId(),
   736  		Email:    model.NewId(),
   737  	})).(*model.User).Id
   738  	defer func() {
   739  		<-ss.User().PermanentDelete(newUserId)
   740  	}()
   741  
   742  	if result := <-ss.Team().SaveMember(&model.TeamMember{
   743  		TeamId: team.Id,
   744  		UserId: newUserId,
   745  	}, maxUsersPerTeam); result.Err == nil {
   746  		t.Fatal("shouldn't be able to save member when at maximum members per team")
   747  	}
   748  
   749  	if result := <-ss.Team().GetTotalMemberCount(team.Id); result.Err != nil {
   750  		t.Fatal(result.Err)
   751  	} else if count := result.Data.(int64); int(count) != maxUsersPerTeam {
   752  		t.Fatalf("should still have 5 team members, had %v instead", count)
   753  	}
   754  
   755  	// Leaving the team from the UI sets DeleteAt instead of using TeamStore.RemoveMember
   756  	store.Must(ss.Team().UpdateMember(&model.TeamMember{
   757  		TeamId:   team.Id,
   758  		UserId:   userIds[0],
   759  		DeleteAt: 1234,
   760  	}))
   761  
   762  	if result := <-ss.Team().GetTotalMemberCount(team.Id); result.Err != nil {
   763  		t.Fatal(result.Err)
   764  	} else if count := result.Data.(int64); int(count) != maxUsersPerTeam-1 {
   765  		t.Fatalf("should now only have 4 team members, had %v instead", count)
   766  	}
   767  
   768  	if result := <-ss.Team().SaveMember(&model.TeamMember{TeamId: team.Id, UserId: newUserId}, maxUsersPerTeam); result.Err != nil {
   769  		t.Fatal("should've been able to save new member after deleting one", result.Err)
   770  	} else {
   771  		defer func(userId string) {
   772  			<-ss.Team().RemoveMember(team.Id, userId)
   773  		}(newUserId)
   774  	}
   775  
   776  	if result := <-ss.Team().GetTotalMemberCount(team.Id); result.Err != nil {
   777  		t.Fatal(result.Err)
   778  	} else if count := result.Data.(int64); int(count) != maxUsersPerTeam {
   779  		t.Fatalf("should have 5 team members again, had %v instead", count)
   780  	}
   781  
   782  	// Deactivating a user should make them stop counting against max members
   783  	user2 := store.Must(ss.User().Get(userIds[1])).(*model.User)
   784  	user2.DeleteAt = 1234
   785  	store.Must(ss.User().Update(user2, true))
   786  
   787  	newUserId2 := store.Must(ss.User().Save(&model.User{
   788  		Username: model.NewId(),
   789  		Email:    model.NewId(),
   790  	})).(*model.User).Id
   791  	if result := <-ss.Team().SaveMember(&model.TeamMember{TeamId: team.Id, UserId: newUserId2}, maxUsersPerTeam); result.Err != nil {
   792  		t.Fatal("should've been able to save new member after deleting one", result.Err)
   793  	} else {
   794  		defer func(userId string) {
   795  			<-ss.Team().RemoveMember(team.Id, userId)
   796  		}(newUserId2)
   797  	}
   798  }
   799  
   800  func testGetTeamMember(t *testing.T, ss store.Store) {
   801  	teamId1 := model.NewId()
   802  
   803  	m1 := &model.TeamMember{TeamId: teamId1, UserId: model.NewId()}
   804  	store.Must(ss.Team().SaveMember(m1, -1))
   805  
   806  	if r := <-ss.Team().GetMember(m1.TeamId, m1.UserId); r.Err != nil {
   807  		t.Fatal(r.Err)
   808  	} else {
   809  		rm1 := r.Data.(*model.TeamMember)
   810  
   811  		if rm1.TeamId != m1.TeamId {
   812  			t.Fatal("bad team id")
   813  		}
   814  
   815  		if rm1.UserId != m1.UserId {
   816  			t.Fatal("bad user id")
   817  		}
   818  	}
   819  
   820  	if r := <-ss.Team().GetMember(m1.TeamId, ""); r.Err == nil {
   821  		t.Fatal("empty user id - should have failed")
   822  	}
   823  
   824  	if r := <-ss.Team().GetMember("", m1.UserId); r.Err == nil {
   825  		t.Fatal("empty team id - should have failed")
   826  	}
   827  
   828  	// Test with a custom team scheme.
   829  	s2 := &model.Scheme{
   830  		Name:        model.NewId(),
   831  		DisplayName: model.NewId(),
   832  		Description: model.NewId(),
   833  		Scope:       model.SCHEME_SCOPE_TEAM,
   834  	}
   835  	s2 = (<-ss.Scheme().Save(s2)).Data.(*model.Scheme)
   836  	t.Log(s2)
   837  
   838  	t2 := store.Must(ss.Team().Save(&model.Team{
   839  		DisplayName: "DisplayName",
   840  		Name:        "z-z-z" + model.NewId() + "b",
   841  		Type:        model.TEAM_OPEN,
   842  		SchemeId:    &s2.Id,
   843  	})).(*model.Team)
   844  
   845  	defer func() {
   846  		<-ss.Team().PermanentDelete(t2.Id)
   847  	}()
   848  
   849  	m2 := &model.TeamMember{TeamId: t2.Id, UserId: model.NewId(), SchemeUser: true}
   850  	store.Must(ss.Team().SaveMember(m2, -1))
   851  
   852  	r2 := <-ss.Team().GetMember(m2.TeamId, m2.UserId)
   853  	require.Nil(t, r2.Err)
   854  	m3 := r2.Data.(*model.TeamMember)
   855  	t.Log(m3)
   856  
   857  	assert.Equal(t, s2.DefaultTeamUserRole, m3.Roles)
   858  }
   859  
   860  func testGetTeamMembersByIds(t *testing.T, ss store.Store) {
   861  	teamId1 := model.NewId()
   862  
   863  	m1 := &model.TeamMember{TeamId: teamId1, UserId: model.NewId()}
   864  	store.Must(ss.Team().SaveMember(m1, -1))
   865  
   866  	if r := <-ss.Team().GetMembersByIds(m1.TeamId, []string{m1.UserId}); r.Err != nil {
   867  		t.Fatal(r.Err)
   868  	} else {
   869  		rm1 := r.Data.([]*model.TeamMember)[0]
   870  
   871  		if rm1.TeamId != m1.TeamId {
   872  			t.Fatal("bad team id")
   873  		}
   874  
   875  		if rm1.UserId != m1.UserId {
   876  			t.Fatal("bad user id")
   877  		}
   878  	}
   879  
   880  	m2 := &model.TeamMember{TeamId: teamId1, UserId: model.NewId()}
   881  	store.Must(ss.Team().SaveMember(m2, -1))
   882  
   883  	if r := <-ss.Team().GetMembersByIds(m1.TeamId, []string{m1.UserId, m2.UserId, model.NewId()}); r.Err != nil {
   884  		t.Fatal(r.Err)
   885  	} else {
   886  		rm := r.Data.([]*model.TeamMember)
   887  
   888  		if len(rm) != 2 {
   889  			t.Fatal("return wrong number of results")
   890  		}
   891  	}
   892  
   893  	if r := <-ss.Team().GetMembersByIds(m1.TeamId, []string{}); r.Err == nil {
   894  		t.Fatal("empty user ids - should have failed")
   895  	}
   896  }
   897  
   898  func testTeamStoreMemberCount(t *testing.T, ss store.Store) {
   899  	u1 := &model.User{}
   900  	u1.Email = model.NewId()
   901  	store.Must(ss.User().Save(u1))
   902  
   903  	u2 := &model.User{}
   904  	u2.Email = model.NewId()
   905  	u2.DeleteAt = 1
   906  	store.Must(ss.User().Save(u2))
   907  
   908  	teamId1 := model.NewId()
   909  	m1 := &model.TeamMember{TeamId: teamId1, UserId: u1.Id}
   910  	store.Must(ss.Team().SaveMember(m1, -1))
   911  
   912  	m2 := &model.TeamMember{TeamId: teamId1, UserId: u2.Id}
   913  	store.Must(ss.Team().SaveMember(m2, -1))
   914  
   915  	if result := <-ss.Team().GetTotalMemberCount(teamId1); result.Err != nil {
   916  		t.Fatal(result.Err)
   917  	} else {
   918  		if result.Data.(int64) != 2 {
   919  			t.Fatal("wrong count")
   920  		}
   921  	}
   922  
   923  	if result := <-ss.Team().GetActiveMemberCount(teamId1); result.Err != nil {
   924  		t.Fatal(result.Err)
   925  	} else {
   926  		if result.Data.(int64) != 1 {
   927  			t.Fatal("wrong count")
   928  		}
   929  	}
   930  
   931  	m3 := &model.TeamMember{TeamId: teamId1, UserId: model.NewId()}
   932  	store.Must(ss.Team().SaveMember(m3, -1))
   933  
   934  	if result := <-ss.Team().GetTotalMemberCount(teamId1); result.Err != nil {
   935  		t.Fatal(result.Err)
   936  	} else {
   937  		if result.Data.(int64) != 2 {
   938  			t.Fatal("wrong count")
   939  		}
   940  	}
   941  
   942  	if result := <-ss.Team().GetActiveMemberCount(teamId1); result.Err != nil {
   943  		t.Fatal(result.Err)
   944  	} else {
   945  		if result.Data.(int64) != 1 {
   946  			t.Fatal("wrong count")
   947  		}
   948  	}
   949  }
   950  
   951  func testGetChannelUnreadsForAllTeams(t *testing.T, ss store.Store) {
   952  	teamId1 := model.NewId()
   953  	teamId2 := model.NewId()
   954  
   955  	uid := model.NewId()
   956  	m1 := &model.TeamMember{TeamId: teamId1, UserId: uid}
   957  	m2 := &model.TeamMember{TeamId: teamId2, UserId: uid}
   958  	store.Must(ss.Team().SaveMember(m1, -1))
   959  	store.Must(ss.Team().SaveMember(m2, -1))
   960  
   961  	c1 := &model.Channel{TeamId: m1.TeamId, Name: model.NewId(), DisplayName: "Town Square", Type: model.CHANNEL_OPEN, TotalMsgCount: 100}
   962  	store.Must(ss.Channel().Save(c1, -1))
   963  	c2 := &model.Channel{TeamId: m2.TeamId, Name: model.NewId(), DisplayName: "Town Square", Type: model.CHANNEL_OPEN, TotalMsgCount: 100}
   964  	store.Must(ss.Channel().Save(c2, -1))
   965  
   966  	cm1 := &model.ChannelMember{ChannelId: c1.Id, UserId: m1.UserId, NotifyProps: model.GetDefaultChannelNotifyProps(), MsgCount: 90}
   967  	store.Must(ss.Channel().SaveMember(cm1))
   968  	cm2 := &model.ChannelMember{ChannelId: c2.Id, UserId: m2.UserId, NotifyProps: model.GetDefaultChannelNotifyProps(), MsgCount: 90}
   969  	store.Must(ss.Channel().SaveMember(cm2))
   970  
   971  	if r1 := <-ss.Team().GetChannelUnreadsForAllTeams("", uid); r1.Err != nil {
   972  		t.Fatal(r1.Err)
   973  	} else {
   974  		ms := r1.Data.([]*model.ChannelUnread)
   975  		membersMap := make(map[string]bool)
   976  		for i := range ms {
   977  			id := ms[i].TeamId
   978  			if _, ok := membersMap[id]; !ok {
   979  				membersMap[id] = true
   980  			}
   981  		}
   982  		if len(membersMap) != 2 {
   983  			t.Fatal("Should be the unreads for all the teams")
   984  		}
   985  
   986  		if ms[0].MsgCount != 10 {
   987  			t.Fatal("subtraction failed")
   988  		}
   989  	}
   990  
   991  	if r2 := <-ss.Team().GetChannelUnreadsForAllTeams(teamId1, uid); r2.Err != nil {
   992  		t.Fatal(r2.Err)
   993  	} else {
   994  		ms := r2.Data.([]*model.ChannelUnread)
   995  		membersMap := make(map[string]bool)
   996  		for i := range ms {
   997  			id := ms[i].TeamId
   998  			if _, ok := membersMap[id]; !ok {
   999  				membersMap[id] = true
  1000  			}
  1001  		}
  1002  
  1003  		if len(membersMap) != 1 {
  1004  			t.Fatal("Should be the unreads for just one team")
  1005  		}
  1006  
  1007  		if ms[0].MsgCount != 10 {
  1008  			t.Fatal("subtraction failed")
  1009  		}
  1010  	}
  1011  
  1012  	if r1 := <-ss.Team().RemoveAllMembersByUser(uid); r1.Err != nil {
  1013  		t.Fatal(r1.Err)
  1014  	}
  1015  }
  1016  
  1017  func testGetChannelUnreadsForTeam(t *testing.T, ss store.Store) {
  1018  	teamId1 := model.NewId()
  1019  
  1020  	uid := model.NewId()
  1021  	m1 := &model.TeamMember{TeamId: teamId1, UserId: uid}
  1022  	store.Must(ss.Team().SaveMember(m1, -1))
  1023  
  1024  	c1 := &model.Channel{TeamId: m1.TeamId, Name: model.NewId(), DisplayName: "Town Square", Type: model.CHANNEL_OPEN, TotalMsgCount: 100}
  1025  	store.Must(ss.Channel().Save(c1, -1))
  1026  	c2 := &model.Channel{TeamId: m1.TeamId, Name: model.NewId(), DisplayName: "Town Square", Type: model.CHANNEL_OPEN, TotalMsgCount: 100}
  1027  	store.Must(ss.Channel().Save(c2, -1))
  1028  
  1029  	cm1 := &model.ChannelMember{ChannelId: c1.Id, UserId: m1.UserId, NotifyProps: model.GetDefaultChannelNotifyProps(), MsgCount: 90}
  1030  	store.Must(ss.Channel().SaveMember(cm1))
  1031  	cm2 := &model.ChannelMember{ChannelId: c2.Id, UserId: m1.UserId, NotifyProps: model.GetDefaultChannelNotifyProps(), MsgCount: 90}
  1032  	store.Must(ss.Channel().SaveMember(cm2))
  1033  
  1034  	if r1 := <-ss.Team().GetChannelUnreadsForTeam(m1.TeamId, m1.UserId); r1.Err != nil {
  1035  		t.Fatal(r1.Err)
  1036  	} else {
  1037  		ms := r1.Data.([]*model.ChannelUnread)
  1038  		if len(ms) != 2 {
  1039  			t.Fatal("wrong length")
  1040  		}
  1041  
  1042  		if ms[0].MsgCount != 10 {
  1043  			t.Fatal("subtraction failed")
  1044  		}
  1045  	}
  1046  }
  1047  
  1048  func testUpdateLastTeamIconUpdate(t *testing.T, ss store.Store) {
  1049  
  1050  	// team icon initially updated a second ago
  1051  	lastTeamIconUpdateInitial := model.GetMillis() - 1000
  1052  
  1053  	o1 := &model.Team{}
  1054  	o1.DisplayName = "Display Name"
  1055  	o1.Name = "z-z-z" + model.NewId() + "b"
  1056  	o1.Email = model.NewId() + "@nowhere.com"
  1057  	o1.Type = model.TEAM_OPEN
  1058  	o1.LastTeamIconUpdate = lastTeamIconUpdateInitial
  1059  	o1 = (<-ss.Team().Save(o1)).Data.(*model.Team)
  1060  
  1061  	curTime := model.GetMillis()
  1062  
  1063  	if err := (<-ss.Team().UpdateLastTeamIconUpdate(o1.Id, curTime)).Err; err != nil {
  1064  		t.Fatal(err)
  1065  	}
  1066  
  1067  	ro1 := (<-ss.Team().Get(o1.Id)).Data.(*model.Team)
  1068  	if ro1.LastTeamIconUpdate <= lastTeamIconUpdateInitial {
  1069  		t.Fatal("LastTeamIconUpdate not updated")
  1070  	}
  1071  }
  1072  
  1073  func testGetTeamsByScheme(t *testing.T, ss store.Store) {
  1074  	// Create some schemes.
  1075  	s1 := &model.Scheme{
  1076  		DisplayName: model.NewId(),
  1077  		Name:        model.NewId(),
  1078  		Description: model.NewId(),
  1079  		Scope:       model.SCHEME_SCOPE_TEAM,
  1080  	}
  1081  
  1082  	s2 := &model.Scheme{
  1083  		DisplayName: model.NewId(),
  1084  		Name:        model.NewId(),
  1085  		Description: model.NewId(),
  1086  		Scope:       model.SCHEME_SCOPE_TEAM,
  1087  	}
  1088  
  1089  	s1 = (<-ss.Scheme().Save(s1)).Data.(*model.Scheme)
  1090  	s2 = (<-ss.Scheme().Save(s2)).Data.(*model.Scheme)
  1091  
  1092  	// Create and save some teams.
  1093  	t1 := &model.Team{
  1094  		Name:        model.NewId(),
  1095  		DisplayName: model.NewId(),
  1096  		Email:       model.NewId() + "@nowhere.com",
  1097  		Type:        model.TEAM_OPEN,
  1098  		SchemeId:    &s1.Id,
  1099  	}
  1100  
  1101  	t2 := &model.Team{
  1102  		Name:        model.NewId(),
  1103  		DisplayName: model.NewId(),
  1104  		Email:       model.NewId() + "@nowhere.com",
  1105  		Type:        model.TEAM_OPEN,
  1106  		SchemeId:    &s1.Id,
  1107  	}
  1108  
  1109  	t3 := &model.Team{
  1110  		Name:        model.NewId(),
  1111  		DisplayName: model.NewId(),
  1112  		Email:       model.NewId() + "@nowhere.com",
  1113  		Type:        model.TEAM_OPEN,
  1114  	}
  1115  
  1116  	t1 = (<-ss.Team().Save(t1)).Data.(*model.Team)
  1117  	t2 = (<-ss.Team().Save(t2)).Data.(*model.Team)
  1118  	t3 = (<-ss.Team().Save(t3)).Data.(*model.Team)
  1119  
  1120  	// Get the teams by a valid Scheme ID.
  1121  	res1 := <-ss.Team().GetTeamsByScheme(s1.Id, 0, 100)
  1122  	assert.Nil(t, res1.Err)
  1123  	d1 := res1.Data.([]*model.Team)
  1124  	assert.Len(t, d1, 2)
  1125  
  1126  	// Get the teams by a valid Scheme ID where there aren't any matching Teams.
  1127  	res2 := <-ss.Team().GetTeamsByScheme(s2.Id, 0, 100)
  1128  	assert.Nil(t, res2.Err)
  1129  	d2 := res2.Data.([]*model.Team)
  1130  	assert.Len(t, d2, 0)
  1131  
  1132  	// Get the teams by an invalid Scheme ID.
  1133  	res3 := <-ss.Team().GetTeamsByScheme(model.NewId(), 0, 100)
  1134  	assert.Nil(t, res3.Err)
  1135  	d3 := res3.Data.([]*model.Team)
  1136  	assert.Len(t, d3, 0)
  1137  }
  1138  
  1139  func testTeamStoreMigrateTeamMembers(t *testing.T, ss store.Store) {
  1140  	s1 := model.NewId()
  1141  	t1 := &model.Team{
  1142  		DisplayName: "Name",
  1143  		Name:        "z-z-z" + model.NewId() + "b",
  1144  		Email:       model.NewId() + "@nowhere.com",
  1145  		Type:        model.TEAM_OPEN,
  1146  		InviteId:    model.NewId(),
  1147  		SchemeId:    &s1,
  1148  	}
  1149  	t1 = store.Must(ss.Team().Save(t1)).(*model.Team)
  1150  
  1151  	tm1 := &model.TeamMember{
  1152  		TeamId:        t1.Id,
  1153  		UserId:        model.NewId(),
  1154  		ExplicitRoles: "team_admin team_user",
  1155  	}
  1156  	tm2 := &model.TeamMember{
  1157  		TeamId:        t1.Id,
  1158  		UserId:        model.NewId(),
  1159  		ExplicitRoles: "team_user",
  1160  	}
  1161  	tm3 := &model.TeamMember{
  1162  		TeamId:        t1.Id,
  1163  		UserId:        model.NewId(),
  1164  		ExplicitRoles: "something_else",
  1165  	}
  1166  
  1167  	tm1 = (<-ss.Team().SaveMember(tm1, -1)).Data.(*model.TeamMember)
  1168  	tm2 = (<-ss.Team().SaveMember(tm2, -1)).Data.(*model.TeamMember)
  1169  	tm3 = (<-ss.Team().SaveMember(tm3, -1)).Data.(*model.TeamMember)
  1170  
  1171  	lastDoneTeamId := strings.Repeat("0", 26)
  1172  	lastDoneUserId := strings.Repeat("0", 26)
  1173  
  1174  	for {
  1175  		res := <-ss.Team().MigrateTeamMembers(lastDoneTeamId, lastDoneUserId)
  1176  		if assert.Nil(t, res.Err) {
  1177  			if res.Data == nil {
  1178  				break
  1179  			}
  1180  			data := res.Data.(map[string]string)
  1181  			lastDoneTeamId = data["TeamId"]
  1182  			lastDoneUserId = data["UserId"]
  1183  		}
  1184  	}
  1185  
  1186  	res1 := <-ss.Team().GetMember(tm1.TeamId, tm1.UserId)
  1187  	assert.Nil(t, res1.Err)
  1188  	tm1b := res1.Data.(*model.TeamMember)
  1189  	assert.Equal(t, "", tm1b.ExplicitRoles)
  1190  	assert.True(t, tm1b.SchemeUser)
  1191  	assert.True(t, tm1b.SchemeAdmin)
  1192  
  1193  	res2 := <-ss.Team().GetMember(tm2.TeamId, tm2.UserId)
  1194  	assert.Nil(t, res2.Err)
  1195  	tm2b := res2.Data.(*model.TeamMember)
  1196  	assert.Equal(t, "", tm2b.ExplicitRoles)
  1197  	assert.True(t, tm2b.SchemeUser)
  1198  	assert.False(t, tm2b.SchemeAdmin)
  1199  
  1200  	res3 := <-ss.Team().GetMember(tm3.TeamId, tm3.UserId)
  1201  	assert.Nil(t, res3.Err)
  1202  	tm3b := res3.Data.(*model.TeamMember)
  1203  	assert.Equal(t, "something_else", tm3b.ExplicitRoles)
  1204  	assert.False(t, tm3b.SchemeUser)
  1205  	assert.False(t, tm3b.SchemeAdmin)
  1206  }
  1207  
  1208  func testResetAllTeamSchemes(t *testing.T, ss store.Store) {
  1209  	s1 := &model.Scheme{
  1210  		Name:        model.NewId(),
  1211  		DisplayName: model.NewId(),
  1212  		Description: model.NewId(),
  1213  		Scope:       model.SCHEME_SCOPE_TEAM,
  1214  	}
  1215  	s1 = (<-ss.Scheme().Save(s1)).Data.(*model.Scheme)
  1216  
  1217  	t1 := &model.Team{
  1218  		Name:        model.NewId(),
  1219  		DisplayName: model.NewId(),
  1220  		Email:       model.NewId() + "@nowhere.com",
  1221  		Type:        model.TEAM_OPEN,
  1222  		SchemeId:    &s1.Id,
  1223  	}
  1224  
  1225  	t2 := &model.Team{
  1226  		Name:        model.NewId(),
  1227  		DisplayName: model.NewId(),
  1228  		Email:       model.NewId() + "@nowhere.com",
  1229  		Type:        model.TEAM_OPEN,
  1230  		SchemeId:    &s1.Id,
  1231  	}
  1232  
  1233  	t1 = (<-ss.Team().Save(t1)).Data.(*model.Team)
  1234  	t2 = (<-ss.Team().Save(t2)).Data.(*model.Team)
  1235  
  1236  	assert.Equal(t, s1.Id, *t1.SchemeId)
  1237  	assert.Equal(t, s1.Id, *t2.SchemeId)
  1238  
  1239  	res := <-ss.Team().ResetAllTeamSchemes()
  1240  	assert.Nil(t, res.Err)
  1241  
  1242  	t1 = (<-ss.Team().Get(t1.Id)).Data.(*model.Team)
  1243  	t2 = (<-ss.Team().Get(t2.Id)).Data.(*model.Team)
  1244  
  1245  	assert.Equal(t, "", *t1.SchemeId)
  1246  	assert.Equal(t, "", *t2.SchemeId)
  1247  }