github.com/dschalla/mattermost-server@v4.8.1-rc1+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.Log(err) 49 t.Fatal("Should create a new team with existing user") 50 } 51 52 if _, err := th.App.CreateTeamWithUser(team, model.NewId()); err == nil { 53 t.Fatal("Should not create a new team - user does not exist") 54 } 55 56 user := model.User{Email: strings.ToLower(model.NewId()) + "success+test", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} 57 ruser, _ := th.App.CreateUser(&user) 58 59 id = model.NewId() 60 team2 := &model.Team{ 61 DisplayName: "dn_" + id, 62 Name: "name" + id, 63 Email: "success2+" + id + "@simulator.amazonses.com", 64 Type: model.TEAM_OPEN, 65 } 66 67 //Fail to create a team with user when user has set email without domain 68 if _, err := th.App.CreateTeamWithUser(team2, ruser.Id); err == nil { 69 t.Log(err.Message) 70 t.Fatal("Should not create a team with user when user has set email without domain") 71 } else { 72 if err.Id != "model.team.is_valid.email.app_error" { 73 t.Log(err) 74 t.Fatal("Invalid error message") 75 } 76 } 77 } 78 79 func TestUpdateTeam(t *testing.T) { 80 th := Setup().InitBasic() 81 defer th.TearDown() 82 83 th.BasicTeam.DisplayName = "Testing 123" 84 85 if updatedTeam, err := th.App.UpdateTeam(th.BasicTeam); err != nil { 86 t.Log(err) 87 t.Fatal("Should update the team") 88 } else { 89 if updatedTeam.DisplayName != "Testing 123" { 90 t.Fatal("Wrong Team DisplayName") 91 } 92 } 93 } 94 95 func TestAddUserToTeam(t *testing.T) { 96 th := Setup().InitBasic() 97 defer th.TearDown() 98 99 user := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} 100 ruser, _ := th.App.CreateUser(&user) 101 102 if _, err := th.App.AddUserToTeam(th.BasicTeam.Id, ruser.Id, ""); err != nil { 103 t.Log(err) 104 t.Fatal("Should add user to the team") 105 } 106 } 107 108 func TestAddUserToTeamByTeamId(t *testing.T) { 109 th := Setup().InitBasic() 110 defer th.TearDown() 111 112 user := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} 113 ruser, _ := th.App.CreateUser(&user) 114 115 if err := th.App.AddUserToTeamByTeamId(th.BasicTeam.Id, ruser); err != nil { 116 t.Log(err) 117 t.Fatal("Should add user to the team") 118 } 119 } 120 121 func TestPermanentDeleteTeam(t *testing.T) { 122 th := Setup().InitBasic() 123 defer th.TearDown() 124 125 team, err := th.App.CreateTeam(&model.Team{ 126 DisplayName: "deletion-test", 127 Name: "deletion-test", 128 Email: "foo@foo.com", 129 Type: model.TEAM_OPEN, 130 }) 131 if err != nil { 132 t.Fatal(err.Error()) 133 } 134 defer func() { 135 th.App.PermanentDeleteTeam(team) 136 }() 137 138 command, err := th.App.CreateCommand(&model.Command{ 139 CreatorId: th.BasicUser.Id, 140 TeamId: team.Id, 141 Trigger: "foo", 142 URL: "http://foo", 143 Method: model.COMMAND_METHOD_POST, 144 }) 145 if err != nil { 146 t.Fatal(err.Error()) 147 } 148 defer th.App.DeleteCommand(command.Id) 149 150 if command, err = th.App.GetCommand(command.Id); command == nil || err != nil { 151 t.Fatal("unable to get new command") 152 } 153 154 if err := th.App.PermanentDeleteTeam(team); err != nil { 155 t.Fatal(err.Error()) 156 } 157 158 if command, err = th.App.GetCommand(command.Id); command != nil || err == nil { 159 t.Fatal("command wasn't deleted") 160 } 161 162 // Test deleting a team with no channels. 163 team = th.CreateTeam() 164 defer func() { 165 th.App.PermanentDeleteTeam(team) 166 }() 167 168 if channels, err := th.App.GetPublicChannelsForTeam(team.Id, 0, 1000); err != nil { 169 t.Fatal(err) 170 } else { 171 for _, channel := range *channels { 172 if err2 := th.App.PermanentDeleteChannel(channel); err2 != nil { 173 t.Fatal(err) 174 } 175 } 176 } 177 178 if err := th.App.PermanentDeleteTeam(team); err != nil { 179 t.Fatal(err) 180 } 181 } 182 183 func TestSanitizeTeam(t *testing.T) { 184 th := Setup() 185 defer th.TearDown() 186 187 team := &model.Team{ 188 Id: model.NewId(), 189 Email: th.MakeEmail(), 190 AllowedDomains: "example.com", 191 } 192 copyTeam := func() *model.Team { 193 copy := &model.Team{} 194 *copy = *team 195 return copy 196 } 197 198 t.Run("not a user of the team", func(t *testing.T) { 199 userId := model.NewId() 200 session := model.Session{ 201 Roles: model.SYSTEM_USER_ROLE_ID, 202 TeamMembers: []*model.TeamMember{ 203 { 204 UserId: userId, 205 TeamId: model.NewId(), 206 Roles: model.TEAM_USER_ROLE_ID, 207 }, 208 }, 209 } 210 211 sanitized := th.App.SanitizeTeam(session, copyTeam()) 212 if sanitized.Email != "" && sanitized.AllowedDomains != "" { 213 t.Fatal("should've sanitized team") 214 } 215 }) 216 217 t.Run("user of the team", func(t *testing.T) { 218 userId := model.NewId() 219 session := model.Session{ 220 Roles: model.SYSTEM_USER_ROLE_ID, 221 TeamMembers: []*model.TeamMember{ 222 { 223 UserId: userId, 224 TeamId: team.Id, 225 Roles: model.TEAM_USER_ROLE_ID, 226 }, 227 }, 228 } 229 230 sanitized := th.App.SanitizeTeam(session, copyTeam()) 231 if sanitized.Email != "" && sanitized.AllowedDomains != "" { 232 t.Fatal("should've sanitized team") 233 } 234 }) 235 236 t.Run("team admin", func(t *testing.T) { 237 userId := model.NewId() 238 session := model.Session{ 239 Roles: model.SYSTEM_USER_ROLE_ID, 240 TeamMembers: []*model.TeamMember{ 241 { 242 UserId: userId, 243 TeamId: team.Id, 244 Roles: model.TEAM_USER_ROLE_ID + " " + model.TEAM_ADMIN_ROLE_ID, 245 }, 246 }, 247 } 248 249 sanitized := th.App.SanitizeTeam(session, copyTeam()) 250 if sanitized.Email == "" && sanitized.AllowedDomains == "" { 251 t.Fatal("shouldn't have sanitized team") 252 } 253 }) 254 255 t.Run("team admin of another team", func(t *testing.T) { 256 userId := model.NewId() 257 session := model.Session{ 258 Roles: model.SYSTEM_USER_ROLE_ID, 259 TeamMembers: []*model.TeamMember{ 260 { 261 UserId: userId, 262 TeamId: model.NewId(), 263 Roles: model.TEAM_USER_ROLE_ID + " " + model.TEAM_ADMIN_ROLE_ID, 264 }, 265 }, 266 } 267 268 sanitized := th.App.SanitizeTeam(session, copyTeam()) 269 if sanitized.Email != "" && sanitized.AllowedDomains != "" { 270 t.Fatal("should've sanitized team") 271 } 272 }) 273 274 t.Run("system admin, not a user of team", func(t *testing.T) { 275 userId := model.NewId() 276 session := model.Session{ 277 Roles: model.SYSTEM_USER_ROLE_ID + " " + model.SYSTEM_ADMIN_ROLE_ID, 278 TeamMembers: []*model.TeamMember{ 279 { 280 UserId: userId, 281 TeamId: model.NewId(), 282 Roles: model.TEAM_USER_ROLE_ID, 283 }, 284 }, 285 } 286 287 sanitized := th.App.SanitizeTeam(session, copyTeam()) 288 if sanitized.Email == "" && sanitized.AllowedDomains == "" { 289 t.Fatal("shouldn't have sanitized team") 290 } 291 }) 292 293 t.Run("system admin, user of team", func(t *testing.T) { 294 userId := model.NewId() 295 session := model.Session{ 296 Roles: model.SYSTEM_USER_ROLE_ID + " " + model.SYSTEM_ADMIN_ROLE_ID, 297 TeamMembers: []*model.TeamMember{ 298 { 299 UserId: userId, 300 TeamId: team.Id, 301 Roles: model.TEAM_USER_ROLE_ID, 302 }, 303 }, 304 } 305 306 sanitized := th.App.SanitizeTeam(session, copyTeam()) 307 if sanitized.Email == "" && sanitized.AllowedDomains == "" { 308 t.Fatal("shouldn't have sanitized team") 309 } 310 }) 311 } 312 313 func TestSanitizeTeams(t *testing.T) { 314 th := Setup() 315 defer th.TearDown() 316 317 t.Run("not a system admin", func(t *testing.T) { 318 teams := []*model.Team{ 319 { 320 Id: model.NewId(), 321 Email: th.MakeEmail(), 322 AllowedDomains: "example.com", 323 }, 324 { 325 Id: model.NewId(), 326 Email: th.MakeEmail(), 327 AllowedDomains: "example.com", 328 }, 329 } 330 331 userId := model.NewId() 332 session := model.Session{ 333 Roles: model.SYSTEM_USER_ROLE_ID, 334 TeamMembers: []*model.TeamMember{ 335 { 336 UserId: userId, 337 TeamId: teams[0].Id, 338 Roles: model.TEAM_USER_ROLE_ID, 339 }, 340 { 341 UserId: userId, 342 TeamId: teams[1].Id, 343 Roles: model.TEAM_USER_ROLE_ID + " " + model.TEAM_ADMIN_ROLE_ID, 344 }, 345 }, 346 } 347 348 sanitized := th.App.SanitizeTeams(session, teams) 349 350 if sanitized[0].Email != "" && sanitized[0].AllowedDomains != "" { 351 t.Fatal("should've sanitized first team") 352 } 353 354 if sanitized[1].Email == "" && sanitized[1].AllowedDomains == "" { 355 t.Fatal("shouldn't have sanitized second team") 356 } 357 }) 358 359 t.Run("system admin", func(t *testing.T) { 360 teams := []*model.Team{ 361 { 362 Id: model.NewId(), 363 Email: th.MakeEmail(), 364 AllowedDomains: "example.com", 365 }, 366 { 367 Id: model.NewId(), 368 Email: th.MakeEmail(), 369 AllowedDomains: "example.com", 370 }, 371 } 372 373 userId := model.NewId() 374 session := model.Session{ 375 Roles: model.SYSTEM_USER_ROLE_ID + " " + model.SYSTEM_ADMIN_ROLE_ID, 376 TeamMembers: []*model.TeamMember{ 377 { 378 UserId: userId, 379 TeamId: teams[0].Id, 380 Roles: model.TEAM_USER_ROLE_ID, 381 }, 382 }, 383 } 384 385 sanitized := th.App.SanitizeTeams(session, teams) 386 387 if sanitized[0].Email == "" && sanitized[0].AllowedDomains == "" { 388 t.Fatal("shouldn't have sanitized first team") 389 } 390 391 if sanitized[1].Email == "" && sanitized[1].AllowedDomains == "" { 392 t.Fatal("shouldn't have sanitized second team") 393 } 394 }) 395 } 396 397 func TestJoinUserToTeam(t *testing.T) { 398 th := Setup().InitBasic() 399 defer th.TearDown() 400 401 id := model.NewId() 402 team := &model.Team{ 403 DisplayName: "dn_" + id, 404 Name: "name" + id, 405 Email: "success+" + id + "@simulator.amazonses.com", 406 Type: model.TEAM_OPEN, 407 } 408 409 if _, err := th.App.CreateTeam(team); err != nil { 410 t.Log(err) 411 t.Fatal("Should create a new team") 412 } 413 414 maxUsersPerTeam := th.App.Config().TeamSettings.MaxUsersPerTeam 415 defer func() { 416 th.App.UpdateConfig(func(cfg *model.Config) { cfg.TeamSettings.MaxUsersPerTeam = maxUsersPerTeam }) 417 th.App.PermanentDeleteTeam(team) 418 }() 419 one := 1 420 th.App.UpdateConfig(func(cfg *model.Config) { cfg.TeamSettings.MaxUsersPerTeam = &one }) 421 422 t.Run("new join", func(t *testing.T) { 423 user := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} 424 ruser, _ := th.App.CreateUser(&user) 425 defer th.App.PermanentDeleteUser(&user) 426 427 if _, alreadyAdded, err := th.App.joinUserToTeam(team, ruser); alreadyAdded || err != nil { 428 t.Fatal("Should return already added equal to false and no error") 429 } 430 }) 431 432 t.Run("join when you are a member", func(t *testing.T) { 433 user := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} 434 ruser, _ := th.App.CreateUser(&user) 435 defer th.App.PermanentDeleteUser(&user) 436 437 th.App.joinUserToTeam(team, ruser) 438 if _, alreadyAdded, err := th.App.joinUserToTeam(team, ruser); !alreadyAdded || err != nil { 439 t.Fatal("Should return already added and no error") 440 } 441 }) 442 443 t.Run("re-join after leaving", func(t *testing.T) { 444 user := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} 445 ruser, _ := th.App.CreateUser(&user) 446 defer th.App.PermanentDeleteUser(&user) 447 448 th.App.joinUserToTeam(team, ruser) 449 th.App.LeaveTeam(team, ruser, ruser.Id) 450 if _, alreadyAdded, err := th.App.joinUserToTeam(team, ruser); alreadyAdded || err != nil { 451 t.Fatal("Should return already added equal to false and no error") 452 } 453 }) 454 455 t.Run("new join with limit problem", func(t *testing.T) { 456 user1 := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} 457 ruser1, _ := th.App.CreateUser(&user1) 458 user2 := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} 459 ruser2, _ := th.App.CreateUser(&user2) 460 defer th.App.PermanentDeleteUser(&user1) 461 defer th.App.PermanentDeleteUser(&user2) 462 th.App.joinUserToTeam(team, ruser1) 463 if _, _, err := th.App.joinUserToTeam(team, ruser2); err == nil { 464 t.Fatal("Should fail") 465 } 466 }) 467 468 t.Run("re-join alfter leaving with limit problem", func(t *testing.T) { 469 user1 := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} 470 ruser1, _ := th.App.CreateUser(&user1) 471 user2 := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} 472 ruser2, _ := th.App.CreateUser(&user2) 473 defer th.App.PermanentDeleteUser(&user1) 474 defer th.App.PermanentDeleteUser(&user2) 475 476 th.App.joinUserToTeam(team, ruser1) 477 th.App.LeaveTeam(team, ruser1, ruser1.Id) 478 th.App.joinUserToTeam(team, ruser2) 479 if _, _, err := th.App.joinUserToTeam(team, ruser1); err == nil { 480 t.Fatal("Should fail") 481 } 482 }) 483 }