github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/app/user_viewmembers_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package app 5 6 import ( 7 "testing" 8 9 "github.com/mattermost/mattermost-server/v5/model" 10 "github.com/mattermost/mattermost-server/v5/store" 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/require" 13 ) 14 15 func TestResctrictedViewMembers(t *testing.T) { 16 th := Setup(t) 17 defer th.TearDown() 18 19 user1 := th.CreateUser() 20 user1.Nickname = "test user1" 21 user1.Username = "test-user-1" 22 th.App.UpdateUser(user1, false) 23 user2 := th.CreateUser() 24 user2.Username = "test-user-2" 25 user2.Nickname = "test user2" 26 th.App.UpdateUser(user2, false) 27 user3 := th.CreateUser() 28 user3.Username = "test-user-3" 29 user3.Nickname = "test user3" 30 th.App.UpdateUser(user3, false) 31 user4 := th.CreateUser() 32 user4.Username = "test-user-4" 33 user4.Nickname = "test user4" 34 th.App.UpdateUser(user4, false) 35 user5 := th.CreateUser() 36 user5.Username = "test-user-5" 37 user5.Nickname = "test user5" 38 th.App.UpdateUser(user5, false) 39 40 // user1 is member of all the channels and teams because is the creator 41 th.BasicUser = user1 42 43 team1 := th.CreateTeam() 44 team2 := th.CreateTeam() 45 46 channel1 := th.CreateChannel(team1) 47 channel2 := th.CreateChannel(team1) 48 channel3 := th.CreateChannel(team2) 49 50 th.LinkUserToTeam(user1, team1) 51 th.LinkUserToTeam(user2, team1) 52 th.LinkUserToTeam(user3, team2) 53 th.LinkUserToTeam(user4, team1) 54 th.LinkUserToTeam(user4, team2) 55 56 th.AddUserToChannel(user1, channel1) 57 th.AddUserToChannel(user2, channel2) 58 th.AddUserToChannel(user3, channel3) 59 th.AddUserToChannel(user4, channel1) 60 th.AddUserToChannel(user4, channel3) 61 62 th.App.SetStatusOnline(user1.Id, true) 63 th.App.SetStatusOnline(user2.Id, true) 64 th.App.SetStatusOnline(user3.Id, true) 65 th.App.SetStatusOnline(user4.Id, true) 66 th.App.SetStatusOnline(user5.Id, true) 67 68 t.Run("SearchUsers", func(t *testing.T) { 69 testCases := []struct { 70 Name string 71 Restrictions *model.ViewUsersRestrictions 72 Search model.UserSearch 73 ExpectedResults []string 74 }{ 75 { 76 "without restrictions team1", 77 nil, 78 model.UserSearch{Term: "test", TeamId: team1.Id}, 79 []string{user1.Id, user2.Id, user4.Id}, 80 }, 81 { 82 "without restrictions team2", 83 nil, 84 model.UserSearch{Term: "test", TeamId: team2.Id}, 85 []string{user3.Id, user4.Id}, 86 }, 87 { 88 "with team restrictions with valid team", 89 &model.ViewUsersRestrictions{ 90 Teams: []string{team1.Id}, 91 }, 92 model.UserSearch{Term: "test", TeamId: team1.Id}, 93 []string{user1.Id, user2.Id, user4.Id}, 94 }, 95 { 96 "with team restrictions with invalid team", 97 &model.ViewUsersRestrictions{ 98 Teams: []string{team1.Id}, 99 }, 100 model.UserSearch{Term: "test", TeamId: team2.Id}, 101 []string{user4.Id}, 102 }, 103 { 104 "with channel restrictions with valid team", 105 &model.ViewUsersRestrictions{ 106 Channels: []string{channel1.Id}, 107 }, 108 model.UserSearch{Term: "test", TeamId: team1.Id}, 109 []string{user1.Id, user4.Id}, 110 }, 111 { 112 "with channel restrictions with invalid team", 113 &model.ViewUsersRestrictions{ 114 Channels: []string{channel1.Id}, 115 }, 116 model.UserSearch{Term: "test", TeamId: team2.Id}, 117 []string{user4.Id}, 118 }, 119 { 120 "with restricting everything", 121 &model.ViewUsersRestrictions{ 122 Channels: []string{}, 123 Teams: []string{}, 124 }, 125 model.UserSearch{Term: "test", TeamId: team1.Id}, 126 []string{}, 127 }, 128 } 129 130 for _, tc := range testCases { 131 t.Run(tc.Name, func(t *testing.T) { 132 options := model.UserSearchOptions{Limit: 100, ViewRestrictions: tc.Restrictions} 133 results, err := th.App.SearchUsers(&tc.Search, &options) 134 require.Nil(t, err) 135 ids := []string{} 136 for _, result := range results { 137 ids = append(ids, result.Id) 138 } 139 assert.ElementsMatch(t, tc.ExpectedResults, ids) 140 }) 141 } 142 }) 143 144 t.Run("SearchUsersInTeam", func(t *testing.T) { 145 testCases := []struct { 146 Name string 147 Restrictions *model.ViewUsersRestrictions 148 TeamId string 149 ExpectedResults []string 150 }{ 151 { 152 "without restrictions team1", 153 nil, 154 team1.Id, 155 []string{user1.Id, user2.Id, user4.Id}, 156 }, 157 { 158 "without restrictions team2", 159 nil, 160 team2.Id, 161 []string{user3.Id, user4.Id}, 162 }, 163 { 164 "with team restrictions with valid team", 165 &model.ViewUsersRestrictions{ 166 Teams: []string{team1.Id}, 167 }, 168 team1.Id, 169 []string{user1.Id, user2.Id, user4.Id}, 170 }, 171 { 172 "with team restrictions with invalid team", 173 &model.ViewUsersRestrictions{ 174 Teams: []string{team1.Id}, 175 }, 176 team2.Id, 177 []string{user4.Id}, 178 }, 179 { 180 "with channel restrictions with valid team", 181 &model.ViewUsersRestrictions{ 182 Channels: []string{channel1.Id}, 183 }, 184 team1.Id, 185 []string{user1.Id, user4.Id}, 186 }, 187 { 188 "with channel restrictions with invalid team", 189 &model.ViewUsersRestrictions{ 190 Channels: []string{channel1.Id}, 191 }, 192 team2.Id, 193 []string{user4.Id}, 194 }, 195 { 196 "with restricting everything", 197 &model.ViewUsersRestrictions{ 198 Channels: []string{}, 199 Teams: []string{}, 200 }, 201 team1.Id, 202 []string{}, 203 }, 204 } 205 206 for _, tc := range testCases { 207 t.Run(tc.Name, func(t *testing.T) { 208 options := model.UserSearchOptions{Limit: 100, ViewRestrictions: tc.Restrictions} 209 results, err := th.App.SearchUsersInTeam(tc.TeamId, "test", &options) 210 require.Nil(t, err) 211 ids := []string{} 212 for _, result := range results { 213 ids = append(ids, result.Id) 214 } 215 assert.ElementsMatch(t, tc.ExpectedResults, ids) 216 }) 217 } 218 }) 219 220 t.Run("AutocompleteUsersInTeam", func(t *testing.T) { 221 testCases := []struct { 222 Name string 223 Restrictions *model.ViewUsersRestrictions 224 TeamId string 225 ExpectedResults []string 226 }{ 227 { 228 "without restrictions team1", 229 nil, 230 team1.Id, 231 []string{user1.Id, user2.Id, user4.Id}, 232 }, 233 { 234 "without restrictions team2", 235 nil, 236 team2.Id, 237 []string{user3.Id, user4.Id}, 238 }, 239 { 240 "with team restrictions with valid team", 241 &model.ViewUsersRestrictions{ 242 Teams: []string{team1.Id}, 243 }, 244 team1.Id, 245 []string{user1.Id, user2.Id, user4.Id}, 246 }, 247 { 248 "with team restrictions with invalid team", 249 &model.ViewUsersRestrictions{ 250 Teams: []string{team1.Id}, 251 }, 252 team2.Id, 253 []string{user4.Id}, 254 }, 255 { 256 "with channel restrictions with valid team", 257 &model.ViewUsersRestrictions{ 258 Channels: []string{channel1.Id}, 259 }, 260 team1.Id, 261 []string{user1.Id, user4.Id}, 262 }, 263 { 264 "with channel restrictions with invalid team", 265 &model.ViewUsersRestrictions{ 266 Channels: []string{channel1.Id}, 267 }, 268 team2.Id, 269 []string{user4.Id}, 270 }, 271 { 272 "with restricting everything", 273 &model.ViewUsersRestrictions{ 274 Channels: []string{}, 275 Teams: []string{}, 276 }, 277 team1.Id, 278 []string{}, 279 }, 280 } 281 282 for _, tc := range testCases { 283 t.Run(tc.Name, func(t *testing.T) { 284 options := model.UserSearchOptions{Limit: 100, ViewRestrictions: tc.Restrictions} 285 results, err := th.App.AutocompleteUsersInTeam(tc.TeamId, "tes", &options) 286 require.Nil(t, err) 287 ids := []string{} 288 for _, result := range results.InTeam { 289 ids = append(ids, result.Id) 290 } 291 assert.ElementsMatch(t, tc.ExpectedResults, ids) 292 }) 293 } 294 }) 295 296 t.Run("AutocompleteUsersInChannel", func(t *testing.T) { 297 testCases := []struct { 298 Name string 299 Restrictions *model.ViewUsersRestrictions 300 TeamId string 301 ChannelId string 302 ExpectedResults []string 303 }{ 304 { 305 "without restrictions channel1", 306 nil, 307 team1.Id, 308 channel1.Id, 309 []string{user1.Id, user4.Id}, 310 }, 311 { 312 "without restrictions channel3", 313 nil, 314 team2.Id, 315 channel3.Id, 316 []string{user1.Id, user3.Id, user4.Id}, 317 }, 318 { 319 "with team restrictions with valid team", 320 &model.ViewUsersRestrictions{ 321 Teams: []string{team1.Id}, 322 }, 323 team1.Id, 324 channel1.Id, 325 []string{user1.Id, user4.Id}, 326 }, 327 { 328 "with team restrictions with invalid team", 329 &model.ViewUsersRestrictions{ 330 Teams: []string{team1.Id}, 331 }, 332 team2.Id, 333 channel3.Id, 334 []string{user1.Id, user4.Id}, 335 }, 336 { 337 "with channel restrictions with valid team", 338 &model.ViewUsersRestrictions{ 339 Channels: []string{channel1.Id}, 340 }, 341 team1.Id, 342 channel1.Id, 343 []string{user1.Id, user4.Id}, 344 }, 345 { 346 "with channel restrictions with invalid team", 347 &model.ViewUsersRestrictions{ 348 Channels: []string{channel1.Id}, 349 }, 350 team2.Id, 351 channel3.Id, 352 []string{user1.Id, user4.Id}, 353 }, 354 { 355 "with restricting everything", 356 &model.ViewUsersRestrictions{ 357 Channels: []string{}, 358 Teams: []string{}, 359 }, 360 team1.Id, 361 channel1.Id, 362 []string{}, 363 }, 364 } 365 366 for _, tc := range testCases { 367 t.Run(tc.Name, func(t *testing.T) { 368 options := model.UserSearchOptions{Limit: 100, ViewRestrictions: tc.Restrictions} 369 results, err := th.App.AutocompleteUsersInChannel(tc.TeamId, tc.ChannelId, "tes", &options) 370 require.Nil(t, err) 371 ids := []string{} 372 for _, result := range results.InChannel { 373 ids = append(ids, result.Id) 374 } 375 assert.ElementsMatch(t, tc.ExpectedResults, ids) 376 }) 377 } 378 }) 379 380 t.Run("GetNewUsersForTeam", func(t *testing.T) { 381 testCases := []struct { 382 Name string 383 Restrictions *model.ViewUsersRestrictions 384 TeamId string 385 ExpectedResults []string 386 }{ 387 { 388 "without restrictions team1", 389 nil, 390 team1.Id, 391 []string{user2.Id, user4.Id}, 392 }, 393 { 394 "without restrictions team2", 395 nil, 396 team2.Id, 397 []string{user3.Id, user4.Id}, 398 }, 399 { 400 "with team restrictions with valid team", 401 &model.ViewUsersRestrictions{ 402 Teams: []string{team1.Id}, 403 }, 404 team1.Id, 405 []string{user2.Id, user4.Id}, 406 }, 407 { 408 "with team restrictions with invalid team", 409 &model.ViewUsersRestrictions{ 410 Teams: []string{team1.Id}, 411 }, 412 team2.Id, 413 []string{user4.Id}, 414 }, 415 { 416 "with channel restrictions with valid team", 417 &model.ViewUsersRestrictions{ 418 Channels: []string{channel1.Id}, 419 }, 420 team1.Id, 421 []string{user1.Id, user4.Id}, 422 }, 423 { 424 "with channel restrictions with invalid team", 425 &model.ViewUsersRestrictions{ 426 Channels: []string{channel1.Id}, 427 }, 428 team2.Id, 429 []string{user4.Id}, 430 }, 431 { 432 "with restricting everything", 433 &model.ViewUsersRestrictions{ 434 Channels: []string{}, 435 Teams: []string{}, 436 }, 437 team1.Id, 438 []string{}, 439 }, 440 } 441 442 for _, tc := range testCases { 443 t.Run(tc.Name, func(t *testing.T) { 444 results, err := th.App.GetNewUsersForTeamPage(tc.TeamId, 0, 2, false, tc.Restrictions) 445 require.Nil(t, err) 446 ids := []string{} 447 for _, result := range results { 448 ids = append(ids, result.Id) 449 } 450 assert.ElementsMatch(t, tc.ExpectedResults, ids) 451 }) 452 } 453 }) 454 455 t.Run("GetRecentlyActiveUsersForTeamPage", func(t *testing.T) { 456 testCases := []struct { 457 Name string 458 Restrictions *model.ViewUsersRestrictions 459 TeamId string 460 ExpectedResults []string 461 }{ 462 { 463 "without restrictions team1", 464 nil, 465 team1.Id, 466 []string{user1.Id, user2.Id, user4.Id}, 467 }, 468 { 469 "without restrictions team2", 470 nil, 471 team2.Id, 472 []string{user3.Id, user4.Id}, 473 }, 474 { 475 "with team restrictions with valid team", 476 &model.ViewUsersRestrictions{ 477 Teams: []string{team1.Id}, 478 }, 479 team1.Id, 480 []string{user1.Id, user2.Id, user4.Id}, 481 }, 482 { 483 "with team restrictions with invalid team", 484 &model.ViewUsersRestrictions{ 485 Teams: []string{team1.Id}, 486 }, 487 team2.Id, 488 []string{user4.Id}, 489 }, 490 { 491 "with channel restrictions with valid team", 492 &model.ViewUsersRestrictions{ 493 Channels: []string{channel1.Id}, 494 }, 495 team1.Id, 496 []string{user1.Id, user4.Id}, 497 }, 498 { 499 "with channel restrictions with invalid team", 500 &model.ViewUsersRestrictions{ 501 Channels: []string{channel1.Id}, 502 }, 503 team2.Id, 504 []string{user4.Id}, 505 }, 506 { 507 "with restricting everything", 508 &model.ViewUsersRestrictions{ 509 Channels: []string{}, 510 Teams: []string{}, 511 }, 512 team1.Id, 513 []string{}, 514 }, 515 } 516 517 for _, tc := range testCases { 518 t.Run(tc.Name, func(t *testing.T) { 519 results, err := th.App.GetRecentlyActiveUsersForTeamPage(tc.TeamId, 0, 3, false, tc.Restrictions) 520 require.Nil(t, err) 521 ids := []string{} 522 for _, result := range results { 523 ids = append(ids, result.Id) 524 } 525 assert.ElementsMatch(t, tc.ExpectedResults, ids) 526 527 results, err = th.App.GetRecentlyActiveUsersForTeamPage(tc.TeamId, 0, 1, false, tc.Restrictions) 528 require.Nil(t, err) 529 if len(tc.ExpectedResults) > 1 { 530 assert.Len(t, results, 1) 531 } else { 532 assert.Len(t, results, len(tc.ExpectedResults)) 533 } 534 }) 535 } 536 }) 537 538 t.Run("GetUsers", func(t *testing.T) { 539 testCases := []struct { 540 Name string 541 Restrictions *model.ViewUsersRestrictions 542 ExpectedResults []string 543 }{ 544 { 545 "without restrictions", 546 nil, 547 []string{user1.Id, user2.Id, user3.Id, user4.Id, user5.Id}, 548 }, 549 { 550 "with team restrictions", 551 &model.ViewUsersRestrictions{ 552 Teams: []string{team1.Id}, 553 }, 554 []string{user1.Id, user2.Id, user4.Id}, 555 }, 556 { 557 "with channel restrictions", 558 &model.ViewUsersRestrictions{ 559 Channels: []string{channel1.Id}, 560 }, 561 []string{user1.Id, user4.Id}, 562 }, 563 { 564 "with restricting everything", 565 &model.ViewUsersRestrictions{ 566 Channels: []string{}, 567 Teams: []string{}, 568 }, 569 []string{}, 570 }, 571 } 572 573 for _, tc := range testCases { 574 t.Run(tc.Name, func(t *testing.T) { 575 options := model.UserGetOptions{Page: 0, PerPage: 100, ViewRestrictions: tc.Restrictions} 576 results, err := th.App.GetUsers(&options) 577 require.Nil(t, err) 578 ids := []string{} 579 for _, result := range results { 580 ids = append(ids, result.Id) 581 } 582 assert.ElementsMatch(t, tc.ExpectedResults, ids) 583 }) 584 } 585 }) 586 587 t.Run("GetUsersWithoutTeam", func(t *testing.T) { 588 testCases := []struct { 589 Name string 590 Restrictions *model.ViewUsersRestrictions 591 ExpectedResults []string 592 }{ 593 { 594 "without restrictions", 595 nil, 596 []string{user5.Id}, 597 }, 598 { 599 "with team restrictions", 600 &model.ViewUsersRestrictions{ 601 Teams: []string{team1.Id}, 602 }, 603 []string{}, 604 }, 605 { 606 "with channel restrictions", 607 &model.ViewUsersRestrictions{ 608 Channels: []string{channel1.Id}, 609 }, 610 []string{}, 611 }, 612 { 613 "with restricting everything", 614 &model.ViewUsersRestrictions{ 615 Channels: []string{}, 616 Teams: []string{}, 617 }, 618 []string{}, 619 }, 620 } 621 622 for _, tc := range testCases { 623 t.Run(tc.Name, func(t *testing.T) { 624 results, err := th.App.GetUsersWithoutTeam(&model.UserGetOptions{Page: 0, PerPage: 100, ViewRestrictions: tc.Restrictions}) 625 require.Nil(t, err) 626 ids := []string{} 627 for _, result := range results { 628 ids = append(ids, result.Id) 629 } 630 assert.ElementsMatch(t, tc.ExpectedResults, ids) 631 }) 632 } 633 }) 634 635 t.Run("GetUsersNotInTeam", func(t *testing.T) { 636 testCases := []struct { 637 Name string 638 Restrictions *model.ViewUsersRestrictions 639 TeamId string 640 ExpectedResults []string 641 }{ 642 { 643 "without restrictions team1", 644 nil, 645 team1.Id, 646 []string{user3.Id, user5.Id}, 647 }, 648 { 649 "without restrictions team2", 650 nil, 651 team2.Id, 652 []string{user1.Id, user2.Id, user5.Id}, 653 }, 654 { 655 "with team restrictions with valid team", 656 &model.ViewUsersRestrictions{ 657 Teams: []string{team1.Id}, 658 }, 659 team2.Id, 660 []string{user1.Id, user2.Id}, 661 }, 662 { 663 "with team restrictions with invalid team", 664 &model.ViewUsersRestrictions{ 665 Teams: []string{team1.Id}, 666 }, 667 team1.Id, 668 []string{}, 669 }, 670 { 671 "with channel restrictions with valid team", 672 &model.ViewUsersRestrictions{ 673 Channels: []string{channel1.Id}, 674 }, 675 team2.Id, 676 []string{user1.Id}, 677 }, 678 { 679 "with channel restrictions with invalid team", 680 &model.ViewUsersRestrictions{ 681 Channels: []string{channel1.Id}, 682 }, 683 team1.Id, 684 []string{}, 685 }, 686 { 687 "with restricting everything", 688 &model.ViewUsersRestrictions{ 689 Channels: []string{}, 690 Teams: []string{}, 691 }, 692 team2.Id, 693 []string{}, 694 }, 695 } 696 697 for _, tc := range testCases { 698 t.Run(tc.Name, func(t *testing.T) { 699 results, err := th.App.GetUsersNotInTeam(tc.TeamId, false, 0, 100, tc.Restrictions) 700 require.Nil(t, err) 701 ids := []string{} 702 for _, result := range results { 703 ids = append(ids, result.Id) 704 } 705 assert.ElementsMatch(t, tc.ExpectedResults, ids) 706 }) 707 } 708 }) 709 710 t.Run("GetUsersNotInChannel", func(t *testing.T) { 711 testCases := []struct { 712 Name string 713 Restrictions *model.ViewUsersRestrictions 714 TeamId string 715 ChannelId string 716 ExpectedResults []string 717 }{ 718 { 719 "without restrictions channel1", 720 nil, 721 team1.Id, 722 channel1.Id, 723 []string{user2.Id}, 724 }, 725 { 726 "without restrictions channel2", 727 nil, 728 team1.Id, 729 channel2.Id, 730 []string{user4.Id}, 731 }, 732 { 733 "with team restrictions with valid team", 734 &model.ViewUsersRestrictions{ 735 Teams: []string{team1.Id}, 736 }, 737 team1.Id, 738 channel1.Id, 739 []string{user2.Id}, 740 }, 741 { 742 "with team restrictions with invalid team", 743 &model.ViewUsersRestrictions{ 744 Teams: []string{team2.Id}, 745 }, 746 team1.Id, 747 channel1.Id, 748 []string{}, 749 }, 750 { 751 "with channel restrictions with valid team", 752 &model.ViewUsersRestrictions{ 753 Channels: []string{channel2.Id}, 754 }, 755 team1.Id, 756 channel1.Id, 757 []string{user2.Id}, 758 }, 759 { 760 "with channel restrictions with invalid team", 761 &model.ViewUsersRestrictions{ 762 Channels: []string{channel2.Id}, 763 }, 764 team1.Id, 765 channel2.Id, 766 []string{}, 767 }, 768 { 769 "with restricting everything", 770 &model.ViewUsersRestrictions{ 771 Channels: []string{}, 772 Teams: []string{}, 773 }, 774 team1.Id, 775 channel1.Id, 776 []string{}, 777 }, 778 } 779 780 for _, tc := range testCases { 781 t.Run(tc.Name, func(t *testing.T) { 782 results, err := th.App.GetUsersNotInChannel(tc.TeamId, tc.ChannelId, false, 0, 100, tc.Restrictions) 783 require.Nil(t, err) 784 ids := []string{} 785 for _, result := range results { 786 ids = append(ids, result.Id) 787 } 788 assert.ElementsMatch(t, tc.ExpectedResults, ids) 789 }) 790 } 791 }) 792 793 t.Run("GetUsersByIds", func(t *testing.T) { 794 testCases := []struct { 795 Name string 796 Restrictions *model.ViewUsersRestrictions 797 UserIds []string 798 ExpectedResults []string 799 }{ 800 { 801 "without restrictions", 802 nil, 803 []string{user1.Id, user2.Id, user3.Id}, 804 []string{user1.Id, user2.Id, user3.Id}, 805 }, 806 { 807 "with team restrictions", 808 &model.ViewUsersRestrictions{ 809 Teams: []string{team1.Id}, 810 }, 811 []string{user1.Id, user2.Id, user3.Id}, 812 []string{user1.Id, user2.Id}, 813 }, 814 { 815 "with channel restrictions", 816 &model.ViewUsersRestrictions{ 817 Channels: []string{channel1.Id}, 818 }, 819 []string{user1.Id, user2.Id, user3.Id}, 820 []string{user1.Id}, 821 }, 822 { 823 "with restricting everything", 824 &model.ViewUsersRestrictions{ 825 Channels: []string{}, 826 Teams: []string{}, 827 }, 828 []string{user1.Id, user2.Id, user3.Id}, 829 []string{}, 830 }, 831 } 832 833 for _, tc := range testCases { 834 t.Run(tc.Name, func(t *testing.T) { 835 results, err := th.App.GetUsersByIds(tc.UserIds, &store.UserGetByIdsOpts{ 836 IsAdmin: false, 837 ViewRestrictions: tc.Restrictions, 838 }) 839 require.Nil(t, err) 840 ids := []string{} 841 for _, result := range results { 842 ids = append(ids, result.Id) 843 } 844 assert.ElementsMatch(t, tc.ExpectedResults, ids) 845 }) 846 } 847 }) 848 849 t.Run("GetUsersByUsernames", func(t *testing.T) { 850 testCases := []struct { 851 Name string 852 Restrictions *model.ViewUsersRestrictions 853 Usernames []string 854 ExpectedResults []string 855 }{ 856 { 857 "without restrictions", 858 nil, 859 []string{user1.Username, user2.Username, user3.Username}, 860 []string{user1.Id, user2.Id, user3.Id}, 861 }, 862 { 863 "with team restrictions", 864 &model.ViewUsersRestrictions{ 865 Teams: []string{team1.Id}, 866 }, 867 []string{user1.Username, user2.Username, user3.Username}, 868 []string{user1.Id, user2.Id}, 869 }, 870 { 871 "with channel restrictions", 872 &model.ViewUsersRestrictions{ 873 Channels: []string{channel1.Id}, 874 }, 875 []string{user1.Username, user2.Username, user3.Username}, 876 []string{user1.Id}, 877 }, 878 { 879 "with restricting everything", 880 &model.ViewUsersRestrictions{ 881 Channels: []string{}, 882 Teams: []string{}, 883 }, 884 []string{user1.Username, user2.Username, user3.Username}, 885 []string{}, 886 }, 887 } 888 889 for _, tc := range testCases { 890 t.Run(tc.Name, func(t *testing.T) { 891 results, err := th.App.GetUsersByUsernames(tc.Usernames, false, tc.Restrictions) 892 require.Nil(t, err) 893 ids := []string{} 894 for _, result := range results { 895 ids = append(ids, result.Id) 896 } 897 assert.ElementsMatch(t, tc.ExpectedResults, ids) 898 }) 899 } 900 }) 901 902 t.Run("GetTotalUsersStats", func(t *testing.T) { 903 testCases := []struct { 904 Name string 905 Restrictions *model.ViewUsersRestrictions 906 ExpectedResult int64 907 }{ 908 { 909 "without restrictions", 910 nil, 911 5, 912 }, 913 { 914 "with team restrictions", 915 &model.ViewUsersRestrictions{ 916 Teams: []string{team1.Id}, 917 }, 918 3, 919 }, 920 { 921 "with channel restrictions", 922 &model.ViewUsersRestrictions{ 923 Channels: []string{channel1.Id}, 924 }, 925 2, 926 }, 927 { 928 "with restricting everything", 929 &model.ViewUsersRestrictions{ 930 Channels: []string{}, 931 Teams: []string{}, 932 }, 933 0, 934 }, 935 } 936 937 for _, tc := range testCases { 938 t.Run(tc.Name, func(t *testing.T) { 939 result, err := th.App.GetTotalUsersStats(tc.Restrictions) 940 require.Nil(t, err) 941 assert.Equal(t, tc.ExpectedResult, result.TotalUsersCount) 942 }) 943 } 944 }) 945 946 t.Run("GetTeamMembers", func(t *testing.T) { 947 testCases := []struct { 948 Name string 949 Restrictions *model.ViewUsersRestrictions 950 TeamId string 951 ExpectedResults []string 952 }{ 953 { 954 "without restrictions team1", 955 nil, 956 team1.Id, 957 []string{user1.Id, user2.Id, user4.Id}, 958 }, 959 { 960 "without restrictions team2", 961 nil, 962 team2.Id, 963 []string{user3.Id, user4.Id}, 964 }, 965 { 966 "with team restrictions with valid team", 967 &model.ViewUsersRestrictions{ 968 Teams: []string{team1.Id}, 969 }, 970 team1.Id, 971 []string{user1.Id, user2.Id, user4.Id}, 972 }, 973 { 974 "with team restrictions with invalid team", 975 &model.ViewUsersRestrictions{ 976 Teams: []string{team1.Id}, 977 }, 978 team2.Id, 979 []string{user4.Id}, 980 }, 981 { 982 "with channel restrictions with valid team", 983 &model.ViewUsersRestrictions{ 984 Channels: []string{channel1.Id}, 985 }, 986 team1.Id, 987 []string{user1.Id, user4.Id}, 988 }, 989 { 990 "with channel restrictions with invalid team", 991 &model.ViewUsersRestrictions{ 992 Channels: []string{channel1.Id}, 993 }, 994 team2.Id, 995 []string{user4.Id}, 996 }, 997 { 998 "with restricting everything", 999 &model.ViewUsersRestrictions{ 1000 Channels: []string{}, 1001 Teams: []string{}, 1002 }, 1003 team1.Id, 1004 []string{}, 1005 }, 1006 } 1007 1008 for _, tc := range testCases { 1009 t.Run(tc.Name, func(t *testing.T) { 1010 getTeamMemberOptions := &model.TeamMembersGetOptions{ 1011 ViewRestrictions: tc.Restrictions, 1012 } 1013 results, err := th.App.GetTeamMembers(tc.TeamId, 0, 100, getTeamMemberOptions) 1014 require.Nil(t, err) 1015 ids := []string{} 1016 for _, result := range results { 1017 ids = append(ids, result.UserId) 1018 } 1019 assert.ElementsMatch(t, tc.ExpectedResults, ids) 1020 }) 1021 } 1022 }) 1023 1024 t.Run("GetTeamMembersByIds", func(t *testing.T) { 1025 testCases := []struct { 1026 Name string 1027 Restrictions *model.ViewUsersRestrictions 1028 TeamId string 1029 UserIds []string 1030 ExpectedResults []string 1031 }{ 1032 { 1033 "without restrictions team1", 1034 nil, 1035 team1.Id, 1036 []string{user1.Id, user2.Id, user3.Id}, 1037 []string{user1.Id, user2.Id}, 1038 }, 1039 { 1040 "without restrictions team2", 1041 nil, 1042 team2.Id, 1043 []string{user1.Id, user2.Id, user3.Id}, 1044 []string{user3.Id}, 1045 }, 1046 { 1047 "with team restrictions with valid team", 1048 &model.ViewUsersRestrictions{ 1049 Teams: []string{team1.Id}, 1050 }, 1051 team1.Id, 1052 []string{user1.Id, user2.Id, user3.Id}, 1053 []string{user1.Id, user2.Id}, 1054 }, 1055 { 1056 "with team restrictions with invalid team", 1057 &model.ViewUsersRestrictions{ 1058 Teams: []string{team1.Id}, 1059 }, 1060 team2.Id, 1061 []string{user2.Id, user4.Id}, 1062 []string{user4.Id}, 1063 }, 1064 { 1065 "with channel restrictions with valid team", 1066 &model.ViewUsersRestrictions{ 1067 Channels: []string{channel1.Id}, 1068 }, 1069 team1.Id, 1070 []string{user2.Id, user4.Id}, 1071 []string{user4.Id}, 1072 }, 1073 { 1074 "with channel restrictions with invalid team", 1075 &model.ViewUsersRestrictions{ 1076 Channels: []string{channel1.Id}, 1077 }, 1078 team2.Id, 1079 []string{user2.Id, user4.Id}, 1080 []string{user4.Id}, 1081 }, 1082 { 1083 "with restricting everything", 1084 &model.ViewUsersRestrictions{ 1085 Channels: []string{}, 1086 Teams: []string{}, 1087 }, 1088 team1.Id, 1089 []string{user1.Id, user2.Id, user2.Id, user4.Id}, 1090 []string{}, 1091 }, 1092 } 1093 1094 for _, tc := range testCases { 1095 t.Run(tc.Name, func(t *testing.T) { 1096 results, err := th.App.GetTeamMembersByIds(tc.TeamId, tc.UserIds, tc.Restrictions) 1097 require.Nil(t, err) 1098 ids := []string{} 1099 for _, result := range results { 1100 ids = append(ids, result.UserId) 1101 } 1102 assert.ElementsMatch(t, tc.ExpectedResults, ids) 1103 }) 1104 } 1105 }) 1106 }