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