github.com/spreadshirt/mattermost-server@v5.3.2-0.20180927191755-a257d501df3d+incompatible/app/plugin_api_test.go (about) 1 // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package app 5 6 import ( 7 "encoding/json" 8 "io/ioutil" 9 "os" 10 "path/filepath" 11 "testing" 12 13 "github.com/mattermost/mattermost-server/model" 14 "github.com/mattermost/mattermost-server/plugin" 15 "github.com/stretchr/testify/assert" 16 "github.com/stretchr/testify/require" 17 ) 18 19 func setupPluginApiTest(t *testing.T, pluginCode string, pluginManifest string, pluginId string, app *App) { 20 pluginDir, err := ioutil.TempDir("", "") 21 require.NoError(t, err) 22 webappPluginDir, err := ioutil.TempDir("", "") 23 require.NoError(t, err) 24 defer os.RemoveAll(pluginDir) 25 defer os.RemoveAll(webappPluginDir) 26 27 env, err := plugin.NewEnvironment(app.NewPluginAPI, pluginDir, webappPluginDir, app.Log) 28 require.NoError(t, err) 29 30 backend := filepath.Join(pluginDir, pluginId, "backend.exe") 31 compileGo(t, pluginCode, backend) 32 33 ioutil.WriteFile(filepath.Join(pluginDir, pluginId, "plugin.json"), []byte(pluginManifest), 0600) 34 env.Activate(pluginId) 35 36 app.Plugins = env 37 } 38 39 func TestPluginAPIUpdateUserStatus(t *testing.T) { 40 th := Setup().InitBasic() 41 defer th.TearDown() 42 api := th.SetupPluginAPI() 43 44 statuses := []string{model.STATUS_ONLINE, model.STATUS_AWAY, model.STATUS_DND, model.STATUS_OFFLINE} 45 46 for _, s := range statuses { 47 status, err := api.UpdateUserStatus(th.BasicUser.Id, s) 48 require.Nil(t, err) 49 require.NotNil(t, status) 50 assert.Equal(t, s, status.Status) 51 } 52 53 status, err := api.UpdateUserStatus(th.BasicUser.Id, "notrealstatus") 54 assert.NotNil(t, err) 55 assert.Nil(t, status) 56 } 57 58 func TestPluginAPILoadPluginConfiguration(t *testing.T) { 59 th := Setup().InitBasic() 60 defer th.TearDown() 61 62 var pluginJson map[string]interface{} 63 if err := json.Unmarshal([]byte(`{"mystringsetting": "str", "MyIntSetting": 32, "myboolsetting": true}`), &pluginJson); err != nil { 64 t.Fatal(err) 65 } 66 th.App.UpdateConfig(func(cfg *model.Config) { 67 cfg.PluginSettings.Plugins["testloadpluginconfig"] = pluginJson 68 }) 69 setupPluginApiTest(t, 70 ` 71 package main 72 73 import ( 74 "github.com/mattermost/mattermost-server/plugin" 75 "github.com/mattermost/mattermost-server/model" 76 "fmt" 77 ) 78 79 type MyPlugin struct { 80 plugin.MattermostPlugin 81 82 MyStringSetting string 83 MyIntSetting int 84 MyBoolSetting bool 85 } 86 87 func (p *MyPlugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) { 88 return nil, fmt.Sprintf("%v%v%v", p.MyStringSetting, p.MyIntSetting, p.MyBoolSetting) 89 } 90 91 func main() { 92 plugin.ClientMain(&MyPlugin{}) 93 } 94 `, 95 `{"id": "testloadpluginconfig", "backend": {"executable": "backend.exe"}, "settings_schema": { 96 "settings": [ 97 { 98 "key": "MyStringSetting", 99 "type": "text" 100 }, 101 { 102 "key": "MyIntSetting", 103 "type": "text" 104 }, 105 { 106 "key": "MyBoolSetting", 107 "type": "bool" 108 } 109 ] 110 }}`, "testloadpluginconfig", th.App) 111 hooks, err := th.App.Plugins.HooksForPlugin("testloadpluginconfig") 112 assert.NoError(t, err) 113 _, ret := hooks.MessageWillBePosted(nil, nil) 114 assert.Equal(t, "str32true", ret) 115 } 116 117 func TestPluginAPILoadPluginConfigurationDefaults(t *testing.T) { 118 th := Setup().InitBasic() 119 defer th.TearDown() 120 121 var pluginJson map[string]interface{} 122 if err := json.Unmarshal([]byte(`{"mystringsetting": "override"}`), &pluginJson); err != nil { 123 t.Fatal(err) 124 } 125 th.App.UpdateConfig(func(cfg *model.Config) { 126 cfg.PluginSettings.Plugins["testloadpluginconfig"] = pluginJson 127 }) 128 setupPluginApiTest(t, 129 ` 130 package main 131 132 import ( 133 "github.com/mattermost/mattermost-server/plugin" 134 "github.com/mattermost/mattermost-server/model" 135 "fmt" 136 ) 137 138 type MyPlugin struct { 139 plugin.MattermostPlugin 140 141 MyStringSetting string 142 MyIntSetting int 143 MyBoolSetting bool 144 } 145 146 func (p *MyPlugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) { 147 return nil, fmt.Sprintf("%v%v%v", p.MyStringSetting, p.MyIntSetting, p.MyBoolSetting) 148 } 149 150 func main() { 151 plugin.ClientMain(&MyPlugin{}) 152 } 153 `, 154 `{"id": "testloadpluginconfig", "backend": {"executable": "backend.exe"}, "settings_schema": { 155 "settings": [ 156 { 157 "key": "MyStringSetting", 158 "type": "text", 159 "default": "notthis" 160 }, 161 { 162 "key": "MyIntSetting", 163 "type": "text", 164 "default": 35 165 }, 166 { 167 "key": "MyBoolSetting", 168 "type": "bool", 169 "default": true 170 } 171 ] 172 }}`, "testloadpluginconfig", th.App) 173 hooks, err := th.App.Plugins.HooksForPlugin("testloadpluginconfig") 174 assert.NoError(t, err) 175 _, ret := hooks.MessageWillBePosted(nil, nil) 176 assert.Equal(t, "override35true", ret) 177 }