2021-09-16 21:31:02 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mattermost/focalboard/server/model"
|
2021-11-09 15:56:04 +01:00
|
|
|
"github.com/mattermost/focalboard/server/server"
|
|
|
|
"github.com/mattermost/focalboard/server/services/config"
|
2021-09-16 21:31:02 +02:00
|
|
|
"github.com/mattermost/focalboard/server/ws"
|
2021-11-09 15:56:04 +01:00
|
|
|
serverModel "github.com/mattermost/mattermost-server/v6/model"
|
2021-09-16 21:31:02 +02:00
|
|
|
"github.com/mattermost/mattermost-server/v6/plugin/plugintest"
|
2021-11-09 15:56:04 +01:00
|
|
|
|
2021-09-16 21:31:02 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2021-11-09 15:56:04 +01:00
|
|
|
type TestHelper struct {
|
|
|
|
Server *server.Server
|
|
|
|
}
|
2021-09-16 21:31:02 +02:00
|
|
|
|
2021-11-09 15:56:04 +01:00
|
|
|
func SetupTestHelper() *TestHelper {
|
|
|
|
th := &TestHelper{}
|
|
|
|
th.Server = newTestServer()
|
|
|
|
return th
|
|
|
|
}
|
2021-09-16 21:31:02 +02:00
|
|
|
|
2021-11-09 15:56:04 +01:00
|
|
|
func newTestServer() *server.Server {
|
|
|
|
srv, err := server.New(server.Params{
|
|
|
|
Cfg: &config.Configuration{},
|
2021-09-16 21:31:02 +02:00
|
|
|
})
|
2021-11-09 15:56:04 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-09-16 21:31:02 +02:00
|
|
|
|
2021-11-09 15:56:04 +01:00
|
|
|
return srv
|
|
|
|
}
|
2021-09-16 21:31:02 +02:00
|
|
|
|
2021-11-09 15:56:04 +01:00
|
|
|
func TestConfigurationNullConfiguration(t *testing.T) {
|
|
|
|
plugin := &Plugin{}
|
|
|
|
assert.NotNil(t, plugin.getConfiguration())
|
2021-09-16 21:31:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestOnConfigurationChange(t *testing.T) {
|
2021-11-09 15:56:04 +01:00
|
|
|
stringRef := ""
|
2021-09-16 21:31:02 +02:00
|
|
|
|
2021-11-09 15:56:04 +01:00
|
|
|
basePlugins := make(map[string]map[string]interface{})
|
|
|
|
basePlugins[pluginName] = make(map[string]interface{})
|
|
|
|
basePlugins[pluginName][sharedBoardsName] = true
|
2021-09-16 21:31:02 +02:00
|
|
|
|
2021-11-09 15:56:04 +01:00
|
|
|
baseFeatureFlags := &serverModel.FeatureFlags{
|
|
|
|
BoardsFeatureFlags: "Feature1-Feature2",
|
|
|
|
}
|
|
|
|
basePluginSettings := &serverModel.PluginSettings{
|
|
|
|
Directory: &stringRef,
|
|
|
|
Plugins: basePlugins,
|
|
|
|
}
|
|
|
|
|
|
|
|
baseConfig := &serverModel.Config{
|
|
|
|
FeatureFlags: baseFeatureFlags,
|
|
|
|
PluginSettings: *basePluginSettings,
|
|
|
|
}
|
2021-09-16 21:31:02 +02:00
|
|
|
|
|
|
|
t.Run("Test Load Plugin Success", func(t *testing.T) {
|
2021-11-09 15:56:04 +01:00
|
|
|
th := SetupTestHelper()
|
2021-09-16 21:31:02 +02:00
|
|
|
api := &plugintest.API{}
|
2021-11-09 15:56:04 +01:00
|
|
|
api.On("GetUnsanitizedConfig").Return(baseConfig)
|
2021-09-16 21:31:02 +02:00
|
|
|
|
|
|
|
p := Plugin{}
|
|
|
|
p.SetAPI(api)
|
2021-11-09 15:56:04 +01:00
|
|
|
p.server = th.Server
|
2021-09-16 21:31:02 +02:00
|
|
|
p.wsPluginAdapter = &FakePluginAdapter{}
|
|
|
|
|
|
|
|
err := p.OnConfigurationChange()
|
2021-11-09 15:56:04 +01:00
|
|
|
assert.NoError(t, err)
|
2021-09-16 21:31:02 +02:00
|
|
|
assert.Equal(t, 1, count)
|
2021-11-09 15:56:04 +01:00
|
|
|
|
|
|
|
// make sure both App and Server got updated
|
|
|
|
assert.True(t, p.server.Config().EnablePublicSharedBoards)
|
|
|
|
assert.True(t, p.server.App().GetClientConfig().EnablePublicSharedBoards)
|
|
|
|
|
|
|
|
assert.Equal(t, "true", p.server.Config().FeatureFlags["Feature1"])
|
|
|
|
assert.Equal(t, "true", p.server.Config().FeatureFlags["Feature2"])
|
|
|
|
assert.Equal(t, "", p.server.Config().FeatureFlags["Feature3"])
|
2021-09-16 21:31:02 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var count = 0
|
|
|
|
|
|
|
|
type FakePluginAdapter struct {
|
|
|
|
ws.PluginAdapter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *FakePluginAdapter) BroadcastConfigChange(clientConfig model.ClientConfig) {
|
|
|
|
count++
|
|
|
|
}
|