github.com/coincircle/mattermost-server@v4.8.1-0.20180321182714-9d701c704416+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 "fmt" 11 12 "sync/atomic" 13 14 "github.com/mattermost/mattermost-server/model" 15 "github.com/mattermost/mattermost-server/store" 16 "github.com/mattermost/mattermost-server/store/storetest" 17 "github.com/mattermost/mattermost-server/utils" 18 "github.com/stretchr/testify/assert" 19 ) 20 21 func TestCreateTeam(t *testing.T) { 22 th := Setup().InitBasic() 23 defer th.TearDown() 24 25 id := model.NewId() 26 team := &model.Team{ 27 DisplayName: "dn_" + id, 28 Name: "name" + id, 29 Email: "success+" + id + "@simulator.amazonses.com", 30 Type: model.TEAM_OPEN, 31 } 32 33 if _, err := th.App.CreateTeam(team); err != nil { 34 t.Log(err) 35 t.Fatal("Should create a new team") 36 } 37 38 if _, err := th.App.CreateTeam(th.BasicTeam); err == nil { 39 t.Fatal("Should not create a new team - team already exist") 40 } 41 } 42 43 func TestCreateTeamWithUser(t *testing.T) { 44 th := Setup().InitBasic() 45 defer th.TearDown() 46 47 id := model.NewId() 48 team := &model.Team{ 49 DisplayName: "dn_" + id, 50 Name: "name" + id, 51 Email: "success+" + id + "@simulator.amazonses.com", 52 Type: model.TEAM_OPEN, 53 } 54 55 if _, err := th.App.CreateTeamWithUser(team, th.BasicUser.Id); err != nil { 56 t.Log(err) 57 t.Fatal("Should create a new team with existing user") 58 } 59 60 if _, err := th.App.CreateTeamWithUser(team, model.NewId()); err == nil { 61 t.Fatal("Should not create a new team - user does not exist") 62 } 63 64 user := model.User{Email: strings.ToLower(model.NewId()) + "success+test", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} 65 ruser, _ := th.App.CreateUser(&user) 66 67 id = model.NewId() 68 team2 := &model.Team{ 69 DisplayName: "dn_" + id, 70 Name: "name" + id, 71 Email: "success2+" + id + "@simulator.amazonses.com", 72 Type: model.TEAM_OPEN, 73 } 74 75 //Fail to create a team with user when user has set email without domain 76 if _, err := th.App.CreateTeamWithUser(team2, ruser.Id); err == nil { 77 t.Log(err.Message) 78 t.Fatal("Should not create a team with user when user has set email without domain") 79 } else { 80 if err.Id != "model.team.is_valid.email.app_error" { 81 t.Log(err) 82 t.Fatal("Invalid error message") 83 } 84 } 85 } 86 87 func TestUpdateTeam(t *testing.T) { 88 th := Setup().InitBasic() 89 defer th.TearDown() 90 91 th.BasicTeam.DisplayName = "Testing 123" 92 93 if updatedTeam, err := th.App.UpdateTeam(th.BasicTeam); err != nil { 94 t.Log(err) 95 t.Fatal("Should update the team") 96 } else { 97 if updatedTeam.DisplayName != "Testing 123" { 98 t.Fatal("Wrong Team DisplayName") 99 } 100 } 101 } 102 103 func TestAddUserToTeam(t *testing.T) { 104 th := Setup().InitBasic() 105 defer th.TearDown() 106 107 user := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} 108 ruser, _ := th.App.CreateUser(&user) 109 110 if _, err := th.App.AddUserToTeam(th.BasicTeam.Id, ruser.Id, ""); err != nil { 111 t.Log(err) 112 t.Fatal("Should add user to the team") 113 } 114 } 115 116 func TestAddUserToTeamByTeamId(t *testing.T) { 117 th := Setup().InitBasic() 118 defer th.TearDown() 119 120 user := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} 121 ruser, _ := th.App.CreateUser(&user) 122 123 if err := th.App.AddUserToTeamByTeamId(th.BasicTeam.Id, ruser); err != nil { 124 t.Log(err) 125 t.Fatal("Should add user to the team") 126 } 127 } 128 129 func TestPermanentDeleteTeam(t *testing.T) { 130 th := Setup().InitBasic() 131 defer th.TearDown() 132 133 team, err := th.App.CreateTeam(&model.Team{ 134 DisplayName: "deletion-test", 135 Name: "deletion-test", 136 Email: "foo@foo.com", 137 Type: model.TEAM_OPEN, 138 }) 139 if err != nil { 140 t.Fatal(err.Error()) 141 } 142 defer func() { 143 th.App.PermanentDeleteTeam(team) 144 }() 145 146 command, err := th.App.CreateCommand(&model.Command{ 147 CreatorId: th.BasicUser.Id, 148 TeamId: team.Id, 149 Trigger: "foo", 150 URL: "http://foo", 151 Method: model.COMMAND_METHOD_POST, 152 }) 153 if err != nil { 154 t.Fatal(err.Error()) 155 } 156 defer th.App.DeleteCommand(command.Id) 157 158 if command, err = th.App.GetCommand(command.Id); command == nil || err != nil { 159 t.Fatal("unable to get new command") 160 } 161 162 if err := th.App.PermanentDeleteTeam(team); err != nil { 163 t.Fatal(err.Error()) 164 } 165 166 if command, err = th.App.GetCommand(command.Id); command != nil || err == nil { 167 t.Fatal("command wasn't deleted") 168 } 169 170 // Test deleting a team with no channels. 171 team = th.CreateTeam() 172 defer func() { 173 th.App.PermanentDeleteTeam(team) 174 }() 175 176 if channels, err := th.App.GetPublicChannelsForTeam(team.Id, 0, 1000); err != nil { 177 t.Fatal(err) 178 } else { 179 for _, channel := range *channels { 180 if err2 := th.App.PermanentDeleteChannel(channel); err2 != nil { 181 t.Fatal(err) 182 } 183 } 184 } 185 186 if err := th.App.PermanentDeleteTeam(team); err != nil { 187 t.Fatal(err) 188 } 189 } 190 191 func TestSanitizeTeam(t *testing.T) { 192 th := Setup() 193 defer th.TearDown() 194 195 team := &model.Team{ 196 Id: model.NewId(), 197 Email: th.MakeEmail(), 198 AllowedDomains: "example.com", 199 } 200 copyTeam := func() *model.Team { 201 copy := &model.Team{} 202 *copy = *team 203 return copy 204 } 205 206 t.Run("not a user of the team", func(t *testing.T) { 207 userId := model.NewId() 208 session := model.Session{ 209 Roles: model.SYSTEM_USER_ROLE_ID, 210 TeamMembers: []*model.TeamMember{ 211 { 212 UserId: userId, 213 TeamId: model.NewId(), 214 Roles: model.TEAM_USER_ROLE_ID, 215 }, 216 }, 217 } 218 219 sanitized := th.App.SanitizeTeam(session, copyTeam()) 220 if sanitized.Email != "" && sanitized.AllowedDomains != "" { 221 t.Fatal("should've sanitized team") 222 } 223 }) 224 225 t.Run("user of the team", func(t *testing.T) { 226 userId := model.NewId() 227 session := model.Session{ 228 Roles: model.SYSTEM_USER_ROLE_ID, 229 TeamMembers: []*model.TeamMember{ 230 { 231 UserId: userId, 232 TeamId: team.Id, 233 Roles: model.TEAM_USER_ROLE_ID, 234 }, 235 }, 236 } 237 238 sanitized := th.App.SanitizeTeam(session, copyTeam()) 239 if sanitized.Email != "" && sanitized.AllowedDomains != "" { 240 t.Fatal("should've sanitized team") 241 } 242 }) 243 244 t.Run("team admin", func(t *testing.T) { 245 userId := model.NewId() 246 session := model.Session{ 247 Roles: model.SYSTEM_USER_ROLE_ID, 248 TeamMembers: []*model.TeamMember{ 249 { 250 UserId: userId, 251 TeamId: team.Id, 252 Roles: model.TEAM_USER_ROLE_ID + " " + model.TEAM_ADMIN_ROLE_ID, 253 }, 254 }, 255 } 256 257 sanitized := th.App.SanitizeTeam(session, copyTeam()) 258 if sanitized.Email == "" && sanitized.AllowedDomains == "" { 259 t.Fatal("shouldn't have sanitized team") 260 } 261 }) 262 263 t.Run("team admin of another team", func(t *testing.T) { 264 userId := model.NewId() 265 session := model.Session{ 266 Roles: model.SYSTEM_USER_ROLE_ID, 267 TeamMembers: []*model.TeamMember{ 268 { 269 UserId: userId, 270 TeamId: model.NewId(), 271 Roles: model.TEAM_USER_ROLE_ID + " " + model.TEAM_ADMIN_ROLE_ID, 272 }, 273 }, 274 } 275 276 sanitized := th.App.SanitizeTeam(session, copyTeam()) 277 if sanitized.Email != "" && sanitized.AllowedDomains != "" { 278 t.Fatal("should've sanitized team") 279 } 280 }) 281 282 t.Run("system admin, not a user of team", func(t *testing.T) { 283 userId := model.NewId() 284 session := model.Session{ 285 Roles: model.SYSTEM_USER_ROLE_ID + " " + model.SYSTEM_ADMIN_ROLE_ID, 286 TeamMembers: []*model.TeamMember{ 287 { 288 UserId: userId, 289 TeamId: model.NewId(), 290 Roles: model.TEAM_USER_ROLE_ID, 291 }, 292 }, 293 } 294 295 sanitized := th.App.SanitizeTeam(session, copyTeam()) 296 if sanitized.Email == "" && sanitized.AllowedDomains == "" { 297 t.Fatal("shouldn't have sanitized team") 298 } 299 }) 300 301 t.Run("system admin, user of team", func(t *testing.T) { 302 userId := model.NewId() 303 session := model.Session{ 304 Roles: model.SYSTEM_USER_ROLE_ID + " " + model.SYSTEM_ADMIN_ROLE_ID, 305 TeamMembers: []*model.TeamMember{ 306 { 307 UserId: userId, 308 TeamId: team.Id, 309 Roles: model.TEAM_USER_ROLE_ID, 310 }, 311 }, 312 } 313 314 sanitized := th.App.SanitizeTeam(session, copyTeam()) 315 if sanitized.Email == "" && sanitized.AllowedDomains == "" { 316 t.Fatal("shouldn't have sanitized team") 317 } 318 }) 319 } 320 321 func TestSanitizeTeams(t *testing.T) { 322 th := Setup() 323 defer th.TearDown() 324 325 t.Run("not a system admin", func(t *testing.T) { 326 teams := []*model.Team{ 327 { 328 Id: model.NewId(), 329 Email: th.MakeEmail(), 330 AllowedDomains: "example.com", 331 }, 332 { 333 Id: model.NewId(), 334 Email: th.MakeEmail(), 335 AllowedDomains: "example.com", 336 }, 337 } 338 339 userId := model.NewId() 340 session := model.Session{ 341 Roles: model.SYSTEM_USER_ROLE_ID, 342 TeamMembers: []*model.TeamMember{ 343 { 344 UserId: userId, 345 TeamId: teams[0].Id, 346 Roles: model.TEAM_USER_ROLE_ID, 347 }, 348 { 349 UserId: userId, 350 TeamId: teams[1].Id, 351 Roles: model.TEAM_USER_ROLE_ID + " " + model.TEAM_ADMIN_ROLE_ID, 352 }, 353 }, 354 } 355 356 sanitized := th.App.SanitizeTeams(session, teams) 357 358 if sanitized[0].Email != "" && sanitized[0].AllowedDomains != "" { 359 t.Fatal("should've sanitized first team") 360 } 361 362 if sanitized[1].Email == "" && sanitized[1].AllowedDomains == "" { 363 t.Fatal("shouldn't have sanitized second team") 364 } 365 }) 366 367 t.Run("system admin", func(t *testing.T) { 368 teams := []*model.Team{ 369 { 370 Id: model.NewId(), 371 Email: th.MakeEmail(), 372 AllowedDomains: "example.com", 373 }, 374 { 375 Id: model.NewId(), 376 Email: th.MakeEmail(), 377 AllowedDomains: "example.com", 378 }, 379 } 380 381 userId := model.NewId() 382 session := model.Session{ 383 Roles: model.SYSTEM_USER_ROLE_ID + " " + model.SYSTEM_ADMIN_ROLE_ID, 384 TeamMembers: []*model.TeamMember{ 385 { 386 UserId: userId, 387 TeamId: teams[0].Id, 388 Roles: model.TEAM_USER_ROLE_ID, 389 }, 390 }, 391 } 392 393 sanitized := th.App.SanitizeTeams(session, teams) 394 395 if sanitized[0].Email == "" && sanitized[0].AllowedDomains == "" { 396 t.Fatal("shouldn't have sanitized first team") 397 } 398 399 if sanitized[1].Email == "" && sanitized[1].AllowedDomains == "" { 400 t.Fatal("shouldn't have sanitized second team") 401 } 402 }) 403 } 404 405 func TestAddUserToTeamByHashMismatchedInviteId(t *testing.T) { 406 mockStore := &storetest.Store{} 407 defer mockStore.AssertExpectations(t) 408 409 teamId := model.NewId() 410 userId := model.NewId() 411 inviteSalt := model.NewId() 412 413 inviteId := model.NewId() 414 teamInviteId := model.NewId() 415 416 // generate a fake email invite - stolen from SendInviteEmails() in email.go 417 props := make(map[string]string) 418 props["email"] = model.NewId() + "@mattermost.com" 419 props["id"] = teamId 420 props["display_name"] = model.NewId() 421 props["name"] = model.NewId() 422 props["time"] = fmt.Sprintf("%v", model.GetMillis()) 423 props["invite_id"] = inviteId 424 data := model.MapToJson(props) 425 hash := utils.HashSha256(fmt.Sprintf("%v:%v", data, inviteSalt)) 426 427 // when the server tries to validate the invite, it will pull the user from our mock store 428 // this can return nil, because we'll fail before we get to trying to use it 429 mockStore.UserStore.On("Get", userId).Return( 430 storetest.NewStoreChannel(store.StoreResult{ 431 Data: nil, 432 Err: nil, 433 }), 434 ) 435 436 // the server will also pull the team. the one we return has a different invite id than the one in the email invite we made above 437 mockStore.TeamStore.On("Get", teamId).Return( 438 storetest.NewStoreChannel(store.StoreResult{ 439 Data: &model.Team{ 440 InviteId: teamInviteId, 441 }, 442 Err: nil, 443 }), 444 ) 445 446 app := App{ 447 Srv: &Server{ 448 Store: mockStore, 449 }, 450 config: atomic.Value{}, 451 } 452 app.config.Store(&model.Config{ 453 EmailSettings: model.EmailSettings{ 454 InviteSalt: inviteSalt, 455 }, 456 }) 457 458 // this should fail because the invite ids are mismatched 459 team, err := app.AddUserToTeamByHash(userId, hash, data) 460 assert.Nil(t, team) 461 assert.Equal(t, "api.user.create_user.signup_link_mismatched_invite_id.app_error", err.Id) 462 } 463 464 func TestJoinUserToTeam(t *testing.T) { 465 th := Setup().InitBasic() 466 defer th.TearDown() 467 468 id := model.NewId() 469 team := &model.Team{ 470 DisplayName: "dn_" + id, 471 Name: "name" + id, 472 Email: "success+" + id + "@simulator.amazonses.com", 473 Type: model.TEAM_OPEN, 474 } 475 476 if _, err := th.App.CreateTeam(team); err != nil { 477 t.Log(err) 478 t.Fatal("Should create a new team") 479 } 480 481 maxUsersPerTeam := th.App.Config().TeamSettings.MaxUsersPerTeam 482 defer func() { 483 th.App.UpdateConfig(func(cfg *model.Config) { cfg.TeamSettings.MaxUsersPerTeam = maxUsersPerTeam }) 484 th.App.PermanentDeleteTeam(team) 485 }() 486 one := 1 487 th.App.UpdateConfig(func(cfg *model.Config) { cfg.TeamSettings.MaxUsersPerTeam = &one }) 488 489 t.Run("new join", func(t *testing.T) { 490 user := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} 491 ruser, _ := th.App.CreateUser(&user) 492 defer th.App.PermanentDeleteUser(&user) 493 494 if _, alreadyAdded, err := th.App.joinUserToTeam(team, ruser); alreadyAdded || err != nil { 495 t.Fatal("Should return already added equal to false and no error") 496 } 497 }) 498 499 t.Run("join when you are a member", func(t *testing.T) { 500 user := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} 501 ruser, _ := th.App.CreateUser(&user) 502 defer th.App.PermanentDeleteUser(&user) 503 504 th.App.joinUserToTeam(team, ruser) 505 if _, alreadyAdded, err := th.App.joinUserToTeam(team, ruser); !alreadyAdded || err != nil { 506 t.Fatal("Should return already added and no error") 507 } 508 }) 509 510 t.Run("re-join after leaving", func(t *testing.T) { 511 user := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} 512 ruser, _ := th.App.CreateUser(&user) 513 defer th.App.PermanentDeleteUser(&user) 514 515 th.App.joinUserToTeam(team, ruser) 516 th.App.LeaveTeam(team, ruser, ruser.Id) 517 if _, alreadyAdded, err := th.App.joinUserToTeam(team, ruser); alreadyAdded || err != nil { 518 t.Fatal("Should return already added equal to false and no error") 519 } 520 }) 521 522 t.Run("new join with limit problem", func(t *testing.T) { 523 user1 := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} 524 ruser1, _ := th.App.CreateUser(&user1) 525 user2 := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} 526 ruser2, _ := th.App.CreateUser(&user2) 527 defer th.App.PermanentDeleteUser(&user1) 528 defer th.App.PermanentDeleteUser(&user2) 529 th.App.joinUserToTeam(team, ruser1) 530 if _, _, err := th.App.joinUserToTeam(team, ruser2); err == nil { 531 t.Fatal("Should fail") 532 } 533 }) 534 535 t.Run("re-join alfter leaving with limit problem", func(t *testing.T) { 536 user1 := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} 537 ruser1, _ := th.App.CreateUser(&user1) 538 user2 := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} 539 ruser2, _ := th.App.CreateUser(&user2) 540 defer th.App.PermanentDeleteUser(&user1) 541 defer th.App.PermanentDeleteUser(&user2) 542 543 th.App.joinUserToTeam(team, ruser1) 544 th.App.LeaveTeam(team, ruser1, ruser1.Id) 545 th.App.joinUserToTeam(team, ruser2) 546 if _, _, err := th.App.joinUserToTeam(team, ruser1); err == nil { 547 t.Fatal("Should fail") 548 } 549 }) 550 }