github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/app/authorization_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  )
    14  
    15  func TestCheckIfRolesGrantPermission(t *testing.T) {
    16  	th := Setup(t)
    17  	defer th.TearDown()
    18  
    19  	cases := []struct {
    20  		roles        []string
    21  		permissionId string
    22  		shouldGrant  bool
    23  	}{
    24  		{[]string{model.SYSTEM_ADMIN_ROLE_ID}, model.PERMISSION_MANAGE_SYSTEM.Id, true},
    25  		{[]string{model.SYSTEM_ADMIN_ROLE_ID}, "non-existent-permission", false},
    26  		{[]string{model.CHANNEL_USER_ROLE_ID}, model.PERMISSION_READ_CHANNEL.Id, true},
    27  		{[]string{model.CHANNEL_USER_ROLE_ID}, model.PERMISSION_MANAGE_SYSTEM.Id, false},
    28  		{[]string{model.SYSTEM_ADMIN_ROLE_ID, model.CHANNEL_USER_ROLE_ID}, model.PERMISSION_MANAGE_SYSTEM.Id, true},
    29  		{[]string{model.CHANNEL_USER_ROLE_ID, model.SYSTEM_ADMIN_ROLE_ID}, model.PERMISSION_MANAGE_SYSTEM.Id, true},
    30  		{[]string{model.TEAM_USER_ROLE_ID, model.TEAM_ADMIN_ROLE_ID}, model.PERMISSION_MANAGE_SLASH_COMMANDS.Id, true},
    31  		{[]string{model.TEAM_ADMIN_ROLE_ID, model.TEAM_USER_ROLE_ID}, model.PERMISSION_MANAGE_SLASH_COMMANDS.Id, true},
    32  	}
    33  
    34  	for _, testcase := range cases {
    35  		require.Equal(t, th.App.RolesGrantPermission(testcase.roles, testcase.permissionId), testcase.shouldGrant)
    36  	}
    37  
    38  }
    39  
    40  func TestChannelRolesGrantPermission(t *testing.T) {
    41  	testPermissionInheritance(t, func(t *testing.T, th *TestHelper, testData permissionInheritanceTestData) {
    42  		require.Equal(t, testData.shouldHavePermission, th.App.RolesGrantPermission([]string{testData.channelRole.Name}, testData.permission.Id), "row: %+v\n", testData.truthTableRow)
    43  	})
    44  }
    45  
    46  func TestHasPermissionToTeam(t *testing.T) {
    47  	th := Setup(t).InitBasic()
    48  	defer th.TearDown()
    49  
    50  	assert.True(t, th.App.HasPermissionToTeam(th.BasicUser.Id, th.BasicTeam.Id, model.PERMISSION_LIST_TEAM_CHANNELS))
    51  
    52  	th.RemoveUserFromTeam(th.BasicUser, th.BasicTeam)
    53  
    54  	assert.False(t, th.App.HasPermissionToTeam(th.BasicUser.Id, th.BasicTeam.Id, model.PERMISSION_LIST_TEAM_CHANNELS))
    55  
    56  	th.LinkUserToTeam(th.SystemAdminUser, th.BasicTeam)
    57  	assert.True(t, th.App.HasPermissionToTeam(th.SystemAdminUser.Id, th.BasicTeam.Id, model.PERMISSION_LIST_TEAM_CHANNELS))
    58  	th.RemoveUserFromTeam(th.SystemAdminUser, th.BasicTeam)
    59  	// This used to fail before MM-26015
    60  	assert.True(t, th.App.HasPermissionToTeam(th.SystemAdminUser.Id, th.BasicTeam.Id, model.PERMISSION_LIST_TEAM_CHANNELS))
    61  }