Gh 10166 (#13021)
* model/config: add required server config field to manifest * plugin/helpers: add check required server configuration method * plugin/helpers: code polish * model/manifest: add documentation * Update plugin/helpers_config.go Co-Authored-By: Ben Schumacher <ben.schumacher@mattermost.com> * plugin/helpers_config: remove stagnant line * plugin/helpers: update to v5 * plugin/helpers_config: add license Co-authored-by: Ben Schumacher <ben.schumacher@mattermost.com> Co-authored-by: Jesse Hallam <jesse.hallam@gmail.com> Co-authored-by: mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
коммит произвёл
Jesse Hallam
родитель
45565cb81f
Коммит
d8ac09b302
@@ -58,6 +58,12 @@ type Helpers interface {
|
||||
// Minimum server version: 5.6
|
||||
KVSetWithExpiryJSON(key string, value interface{}, expireInSeconds int64) error
|
||||
|
||||
// CheckRequiredServerConfiguration checks if the server is configured according to
|
||||
// plugin requirements.
|
||||
//
|
||||
// Minimum server version: 5.2
|
||||
CheckRequiredServerConfiguration(req *model.Config) (bool, error)
|
||||
|
||||
// ShouldProcessMessage returns if the message should be processed by a message hook.
|
||||
//
|
||||
// Use this method to avoid processing unnecessary messages in a MessageHasBeenPosted
|
||||
|
||||
32
plugin/helpers_config.go
Обычный файл
32
plugin/helpers_config.go
Обычный файл
@@ -0,0 +1,32 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package plugin
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v5/model"
|
||||
"github.com/mattermost/mattermost-server/v5/utils"
|
||||
)
|
||||
|
||||
// CheckRequiredServerConfiguration implements Helpers.CheckRequiredServerConfiguration
|
||||
func (p *HelpersImpl) CheckRequiredServerConfiguration(req *model.Config) (bool, error) {
|
||||
if req == nil {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
cfg := p.API.GetConfig()
|
||||
|
||||
mc, err := utils.Merge(req, cfg, nil)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "could not merge configurations")
|
||||
}
|
||||
|
||||
mergedCfg := mc.(model.Config)
|
||||
if mergedCfg.ToJson() != cfg.ToJson() {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
82
plugin/helpers_config_test.go
Обычный файл
82
plugin/helpers_config_test.go
Обычный файл
@@ -0,0 +1,82 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package plugin_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v5/model"
|
||||
"github.com/mattermost/mattermost-server/v5/plugin"
|
||||
"github.com/mattermost/mattermost-server/v5/plugin/plugintest"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCheckRequiredServerConfiguration(t *testing.T) {
|
||||
for name, test := range map[string]struct {
|
||||
SetupAPI func(*plugintest.API) *plugintest.API
|
||||
Input *model.Config
|
||||
ShouldReturn bool
|
||||
ShouldError bool
|
||||
}{
|
||||
"no required config therefore it should be compatible": {
|
||||
SetupAPI: func(api *plugintest.API) *plugintest.API {
|
||||
return api
|
||||
},
|
||||
Input: nil,
|
||||
ShouldReturn: true,
|
||||
ShouldError: false,
|
||||
},
|
||||
"same configurations": {
|
||||
SetupAPI: func(api *plugintest.API) *plugintest.API {
|
||||
api.On("GetConfig").Return(&model.Config{
|
||||
ServiceSettings: model.ServiceSettings{
|
||||
EnableCommands: model.NewBool(true),
|
||||
},
|
||||
})
|
||||
|
||||
return api
|
||||
},
|
||||
Input: &model.Config{
|
||||
ServiceSettings: model.ServiceSettings{
|
||||
EnableCommands: model.NewBool(true),
|
||||
},
|
||||
},
|
||||
ShouldReturn: true,
|
||||
ShouldError: false,
|
||||
},
|
||||
"different configurations": {
|
||||
SetupAPI: func(api *plugintest.API) *plugintest.API {
|
||||
api.On("GetConfig").Return(&model.Config{})
|
||||
|
||||
return api
|
||||
},
|
||||
Input: &model.Config{
|
||||
ServiceSettings: model.ServiceSettings{
|
||||
EnableCommands: model.NewBool(true),
|
||||
},
|
||||
},
|
||||
ShouldReturn: false,
|
||||
ShouldError: false,
|
||||
},
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
api := test.SetupAPI(&plugintest.API{})
|
||||
defer api.AssertExpectations(t)
|
||||
|
||||
p := &plugin.HelpersImpl{}
|
||||
p.API = api
|
||||
|
||||
ok, err := p.CheckRequiredServerConfiguration(test.Input)
|
||||
|
||||
if !ok {
|
||||
assert.False(t, ok)
|
||||
}
|
||||
if test.ShouldError {
|
||||
assert.NotNil(t, err)
|
||||
} else {
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,27 @@ type Helpers struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
// CheckRequiredServerConfiguration provides a mock function with given fields: req
|
||||
func (_m *Helpers) CheckRequiredServerConfiguration(req *model.Config) (bool, error) {
|
||||
ret := _m.Called(req)
|
||||
|
||||
var r0 bool
|
||||
if rf, ok := ret.Get(0).(func(*model.Config) bool); ok {
|
||||
r0 = rf(req)
|
||||
} else {
|
||||
r0 = ret.Get(0).(bool)
|
||||
}
|
||||
|
||||
var r1 error
|
||||
if rf, ok := ret.Get(1).(func(*model.Config) error); ok {
|
||||
r1 = rf(req)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// EnsureBot provides a mock function with given fields: bot, options
|
||||
func (_m *Helpers) EnsureBot(bot *model.Bot, options ...plugin.EnsureBotOption) (string, error) {
|
||||
_va := make([]interface{}, len(options))
|
||||
|
||||
Ссылка в новой задаче
Block a user