github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/store/storetest/scheme_store.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package storetest
     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 TestSchemeStore(t *testing.T, ss store.Store) {
    17  	createDefaultRoles(ss)
    18  
    19  	t.Run("Save", func(t *testing.T) { testSchemeStoreSave(t, ss) })
    20  	t.Run("Get", func(t *testing.T) { testSchemeStoreGet(t, ss) })
    21  	t.Run("GetAllPage", func(t *testing.T) { testSchemeStoreGetAllPage(t, ss) })
    22  	t.Run("Delete", func(t *testing.T) { testSchemeStoreDelete(t, ss) })
    23  	t.Run("PermanentDeleteAll", func(t *testing.T) { testSchemeStorePermanentDeleteAll(t, ss) })
    24  	t.Run("GetByName", func(t *testing.T) { testSchemeStoreGetByName(t, ss) })
    25  	t.Run("CountByScope", func(t *testing.T) { testSchemeStoreCountByScope(t, ss) })
    26  	t.Run("CountWithoutPermission", func(t *testing.T) { testCountWithoutPermission(t, ss) })
    27  }
    28  
    29  func createDefaultRoles(ss store.Store) {
    30  	ss.Role().Save(&model.Role{
    31  		Name:        model.TEAM_ADMIN_ROLE_ID,
    32  		DisplayName: model.TEAM_ADMIN_ROLE_ID,
    33  		Permissions: []string{
    34  			model.PERMISSION_DELETE_OTHERS_POSTS.Id,
    35  		},
    36  	})
    37  
    38  	ss.Role().Save(&model.Role{
    39  		Name:        model.TEAM_USER_ROLE_ID,
    40  		DisplayName: model.TEAM_USER_ROLE_ID,
    41  		Permissions: []string{
    42  			model.PERMISSION_VIEW_TEAM.Id,
    43  			model.PERMISSION_ADD_USER_TO_TEAM.Id,
    44  		},
    45  	})
    46  
    47  	ss.Role().Save(&model.Role{
    48  		Name:        model.TEAM_GUEST_ROLE_ID,
    49  		DisplayName: model.TEAM_GUEST_ROLE_ID,
    50  		Permissions: []string{
    51  			model.PERMISSION_VIEW_TEAM.Id,
    52  		},
    53  	})
    54  
    55  	ss.Role().Save(&model.Role{
    56  		Name:        model.CHANNEL_ADMIN_ROLE_ID,
    57  		DisplayName: model.CHANNEL_ADMIN_ROLE_ID,
    58  		Permissions: []string{
    59  			model.PERMISSION_MANAGE_PUBLIC_CHANNEL_MEMBERS.Id,
    60  			model.PERMISSION_MANAGE_PRIVATE_CHANNEL_MEMBERS.Id,
    61  		},
    62  	})
    63  
    64  	ss.Role().Save(&model.Role{
    65  		Name:        model.CHANNEL_USER_ROLE_ID,
    66  		DisplayName: model.CHANNEL_USER_ROLE_ID,
    67  		Permissions: []string{
    68  			model.PERMISSION_READ_CHANNEL.Id,
    69  			model.PERMISSION_CREATE_POST.Id,
    70  		},
    71  	})
    72  
    73  	ss.Role().Save(&model.Role{
    74  		Name:        model.CHANNEL_GUEST_ROLE_ID,
    75  		DisplayName: model.CHANNEL_GUEST_ROLE_ID,
    76  		Permissions: []string{
    77  			model.PERMISSION_READ_CHANNEL.Id,
    78  			model.PERMISSION_CREATE_POST.Id,
    79  		},
    80  	})
    81  }
    82  
    83  func testSchemeStoreSave(t *testing.T, ss store.Store) {
    84  	// Save a new scheme.
    85  	s1 := &model.Scheme{
    86  		DisplayName: model.NewId(),
    87  		Name:        model.NewId(),
    88  		Description: model.NewId(),
    89  		Scope:       model.SCHEME_SCOPE_TEAM,
    90  	}
    91  
    92  	// Check all fields saved correctly.
    93  	d1, err := ss.Scheme().Save(s1)
    94  	assert.NoError(t, err)
    95  	assert.Len(t, d1.Id, 26)
    96  	assert.Equal(t, s1.DisplayName, d1.DisplayName)
    97  	assert.Equal(t, s1.Name, d1.Name)
    98  	assert.Equal(t, s1.Description, d1.Description)
    99  	assert.NotZero(t, d1.CreateAt)
   100  	assert.NotZero(t, d1.UpdateAt)
   101  	assert.Zero(t, d1.DeleteAt)
   102  	assert.Equal(t, s1.Scope, d1.Scope)
   103  	assert.Len(t, d1.DefaultTeamAdminRole, 26)
   104  	assert.Len(t, d1.DefaultTeamUserRole, 26)
   105  	assert.Len(t, d1.DefaultTeamGuestRole, 26)
   106  	assert.Len(t, d1.DefaultChannelAdminRole, 26)
   107  	assert.Len(t, d1.DefaultChannelUserRole, 26)
   108  	assert.Len(t, d1.DefaultChannelGuestRole, 26)
   109  
   110  	// Check the default roles were created correctly.
   111  	role1, err := ss.Role().GetByName(d1.DefaultTeamAdminRole)
   112  	assert.NoError(t, err)
   113  	assert.Equal(t, role1.Permissions, []string{"delete_others_posts"})
   114  	assert.True(t, role1.SchemeManaged)
   115  
   116  	role2, err := ss.Role().GetByName(d1.DefaultTeamUserRole)
   117  	assert.NoError(t, err)
   118  	assert.Equal(t, role2.Permissions, []string{"view_team", "add_user_to_team"})
   119  	assert.True(t, role2.SchemeManaged)
   120  
   121  	role3, err := ss.Role().GetByName(d1.DefaultChannelAdminRole)
   122  	assert.NoError(t, err)
   123  	assert.Equal(t, role3.Permissions, []string{"manage_public_channel_members", "manage_private_channel_members"})
   124  	assert.True(t, role3.SchemeManaged)
   125  
   126  	role4, err := ss.Role().GetByName(d1.DefaultChannelUserRole)
   127  	assert.NoError(t, err)
   128  	assert.Equal(t, role4.Permissions, []string{"read_channel", "create_post"})
   129  	assert.True(t, role4.SchemeManaged)
   130  
   131  	role5, err := ss.Role().GetByName(d1.DefaultTeamGuestRole)
   132  	assert.NoError(t, err)
   133  	assert.Equal(t, role5.Permissions, []string{"view_team"})
   134  	assert.True(t, role5.SchemeManaged)
   135  
   136  	role6, err := ss.Role().GetByName(d1.DefaultChannelGuestRole)
   137  	assert.NoError(t, err)
   138  	assert.Equal(t, role6.Permissions, []string{"read_channel", "create_post"})
   139  	assert.True(t, role6.SchemeManaged)
   140  
   141  	// Change the scheme description and update.
   142  	d1.Description = model.NewId()
   143  
   144  	d2, err := ss.Scheme().Save(d1)
   145  	assert.NoError(t, err)
   146  	assert.Equal(t, d1.Id, d2.Id)
   147  	assert.Equal(t, s1.DisplayName, d2.DisplayName)
   148  	assert.Equal(t, s1.Name, d2.Name)
   149  	assert.Equal(t, d1.Description, d2.Description)
   150  	assert.NotZero(t, d2.CreateAt)
   151  	assert.NotZero(t, d2.UpdateAt)
   152  	assert.Zero(t, d2.DeleteAt)
   153  	assert.Equal(t, s1.Scope, d2.Scope)
   154  	assert.Equal(t, d1.DefaultTeamAdminRole, d2.DefaultTeamAdminRole)
   155  	assert.Equal(t, d1.DefaultTeamUserRole, d2.DefaultTeamUserRole)
   156  	assert.Equal(t, d1.DefaultTeamGuestRole, d2.DefaultTeamGuestRole)
   157  	assert.Equal(t, d1.DefaultChannelAdminRole, d2.DefaultChannelAdminRole)
   158  	assert.Equal(t, d1.DefaultChannelUserRole, d2.DefaultChannelUserRole)
   159  	assert.Equal(t, d1.DefaultChannelGuestRole, d2.DefaultChannelGuestRole)
   160  
   161  	// Try saving one with an invalid ID set.
   162  	s3 := &model.Scheme{
   163  		Id:          model.NewId(),
   164  		DisplayName: model.NewId(),
   165  		Name:        model.NewId(),
   166  		Description: model.NewId(),
   167  		Scope:       model.SCHEME_SCOPE_TEAM,
   168  	}
   169  
   170  	_, err = ss.Scheme().Save(s3)
   171  	assert.Error(t, err)
   172  }
   173  
   174  func testSchemeStoreGet(t *testing.T, ss store.Store) {
   175  	// Save a scheme to test with.
   176  	s1 := &model.Scheme{
   177  		DisplayName: model.NewId(),
   178  		Name:        model.NewId(),
   179  		Description: model.NewId(),
   180  		Scope:       model.SCHEME_SCOPE_TEAM,
   181  	}
   182  
   183  	d1, err := ss.Scheme().Save(s1)
   184  	assert.NoError(t, err)
   185  	assert.Len(t, d1.Id, 26)
   186  
   187  	// Get a valid scheme
   188  	d2, err := ss.Scheme().Get(d1.Id)
   189  	assert.NoError(t, err)
   190  	assert.Equal(t, d1.Id, d2.Id)
   191  	assert.Equal(t, s1.DisplayName, d2.DisplayName)
   192  	assert.Equal(t, s1.Name, d2.Name)
   193  	assert.Equal(t, d1.Description, d2.Description)
   194  	assert.NotZero(t, d2.CreateAt)
   195  	assert.NotZero(t, d2.UpdateAt)
   196  	assert.Zero(t, d2.DeleteAt)
   197  	assert.Equal(t, s1.Scope, d2.Scope)
   198  	assert.Equal(t, d1.DefaultTeamAdminRole, d2.DefaultTeamAdminRole)
   199  	assert.Equal(t, d1.DefaultTeamUserRole, d2.DefaultTeamUserRole)
   200  	assert.Equal(t, d1.DefaultTeamGuestRole, d2.DefaultTeamGuestRole)
   201  	assert.Equal(t, d1.DefaultChannelAdminRole, d2.DefaultChannelAdminRole)
   202  	assert.Equal(t, d1.DefaultChannelUserRole, d2.DefaultChannelUserRole)
   203  	assert.Equal(t, d1.DefaultChannelGuestRole, d2.DefaultChannelGuestRole)
   204  
   205  	// Get an invalid scheme
   206  	_, err = ss.Scheme().Get(model.NewId())
   207  	assert.Error(t, err)
   208  }
   209  
   210  func testSchemeStoreGetByName(t *testing.T, ss store.Store) {
   211  	// Save a scheme to test with.
   212  	s1 := &model.Scheme{
   213  		DisplayName: model.NewId(),
   214  		Name:        model.NewId(),
   215  		Description: model.NewId(),
   216  		Scope:       model.SCHEME_SCOPE_TEAM,
   217  	}
   218  
   219  	d1, err := ss.Scheme().Save(s1)
   220  	assert.NoError(t, err)
   221  	assert.Len(t, d1.Id, 26)
   222  
   223  	// Get a valid scheme
   224  	d2, err := ss.Scheme().GetByName(d1.Name)
   225  	assert.NoError(t, err)
   226  	assert.Equal(t, d1.Id, d2.Id)
   227  	assert.Equal(t, s1.DisplayName, d2.DisplayName)
   228  	assert.Equal(t, s1.Name, d2.Name)
   229  	assert.Equal(t, d1.Description, d2.Description)
   230  	assert.NotZero(t, d2.CreateAt)
   231  	assert.NotZero(t, d2.UpdateAt)
   232  	assert.Zero(t, d2.DeleteAt)
   233  	assert.Equal(t, s1.Scope, d2.Scope)
   234  	assert.Equal(t, d1.DefaultTeamAdminRole, d2.DefaultTeamAdminRole)
   235  	assert.Equal(t, d1.DefaultTeamUserRole, d2.DefaultTeamUserRole)
   236  	assert.Equal(t, d1.DefaultTeamGuestRole, d2.DefaultTeamGuestRole)
   237  	assert.Equal(t, d1.DefaultChannelAdminRole, d2.DefaultChannelAdminRole)
   238  	assert.Equal(t, d1.DefaultChannelUserRole, d2.DefaultChannelUserRole)
   239  	assert.Equal(t, d1.DefaultChannelGuestRole, d2.DefaultChannelGuestRole)
   240  
   241  	// Get an invalid scheme
   242  	_, err = ss.Scheme().GetByName(model.NewId())
   243  	assert.Error(t, err)
   244  }
   245  
   246  func testSchemeStoreGetAllPage(t *testing.T, ss store.Store) {
   247  	// Save a scheme to test with.
   248  	schemes := []*model.Scheme{
   249  		{
   250  			DisplayName: model.NewId(),
   251  			Name:        model.NewId(),
   252  			Description: model.NewId(),
   253  			Scope:       model.SCHEME_SCOPE_TEAM,
   254  		},
   255  		{
   256  			DisplayName: model.NewId(),
   257  			Name:        model.NewId(),
   258  			Description: model.NewId(),
   259  			Scope:       model.SCHEME_SCOPE_CHANNEL,
   260  		},
   261  		{
   262  			DisplayName: model.NewId(),
   263  			Name:        model.NewId(),
   264  			Description: model.NewId(),
   265  			Scope:       model.SCHEME_SCOPE_TEAM,
   266  		},
   267  		{
   268  			DisplayName: model.NewId(),
   269  			Name:        model.NewId(),
   270  			Description: model.NewId(),
   271  			Scope:       model.SCHEME_SCOPE_CHANNEL,
   272  		},
   273  	}
   274  
   275  	for _, scheme := range schemes {
   276  		_, err := ss.Scheme().Save(scheme)
   277  		require.NoError(t, err)
   278  	}
   279  
   280  	s1, err := ss.Scheme().GetAllPage("", 0, 2)
   281  	assert.NoError(t, err)
   282  	assert.Len(t, s1, 2)
   283  
   284  	s2, err := ss.Scheme().GetAllPage("", 2, 2)
   285  	assert.NoError(t, err)
   286  	assert.Len(t, s2, 2)
   287  	assert.NotEqual(t, s1[0].DisplayName, s2[0].DisplayName)
   288  	assert.NotEqual(t, s1[0].DisplayName, s2[1].DisplayName)
   289  	assert.NotEqual(t, s1[1].DisplayName, s2[0].DisplayName)
   290  	assert.NotEqual(t, s1[1].DisplayName, s2[1].DisplayName)
   291  	assert.NotEqual(t, s1[0].Name, s2[0].Name)
   292  	assert.NotEqual(t, s1[0].Name, s2[1].Name)
   293  	assert.NotEqual(t, s1[1].Name, s2[0].Name)
   294  	assert.NotEqual(t, s1[1].Name, s2[1].Name)
   295  
   296  	s3, err := ss.Scheme().GetAllPage("team", 0, 1000)
   297  	assert.NoError(t, err)
   298  	assert.NotZero(t, len(s3))
   299  	for _, s := range s3 {
   300  		assert.Equal(t, "team", s.Scope)
   301  	}
   302  
   303  	s4, err := ss.Scheme().GetAllPage("channel", 0, 1000)
   304  	assert.NoError(t, err)
   305  	assert.NotZero(t, len(s4))
   306  	for _, s := range s4 {
   307  		assert.Equal(t, "channel", s.Scope)
   308  	}
   309  }
   310  
   311  func testSchemeStoreDelete(t *testing.T, ss store.Store) {
   312  	// Save a new scheme.
   313  	s1 := &model.Scheme{
   314  		DisplayName: model.NewId(),
   315  		Name:        model.NewId(),
   316  		Description: model.NewId(),
   317  		Scope:       model.SCHEME_SCOPE_TEAM,
   318  	}
   319  
   320  	// Check all fields saved correctly.
   321  	d1, err := ss.Scheme().Save(s1)
   322  	assert.NoError(t, err)
   323  	assert.Len(t, d1.Id, 26)
   324  	assert.Equal(t, s1.DisplayName, d1.DisplayName)
   325  	assert.Equal(t, s1.Name, d1.Name)
   326  	assert.Equal(t, s1.Description, d1.Description)
   327  	assert.NotZero(t, d1.CreateAt)
   328  	assert.NotZero(t, d1.UpdateAt)
   329  	assert.Zero(t, d1.DeleteAt)
   330  	assert.Equal(t, s1.Scope, d1.Scope)
   331  	assert.Len(t, d1.DefaultTeamAdminRole, 26)
   332  	assert.Len(t, d1.DefaultTeamUserRole, 26)
   333  	assert.Len(t, d1.DefaultTeamGuestRole, 26)
   334  	assert.Len(t, d1.DefaultChannelAdminRole, 26)
   335  	assert.Len(t, d1.DefaultChannelUserRole, 26)
   336  	assert.Len(t, d1.DefaultChannelGuestRole, 26)
   337  
   338  	// Check the default roles were created correctly.
   339  	role1, err := ss.Role().GetByName(d1.DefaultTeamAdminRole)
   340  	assert.NoError(t, err)
   341  	assert.Equal(t, role1.Permissions, []string{"delete_others_posts"})
   342  	assert.True(t, role1.SchemeManaged)
   343  
   344  	role2, err := ss.Role().GetByName(d1.DefaultTeamUserRole)
   345  	assert.NoError(t, err)
   346  	assert.Equal(t, role2.Permissions, []string{"view_team", "add_user_to_team"})
   347  	assert.True(t, role2.SchemeManaged)
   348  
   349  	role3, err := ss.Role().GetByName(d1.DefaultChannelAdminRole)
   350  	assert.NoError(t, err)
   351  	assert.Equal(t, role3.Permissions, []string{"manage_public_channel_members", "manage_private_channel_members"})
   352  	assert.True(t, role3.SchemeManaged)
   353  
   354  	role4, err := ss.Role().GetByName(d1.DefaultChannelUserRole)
   355  	assert.NoError(t, err)
   356  	assert.Equal(t, role4.Permissions, []string{"read_channel", "create_post"})
   357  	assert.True(t, role4.SchemeManaged)
   358  
   359  	role5, err := ss.Role().GetByName(d1.DefaultTeamGuestRole)
   360  	assert.NoError(t, err)
   361  	assert.Equal(t, role5.Permissions, []string{"view_team"})
   362  	assert.True(t, role5.SchemeManaged)
   363  
   364  	role6, err := ss.Role().GetByName(d1.DefaultChannelGuestRole)
   365  	assert.NoError(t, err)
   366  	assert.Equal(t, role6.Permissions, []string{"read_channel", "create_post"})
   367  	assert.True(t, role6.SchemeManaged)
   368  
   369  	// Delete the scheme.
   370  	d2, err := ss.Scheme().Delete(d1.Id)
   371  	require.NoError(t, err)
   372  	assert.NotZero(t, d2.DeleteAt)
   373  
   374  	// Check that the roles are deleted too.
   375  	role7, err := ss.Role().GetByName(d1.DefaultTeamAdminRole)
   376  	assert.NoError(t, err)
   377  	assert.NotZero(t, role7.DeleteAt)
   378  
   379  	role8, err := ss.Role().GetByName(d1.DefaultTeamUserRole)
   380  	assert.NoError(t, err)
   381  	assert.NotZero(t, role8.DeleteAt)
   382  
   383  	role9, err := ss.Role().GetByName(d1.DefaultChannelAdminRole)
   384  	assert.NoError(t, err)
   385  	assert.NotZero(t, role9.DeleteAt)
   386  
   387  	role10, err := ss.Role().GetByName(d1.DefaultChannelUserRole)
   388  	assert.NoError(t, err)
   389  	assert.NotZero(t, role10.DeleteAt)
   390  
   391  	role11, err := ss.Role().GetByName(d1.DefaultTeamGuestRole)
   392  	assert.NoError(t, err)
   393  	assert.NotZero(t, role11.DeleteAt)
   394  
   395  	role12, err := ss.Role().GetByName(d1.DefaultChannelGuestRole)
   396  	assert.NoError(t, err)
   397  	assert.NotZero(t, role12.DeleteAt)
   398  
   399  	// Try deleting a scheme that does not exist.
   400  	_, err = ss.Scheme().Delete(model.NewId())
   401  	assert.Error(t, err)
   402  
   403  	// Try deleting a team scheme that's in use.
   404  	s4 := &model.Scheme{
   405  		DisplayName: model.NewId(),
   406  		Name:        model.NewId(),
   407  		Description: model.NewId(),
   408  		Scope:       model.SCHEME_SCOPE_TEAM,
   409  	}
   410  	d4, err := ss.Scheme().Save(s4)
   411  	assert.NoError(t, err)
   412  
   413  	t4 := &model.Team{
   414  		Name:        "xx" + model.NewId(),
   415  		DisplayName: model.NewId(),
   416  		Email:       MakeEmail(),
   417  		Type:        model.TEAM_OPEN,
   418  		SchemeId:    &d4.Id,
   419  	}
   420  	t4, err = ss.Team().Save(t4)
   421  	require.NoError(t, err)
   422  
   423  	_, err = ss.Scheme().Delete(d4.Id)
   424  	assert.NoError(t, err)
   425  
   426  	t5, err := ss.Team().Get(t4.Id)
   427  	require.NoError(t, err)
   428  	assert.Equal(t, "", *t5.SchemeId)
   429  
   430  	// Try deleting a channel scheme that's in use.
   431  	s5 := &model.Scheme{
   432  		DisplayName: model.NewId(),
   433  		Name:        model.NewId(),
   434  		Description: model.NewId(),
   435  		Scope:       model.SCHEME_SCOPE_CHANNEL,
   436  	}
   437  	d5, err := ss.Scheme().Save(s5)
   438  	assert.NoError(t, err)
   439  
   440  	c5 := &model.Channel{
   441  		TeamId:      model.NewId(),
   442  		DisplayName: model.NewId(),
   443  		Name:        model.NewId(),
   444  		Type:        model.CHANNEL_OPEN,
   445  		SchemeId:    &d5.Id,
   446  	}
   447  	c5, nErr := ss.Channel().Save(c5, -1)
   448  	assert.NoError(t, nErr)
   449  
   450  	_, err = ss.Scheme().Delete(d5.Id)
   451  	assert.NoError(t, err)
   452  
   453  	c6, nErr := ss.Channel().Get(c5.Id, true)
   454  	assert.NoError(t, nErr)
   455  	assert.Equal(t, "", *c6.SchemeId)
   456  }
   457  
   458  func testSchemeStorePermanentDeleteAll(t *testing.T, ss store.Store) {
   459  	s1 := &model.Scheme{
   460  		Name:        model.NewId(),
   461  		DisplayName: model.NewId(),
   462  		Description: model.NewId(),
   463  		Scope:       model.SCHEME_SCOPE_TEAM,
   464  	}
   465  
   466  	s2 := &model.Scheme{
   467  		Name:        model.NewId(),
   468  		DisplayName: model.NewId(),
   469  		Description: model.NewId(),
   470  		Scope:       model.SCHEME_SCOPE_CHANNEL,
   471  	}
   472  
   473  	s1, err := ss.Scheme().Save(s1)
   474  	require.NoError(t, err)
   475  	s2, err = ss.Scheme().Save(s2)
   476  	require.NoError(t, err)
   477  
   478  	err = ss.Scheme().PermanentDeleteAll()
   479  	assert.NoError(t, err)
   480  
   481  	_, err = ss.Scheme().Get(s1.Id)
   482  	assert.Error(t, err)
   483  
   484  	_, err = ss.Scheme().Get(s2.Id)
   485  	assert.Error(t, err)
   486  
   487  	schemes, err := ss.Scheme().GetAllPage("", 0, 100000)
   488  	assert.NoError(t, err)
   489  	assert.Empty(t, schemes)
   490  }
   491  
   492  func testSchemeStoreCountByScope(t *testing.T, ss store.Store) {
   493  	testCounts := func(expectedTeamCount, expectedChannelCount int) {
   494  		actualCount, err := ss.Scheme().CountByScope(model.SCHEME_SCOPE_TEAM)
   495  		require.NoError(t, err)
   496  		require.Equal(t, int64(expectedTeamCount), actualCount)
   497  
   498  		actualCount, err = ss.Scheme().CountByScope(model.SCHEME_SCOPE_CHANNEL)
   499  		require.NoError(t, err)
   500  		require.Equal(t, int64(expectedChannelCount), actualCount)
   501  	}
   502  
   503  	createScheme := func(scope string) {
   504  		_, err := ss.Scheme().Save(&model.Scheme{
   505  			Name:        model.NewId(),
   506  			DisplayName: model.NewId(),
   507  			Description: model.NewId(),
   508  			Scope:       scope,
   509  		})
   510  		require.NoError(t, err)
   511  	}
   512  
   513  	err := ss.Scheme().PermanentDeleteAll()
   514  	require.NoError(t, err)
   515  
   516  	createScheme(model.SCHEME_SCOPE_CHANNEL)
   517  	createScheme(model.SCHEME_SCOPE_TEAM)
   518  	testCounts(1, 1)
   519  	createScheme(model.SCHEME_SCOPE_TEAM)
   520  	testCounts(2, 1)
   521  	createScheme(model.SCHEME_SCOPE_CHANNEL)
   522  	testCounts(2, 2)
   523  }
   524  
   525  func testCountWithoutPermission(t *testing.T, ss store.Store) {
   526  	perm := model.PERMISSION_CREATE_POST.Id
   527  
   528  	createScheme := func(scope string) *model.Scheme {
   529  		scheme, err := ss.Scheme().Save(&model.Scheme{
   530  			Name:        model.NewId(),
   531  			DisplayName: model.NewId(),
   532  			Description: model.NewId(),
   533  			Scope:       scope,
   534  		})
   535  		require.NoError(t, err)
   536  		return scheme
   537  	}
   538  
   539  	getRoles := func(scheme *model.Scheme) (channelUser, channelGuest *model.Role) {
   540  		var err error
   541  		channelUser, err = ss.Role().GetByName(scheme.DefaultChannelUserRole)
   542  		require.NoError(t, err)
   543  		require.NotNil(t, channelUser)
   544  		channelGuest, err = ss.Role().GetByName(scheme.DefaultChannelGuestRole)
   545  		require.NoError(t, err)
   546  		require.NotNil(t, channelGuest)
   547  		return
   548  	}
   549  
   550  	teamScheme1 := createScheme(model.SCHEME_SCOPE_TEAM)
   551  	defer ss.Scheme().Delete(teamScheme1.Id)
   552  	teamScheme2 := createScheme(model.SCHEME_SCOPE_TEAM)
   553  	defer ss.Scheme().Delete(teamScheme2.Id)
   554  	channelScheme1 := createScheme(model.SCHEME_SCOPE_CHANNEL)
   555  	defer ss.Scheme().Delete(channelScheme1.Id)
   556  	channelScheme2 := createScheme(model.SCHEME_SCOPE_CHANNEL)
   557  	defer ss.Scheme().Delete(channelScheme2.Id)
   558  
   559  	ts1User, ts1Guest := getRoles(teamScheme1)
   560  	ts2User, ts2Guest := getRoles(teamScheme2)
   561  	cs1User, cs1Guest := getRoles(channelScheme1)
   562  	cs2User, cs2Guest := getRoles(channelScheme2)
   563  
   564  	allRoles := []*model.Role{
   565  		ts1User,
   566  		ts1Guest,
   567  		ts2User,
   568  		ts2Guest,
   569  		cs1User,
   570  		cs1Guest,
   571  		cs2User,
   572  		cs2Guest,
   573  	}
   574  
   575  	teamUserCount, err := ss.Scheme().CountWithoutPermission(model.SCHEME_SCOPE_TEAM, perm, model.RoleScopeChannel, model.RoleTypeUser)
   576  	require.NoError(t, err)
   577  	require.Equal(t, int64(0), teamUserCount)
   578  
   579  	teamGuestCount, err := ss.Scheme().CountWithoutPermission(model.SCHEME_SCOPE_TEAM, perm, model.RoleScopeChannel, model.RoleTypeGuest)
   580  	require.NoError(t, err)
   581  	require.Equal(t, int64(0), teamGuestCount)
   582  
   583  	var tests = []struct {
   584  		removePermissionFromRole             *model.Role
   585  		expectTeamSchemeChannelUserCount     int
   586  		expectTeamSchemeChannelGuestCount    int
   587  		expectChannelSchemeChannelUserCount  int
   588  		expectChannelSchemeChannelGuestCount int
   589  	}{
   590  		{ts1User, 1, 0, 0, 0},
   591  		{ts1Guest, 1, 1, 0, 0},
   592  		{ts2User, 2, 1, 0, 0},
   593  		{ts2Guest, 2, 2, 0, 0},
   594  		{cs1User, 2, 2, 1, 0},
   595  		{cs1Guest, 2, 2, 1, 1},
   596  		{cs2User, 2, 2, 2, 1},
   597  		{cs2Guest, 2, 2, 2, 2},
   598  	}
   599  
   600  	removePermission := func(targetRole *model.Role) {
   601  		roleMatched := false
   602  		for _, role := range allRoles {
   603  			if targetRole == role {
   604  				roleMatched = true
   605  				role.Permissions = []string{}
   606  				_, err = ss.Role().Save(role)
   607  				require.NoError(t, err)
   608  			}
   609  		}
   610  		require.True(t, roleMatched)
   611  	}
   612  
   613  	for _, test := range tests {
   614  		removePermission(test.removePermissionFromRole)
   615  
   616  		count, err := ss.Scheme().CountWithoutPermission(model.SCHEME_SCOPE_TEAM, perm, model.RoleScopeChannel, model.RoleTypeUser)
   617  		require.NoError(t, err)
   618  		require.Equal(t, int64(test.expectTeamSchemeChannelUserCount), count)
   619  
   620  		count, err = ss.Scheme().CountWithoutPermission(model.SCHEME_SCOPE_TEAM, perm, model.RoleScopeChannel, model.RoleTypeGuest)
   621  		require.NoError(t, err)
   622  		require.Equal(t, int64(test.expectTeamSchemeChannelGuestCount), count)
   623  
   624  		count, err = ss.Scheme().CountWithoutPermission(model.SCHEME_SCOPE_CHANNEL, perm, model.RoleScopeChannel, model.RoleTypeUser)
   625  		require.NoError(t, err)
   626  		require.Equal(t, int64(test.expectChannelSchemeChannelUserCount), count)
   627  
   628  		count, err = ss.Scheme().CountWithoutPermission(model.SCHEME_SCOPE_CHANNEL, perm, model.RoleScopeChannel, model.RoleTypeGuest)
   629  		require.NoError(t, err)
   630  		require.Equal(t, int64(test.expectChannelSchemeChannelGuestCount), count)
   631  	}
   632  }