[MM-18121] Fix config set panic (#13350)
* Fix config set panic * Add test * Fix golint issues Co-authored-by: mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
коммит произвёл
Jesse Hallam
родитель
5eabfaac67
Коммит
71ceb7f18f
@@ -320,10 +320,27 @@ func UpdateMap(configMap map[string]interface{}, configSettings []string, newVal
|
||||
if len(configSettings) == 1 {
|
||||
return errors.New("unable to set multiple settings at once")
|
||||
}
|
||||
if value.Len() == 0 {
|
||||
return fmt.Errorf(noSettingsNamed, configSettings[1])
|
||||
simpleMap, ok := res.(map[string]interface{})
|
||||
if ok {
|
||||
return UpdateMap(simpleMap, configSettings[1:], newVal)
|
||||
}
|
||||
return UpdateMap(res.(map[string]interface{}), configSettings[1:], newVal)
|
||||
mapOfTheMap, ok := res.(map[string]map[string]interface{})
|
||||
if ok {
|
||||
convertedMap := make(map[string]interface{})
|
||||
for k, v := range mapOfTheMap {
|
||||
convertedMap[k] = v
|
||||
}
|
||||
return UpdateMap(convertedMap, configSettings[1:], newVal)
|
||||
}
|
||||
pluginStateMap, ok := res.(map[string]*model.PluginState)
|
||||
if ok {
|
||||
convertedMap := make(map[string]interface{})
|
||||
for k, v := range pluginStateMap {
|
||||
convertedMap[k] = v
|
||||
}
|
||||
return UpdateMap(convertedMap, configSettings[1:], newVal)
|
||||
}
|
||||
return fmt.Errorf(noSettingsNamed, configSettings[1])
|
||||
|
||||
case reflect.Int:
|
||||
if len(configSettings) == 1 {
|
||||
@@ -372,6 +389,18 @@ func UpdateMap(configMap map[string]interface{}, configSettings []string, newVal
|
||||
}
|
||||
return fmt.Errorf(noSettingsNamed, configSettings[0])
|
||||
|
||||
case reflect.Ptr:
|
||||
state, ok := res.(*model.PluginState)
|
||||
if !ok || len(configSettings) != 2 {
|
||||
return errors.New("type not supported yet")
|
||||
}
|
||||
val, err := strconv.ParseBool(newVal[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
state.Enable = val
|
||||
return nil
|
||||
|
||||
default:
|
||||
return errors.New("type not supported yet")
|
||||
}
|
||||
|
||||
@@ -73,6 +73,14 @@ type TestNewTeamSettings struct {
|
||||
MaxUserPerTeam *int
|
||||
}
|
||||
|
||||
type TestPluginSettings struct {
|
||||
Enable *bool
|
||||
Directory *string `restricted:"true"`
|
||||
Plugins map[string]map[string]interface{}
|
||||
PluginStates map[string]*model.PluginState
|
||||
SignaturePublicKeyFiles []string
|
||||
}
|
||||
|
||||
func TestConfigValidate(t *testing.T) {
|
||||
th := Setup()
|
||||
defer th.TearDown()
|
||||
@@ -518,7 +526,6 @@ func TestUpdateMap(t *testing.T) {
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestConfigMigrate(t *testing.T) {
|
||||
@@ -574,3 +581,65 @@ func contains(configMap map[string]interface{}, v interface{}, configSettings []
|
||||
return value.Interface() == v
|
||||
}
|
||||
}
|
||||
|
||||
func TestPluginConfigs(t *testing.T) {
|
||||
pluginConfig := TestPluginSettings{
|
||||
Enable: model.NewBool(true),
|
||||
Directory: model.NewString("dir"),
|
||||
Plugins: map[string]map[string]interface{}{
|
||||
"antivirus": {
|
||||
"clamavhostport": "localhost:3310",
|
||||
"scantimeoutseconds": 12,
|
||||
},
|
||||
"com.mattermost.demo-plugin": {
|
||||
"channelname": "demo_plugin",
|
||||
"customsetting": "7",
|
||||
"enablementionuser": false,
|
||||
"lastname": "Plugin User",
|
||||
"mentionuser": "demo_plugin",
|
||||
"randomsecret": "random secret",
|
||||
"secretmessage": "Changed value.",
|
||||
"textstyle": "",
|
||||
"username": "demo_plugin",
|
||||
},
|
||||
"com.mattermost.webex": {
|
||||
"sitehost": "praptishrestha.my.webex.com",
|
||||
},
|
||||
"jira": {
|
||||
"enablejiraui": true,
|
||||
"groupsallowedtoeditjirasubscriptions": "",
|
||||
"rolesallowedtoeditjirasubscriptions": "system_admin",
|
||||
"secret": "some secret",
|
||||
},
|
||||
"mattermost-autolink": {
|
||||
"enableadmincommand": false,
|
||||
},
|
||||
},
|
||||
PluginStates: map[string]*model.PluginState{
|
||||
"antivirus": {
|
||||
Enable: false,
|
||||
},
|
||||
"com.github.manland.mattermost-plugin-gitlab": {
|
||||
Enable: true,
|
||||
},
|
||||
},
|
||||
SignaturePublicKeyFiles: []string{"Hello", "World"},
|
||||
}
|
||||
|
||||
configMap := configToMap(pluginConfig)
|
||||
err := UpdateMap(configMap, []string{"Enable"}, []string{"false"})
|
||||
require.Nil(t, err, "Wasn't expecting an error")
|
||||
assert.Equal(t, false, configMap["Enable"].(bool))
|
||||
|
||||
err = UpdateMap(configMap, []string{"Plugins", "antivirus", "clamavhostport"}, []string{"some text"})
|
||||
require.Nil(t, err, "Wasn't expecting an error")
|
||||
assert.Equal(t, "some text", configMap["Plugins"].(map[string]map[string]interface{})["antivirus"]["clamavhostport"].(string))
|
||||
|
||||
err = UpdateMap(configMap, []string{"Plugins", "mattermost-autolink", "enableadmincommand"}, []string{"true"})
|
||||
require.Nil(t, err, "Wasn't expecting an error")
|
||||
assert.Equal(t, true, configMap["Plugins"].(map[string]map[string]interface{})["mattermost-autolink"]["enableadmincommand"].(bool))
|
||||
|
||||
err = UpdateMap(configMap, []string{"PluginStates", "antivirus", "Enable"}, []string{"true"})
|
||||
require.Nil(t, err, "Wasn't expecting an error")
|
||||
assert.Equal(t, true, configMap["PluginStates"].(map[string]*model.PluginState)["antivirus"].Enable)
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user