github.com/jlevesy/mattermost-server@v5.3.2-0.20181003190404-7468f35cb0c8+incompatible/app/team_test.go (about)

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/mattermost/mattermost-server/model"
    11  )
    12  
    13  func TestCreateTeam(t *testing.T) {
    14  	th := Setup().InitBasic()
    15  	defer th.TearDown()
    16  
    17  	id := model.NewId()
    18  	team := &model.Team{
    19  		DisplayName: "dn_" + id,
    20  		Name:        "name" + id,
    21  		Email:       "success+" + id + "@simulator.amazonses.com",
    22  		Type:        model.TEAM_OPEN,
    23  	}
    24  
    25  	if _, err := th.App.CreateTeam(team); err != nil {
    26  		t.Log(err)
    27  		t.Fatal("Should create a new team")
    28  	}
    29  
    30  	if _, err := th.App.CreateTeam(th.BasicTeam); err == nil {
    31  		t.Fatal("Should not create a new team - team already exist")
    32  	}
    33  }
    34  
    35  func TestCreateTeamWithUser(t *testing.T) {
    36  	th := Setup().InitBasic()
    37  	defer th.TearDown()
    38  
    39  	id := model.NewId()
    40  	team := &model.Team{
    41  		DisplayName: "dn_" + id,
    42  		Name:        "name" + id,
    43  		Email:       "success+" + id + "@simulator.amazonses.com",
    44  		Type:        model.TEAM_OPEN,
    45  	}
    46  
    47  	if _, err := th.App.CreateTeamWithUser(team, th.BasicUser.Id); err != nil {
    48  		t.Fatal("Should create a new team with existing user", err)
    49  	}
    50  
    51  	if _, err := th.App.CreateTeamWithUser(team, model.NewId()); err == nil {
    52  		t.Fatal("Should not create a new team - user does not exist")
    53  	}
    54  }
    55  
    56  func TestUpdateTeam(t *testing.T) {
    57  	th := Setup().InitBasic()
    58  	defer th.TearDown()
    59  
    60  	th.BasicTeam.DisplayName = "Testing 123"
    61  
    62  	if updatedTeam, err := th.App.UpdateTeam(th.BasicTeam); err != nil {
    63  		t.Log(err)
    64  		t.Fatal("Should update the team")
    65  	} else {
    66  		if updatedTeam.DisplayName != "Testing 123" {
    67  			t.Fatal("Wrong Team DisplayName")
    68  		}
    69  	}
    70  }
    71  
    72  func TestAddUserToTeam(t *testing.T) {
    73  	th := Setup().InitBasic()
    74  	defer th.TearDown()
    75  
    76  	t.Run("add user", func(t *testing.T) {
    77  		user := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""}
    78  		ruser, _ := th.App.CreateUser(&user)
    79  		defer th.App.PermanentDeleteUser(&user)
    80  
    81  		if _, err := th.App.AddUserToTeam(th.BasicTeam.Id, ruser.Id, ""); err != nil {
    82  			t.Log(err)
    83  			t.Fatal("Should add user to the team")
    84  		}
    85  	})
    86  
    87  	t.Run("allow user by domain", func(t *testing.T) {
    88  		th.BasicTeam.AllowedDomains = "example.com"
    89  		if _, err := th.App.UpdateTeam(th.BasicTeam); err != nil {
    90  			t.Log(err)
    91  			t.Fatal("Should update the team")
    92  		}
    93  
    94  		user := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""}
    95  		ruser, _ := th.App.CreateUser(&user)
    96  		defer th.App.PermanentDeleteUser(&user)
    97  
    98  		if _, err := th.App.AddUserToTeam(th.BasicTeam.Id, ruser.Id, ""); err != nil {
    99  			t.Log(err)
   100  			t.Fatal("Should have allowed whitelisted user")
   101  		}
   102  	})
   103  
   104  	t.Run("block user by domain", func(t *testing.T) {
   105  		th.BasicTeam.AllowedDomains = "example.com"
   106  		if _, err := th.App.UpdateTeam(th.BasicTeam); err != nil {
   107  			t.Log(err)
   108  			t.Fatal("Should update the team")
   109  		}
   110  
   111  		user := model.User{Email: strings.ToLower(model.NewId()) + "test@invalid.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""}
   112  		ruser, _ := th.App.CreateUser(&user)
   113  		defer th.App.PermanentDeleteUser(&user)
   114  
   115  		if _, err := th.App.AddUserToTeam(th.BasicTeam.Id, ruser.Id, ""); err == nil || err.Where != "JoinUserToTeam" {
   116  			t.Log(err)
   117  			t.Fatal("Should not add restricted user")
   118  		}
   119  	})
   120  
   121  	t.Run("block user with subdomain", func(t *testing.T) {
   122  		th.BasicTeam.AllowedDomains = "example.com"
   123  		if _, err := th.App.UpdateTeam(th.BasicTeam); err != nil {
   124  			t.Log(err)
   125  			t.Fatal("Should update the team")
   126  		}
   127  
   128  		user := model.User{Email: strings.ToLower(model.NewId()) + "test@invalid.example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""}
   129  		ruser, _ := th.App.CreateUser(&user)
   130  		defer th.App.PermanentDeleteUser(&user)
   131  
   132  		if _, err := th.App.AddUserToTeam(th.BasicTeam.Id, ruser.Id, ""); err == nil || err.Where != "JoinUserToTeam" {
   133  			t.Log(err)
   134  			t.Fatal("Should not add restricted user")
   135  		}
   136  	})
   137  
   138  	t.Run("allow users by multiple domains", func(t *testing.T) {
   139  		th.BasicTeam.AllowedDomains = "foo.com, bar.com"
   140  		if _, err := th.App.UpdateTeam(th.BasicTeam); err != nil {
   141  			t.Log(err)
   142  			t.Fatal("Should update the team")
   143  		}
   144  
   145  		user1 := model.User{Email: strings.ToLower(model.NewId()) + "success+test@foo.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""}
   146  		ruser1, _ := th.App.CreateUser(&user1)
   147  		user2 := model.User{Email: strings.ToLower(model.NewId()) + "success+test@bar.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""}
   148  		ruser2, _ := th.App.CreateUser(&user2)
   149  		user3 := model.User{Email: strings.ToLower(model.NewId()) + "success+test@invalid.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""}
   150  		ruser3, _ := th.App.CreateUser(&user3)
   151  		defer th.App.PermanentDeleteUser(&user1)
   152  		defer th.App.PermanentDeleteUser(&user2)
   153  		defer th.App.PermanentDeleteUser(&user3)
   154  
   155  		if _, err := th.App.AddUserToTeam(th.BasicTeam.Id, ruser1.Id, ""); err != nil {
   156  			t.Log(err)
   157  			t.Fatal("Should have allowed whitelisted user1")
   158  		}
   159  		if _, err := th.App.AddUserToTeam(th.BasicTeam.Id, ruser2.Id, ""); err != nil {
   160  			t.Log(err)
   161  			t.Fatal("Should have allowed whitelisted user2")
   162  		}
   163  		if _, err := th.App.AddUserToTeam(th.BasicTeam.Id, ruser3.Id, ""); err == nil || err.Where != "JoinUserToTeam" {
   164  			t.Log(err)
   165  			t.Fatal("Should not have allowed restricted user3")
   166  		}
   167  
   168  	})
   169  }
   170  
   171  func TestAddUserToTeamByToken(t *testing.T) {
   172  	th := Setup().InitBasic()
   173  	defer th.TearDown()
   174  
   175  	user := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""}
   176  	ruser, _ := th.App.CreateUser(&user)
   177  
   178  	t.Run("invalid token", func(t *testing.T) {
   179  		if _, err := th.App.AddUserToTeamByToken(ruser.Id, "123"); err == nil {
   180  			t.Fatal("Should fail on unexisting token")
   181  		}
   182  	})
   183  
   184  	t.Run("invalid token type", func(t *testing.T) {
   185  		token := model.NewToken(
   186  			TOKEN_TYPE_VERIFY_EMAIL,
   187  			model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id}),
   188  		)
   189  		<-th.App.Srv.Store.Token().Save(token)
   190  		defer th.App.DeleteToken(token)
   191  		if _, err := th.App.AddUserToTeamByToken(ruser.Id, token.Token); err == nil {
   192  			t.Fatal("Should fail on bad token type")
   193  		}
   194  	})
   195  
   196  	t.Run("expired token", func(t *testing.T) {
   197  		token := model.NewToken(
   198  			TOKEN_TYPE_TEAM_INVITATION,
   199  			model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id}),
   200  		)
   201  		token.CreateAt = model.GetMillis() - TEAM_INVITATION_EXPIRY_TIME - 1
   202  		<-th.App.Srv.Store.Token().Save(token)
   203  		defer th.App.DeleteToken(token)
   204  		if _, err := th.App.AddUserToTeamByToken(ruser.Id, token.Token); err == nil {
   205  			t.Fatal("Should fail on expired token")
   206  		}
   207  	})
   208  
   209  	t.Run("invalid team id", func(t *testing.T) {
   210  		token := model.NewToken(
   211  			TOKEN_TYPE_TEAM_INVITATION,
   212  			model.MapToJson(map[string]string{"teamId": model.NewId()}),
   213  		)
   214  		<-th.App.Srv.Store.Token().Save(token)
   215  		defer th.App.DeleteToken(token)
   216  		if _, err := th.App.AddUserToTeamByToken(ruser.Id, token.Token); err == nil {
   217  			t.Fatal("Should fail on bad team id")
   218  		}
   219  	})
   220  
   221  	t.Run("invalid user id", func(t *testing.T) {
   222  		token := model.NewToken(
   223  			TOKEN_TYPE_TEAM_INVITATION,
   224  			model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id}),
   225  		)
   226  		<-th.App.Srv.Store.Token().Save(token)
   227  		defer th.App.DeleteToken(token)
   228  		if _, err := th.App.AddUserToTeamByToken(model.NewId(), token.Token); err == nil {
   229  			t.Fatal("Should fail on bad user id")
   230  		}
   231  	})
   232  
   233  	t.Run("valid request", func(t *testing.T) {
   234  		token := model.NewToken(
   235  			TOKEN_TYPE_TEAM_INVITATION,
   236  			model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id}),
   237  		)
   238  		<-th.App.Srv.Store.Token().Save(token)
   239  		if _, err := th.App.AddUserToTeamByToken(ruser.Id, token.Token); err != nil {
   240  			t.Log(err)
   241  			t.Fatal("Should add user to the team")
   242  		}
   243  		if result := <-th.App.Srv.Store.Token().GetByToken(token.Token); result.Err == nil {
   244  			t.Fatal("The token must be deleted after be used")
   245  		}
   246  	})
   247  
   248  	t.Run("block user", func(t *testing.T) {
   249  		th.BasicTeam.AllowedDomains = "example.com"
   250  		if _, err := th.App.UpdateTeam(th.BasicTeam); err != nil {
   251  			t.Log(err)
   252  			t.Fatal("Should update the team")
   253  		}
   254  
   255  		user := model.User{Email: strings.ToLower(model.NewId()) + "test@invalid.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""}
   256  		ruser, _ := th.App.CreateUser(&user)
   257  		defer th.App.PermanentDeleteUser(&user)
   258  
   259  		token := model.NewToken(
   260  			TOKEN_TYPE_TEAM_INVITATION,
   261  			model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id}),
   262  		)
   263  		<-th.App.Srv.Store.Token().Save(token)
   264  
   265  		if _, err := th.App.AddUserToTeamByToken(ruser.Id, token.Token); err == nil || err.Where != "JoinUserToTeam" {
   266  			t.Log(err)
   267  			t.Fatal("Should not add restricted user")
   268  		}
   269  	})
   270  }
   271  
   272  func TestAddUserToTeamByTeamId(t *testing.T) {
   273  	th := Setup().InitBasic()
   274  	defer th.TearDown()
   275  
   276  	t.Run("add user", func(t *testing.T) {
   277  		user := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""}
   278  		ruser, _ := th.App.CreateUser(&user)
   279  
   280  		if err := th.App.AddUserToTeamByTeamId(th.BasicTeam.Id, ruser); err != nil {
   281  			t.Log(err)
   282  			t.Fatal("Should add user to the team")
   283  		}
   284  	})
   285  
   286  	t.Run("block user", func(t *testing.T) {
   287  		th.BasicTeam.AllowedDomains = "example.com"
   288  		if _, err := th.App.UpdateTeam(th.BasicTeam); err != nil {
   289  			t.Log(err)
   290  			t.Fatal("Should update the team")
   291  		}
   292  
   293  		user := model.User{Email: strings.ToLower(model.NewId()) + "test@invalid.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""}
   294  		ruser, _ := th.App.CreateUser(&user)
   295  		defer th.App.PermanentDeleteUser(&user)
   296  
   297  		if err := th.App.AddUserToTeamByTeamId(th.BasicTeam.Id, ruser); err == nil || err.Where != "JoinUserToTeam" {
   298  			t.Log(err)
   299  			t.Fatal("Should not add restricted user")
   300  		}
   301  	})
   302  
   303  }
   304  
   305  func TestPermanentDeleteTeam(t *testing.T) {
   306  	th := Setup().InitBasic()
   307  	defer th.TearDown()
   308  
   309  	team, err := th.App.CreateTeam(&model.Team{
   310  		DisplayName: "deletion-test",
   311  		Name:        "deletion-test",
   312  		Email:       "foo@foo.com",
   313  		Type:        model.TEAM_OPEN,
   314  	})
   315  	if err != nil {
   316  		t.Fatal(err.Error())
   317  	}
   318  	defer func() {
   319  		th.App.PermanentDeleteTeam(team)
   320  	}()
   321  
   322  	command, err := th.App.CreateCommand(&model.Command{
   323  		CreatorId: th.BasicUser.Id,
   324  		TeamId:    team.Id,
   325  		Trigger:   "foo",
   326  		URL:       "http://foo",
   327  		Method:    model.COMMAND_METHOD_POST,
   328  	})
   329  	if err != nil {
   330  		t.Fatal(err.Error())
   331  	}
   332  	defer th.App.DeleteCommand(command.Id)
   333  
   334  	if command, err = th.App.GetCommand(command.Id); command == nil || err != nil {
   335  		t.Fatal("unable to get new command")
   336  	}
   337  
   338  	if err := th.App.PermanentDeleteTeam(team); err != nil {
   339  		t.Fatal(err.Error())
   340  	}
   341  
   342  	if command, err = th.App.GetCommand(command.Id); command != nil || err == nil {
   343  		t.Fatal("command wasn't deleted")
   344  	}
   345  
   346  	// Test deleting a team with no channels.
   347  	team = th.CreateTeam()
   348  	defer func() {
   349  		th.App.PermanentDeleteTeam(team)
   350  	}()
   351  
   352  	if channels, err := th.App.GetPublicChannelsForTeam(team.Id, 0, 1000); err != nil {
   353  		t.Fatal(err)
   354  	} else {
   355  		for _, channel := range *channels {
   356  			if err2 := th.App.PermanentDeleteChannel(channel); err2 != nil {
   357  				t.Fatal(err)
   358  			}
   359  		}
   360  	}
   361  
   362  	if err := th.App.PermanentDeleteTeam(team); err != nil {
   363  		t.Fatal(err)
   364  	}
   365  }
   366  
   367  func TestSanitizeTeam(t *testing.T) {
   368  	th := Setup()
   369  	defer th.TearDown()
   370  
   371  	team := &model.Team{
   372  		Id:             model.NewId(),
   373  		Email:          th.MakeEmail(),
   374  		AllowedDomains: "example.com",
   375  	}
   376  	copyTeam := func() *model.Team {
   377  		copy := &model.Team{}
   378  		*copy = *team
   379  		return copy
   380  	}
   381  
   382  	t.Run("not a user of the team", func(t *testing.T) {
   383  		userId := model.NewId()
   384  		session := model.Session{
   385  			Roles: model.SYSTEM_USER_ROLE_ID,
   386  			TeamMembers: []*model.TeamMember{
   387  				{
   388  					UserId: userId,
   389  					TeamId: model.NewId(),
   390  					Roles:  model.TEAM_USER_ROLE_ID,
   391  				},
   392  			},
   393  		}
   394  
   395  		sanitized := th.App.SanitizeTeam(session, copyTeam())
   396  		if sanitized.Email != "" {
   397  			t.Fatal("should've sanitized team")
   398  		}
   399  	})
   400  
   401  	t.Run("user of the team", func(t *testing.T) {
   402  		userId := model.NewId()
   403  		session := model.Session{
   404  			Roles: model.SYSTEM_USER_ROLE_ID,
   405  			TeamMembers: []*model.TeamMember{
   406  				{
   407  					UserId: userId,
   408  					TeamId: team.Id,
   409  					Roles:  model.TEAM_USER_ROLE_ID,
   410  				},
   411  			},
   412  		}
   413  
   414  		sanitized := th.App.SanitizeTeam(session, copyTeam())
   415  		if sanitized.Email != "" {
   416  			t.Fatal("should've sanitized team")
   417  		}
   418  	})
   419  
   420  	t.Run("team admin", func(t *testing.T) {
   421  		userId := model.NewId()
   422  		session := model.Session{
   423  			Roles: model.SYSTEM_USER_ROLE_ID,
   424  			TeamMembers: []*model.TeamMember{
   425  				{
   426  					UserId: userId,
   427  					TeamId: team.Id,
   428  					Roles:  model.TEAM_USER_ROLE_ID + " " + model.TEAM_ADMIN_ROLE_ID,
   429  				},
   430  			},
   431  		}
   432  
   433  		sanitized := th.App.SanitizeTeam(session, copyTeam())
   434  		if sanitized.Email == "" {
   435  			t.Fatal("shouldn't have sanitized team")
   436  		}
   437  	})
   438  
   439  	t.Run("team admin of another team", func(t *testing.T) {
   440  		userId := model.NewId()
   441  		session := model.Session{
   442  			Roles: model.SYSTEM_USER_ROLE_ID,
   443  			TeamMembers: []*model.TeamMember{
   444  				{
   445  					UserId: userId,
   446  					TeamId: model.NewId(),
   447  					Roles:  model.TEAM_USER_ROLE_ID + " " + model.TEAM_ADMIN_ROLE_ID,
   448  				},
   449  			},
   450  		}
   451  
   452  		sanitized := th.App.SanitizeTeam(session, copyTeam())
   453  		if sanitized.Email != "" {
   454  			t.Fatal("should've sanitized team")
   455  		}
   456  	})
   457  
   458  	t.Run("system admin, not a user of team", func(t *testing.T) {
   459  		userId := model.NewId()
   460  		session := model.Session{
   461  			Roles: model.SYSTEM_USER_ROLE_ID + " " + model.SYSTEM_ADMIN_ROLE_ID,
   462  			TeamMembers: []*model.TeamMember{
   463  				{
   464  					UserId: userId,
   465  					TeamId: model.NewId(),
   466  					Roles:  model.TEAM_USER_ROLE_ID,
   467  				},
   468  			},
   469  		}
   470  
   471  		sanitized := th.App.SanitizeTeam(session, copyTeam())
   472  		if sanitized.Email == "" {
   473  			t.Fatal("shouldn't have sanitized team")
   474  		}
   475  	})
   476  
   477  	t.Run("system admin, user of team", func(t *testing.T) {
   478  		userId := model.NewId()
   479  		session := model.Session{
   480  			Roles: model.SYSTEM_USER_ROLE_ID + " " + model.SYSTEM_ADMIN_ROLE_ID,
   481  			TeamMembers: []*model.TeamMember{
   482  				{
   483  					UserId: userId,
   484  					TeamId: team.Id,
   485  					Roles:  model.TEAM_USER_ROLE_ID,
   486  				},
   487  			},
   488  		}
   489  
   490  		sanitized := th.App.SanitizeTeam(session, copyTeam())
   491  		if sanitized.Email == "" {
   492  			t.Fatal("shouldn't have sanitized team")
   493  		}
   494  	})
   495  }
   496  
   497  func TestSanitizeTeams(t *testing.T) {
   498  	th := Setup()
   499  	defer th.TearDown()
   500  
   501  	t.Run("not a system admin", func(t *testing.T) {
   502  		teams := []*model.Team{
   503  			{
   504  				Id:             model.NewId(),
   505  				Email:          th.MakeEmail(),
   506  				AllowedDomains: "example.com",
   507  			},
   508  			{
   509  				Id:             model.NewId(),
   510  				Email:          th.MakeEmail(),
   511  				AllowedDomains: "example.com",
   512  			},
   513  		}
   514  
   515  		userId := model.NewId()
   516  		session := model.Session{
   517  			Roles: model.SYSTEM_USER_ROLE_ID,
   518  			TeamMembers: []*model.TeamMember{
   519  				{
   520  					UserId: userId,
   521  					TeamId: teams[0].Id,
   522  					Roles:  model.TEAM_USER_ROLE_ID,
   523  				},
   524  				{
   525  					UserId: userId,
   526  					TeamId: teams[1].Id,
   527  					Roles:  model.TEAM_USER_ROLE_ID + " " + model.TEAM_ADMIN_ROLE_ID,
   528  				},
   529  			},
   530  		}
   531  
   532  		sanitized := th.App.SanitizeTeams(session, teams)
   533  
   534  		if sanitized[0].Email != "" {
   535  			t.Fatal("should've sanitized first team")
   536  		}
   537  
   538  		if sanitized[1].Email == "" {
   539  			t.Fatal("shouldn't have sanitized second team")
   540  		}
   541  	})
   542  
   543  	t.Run("system admin", func(t *testing.T) {
   544  		teams := []*model.Team{
   545  			{
   546  				Id:             model.NewId(),
   547  				Email:          th.MakeEmail(),
   548  				AllowedDomains: "example.com",
   549  			},
   550  			{
   551  				Id:             model.NewId(),
   552  				Email:          th.MakeEmail(),
   553  				AllowedDomains: "example.com",
   554  			},
   555  		}
   556  
   557  		userId := model.NewId()
   558  		session := model.Session{
   559  			Roles: model.SYSTEM_USER_ROLE_ID + " " + model.SYSTEM_ADMIN_ROLE_ID,
   560  			TeamMembers: []*model.TeamMember{
   561  				{
   562  					UserId: userId,
   563  					TeamId: teams[0].Id,
   564  					Roles:  model.TEAM_USER_ROLE_ID,
   565  				},
   566  			},
   567  		}
   568  
   569  		sanitized := th.App.SanitizeTeams(session, teams)
   570  
   571  		if sanitized[0].Email == "" {
   572  			t.Fatal("shouldn't have sanitized first team")
   573  		}
   574  
   575  		if sanitized[1].Email == "" {
   576  			t.Fatal("shouldn't have sanitized second team")
   577  		}
   578  	})
   579  }
   580  
   581  func TestJoinUserToTeam(t *testing.T) {
   582  	th := Setup().InitBasic()
   583  	defer th.TearDown()
   584  
   585  	id := model.NewId()
   586  	team := &model.Team{
   587  		DisplayName: "dn_" + id,
   588  		Name:        "name" + id,
   589  		Email:       "success+" + id + "@simulator.amazonses.com",
   590  		Type:        model.TEAM_OPEN,
   591  	}
   592  
   593  	if _, err := th.App.CreateTeam(team); err != nil {
   594  		t.Log(err)
   595  		t.Fatal("Should create a new team")
   596  	}
   597  
   598  	maxUsersPerTeam := th.App.Config().TeamSettings.MaxUsersPerTeam
   599  	defer func() {
   600  		th.App.UpdateConfig(func(cfg *model.Config) { cfg.TeamSettings.MaxUsersPerTeam = maxUsersPerTeam })
   601  		th.App.PermanentDeleteTeam(team)
   602  	}()
   603  	one := 1
   604  	th.App.UpdateConfig(func(cfg *model.Config) { cfg.TeamSettings.MaxUsersPerTeam = &one })
   605  
   606  	t.Run("new join", func(t *testing.T) {
   607  		user := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""}
   608  		ruser, _ := th.App.CreateUser(&user)
   609  		defer th.App.PermanentDeleteUser(&user)
   610  
   611  		if _, alreadyAdded, err := th.App.joinUserToTeam(team, ruser); alreadyAdded || err != nil {
   612  			t.Fatal("Should return already added equal to false and no error")
   613  		}
   614  	})
   615  
   616  	t.Run("join when you are a member", func(t *testing.T) {
   617  		user := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""}
   618  		ruser, _ := th.App.CreateUser(&user)
   619  		defer th.App.PermanentDeleteUser(&user)
   620  
   621  		th.App.joinUserToTeam(team, ruser)
   622  		if _, alreadyAdded, err := th.App.joinUserToTeam(team, ruser); !alreadyAdded || err != nil {
   623  			t.Fatal("Should return already added and no error")
   624  		}
   625  	})
   626  
   627  	t.Run("re-join after leaving", func(t *testing.T) {
   628  		user := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""}
   629  		ruser, _ := th.App.CreateUser(&user)
   630  		defer th.App.PermanentDeleteUser(&user)
   631  
   632  		th.App.joinUserToTeam(team, ruser)
   633  		th.App.LeaveTeam(team, ruser, ruser.Id)
   634  		if _, alreadyAdded, err := th.App.joinUserToTeam(team, ruser); alreadyAdded || err != nil {
   635  			t.Fatal("Should return already added equal to false and no error")
   636  		}
   637  	})
   638  
   639  	t.Run("new join with limit problem", func(t *testing.T) {
   640  		user1 := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""}
   641  		ruser1, _ := th.App.CreateUser(&user1)
   642  		user2 := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""}
   643  		ruser2, _ := th.App.CreateUser(&user2)
   644  		defer th.App.PermanentDeleteUser(&user1)
   645  		defer th.App.PermanentDeleteUser(&user2)
   646  		th.App.joinUserToTeam(team, ruser1)
   647  		if _, _, err := th.App.joinUserToTeam(team, ruser2); err == nil {
   648  			t.Fatal("Should fail")
   649  		}
   650  	})
   651  
   652  	t.Run("re-join alfter leaving with limit problem", func(t *testing.T) {
   653  		user1 := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""}
   654  		ruser1, _ := th.App.CreateUser(&user1)
   655  		user2 := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""}
   656  		ruser2, _ := th.App.CreateUser(&user2)
   657  		defer th.App.PermanentDeleteUser(&user1)
   658  		defer th.App.PermanentDeleteUser(&user2)
   659  
   660  		th.App.joinUserToTeam(team, ruser1)
   661  		th.App.LeaveTeam(team, ruser1, ruser1.Id)
   662  		th.App.joinUserToTeam(team, ruser2)
   663  		if _, _, err := th.App.joinUserToTeam(team, ruser1); err == nil {
   664  			t.Fatal("Should fail")
   665  		}
   666  	})
   667  }
   668  
   669  func TestAppUpdateTeamScheme(t *testing.T) {
   670  	th := Setup().InitBasic()
   671  	defer th.TearDown()
   672  
   673  	team := th.BasicTeam
   674  	mockID := model.NewString("x")
   675  	team.SchemeId = mockID
   676  
   677  	updatedTeam, err := th.App.UpdateTeamScheme(th.BasicTeam)
   678  	if err != nil {
   679  		t.Fatal(err)
   680  	}
   681  
   682  	if updatedTeam.SchemeId != mockID {
   683  		t.Fatal("Wrong Team SchemeId")
   684  	}
   685  }