api4/role: add role endpoints to local mode (#14876)

Этот коммит содержится в:
Ibrahim Serdar Acikgoz
2020-06-23 12:56:52 +03:00
коммит произвёл GitHub
родитель 516ceaed8e
Коммит c6bc7fed6d
3 изменённых файлов: 121 добавлений и 86 удалений

Просмотреть файл

@@ -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))

11
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")
}

Просмотреть файл

@@ -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)
})
})
}