github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/cmd/hkserver/commands/team_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package commands
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/masterhung0112/hk_server/v5/model"
    13  )
    14  
    15  func TestCreateTeam(t *testing.T) {
    16  	th := Setup(t).InitBasic()
    17  	defer th.TearDown()
    18  
    19  	id := model.NewId()
    20  	name := "name" + id
    21  	displayName := "Name " + id
    22  
    23  	th.CheckCommand(t, "team", "create", "--name", name, "--display_name", displayName)
    24  
    25  	found := th.SystemAdminClient.Must(th.SystemAdminClient.TeamExists(name, "")).(bool)
    26  
    27  	require.True(t, found, "Failed to create Team")
    28  }
    29  
    30  func TestJoinTeam(t *testing.T) {
    31  	th := Setup(t).InitBasic()
    32  	defer th.TearDown()
    33  
    34  	th.CheckCommand(t, "team", "add", th.BasicTeam.Name, th.BasicUser.Email)
    35  
    36  	profiles := th.SystemAdminClient.Must(th.SystemAdminClient.GetUsersInTeam(th.BasicTeam.Id, 0, 1000, "")).([]*model.User)
    37  
    38  	found := false
    39  
    40  	for _, user := range profiles {
    41  		if user.Email == th.BasicUser.Email {
    42  			found = true
    43  		}
    44  
    45  	}
    46  
    47  	require.True(t, found, "Failed to create User")
    48  }
    49  
    50  func TestLeaveTeam(t *testing.T) {
    51  	th := Setup(t).InitBasic()
    52  	defer th.TearDown()
    53  
    54  	th.CheckCommand(t, "team", "remove", th.BasicTeam.Name, th.BasicUser.Email)
    55  
    56  	profiles := th.Client.Must(th.Client.GetUsersInTeam(th.BasicTeam.Id, 0, 1000, "")).([]*model.User)
    57  
    58  	found := false
    59  
    60  	for _, user := range profiles {
    61  		if user.Email == th.BasicUser.Email {
    62  			found = true
    63  		}
    64  
    65  	}
    66  
    67  	require.False(t, found, "profile should not be on team")
    68  
    69  	teams, err := th.App.Srv().Store.Team().GetTeamsByUserId(th.BasicUser.Id)
    70  	require.NoError(t, err)
    71  	require.Equal(t, 0, len(teams), "Shouldn't be in team")
    72  }
    73  
    74  func TestListTeams(t *testing.T) {
    75  	th := Setup(t).InitBasic()
    76  	defer th.TearDown()
    77  
    78  	id := model.NewId()
    79  	name := "name" + id
    80  	displayName := "Name " + id
    81  
    82  	th.CheckCommand(t, "team", "create", "--name", name, "--display_name", displayName)
    83  
    84  	output := th.CheckCommand(t, "team", "list", th.BasicTeam.Name, th.BasicUser.Email)
    85  
    86  	assert.Contains(t, output, name, "should have the created team")
    87  }
    88  
    89  func TestListArchivedTeams(t *testing.T) {
    90  	th := Setup(t).InitBasic()
    91  	defer th.TearDown()
    92  
    93  	id := model.NewId()
    94  	name := "name" + id
    95  	displayName := "Name " + id
    96  
    97  	th.CheckCommand(t, "team", "create", "--name", name, "--display_name", displayName)
    98  
    99  	th.CheckCommand(t, "team", "archive", name)
   100  
   101  	output := th.CheckCommand(t, "team", "list", th.BasicTeam.Name, th.BasicUser.Email)
   102  
   103  	assert.Contains(t, output, name+" (archived)", "should have archived team")
   104  }
   105  
   106  func TestSearchTeamsByName(t *testing.T) {
   107  	th := Setup(t).InitBasic()
   108  	defer th.TearDown()
   109  
   110  	id := model.NewId()
   111  	name := "name" + id
   112  	displayName := "Name " + id
   113  
   114  	th.CheckCommand(t, "team", "create", "--name", name, "--display_name", displayName)
   115  
   116  	output := th.CheckCommand(t, "team", "search", name)
   117  
   118  	assert.Contains(t, output, name, "should have the created team")
   119  }
   120  
   121  func TestSearchTeamsByDisplayName(t *testing.T) {
   122  	th := Setup(t).InitBasic()
   123  	defer th.TearDown()
   124  
   125  	id := model.NewId()
   126  	name := "name" + id
   127  	displayName := "Name " + id
   128  
   129  	th.CheckCommand(t, "team", "create", "--name", name, "--display_name", displayName)
   130  
   131  	output := th.CheckCommand(t, "team", "search", displayName)
   132  
   133  	assert.Contains(t, output, name, "should have the created team")
   134  }
   135  
   136  func TestSearchArchivedTeamsByName(t *testing.T) {
   137  	th := Setup(t).InitBasic()
   138  	defer th.TearDown()
   139  
   140  	id := model.NewId()
   141  	name := "name" + id
   142  	displayName := "Name " + id
   143  
   144  	th.CheckCommand(t, "team", "create", "--name", name, "--display_name", displayName)
   145  
   146  	th.CheckCommand(t, "team", "archive", name)
   147  
   148  	output := th.CheckCommand(t, "team", "search", name)
   149  
   150  	assert.Contains(t, output, "(archived)", "should have archived team")
   151  }
   152  
   153  func TestArchiveTeams(t *testing.T) {
   154  	th := Setup(t).InitBasic()
   155  	defer th.TearDown()
   156  
   157  	id := model.NewId()
   158  	name := "name" + id
   159  	displayName := "Name " + id
   160  
   161  	th.CheckCommand(t, "team", "create", "--name", name, "--display_name", displayName)
   162  
   163  	th.CheckCommand(t, "team", "archive", name)
   164  
   165  	output := th.CheckCommand(t, "team", "list")
   166  
   167  	assert.Contains(t, output, name+" (archived)", "should have archived team")
   168  }
   169  
   170  func TestRestoreTeams(t *testing.T) {
   171  	th := Setup(t).InitBasic()
   172  	defer th.TearDown()
   173  
   174  	id := model.NewId()
   175  	name := "name" + id
   176  	displayName := "Name " + id
   177  
   178  	th.CheckCommand(t, "team", "create", "--name", name, "--display_name", displayName)
   179  
   180  	th.CheckCommand(t, "team", "archive", name)
   181  
   182  	th.CheckCommand(t, "team", "restore", name)
   183  
   184  	found := th.SystemAdminClient.Must(th.SystemAdminClient.TeamExists(name, "")).(bool)
   185  
   186  	require.True(t, found)
   187  }
   188  
   189  func TestRenameTeam(t *testing.T) {
   190  	th := Setup(t).InitBasic()
   191  	defer th.TearDown()
   192  
   193  	team := th.CreateTeam()
   194  
   195  	newTeamName := "newteamnamex3"
   196  	newDisplayName := "New Display NameX"
   197  
   198  	th.CheckCommand(t, "team", "rename", team.Name, newTeamName, "--display_name", newDisplayName)
   199  
   200  	// Get the team from the DB
   201  	updatedTeam, _ := th.App.GetTeam(team.Id)
   202  
   203  	require.Equal(t, updatedTeam.Name, newTeamName, "failed renaming team")
   204  	require.Equal(t, updatedTeam.DisplayName, newDisplayName, "failed updating team display name")
   205  
   206  	// Try to rename to occupied name
   207  	team2 := th.CreateTeam()
   208  	n := team2.Name
   209  	dn := team2.DisplayName
   210  
   211  	th.CheckCommand(t, "team", "rename", team2.Name, newTeamName, "--display_name", newDisplayName)
   212  
   213  	// No renaming should have occurred
   214  	require.Equal(t, team2.Name, n, "team was renamed when it should have not been")
   215  	require.Equal(t, team2.DisplayName, dn, "team display name was changed when it should have not been")
   216  
   217  	// Try to change only Display Name
   218  	team3 := th.CreateTeam()
   219  
   220  	// trying to change only Display Name (using "-" as a new team name)
   221  	th.CheckCommand(t, "team", "rename", team3.Name, "-", "--display_name", newDisplayName)
   222  
   223  	// Get the team from the DB
   224  	updatedTeam, _ = th.App.GetTeam(team3.Id)
   225  
   226  	require.NotEqual(t, updatedTeam.Name, "-", "team was renamed to `-` but only display name should have been changed")
   227  	require.Equal(t, updatedTeam.DisplayName, newDisplayName, "team Display Name was not properly updated")
   228  
   229  	// now try to change Display Name using old team name
   230  	th.CheckCommand(t, "team", "rename", team3.Name, team3.Name, "--display_name", "Brand New DName")
   231  
   232  	// Get the team from the DB
   233  	updatedTeam, _ = th.App.GetTeam(team3.Id)
   234  
   235  	require.Equal(t, updatedTeam.DisplayName, "Brand New DName", "team Display Name was not properly updated")
   236  }
   237  
   238  func TestModifyTeam(t *testing.T) {
   239  	th := Setup(t).InitBasic()
   240  	defer th.TearDown()
   241  
   242  	team := th.CreateTeam()
   243  
   244  	th.CheckCommand(t, "team", "modify", team.Name, "--private")
   245  
   246  	updatedTeam, _ := th.App.GetTeam(team.Id)
   247  
   248  	require.False(t, !updatedTeam.AllowOpenInvite && team.Type == model.TEAM_INVITE, "Failed modifying team's privacy to private")
   249  
   250  	th.CheckCommand(t, "team", "modify", team.Name, "--public")
   251  
   252  	require.False(t, updatedTeam.AllowOpenInvite && team.Type == model.TEAM_OPEN, "Failed modifying team's privacy to private")
   253  }