From c6bc7fed6d6efa1e7e283bf7be91ea795829ba6f Mon Sep 17 00:00:00 2001 From: Ibrahim Serdar Acikgoz Date: Tue, 23 Jun 2020 12:56:52 +0300 Subject: [PATCH] api4/role: add role endpoints to local mode (#14876) --- api4/api.go | 3 + api4/role_local.go | 11 +++ api4/role_test.go | 193 +++++++++++++++++++++++++-------------------- 3 files changed, 121 insertions(+), 86 deletions(-) create mode 100644 api4/role_local.go diff --git a/api4/api.go b/api4/api.go index 8deb72c025..bbf22a3f73 100644 --- a/api4/api.go +++ b/api4/api.go @@ -308,6 +308,8 @@ func InitLocal(configservice configservice.ConfigService, globalOptionsFunc app. api.BaseRoutes.Post = api.BaseRoutes.Posts.PathPrefix("/{post_id:[A-Za-z0-9]+}").Subrouter() api.BaseRoutes.PostsForChannel = api.BaseRoutes.Channel.PathPrefix("/posts").Subrouter() + api.BaseRoutes.Roles = api.BaseRoutes.ApiRoot.PathPrefix("/roles").Subrouter() + api.InitUserLocal() api.InitTeamLocal() api.InitChannelLocal() @@ -320,6 +322,7 @@ func InitLocal(configservice configservice.ConfigService, globalOptionsFunc app. api.InitLdapLocal() api.InitSystemLocal() api.InitPostLocal() + api.InitRoleLocal() root.Handle("/api/v4/{anything:.*}", http.HandlerFunc(api.Handle404)) diff --git a/api4/role_local.go b/api4/role_local.go new file mode 100644 index 0000000000..a738690eae --- /dev/null +++ b/api4/role_local.go @@ -0,0 +1,11 @@ +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. +// See LICENSE.txt for license information. + +package api4 + +func (api *API) InitRoleLocal() { + api.BaseRoutes.Roles.Handle("/{role_id:[A-Za-z0-9]+}", api.ApiLocal(getRole)).Methods("GET") + api.BaseRoutes.Roles.Handle("/name/{role_name:[a-z0-9_]+}", api.ApiLocal(getRoleByName)).Methods("GET") + api.BaseRoutes.Roles.Handle("/names", api.ApiLocal(getRolesByNames)).Methods("POST") + api.BaseRoutes.Roles.Handle("/{role_id:[A-Za-z0-9]+}/patch", api.ApiLocal(patchRole)).Methods("PUT") +} diff --git a/api4/role_test.go b/api4/role_test.go index e97b70384f..be7de78e19 100644 --- a/api4/role_test.go +++ b/api4/role_test.go @@ -29,21 +29,25 @@ func TestGetRole(t *testing.T) { assert.Nil(t, err) defer th.App.Srv().Store.Job().Delete(role.Id) - received, resp := th.Client.GetRole(role.Id) - CheckNoError(t, resp) + th.TestForAllClients(t, func(t *testing.T, client *model.Client4) { + received, resp := client.GetRole(role.Id) + CheckNoError(t, resp) - assert.Equal(t, received.Id, role.Id) - assert.Equal(t, received.Name, role.Name) - assert.Equal(t, received.DisplayName, role.DisplayName) - assert.Equal(t, received.Description, role.Description) - assert.EqualValues(t, received.Permissions, role.Permissions) - assert.Equal(t, received.SchemeManaged, role.SchemeManaged) + assert.Equal(t, received.Id, role.Id) + assert.Equal(t, received.Name, role.Name) + assert.Equal(t, received.DisplayName, role.DisplayName) + assert.Equal(t, received.Description, role.Description) + assert.EqualValues(t, received.Permissions, role.Permissions) + assert.Equal(t, received.SchemeManaged, role.SchemeManaged) + }) - _, resp = th.SystemAdminClient.GetRole("1234") - CheckBadRequestStatus(t, resp) + th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) { + _, resp := client.GetRole("1234") + CheckBadRequestStatus(t, resp) - _, resp = th.SystemAdminClient.GetRole(model.NewId()) - CheckNotFoundStatus(t, resp) + _, resp = client.GetRole(model.NewId()) + CheckNotFoundStatus(t, resp) + }) } func TestGetRoleByName(t *testing.T) { @@ -62,21 +66,25 @@ func TestGetRoleByName(t *testing.T) { assert.Nil(t, err) defer th.App.Srv().Store.Job().Delete(role.Id) - received, resp := th.Client.GetRoleByName(role.Name) - CheckNoError(t, resp) + th.TestForAllClients(t, func(t *testing.T, client *model.Client4) { + received, resp := client.GetRoleByName(role.Name) + CheckNoError(t, resp) - assert.Equal(t, received.Id, role.Id) - assert.Equal(t, received.Name, role.Name) - assert.Equal(t, received.DisplayName, role.DisplayName) - assert.Equal(t, received.Description, role.Description) - assert.EqualValues(t, received.Permissions, role.Permissions) - assert.Equal(t, received.SchemeManaged, role.SchemeManaged) + assert.Equal(t, received.Id, role.Id) + assert.Equal(t, received.Name, role.Name) + assert.Equal(t, received.DisplayName, role.DisplayName) + assert.Equal(t, received.Description, role.Description) + assert.EqualValues(t, received.Permissions, role.Permissions) + assert.Equal(t, received.SchemeManaged, role.SchemeManaged) + }) - _, resp = th.SystemAdminClient.GetRoleByName(strings.Repeat("abcdefghij", 10)) - CheckBadRequestStatus(t, resp) + th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) { + _, resp := client.GetRoleByName(strings.Repeat("abcdefghij", 10)) + CheckBadRequestStatus(t, resp) - _, resp = th.SystemAdminClient.GetRoleByName(model.NewId()) - CheckNotFoundStatus(t, resp) + _, resp = client.GetRoleByName(model.NewId()) + CheckNotFoundStatus(t, resp) + }) } func TestGetRolesByNames(t *testing.T) { @@ -117,29 +125,36 @@ func TestGetRolesByNames(t *testing.T) { assert.Nil(t, err) defer th.App.Srv().Store.Job().Delete(role3.Id) - // Check all three roles can be found. - received, resp := th.Client.GetRolesByNames([]string{role1.Name, role2.Name, role3.Name}) - CheckNoError(t, resp) + th.TestForAllClients(t, func(t *testing.T, client *model.Client4) { + // Check all three roles can be found. + received, resp := client.GetRolesByNames([]string{role1.Name, role2.Name, role3.Name}) + CheckNoError(t, resp) - assert.Contains(t, received, role1) - assert.Contains(t, received, role2) - assert.Contains(t, received, role3) + assert.Contains(t, received, role1) + assert.Contains(t, received, role2) + assert.Contains(t, received, role3) - // Check a list of non-existent roles. - _, resp = th.Client.GetRolesByNames([]string{model.NewId(), model.NewId()}) - CheckNoError(t, resp) + // Check a list of non-existent roles. + _, resp = client.GetRolesByNames([]string{model.NewId(), model.NewId()}) + CheckNoError(t, resp) + }) - // Empty list should error. - _, resp = th.SystemAdminClient.GetRolesByNames([]string{}) - CheckBadRequestStatus(t, resp) + th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) { + // Empty list should error. + _, resp := client.GetRolesByNames([]string{}) + CheckBadRequestStatus(t, resp) + }) - // Invalid role name should error. - _, resp = th.Client.GetRolesByNames([]string{model.NewId(), model.NewId(), "!!!!!!"}) - CheckBadRequestStatus(t, resp) + th.TestForAllClients(t, func(t *testing.T, client *model.Client4) { + // Invalid role name should error. + _, resp := client.GetRolesByNames([]string{model.NewId(), model.NewId(), "!!!!!!"}) + CheckBadRequestStatus(t, resp) + + // Empty/whitespace rolenames should be ignored. + _, resp = client.GetRolesByNames([]string{model.NewId(), model.NewId(), "", " "}) + CheckNoError(t, resp) + }) - // Empty/whitespace rolenames should be ignored. - _, resp = th.Client.GetRolesByNames([]string{model.NewId(), model.NewId(), "", " "}) - CheckNoError(t, resp) } func TestPatchRole(t *testing.T) { @@ -162,24 +177,26 @@ func TestPatchRole(t *testing.T) { Permissions: &[]string{"manage_system", "create_public_channel", "manage_incoming_webhooks", "manage_outgoing_webhooks"}, } - received, resp := th.SystemAdminClient.PatchRole(role.Id, patch) - CheckNoError(t, resp) + th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) { + received, resp := client.PatchRole(role.Id, patch) + CheckNoError(t, resp) - assert.Equal(t, received.Id, role.Id) - assert.Equal(t, received.Name, role.Name) - assert.Equal(t, received.DisplayName, role.DisplayName) - assert.Equal(t, received.Description, role.Description) - assert.EqualValues(t, received.Permissions, []string{"manage_system", "create_public_channel", "manage_incoming_webhooks", "manage_outgoing_webhooks"}) - assert.Equal(t, received.SchemeManaged, role.SchemeManaged) + assert.Equal(t, received.Id, role.Id) + assert.Equal(t, received.Name, role.Name) + assert.Equal(t, received.DisplayName, role.DisplayName) + assert.Equal(t, received.Description, role.Description) + assert.EqualValues(t, received.Permissions, []string{"manage_system", "create_public_channel", "manage_incoming_webhooks", "manage_outgoing_webhooks"}) + assert.Equal(t, received.SchemeManaged, role.SchemeManaged) - // Check a no-op patch succeeds. - _, resp = th.SystemAdminClient.PatchRole(role.Id, patch) - CheckNoError(t, resp) + // Check a no-op patch succeeds. + _, resp = client.PatchRole(role.Id, patch) + CheckNoError(t, resp) - _, resp = th.SystemAdminClient.PatchRole("junk", patch) - CheckBadRequestStatus(t, resp) + _, resp = client.PatchRole("junk", patch) + CheckBadRequestStatus(t, resp) + }) - _, resp = th.Client.PatchRole(model.NewId(), patch) + _, resp := th.Client.PatchRole(model.NewId(), patch) CheckNotFoundStatus(t, resp) _, resp = th.Client.PatchRole(role.Id, patch) @@ -190,8 +207,10 @@ func TestPatchRole(t *testing.T) { Permissions: &[]string{"manage_system", "manage_incoming_webhooks", "manage_outgoing_webhooks"}, } - _, resp = th.SystemAdminClient.PatchRole(role.Id, patch) - CheckNotImplementedStatus(t, resp) + th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) { + _, resp := client.PatchRole(role.Id, patch) + CheckNotImplementedStatus(t, resp) + }) // Add a license. license := model.NewTestLicense() @@ -199,34 +218,36 @@ func TestPatchRole(t *testing.T) { th.App.Srv().SetLicense(license) // Try again, should succeed - received, resp = th.SystemAdminClient.PatchRole(role.Id, patch) - CheckNoError(t, resp) - - assert.Equal(t, received.Id, role.Id) - assert.Equal(t, received.Name, role.Name) - assert.Equal(t, received.DisplayName, role.DisplayName) - assert.Equal(t, received.Description, role.Description) - assert.EqualValues(t, received.Permissions, []string{"manage_system", "manage_incoming_webhooks", "manage_outgoing_webhooks"}) - assert.Equal(t, received.SchemeManaged, role.SchemeManaged) - - t.Run("Check guest permissions editing without E20 license", func(t *testing.T) { - license := model.NewTestLicense() - license.Features.GuestAccountsPermissions = model.NewBool(false) - th.App.Srv().SetLicense(license) - - guestRole, err := th.App.Srv().Store.Role().GetByName("system_guest") - require.Nil(t, err) - received, resp = th.SystemAdminClient.PatchRole(guestRole.Id, patch) - CheckNotImplementedStatus(t, resp) - }) - - t.Run("Check guest permissions editing with E20 license", func(t *testing.T) { - license := model.NewTestLicense() - license.Features.GuestAccountsPermissions = model.NewBool(true) - th.App.Srv().SetLicense(license) - guestRole, err := th.App.Srv().Store.Role().GetByName("system_guest") - require.Nil(t, err) - _, resp = th.SystemAdminClient.PatchRole(guestRole.Id, patch) + th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) { + received, resp := client.PatchRole(role.Id, patch) CheckNoError(t, resp) + + assert.Equal(t, received.Id, role.Id) + assert.Equal(t, received.Name, role.Name) + assert.Equal(t, received.DisplayName, role.DisplayName) + assert.Equal(t, received.Description, role.Description) + assert.EqualValues(t, received.Permissions, []string{"manage_system", "manage_incoming_webhooks", "manage_outgoing_webhooks"}) + assert.Equal(t, received.SchemeManaged, role.SchemeManaged) + + t.Run("Check guest permissions editing without E20 license", func(t *testing.T) { + license := model.NewTestLicense() + license.Features.GuestAccountsPermissions = model.NewBool(false) + th.App.Srv().SetLicense(license) + + guestRole, err := th.App.Srv().Store.Role().GetByName("system_guest") + require.Nil(t, err) + received, resp = client.PatchRole(guestRole.Id, patch) + CheckNotImplementedStatus(t, resp) + }) + + t.Run("Check guest permissions editing with E20 license", func(t *testing.T) { + license := model.NewTestLicense() + license.Features.GuestAccountsPermissions = model.NewBool(true) + th.App.Srv().SetLicense(license) + guestRole, err := th.App.Srv().Store.Role().GetByName("system_guest") + require.Nil(t, err) + _, resp = client.PatchRole(guestRole.Id, patch) + CheckNoError(t, resp) + }) }) }