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