github.com/crspeller/mattermost-server@v0.0.0-20190328001957-a200beb3d111/app/channel_test.go (about) 1 // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package app 5 6 import ( 7 "fmt" 8 "sort" 9 "strings" 10 "testing" 11 12 "github.com/stretchr/testify/assert" 13 "github.com/stretchr/testify/require" 14 15 "github.com/crspeller/mattermost-server/model" 16 "github.com/crspeller/mattermost-server/store" 17 ) 18 19 func TestPermanentDeleteChannel(t *testing.T) { 20 th := Setup(t).InitBasic() 21 defer th.TearDown() 22 23 th.App.UpdateConfig(func(cfg *model.Config) { 24 *cfg.ServiceSettings.EnableIncomingWebhooks = true 25 *cfg.ServiceSettings.EnableOutgoingWebhooks = true 26 }) 27 28 channel, err := th.App.CreateChannel(&model.Channel{DisplayName: "deletion-test", Name: "deletion-test", Type: model.CHANNEL_OPEN, TeamId: th.BasicTeam.Id}, false) 29 if err != nil { 30 t.Fatal(err.Error()) 31 } 32 defer func() { 33 th.App.PermanentDeleteChannel(channel) 34 }() 35 36 incoming, err := th.App.CreateIncomingWebhookForChannel(th.BasicUser.Id, channel, &model.IncomingWebhook{ChannelId: channel.Id}) 37 if err != nil { 38 t.Fatal(err.Error()) 39 } 40 defer th.App.DeleteIncomingWebhook(incoming.Id) 41 42 if incoming, err = th.App.GetIncomingWebhook(incoming.Id); incoming == nil || err != nil { 43 t.Fatal("unable to get new incoming webhook") 44 } 45 46 outgoing, err := th.App.CreateOutgoingWebhook(&model.OutgoingWebhook{ 47 ChannelId: channel.Id, 48 TeamId: channel.TeamId, 49 CreatorId: th.BasicUser.Id, 50 CallbackURLs: []string{"http://foo"}, 51 }) 52 if err != nil { 53 t.Fatal(err.Error()) 54 } 55 defer th.App.DeleteOutgoingWebhook(outgoing.Id) 56 57 if outgoing, err = th.App.GetOutgoingWebhook(outgoing.Id); outgoing == nil || err != nil { 58 t.Fatal("unable to get new outgoing webhook") 59 } 60 61 err = th.App.PermanentDeleteChannel(channel) 62 require.Nil(t, err) 63 64 if incoming, err = th.App.GetIncomingWebhook(incoming.Id); incoming != nil || err == nil { 65 t.Error("incoming webhook wasn't deleted") 66 } 67 68 if outgoing, err = th.App.GetOutgoingWebhook(outgoing.Id); outgoing != nil || err == nil { 69 t.Error("outgoing webhook wasn't deleted") 70 } 71 } 72 73 func TestMoveChannel(t *testing.T) { 74 th := Setup(t).InitBasic() 75 defer th.TearDown() 76 77 sourceTeam := th.CreateTeam() 78 targetTeam := th.CreateTeam() 79 channel1 := th.CreateChannel(sourceTeam) 80 defer func() { 81 th.App.PermanentDeleteChannel(channel1) 82 th.App.PermanentDeleteTeam(sourceTeam) 83 th.App.PermanentDeleteTeam(targetTeam) 84 }() 85 86 if _, err := th.App.AddUserToTeam(sourceTeam.Id, th.BasicUser.Id, ""); err != nil { 87 t.Fatal(err) 88 } 89 if _, err := th.App.AddUserToTeam(sourceTeam.Id, th.BasicUser2.Id, ""); err != nil { 90 t.Fatal(err) 91 } 92 93 if _, err := th.App.AddUserToTeam(targetTeam.Id, th.BasicUser.Id, ""); err != nil { 94 t.Fatal(err) 95 } 96 97 if _, err := th.App.AddUserToChannel(th.BasicUser, channel1); err != nil { 98 t.Fatal(err) 99 } 100 if _, err := th.App.AddUserToChannel(th.BasicUser2, channel1); err != nil { 101 t.Fatal(err) 102 } 103 104 if err := th.App.MoveChannel(targetTeam, channel1, th.BasicUser, false); err == nil { 105 t.Fatal("Should have failed due to mismatched members.") 106 } 107 108 if _, err := th.App.AddUserToTeam(targetTeam.Id, th.BasicUser2.Id, ""); err != nil { 109 t.Fatal(err) 110 } 111 112 if err := th.App.MoveChannel(targetTeam, channel1, th.BasicUser, false); err != nil { 113 t.Fatal(err) 114 } 115 116 // Test moving a channel with a deactivated user who isn't in the destination team. 117 // It should fail, unless removeDeactivatedMembers is true. 118 deacivatedUser := th.CreateUser() 119 channel2 := th.CreateChannel(sourceTeam) 120 121 if _, err := th.App.AddUserToTeam(sourceTeam.Id, deacivatedUser.Id, ""); err != nil { 122 t.Fatal(err) 123 } 124 if _, err := th.App.AddUserToChannel(th.BasicUser, channel2); err != nil { 125 t.Fatal(err) 126 } 127 if _, err := th.App.AddUserToChannel(deacivatedUser, channel2); err != nil { 128 t.Fatal(err) 129 } 130 131 if _, err := th.App.UpdateActive(deacivatedUser, false); err != nil { 132 t.Fatal(err) 133 } 134 135 if err := th.App.MoveChannel(targetTeam, channel2, th.BasicUser, false); err == nil { 136 t.Fatal("Should have failed due to mismatched deacivated member.") 137 } 138 139 if err := th.App.MoveChannel(targetTeam, channel2, th.BasicUser, true); err != nil { 140 t.Fatal(err) 141 } 142 143 // Test moving a channel with no members. 144 channel3 := &model.Channel{ 145 DisplayName: "dn_" + model.NewId(), 146 Name: "name_" + model.NewId(), 147 Type: model.CHANNEL_OPEN, 148 TeamId: sourceTeam.Id, 149 CreatorId: th.BasicUser.Id, 150 } 151 152 var err *model.AppError 153 channel3, err = th.App.CreateChannel(channel3, false) 154 require.Nil(t, err) 155 156 err = th.App.MoveChannel(targetTeam, channel3, th.BasicUser, false) 157 assert.Nil(t, err) 158 } 159 160 func TestJoinDefaultChannelsCreatesChannelMemberHistoryRecordTownSquare(t *testing.T) { 161 th := Setup(t).InitBasic() 162 defer th.TearDown() 163 164 // figure out the initial number of users in town square 165 townSquareChannelId := store.Must(th.App.Srv.Store.Channel().GetByName(th.BasicTeam.Id, "town-square", true)).(*model.Channel).Id 166 initialNumTownSquareUsers := len(store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, townSquareChannelId)).([]*model.ChannelMemberHistoryResult)) 167 168 // create a new user that joins the default channels 169 user := th.CreateUser() 170 th.App.JoinDefaultChannels(th.BasicTeam.Id, user, false, "") 171 172 // there should be a ChannelMemberHistory record for the user 173 histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, townSquareChannelId)).([]*model.ChannelMemberHistoryResult) 174 assert.Len(t, histories, initialNumTownSquareUsers+1) 175 176 found := false 177 for _, history := range histories { 178 if user.Id == history.UserId && townSquareChannelId == history.ChannelId { 179 found = true 180 break 181 } 182 } 183 assert.True(t, found) 184 } 185 186 func TestJoinDefaultChannelsCreatesChannelMemberHistoryRecordOffTopic(t *testing.T) { 187 th := Setup(t).InitBasic() 188 defer th.TearDown() 189 190 // figure out the initial number of users in off-topic 191 offTopicChannelId := store.Must(th.App.Srv.Store.Channel().GetByName(th.BasicTeam.Id, "off-topic", true)).(*model.Channel).Id 192 initialNumTownSquareUsers := len(store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, offTopicChannelId)).([]*model.ChannelMemberHistoryResult)) 193 194 // create a new user that joins the default channels 195 user := th.CreateUser() 196 th.App.JoinDefaultChannels(th.BasicTeam.Id, user, false, "") 197 198 // there should be a ChannelMemberHistory record for the user 199 histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, offTopicChannelId)).([]*model.ChannelMemberHistoryResult) 200 assert.Len(t, histories, initialNumTownSquareUsers+1) 201 202 found := false 203 for _, history := range histories { 204 if user.Id == history.UserId && offTopicChannelId == history.ChannelId { 205 found = true 206 break 207 } 208 } 209 assert.True(t, found) 210 } 211 212 func TestJoinDefaultChannelsExperimentalDefaultChannels(t *testing.T) { 213 th := Setup(t).InitBasic() 214 defer th.TearDown() 215 216 basicChannel2 := th.CreateChannel(th.BasicTeam) 217 defaultChannelList := []string{th.BasicChannel.Name, basicChannel2.Name, basicChannel2.Name} 218 th.App.Config().TeamSettings.ExperimentalDefaultChannels = defaultChannelList 219 220 user := th.CreateUser() 221 th.App.JoinDefaultChannels(th.BasicTeam.Id, user, false, "") 222 223 for _, channelName := range defaultChannelList { 224 channel, err := th.App.GetChannelByName(channelName, th.BasicTeam.Id, false) 225 226 if err != nil { 227 t.Errorf("Expected nil, got %s", err) 228 } 229 230 member, err := th.App.GetChannelMember(channel.Id, user.Id) 231 232 if member == nil { 233 t.Errorf("Expected member object, got nil") 234 } 235 236 if err != nil { 237 t.Errorf("Expected nil object, got %s", err) 238 } 239 } 240 } 241 242 func TestCreateChannelPublicCreatesChannelMemberHistoryRecord(t *testing.T) { 243 th := Setup(t).InitBasic() 244 defer th.TearDown() 245 246 // creates a public channel and adds basic user to it 247 publicChannel := th.createChannel(th.BasicTeam, model.CHANNEL_OPEN) 248 249 // there should be a ChannelMemberHistory record for the user 250 histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, publicChannel.Id)).([]*model.ChannelMemberHistoryResult) 251 assert.Len(t, histories, 1) 252 assert.Equal(t, th.BasicUser.Id, histories[0].UserId) 253 assert.Equal(t, publicChannel.Id, histories[0].ChannelId) 254 } 255 256 func TestCreateChannelPrivateCreatesChannelMemberHistoryRecord(t *testing.T) { 257 th := Setup(t).InitBasic() 258 defer th.TearDown() 259 260 // creates a private channel and adds basic user to it 261 privateChannel := th.createChannel(th.BasicTeam, model.CHANNEL_PRIVATE) 262 263 // there should be a ChannelMemberHistory record for the user 264 histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, privateChannel.Id)).([]*model.ChannelMemberHistoryResult) 265 assert.Len(t, histories, 1) 266 assert.Equal(t, th.BasicUser.Id, histories[0].UserId) 267 assert.Equal(t, privateChannel.Id, histories[0].ChannelId) 268 } 269 270 func TestUpdateChannelPrivacy(t *testing.T) { 271 th := Setup(t).InitBasic() 272 defer th.TearDown() 273 274 privateChannel := th.createChannel(th.BasicTeam, model.CHANNEL_PRIVATE) 275 privateChannel.Type = model.CHANNEL_OPEN 276 277 if publicChannel, err := th.App.UpdateChannelPrivacy(privateChannel, th.BasicUser); err != nil { 278 t.Fatal("Failed to update channel privacy. Error: " + err.Error()) 279 } else { 280 assert.Equal(t, publicChannel.Id, privateChannel.Id) 281 assert.Equal(t, publicChannel.Type, model.CHANNEL_OPEN) 282 } 283 } 284 285 func TestCreateGroupChannelCreatesChannelMemberHistoryRecord(t *testing.T) { 286 th := Setup(t).InitBasic() 287 defer th.TearDown() 288 289 user1 := th.CreateUser() 290 user2 := th.CreateUser() 291 292 groupUserIds := make([]string, 0) 293 groupUserIds = append(groupUserIds, user1.Id) 294 groupUserIds = append(groupUserIds, user2.Id) 295 groupUserIds = append(groupUserIds, th.BasicUser.Id) 296 297 if channel, err := th.App.CreateGroupChannel(groupUserIds, th.BasicUser.Id); err != nil { 298 t.Fatal("Failed to create group channel. Error: " + err.Message) 299 } else { 300 // there should be a ChannelMemberHistory record for each user 301 histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, channel.Id)).([]*model.ChannelMemberHistoryResult) 302 assert.Len(t, histories, 3) 303 304 channelMemberHistoryUserIds := make([]string, 0) 305 for _, history := range histories { 306 assert.Equal(t, channel.Id, history.ChannelId) 307 channelMemberHistoryUserIds = append(channelMemberHistoryUserIds, history.UserId) 308 } 309 310 sort.Strings(groupUserIds) 311 sort.Strings(channelMemberHistoryUserIds) 312 assert.Equal(t, groupUserIds, channelMemberHistoryUserIds) 313 } 314 } 315 316 func TestCreateDirectChannelCreatesChannelMemberHistoryRecord(t *testing.T) { 317 th := Setup(t).InitBasic() 318 defer th.TearDown() 319 320 user1 := th.CreateUser() 321 user2 := th.CreateUser() 322 323 if channel, err := th.App.GetOrCreateDirectChannel(user1.Id, user2.Id); err != nil { 324 t.Fatal("Failed to create direct channel. Error: " + err.Message) 325 } else { 326 // there should be a ChannelMemberHistory record for both users 327 histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, channel.Id)).([]*model.ChannelMemberHistoryResult) 328 assert.Len(t, histories, 2) 329 330 historyId0 := histories[0].UserId 331 historyId1 := histories[1].UserId 332 switch historyId0 { 333 case user1.Id: 334 assert.Equal(t, user2.Id, historyId1) 335 case user2.Id: 336 assert.Equal(t, user1.Id, historyId1) 337 default: 338 t.Fatal("Unexpected user id " + historyId0 + " in ChannelMemberHistory table") 339 } 340 } 341 } 342 343 func TestGetDirectChannelCreatesChannelMemberHistoryRecord(t *testing.T) { 344 th := Setup(t).InitBasic() 345 defer th.TearDown() 346 347 user1 := th.CreateUser() 348 user2 := th.CreateUser() 349 350 // this function call implicitly creates a direct channel between the two users if one doesn't already exist 351 if channel, err := th.App.GetOrCreateDirectChannel(user1.Id, user2.Id); err != nil { 352 t.Fatal("Failed to create direct channel. Error: " + err.Message) 353 } else { 354 // there should be a ChannelMemberHistory record for both users 355 histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, channel.Id)).([]*model.ChannelMemberHistoryResult) 356 assert.Len(t, histories, 2) 357 358 historyId0 := histories[0].UserId 359 historyId1 := histories[1].UserId 360 switch historyId0 { 361 case user1.Id: 362 assert.Equal(t, user2.Id, historyId1) 363 case user2.Id: 364 assert.Equal(t, user1.Id, historyId1) 365 default: 366 t.Fatal("Unexpected user id " + historyId0 + " in ChannelMemberHistory table") 367 } 368 } 369 } 370 371 func TestAddUserToChannelCreatesChannelMemberHistoryRecord(t *testing.T) { 372 th := Setup(t).InitBasic() 373 defer th.TearDown() 374 375 // create a user and add it to a channel 376 user := th.CreateUser() 377 if _, err := th.App.AddTeamMember(th.BasicTeam.Id, user.Id); err != nil { 378 t.Fatal("Failed to add user to team. Error: " + err.Message) 379 } 380 381 groupUserIds := make([]string, 0) 382 groupUserIds = append(groupUserIds, th.BasicUser.Id) 383 groupUserIds = append(groupUserIds, user.Id) 384 385 channel := th.createChannel(th.BasicTeam, model.CHANNEL_OPEN) 386 if _, err := th.App.AddUserToChannel(user, channel); err != nil { 387 t.Fatal("Failed to add user to channel. Error: " + err.Message) 388 } 389 390 // there should be a ChannelMemberHistory record for the user 391 histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, channel.Id)).([]*model.ChannelMemberHistoryResult) 392 assert.Len(t, histories, 2) 393 channelMemberHistoryUserIds := make([]string, 0) 394 for _, history := range histories { 395 assert.Equal(t, channel.Id, history.ChannelId) 396 channelMemberHistoryUserIds = append(channelMemberHistoryUserIds, history.UserId) 397 } 398 assert.Equal(t, groupUserIds, channelMemberHistoryUserIds) 399 } 400 401 /*func TestRemoveUserFromChannelUpdatesChannelMemberHistoryRecord(t *testing.T) { 402 th := Setup(t).InitBasic() 403 defer th.TearDown() 404 405 // a user creates a channel 406 publicChannel := th.createChannel(th.BasicTeam, model.CHANNEL_OPEN) 407 histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, publicChannel.Id)).([]*model.ChannelMemberHistoryResult) 408 assert.Len(t, histories, 1) 409 assert.Equal(t, th.BasicUser.Id, histories[0].UserId) 410 assert.Equal(t, publicChannel.Id, histories[0].ChannelId) 411 assert.Nil(t, histories[0].LeaveTime) 412 413 // the user leaves that channel 414 if err := th.App.LeaveChannel(publicChannel.Id, th.BasicUser.Id); err != nil { 415 t.Fatal("Failed to remove user from channel. Error: " + err.Message) 416 } 417 histories = store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, publicChannel.Id)).([]*model.ChannelMemberHistoryResult) 418 assert.Len(t, histories, 1) 419 assert.Equal(t, th.BasicUser.Id, histories[0].UserId) 420 assert.Equal(t, publicChannel.Id, histories[0].ChannelId) 421 assert.NotNil(t, histories[0].LeaveTime) 422 }*/ 423 424 func TestAddChannelMemberNoUserRequestor(t *testing.T) { 425 th := Setup(t).InitBasic() 426 defer th.TearDown() 427 428 // create a user and add it to a channel 429 user := th.CreateUser() 430 if _, err := th.App.AddTeamMember(th.BasicTeam.Id, user.Id); err != nil { 431 t.Fatal("Failed to add user to team. Error: " + err.Message) 432 } 433 434 groupUserIds := make([]string, 0) 435 groupUserIds = append(groupUserIds, th.BasicUser.Id) 436 groupUserIds = append(groupUserIds, user.Id) 437 438 channel := th.createChannel(th.BasicTeam, model.CHANNEL_OPEN) 439 userRequestorId := "" 440 postRootId := "" 441 if _, err := th.App.AddChannelMember(user.Id, channel, userRequestorId, postRootId, ""); err != nil { 442 t.Fatal("Failed to add user to channel. Error: " + err.Message) 443 } 444 445 // there should be a ChannelMemberHistory record for the user 446 histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, channel.Id)).([]*model.ChannelMemberHistoryResult) 447 assert.Len(t, histories, 2) 448 channelMemberHistoryUserIds := make([]string, 0) 449 for _, history := range histories { 450 assert.Equal(t, channel.Id, history.ChannelId) 451 channelMemberHistoryUserIds = append(channelMemberHistoryUserIds, history.UserId) 452 } 453 assert.Equal(t, groupUserIds, channelMemberHistoryUserIds) 454 455 postList := store.Must(th.App.Srv.Store.Post().GetPosts(channel.Id, 0, 1, false)).(*model.PostList) 456 if assert.Len(t, postList.Order, 1) { 457 post := postList.Posts[postList.Order[0]] 458 459 assert.Equal(t, model.POST_JOIN_CHANNEL, post.Type) 460 assert.Equal(t, user.Id, post.UserId) 461 assert.Equal(t, user.Username, post.Props["username"]) 462 } 463 } 464 465 func TestAppUpdateChannelScheme(t *testing.T) { 466 th := Setup(t).InitBasic() 467 defer th.TearDown() 468 469 channel := th.BasicChannel 470 mockID := model.NewString("x") 471 channel.SchemeId = mockID 472 473 updatedChannel, err := th.App.UpdateChannelScheme(channel) 474 if err != nil { 475 t.Fatal(err) 476 } 477 478 if updatedChannel.SchemeId != mockID { 479 t.Fatal("Wrong Channel SchemeId") 480 } 481 } 482 483 func TestFillInChannelProps(t *testing.T) { 484 th := Setup(t).InitBasic() 485 defer th.TearDown() 486 487 channelPublic1, err := th.App.CreateChannel(&model.Channel{DisplayName: "Public 1", Name: "public1", Type: model.CHANNEL_OPEN, TeamId: th.BasicTeam.Id}, false) 488 require.Nil(t, err) 489 defer th.App.PermanentDeleteChannel(channelPublic1) 490 491 channelPublic2, err := th.App.CreateChannel(&model.Channel{DisplayName: "Public 2", Name: "public2", Type: model.CHANNEL_OPEN, TeamId: th.BasicTeam.Id}, false) 492 require.Nil(t, err) 493 defer th.App.PermanentDeleteChannel(channelPublic2) 494 495 channelPrivate, err := th.App.CreateChannel(&model.Channel{DisplayName: "Private", Name: "private", Type: model.CHANNEL_PRIVATE, TeamId: th.BasicTeam.Id}, false) 496 require.Nil(t, err) 497 defer th.App.PermanentDeleteChannel(channelPrivate) 498 499 otherTeamId := model.NewId() 500 otherTeam := &model.Team{ 501 DisplayName: "dn_" + otherTeamId, 502 Name: "name" + otherTeamId, 503 Email: "success+" + otherTeamId + "@simulator.amazonses.com", 504 Type: model.TEAM_OPEN, 505 } 506 otherTeam, err = th.App.CreateTeam(otherTeam) 507 require.Nil(t, err) 508 defer th.App.PermanentDeleteTeam(otherTeam) 509 510 channelOtherTeam, err := th.App.CreateChannel(&model.Channel{DisplayName: "Other Team Channel", Name: "other-team", Type: model.CHANNEL_OPEN, TeamId: otherTeam.Id}, false) 511 require.Nil(t, err) 512 defer th.App.PermanentDeleteChannel(channelOtherTeam) 513 514 // Note that purpose is intentionally plaintext below. 515 516 t.Run("single channels", func(t *testing.T) { 517 testCases := []struct { 518 Description string 519 Channel *model.Channel 520 ExpectedChannelProps map[string]interface{} 521 }{ 522 { 523 "channel on basic team without references", 524 &model.Channel{ 525 TeamId: th.BasicTeam.Id, 526 Header: "No references", 527 Purpose: "No references", 528 }, 529 nil, 530 }, 531 { 532 "channel on basic team", 533 &model.Channel{ 534 TeamId: th.BasicTeam.Id, 535 Header: "~public1, ~private, ~other-team", 536 Purpose: "~public2, ~private, ~other-team", 537 }, 538 map[string]interface{}{ 539 "channel_mentions": map[string]interface{}{ 540 "public1": map[string]interface{}{ 541 "display_name": "Public 1", 542 }, 543 }, 544 }, 545 }, 546 { 547 "channel on other team", 548 &model.Channel{ 549 TeamId: otherTeam.Id, 550 Header: "~public1, ~private, ~other-team", 551 Purpose: "~public2, ~private, ~other-team", 552 }, 553 map[string]interface{}{ 554 "channel_mentions": map[string]interface{}{ 555 "other-team": map[string]interface{}{ 556 "display_name": "Other Team Channel", 557 }, 558 }, 559 }, 560 }, 561 } 562 563 for _, testCase := range testCases { 564 t.Run(testCase.Description, func(t *testing.T) { 565 err = th.App.FillInChannelProps(testCase.Channel) 566 require.Nil(t, err) 567 568 assert.Equal(t, testCase.ExpectedChannelProps, testCase.Channel.Props) 569 }) 570 } 571 }) 572 573 t.Run("multiple channels", func(t *testing.T) { 574 testCases := []struct { 575 Description string 576 Channels *model.ChannelList 577 ExpectedChannelProps map[string]interface{} 578 }{ 579 { 580 "single channel on basic team", 581 &model.ChannelList{ 582 { 583 Name: "test", 584 TeamId: th.BasicTeam.Id, 585 Header: "~public1, ~private, ~other-team", 586 Purpose: "~public2, ~private, ~other-team", 587 }, 588 }, 589 map[string]interface{}{ 590 "test": map[string]interface{}{ 591 "channel_mentions": map[string]interface{}{ 592 "public1": map[string]interface{}{ 593 "display_name": "Public 1", 594 }, 595 }, 596 }, 597 }, 598 }, 599 { 600 "multiple channels on basic team", 601 &model.ChannelList{ 602 { 603 Name: "test", 604 TeamId: th.BasicTeam.Id, 605 Header: "~public1, ~private, ~other-team", 606 Purpose: "~public2, ~private, ~other-team", 607 }, 608 { 609 Name: "test2", 610 TeamId: th.BasicTeam.Id, 611 Header: "~private, ~other-team", 612 Purpose: "~public2, ~private, ~other-team", 613 }, 614 { 615 Name: "test3", 616 TeamId: th.BasicTeam.Id, 617 Header: "No references", 618 Purpose: "No references", 619 }, 620 }, 621 map[string]interface{}{ 622 "test": map[string]interface{}{ 623 "channel_mentions": map[string]interface{}{ 624 "public1": map[string]interface{}{ 625 "display_name": "Public 1", 626 }, 627 }, 628 }, 629 "test2": map[string]interface{}(nil), 630 "test3": map[string]interface{}(nil), 631 }, 632 }, 633 { 634 "multiple channels across teams", 635 &model.ChannelList{ 636 { 637 Name: "test", 638 TeamId: th.BasicTeam.Id, 639 Header: "~public1, ~private, ~other-team", 640 Purpose: "~public2, ~private, ~other-team", 641 }, 642 { 643 Name: "test2", 644 TeamId: otherTeam.Id, 645 Header: "~private, ~other-team", 646 Purpose: "~public2, ~private, ~other-team", 647 }, 648 { 649 Name: "test3", 650 TeamId: th.BasicTeam.Id, 651 Header: "No references", 652 Purpose: "No references", 653 }, 654 }, 655 map[string]interface{}{ 656 "test": map[string]interface{}{ 657 "channel_mentions": map[string]interface{}{ 658 "public1": map[string]interface{}{ 659 "display_name": "Public 1", 660 }, 661 }, 662 }, 663 "test2": map[string]interface{}{ 664 "channel_mentions": map[string]interface{}{ 665 "other-team": map[string]interface{}{ 666 "display_name": "Other Team Channel", 667 }, 668 }, 669 }, 670 "test3": map[string]interface{}(nil), 671 }, 672 }, 673 } 674 675 for _, testCase := range testCases { 676 t.Run(testCase.Description, func(t *testing.T) { 677 err = th.App.FillInChannelsProps(testCase.Channels) 678 require.Nil(t, err) 679 680 for _, channel := range *testCase.Channels { 681 assert.Equal(t, testCase.ExpectedChannelProps[channel.Name], channel.Props) 682 } 683 }) 684 } 685 }) 686 } 687 688 func TestRenameChannel(t *testing.T) { 689 th := Setup(t).InitBasic() 690 defer th.TearDown() 691 692 testCases := []struct { 693 Name string 694 Channel *model.Channel 695 ExpectError bool 696 ExpectedName string 697 ExpectedDisplayName string 698 }{ 699 { 700 "Rename open channel", 701 th.createChannel(th.BasicTeam, model.CHANNEL_OPEN), 702 false, 703 "newchannelname", 704 "New Display Name", 705 }, 706 { 707 "Fail on rename direct message channel", 708 th.CreateDmChannel(th.BasicUser2), 709 true, 710 "", 711 "", 712 }, 713 { 714 "Fail on rename direct message channel", 715 th.CreateGroupChannel(th.BasicUser2, th.CreateUser()), 716 true, 717 "", 718 "", 719 }, 720 } 721 722 for _, tc := range testCases { 723 t.Run(tc.Name, func(t *testing.T) { 724 channel, err := th.App.RenameChannel(tc.Channel, "newchannelname", "New Display Name") 725 if tc.ExpectError { 726 assert.NotNil(t, err) 727 } else { 728 assert.Equal(t, tc.ExpectedName, channel.Name) 729 assert.Equal(t, tc.ExpectedDisplayName, channel.DisplayName) 730 } 731 }) 732 } 733 } 734 735 func TestGetChannelMembersTimezones(t *testing.T) { 736 th := Setup(t).InitBasic() 737 defer th.TearDown() 738 739 userRequestorId := "" 740 postRootId := "" 741 if _, err := th.App.AddChannelMember(th.BasicUser2.Id, th.BasicChannel, userRequestorId, postRootId, ""); err != nil { 742 t.Fatal("Failed to add user to channel. Error: " + err.Message) 743 } 744 745 user := th.BasicUser 746 user.Timezone["useAutomaticTimezone"] = "false" 747 user.Timezone["manualTimezone"] = "XOXO/BLABLA" 748 th.App.UpdateUser(user, false) 749 750 user2 := th.BasicUser2 751 user2.Timezone["automaticTimezone"] = "NoWhere/Island" 752 th.App.UpdateUser(user2, false) 753 754 user3 := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} 755 ruser, _ := th.App.CreateUser(&user3) 756 th.App.AddUserToChannel(ruser, th.BasicChannel) 757 758 ruser.Timezone["automaticTimezone"] = "NoWhere/Island" 759 th.App.UpdateUser(ruser, false) 760 761 user4 := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} 762 ruser, _ = th.App.CreateUser(&user4) 763 th.App.AddUserToChannel(ruser, th.BasicChannel) 764 765 timezones, err := th.App.GetChannelMembersTimezones(th.BasicChannel.Id) 766 if err != nil { 767 t.Fatal("Failed to get the timezones for a channel. Error: " + err.Error()) 768 } 769 assert.Equal(t, 2, len(timezones)) 770 } 771 772 func TestGetPublicChannelsForTeam(t *testing.T) { 773 th := Setup(t) 774 team := th.CreateTeam() 775 defer th.TearDown() 776 777 var expectedChannels []*model.Channel 778 779 townSquare, err := th.App.GetChannelByName("town-square", team.Id, false) 780 require.Nil(t, err) 781 require.NotNil(t, townSquare) 782 expectedChannels = append(expectedChannels, townSquare) 783 784 offTopic, err := th.App.GetChannelByName("off-topic", team.Id, false) 785 require.Nil(t, err) 786 require.NotNil(t, offTopic) 787 expectedChannels = append(expectedChannels, offTopic) 788 789 for i := 0; i < 8; i++ { 790 channel := model.Channel{ 791 DisplayName: fmt.Sprintf("Public %v", i), 792 Name: fmt.Sprintf("public_%v", i), 793 Type: model.CHANNEL_OPEN, 794 TeamId: team.Id, 795 } 796 var rchannel *model.Channel 797 rchannel, err = th.App.CreateChannel(&channel, false) 798 require.Nil(t, err) 799 require.NotNil(t, rchannel) 800 defer th.App.PermanentDeleteChannel(rchannel) 801 802 // Store the user ids for comparison later 803 expectedChannels = append(expectedChannels, rchannel) 804 } 805 806 // Fetch public channels multipile times 807 channelList, err := th.App.GetPublicChannelsForTeam(team.Id, 0, 5) 808 require.Nil(t, err) 809 channelList2, err := th.App.GetPublicChannelsForTeam(team.Id, 5, 5) 810 require.Nil(t, err) 811 812 channels := append(*channelList, *channelList2...) 813 assert.ElementsMatch(t, expectedChannels, channels) 814 }