github.com/keys-pub/mattermost-server@v4.10.10+incompatible/api4/role_test.go (about)

     1  // Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package api4
     5  
     6  import (
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  
    12  	"github.com/mattermost/mattermost-server/model"
    13  )
    14  
    15  func TestGetRole(t *testing.T) {
    16  	th := Setup().InitBasic().InitSystemAdmin()
    17  	defer th.TearDown()
    18  
    19  	role := &model.Role{
    20  		Name:          model.NewId(),
    21  		DisplayName:   model.NewId(),
    22  		Description:   model.NewId(),
    23  		Permissions:   []string{"manage_system", "create_public_channel"},
    24  		SchemeManaged: true,
    25  	}
    26  
    27  	res1 := <-th.App.Srv.Store.Role().Save(role)
    28  	assert.Nil(t, res1.Err)
    29  	role = res1.Data.(*model.Role)
    30  	defer th.App.Srv.Store.Job().Delete(role.Id)
    31  
    32  	received, resp := th.Client.GetRole(role.Id)
    33  	CheckNoError(t, resp)
    34  
    35  	assert.Equal(t, received.Id, role.Id)
    36  	assert.Equal(t, received.Name, role.Name)
    37  	assert.Equal(t, received.DisplayName, role.DisplayName)
    38  	assert.Equal(t, received.Description, role.Description)
    39  	assert.EqualValues(t, received.Permissions, role.Permissions)
    40  	assert.Equal(t, received.SchemeManaged, role.SchemeManaged)
    41  
    42  	_, resp = th.SystemAdminClient.GetRole("1234")
    43  	CheckBadRequestStatus(t, resp)
    44  
    45  	_, resp = th.SystemAdminClient.GetRole(model.NewId())
    46  	CheckNotFoundStatus(t, resp)
    47  }
    48  
    49  func TestGetRoleByName(t *testing.T) {
    50  	th := Setup().InitBasic().InitSystemAdmin()
    51  	defer th.TearDown()
    52  
    53  	role := &model.Role{
    54  		Name:          model.NewId(),
    55  		DisplayName:   model.NewId(),
    56  		Description:   model.NewId(),
    57  		Permissions:   []string{"manage_system", "create_public_channel"},
    58  		SchemeManaged: true,
    59  	}
    60  
    61  	res1 := <-th.App.Srv.Store.Role().Save(role)
    62  	assert.Nil(t, res1.Err)
    63  	role = res1.Data.(*model.Role)
    64  	defer th.App.Srv.Store.Job().Delete(role.Id)
    65  
    66  	received, resp := th.Client.GetRoleByName(role.Name)
    67  	CheckNoError(t, resp)
    68  
    69  	assert.Equal(t, received.Id, role.Id)
    70  	assert.Equal(t, received.Name, role.Name)
    71  	assert.Equal(t, received.DisplayName, role.DisplayName)
    72  	assert.Equal(t, received.Description, role.Description)
    73  	assert.EqualValues(t, received.Permissions, role.Permissions)
    74  	assert.Equal(t, received.SchemeManaged, role.SchemeManaged)
    75  
    76  	_, resp = th.SystemAdminClient.GetRoleByName(strings.Repeat("abcdefghij", 10))
    77  	CheckBadRequestStatus(t, resp)
    78  
    79  	_, resp = th.SystemAdminClient.GetRoleByName(model.NewId())
    80  	CheckNotFoundStatus(t, resp)
    81  }
    82  
    83  func TestGetRolesByNames(t *testing.T) {
    84  	th := Setup().InitBasic().InitSystemAdmin()
    85  	defer th.TearDown()
    86  
    87  	role1 := &model.Role{
    88  		Name:          model.NewId(),
    89  		DisplayName:   model.NewId(),
    90  		Description:   model.NewId(),
    91  		Permissions:   []string{"manage_system", "create_public_channel"},
    92  		SchemeManaged: true,
    93  	}
    94  	role2 := &model.Role{
    95  		Name:          model.NewId(),
    96  		DisplayName:   model.NewId(),
    97  		Description:   model.NewId(),
    98  		Permissions:   []string{"manage_system", "delete_private_channel"},
    99  		SchemeManaged: true,
   100  	}
   101  	role3 := &model.Role{
   102  		Name:          model.NewId(),
   103  		DisplayName:   model.NewId(),
   104  		Description:   model.NewId(),
   105  		Permissions:   []string{"manage_system", "manage_public_channel_properties"},
   106  		SchemeManaged: true,
   107  	}
   108  
   109  	res1 := <-th.App.Srv.Store.Role().Save(role1)
   110  	assert.Nil(t, res1.Err)
   111  	role1 = res1.Data.(*model.Role)
   112  	defer th.App.Srv.Store.Job().Delete(role1.Id)
   113  
   114  	res2 := <-th.App.Srv.Store.Role().Save(role2)
   115  	assert.Nil(t, res2.Err)
   116  	role2 = res2.Data.(*model.Role)
   117  	defer th.App.Srv.Store.Job().Delete(role2.Id)
   118  
   119  	res3 := <-th.App.Srv.Store.Role().Save(role3)
   120  	assert.Nil(t, res3.Err)
   121  	role3 = res3.Data.(*model.Role)
   122  	defer th.App.Srv.Store.Job().Delete(role3.Id)
   123  
   124  	// Check all three roles can be found.
   125  	received, resp := th.Client.GetRolesByNames([]string{role1.Name, role2.Name, role3.Name})
   126  	CheckNoError(t, resp)
   127  
   128  	assert.Contains(t, received, role1)
   129  	assert.Contains(t, received, role2)
   130  	assert.Contains(t, received, role3)
   131  
   132  	// Check a list of non-existent roles.
   133  	received, resp = th.Client.GetRolesByNames([]string{model.NewId(), model.NewId()})
   134  	CheckNoError(t, resp)
   135  
   136  	// Empty list should error.
   137  	_, resp = th.SystemAdminClient.GetRolesByNames([]string{})
   138  	CheckBadRequestStatus(t, resp)
   139  
   140  	// Invalid role name should error.
   141  	received, resp = th.Client.GetRolesByNames([]string{model.NewId(), model.NewId(), "!!!!!!"})
   142  	CheckBadRequestStatus(t, resp)
   143  
   144  	// Empty/whitespace rolenames should be ignored.
   145  	received, resp = th.Client.GetRolesByNames([]string{model.NewId(), model.NewId(), "", "    "})
   146  	CheckNoError(t, resp)
   147  }
   148  
   149  func TestPatchRole(t *testing.T) {
   150  	th := Setup().InitBasic().InitSystemAdmin()
   151  	defer th.TearDown()
   152  
   153  	role := &model.Role{
   154  		Name:          model.NewId(),
   155  		DisplayName:   model.NewId(),
   156  		Description:   model.NewId(),
   157  		Permissions:   []string{"manage_system", "create_public_channel", "manage_slash_commands"},
   158  		SchemeManaged: true,
   159  	}
   160  
   161  	res1 := <-th.App.Srv.Store.Role().Save(role)
   162  	assert.Nil(t, res1.Err)
   163  	role = res1.Data.(*model.Role)
   164  	defer th.App.Srv.Store.Job().Delete(role.Id)
   165  
   166  	patch := &model.RolePatch{
   167  		Permissions: &[]string{"manage_system", "create_public_channel", "manage_webhooks"},
   168  	}
   169  
   170  	received, resp := th.SystemAdminClient.PatchRole(role.Id, patch)
   171  	CheckNoError(t, resp)
   172  
   173  	assert.Equal(t, received.Id, role.Id)
   174  	assert.Equal(t, received.Name, role.Name)
   175  	assert.Equal(t, received.DisplayName, role.DisplayName)
   176  	assert.Equal(t, received.Description, role.Description)
   177  	assert.EqualValues(t, received.Permissions, []string{"manage_system", "create_public_channel", "manage_webhooks"})
   178  	assert.Equal(t, received.SchemeManaged, role.SchemeManaged)
   179  
   180  	// Check a no-op patch succeeds.
   181  	received, resp = th.SystemAdminClient.PatchRole(role.Id, patch)
   182  	CheckNoError(t, resp)
   183  
   184  	received, resp = th.SystemAdminClient.PatchRole("junk", patch)
   185  	CheckBadRequestStatus(t, resp)
   186  
   187  	received, resp = th.Client.PatchRole(model.NewId(), patch)
   188  	CheckNotFoundStatus(t, resp)
   189  
   190  	received, resp = th.Client.PatchRole(role.Id, patch)
   191  	CheckForbiddenStatus(t, resp)
   192  
   193  	// Check a change that the license would not allow.
   194  	patch = &model.RolePatch{
   195  		Permissions: &[]string{"manage_system", "manage_webhooks"},
   196  	}
   197  
   198  	received, resp = th.SystemAdminClient.PatchRole(role.Id, patch)
   199  	CheckNotImplementedStatus(t, resp)
   200  
   201  	// Add a license.
   202  	th.App.SetLicense(model.NewTestLicense())
   203  
   204  	// Try again, should succeed
   205  	received, resp = th.SystemAdminClient.PatchRole(role.Id, patch)
   206  	CheckNoError(t, resp)
   207  
   208  	assert.Equal(t, received.Id, role.Id)
   209  	assert.Equal(t, received.Name, role.Name)
   210  	assert.Equal(t, received.DisplayName, role.DisplayName)
   211  	assert.Equal(t, received.Description, role.Description)
   212  	assert.EqualValues(t, received.Permissions, []string{"manage_system", "manage_webhooks"})
   213  	assert.Equal(t, received.SchemeManaged, role.SchemeManaged)
   214  }