github.com/gigforks/mattermost-server@v4.9.1-0.20180619094218-800d97fa55d0+incompatible/model/manifest_test.go (about) 1 // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package model 5 6 import ( 7 "encoding/json" 8 "io/ioutil" 9 "os" 10 "path/filepath" 11 "strings" 12 "testing" 13 14 "gopkg.in/yaml.v2" 15 16 "github.com/stretchr/testify/assert" 17 "github.com/stretchr/testify/require" 18 ) 19 20 func TestFindManifest(t *testing.T) { 21 for _, tc := range []struct { 22 Filename string 23 Contents string 24 ExpectError bool 25 ExpectNotExist bool 26 }{ 27 {"foo", "bar", true, true}, 28 {"plugin.json", "bar", true, false}, 29 {"plugin.json", `{"id": "foo"}`, false, false}, 30 {"plugin.yaml", `id: foo`, false, false}, 31 {"plugin.yaml", "bar", true, false}, 32 {"plugin.yml", `id: foo`, false, false}, 33 {"plugin.yml", "bar", true, false}, 34 } { 35 dir, err := ioutil.TempDir("", "mm-plugin-test") 36 require.NoError(t, err) 37 defer os.RemoveAll(dir) 38 39 path := filepath.Join(dir, tc.Filename) 40 f, err := os.Create(path) 41 require.NoError(t, err) 42 _, err = f.WriteString(tc.Contents) 43 f.Close() 44 require.NoError(t, err) 45 46 m, mpath, err := FindManifest(dir) 47 assert.True(t, (err != nil) == tc.ExpectError, tc.Filename) 48 assert.True(t, (err != nil && os.IsNotExist(err)) == tc.ExpectNotExist, tc.Filename) 49 if !tc.ExpectNotExist { 50 assert.Equal(t, path, mpath, tc.Filename) 51 } else { 52 assert.Empty(t, mpath, tc.Filename) 53 } 54 if !tc.ExpectError { 55 require.NotNil(t, m, tc.Filename) 56 assert.NotEmpty(t, m.Id, tc.Filename) 57 } 58 } 59 } 60 61 func TestManifestUnmarshal(t *testing.T) { 62 expected := Manifest{ 63 Id: "theid", 64 Backend: &ManifestBackend{ 65 Executable: "theexecutable", 66 }, 67 Webapp: &ManifestWebapp{ 68 BundlePath: "thebundlepath", 69 }, 70 SettingsSchema: &PluginSettingsSchema{ 71 Header: "theheadertext", 72 Footer: "thefootertext", 73 Settings: []*PluginSetting{ 74 &PluginSetting{ 75 Key: "thesetting", 76 DisplayName: "thedisplayname", 77 Type: "dropdown", 78 HelpText: "thehelptext", 79 RegenerateHelpText: "theregeneratehelptext", 80 Placeholder: "theplaceholder", 81 Options: []*PluginOption{ 82 &PluginOption{ 83 DisplayName: "theoptiondisplayname", 84 Value: "thevalue", 85 }, 86 }, 87 Default: "thedefault", 88 }, 89 }, 90 }, 91 } 92 93 var yamlResult Manifest 94 require.NoError(t, yaml.Unmarshal([]byte(` 95 id: theid 96 backend: 97 executable: theexecutable 98 webapp: 99 bundle_path: thebundlepath 100 settings_schema: 101 header: theheadertext 102 footer: thefootertext 103 settings: 104 - key: thesetting 105 display_name: thedisplayname 106 type: dropdown 107 help_text: thehelptext 108 regenerate_help_text: theregeneratehelptext 109 placeholder: theplaceholder 110 options: 111 - display_name: theoptiondisplayname 112 value: thevalue 113 default: thedefault 114 `), &yamlResult)) 115 assert.Equal(t, expected, yamlResult) 116 117 var jsonResult Manifest 118 require.NoError(t, json.Unmarshal([]byte(`{ 119 "id": "theid", 120 "backend": { 121 "executable": "theexecutable" 122 }, 123 "webapp": { 124 "bundle_path": "thebundlepath" 125 }, 126 "settings_schema": { 127 "header": "theheadertext", 128 "footer": "thefootertext", 129 "settings": [ 130 { 131 "key": "thesetting", 132 "display_name": "thedisplayname", 133 "type": "dropdown", 134 "help_text": "thehelptext", 135 "regenerate_help_text": "theregeneratehelptext", 136 "placeholder": "theplaceholder", 137 "options": [ 138 { 139 "display_name": "theoptiondisplayname", 140 "value": "thevalue" 141 } 142 ], 143 "default": "thedefault" 144 } 145 ] 146 } 147 }`), &jsonResult)) 148 assert.Equal(t, expected, jsonResult) 149 } 150 151 func TestFindManifest_FileErrors(t *testing.T) { 152 for _, tc := range []string{"plugin.yaml", "plugin.json"} { 153 dir, err := ioutil.TempDir("", "mm-plugin-test") 154 require.NoError(t, err) 155 defer os.RemoveAll(dir) 156 157 path := filepath.Join(dir, tc) 158 require.NoError(t, os.Mkdir(path, 0700)) 159 160 m, mpath, err := FindManifest(dir) 161 assert.Nil(t, m) 162 assert.Equal(t, path, mpath) 163 assert.Error(t, err, tc) 164 assert.False(t, os.IsNotExist(err), tc) 165 } 166 } 167 168 func TestManifestJson(t *testing.T) { 169 manifest := &Manifest{ 170 Id: "theid", 171 Backend: &ManifestBackend{ 172 Executable: "theexecutable", 173 }, 174 Webapp: &ManifestWebapp{ 175 BundlePath: "thebundlepath", 176 }, 177 SettingsSchema: &PluginSettingsSchema{ 178 Header: "theheadertext", 179 Footer: "thefootertext", 180 Settings: []*PluginSetting{ 181 &PluginSetting{ 182 Key: "thesetting", 183 DisplayName: "thedisplayname", 184 Type: "dropdown", 185 HelpText: "thehelptext", 186 RegenerateHelpText: "theregeneratehelptext", 187 Placeholder: "theplaceholder", 188 Options: []*PluginOption{ 189 &PluginOption{ 190 DisplayName: "theoptiondisplayname", 191 Value: "thevalue", 192 }, 193 }, 194 Default: "thedefault", 195 }, 196 }, 197 }, 198 } 199 200 json := manifest.ToJson() 201 newManifest := ManifestFromJson(strings.NewReader(json)) 202 assert.Equal(t, newManifest, manifest) 203 assert.Equal(t, newManifest.ToJson(), json) 204 assert.Equal(t, ManifestFromJson(strings.NewReader("junk")), (*Manifest)(nil)) 205 206 manifestList := []*Manifest{manifest} 207 json = ManifestListToJson(manifestList) 208 newManifestList := ManifestListFromJson(strings.NewReader(json)) 209 assert.Equal(t, newManifestList, manifestList) 210 assert.Equal(t, ManifestListToJson(newManifestList), json) 211 } 212 213 func TestManifestHasClient(t *testing.T) { 214 manifest := &Manifest{ 215 Id: "theid", 216 Backend: &ManifestBackend{ 217 Executable: "theexecutable", 218 }, 219 Webapp: &ManifestWebapp{ 220 BundlePath: "thebundlepath", 221 }, 222 } 223 224 assert.True(t, manifest.HasClient()) 225 226 manifest.Webapp = nil 227 assert.False(t, manifest.HasClient()) 228 } 229 230 func TestManifestClientManifest(t *testing.T) { 231 manifest := &Manifest{ 232 Id: "theid", 233 Name: "thename", 234 Description: "thedescription", 235 Version: "0.0.1", 236 Backend: &ManifestBackend{ 237 Executable: "theexecutable", 238 }, 239 Webapp: &ManifestWebapp{ 240 BundlePath: "thebundlepath", 241 }, 242 SettingsSchema: &PluginSettingsSchema{ 243 Header: "theheadertext", 244 Footer: "thefootertext", 245 Settings: []*PluginSetting{ 246 &PluginSetting{ 247 Key: "thesetting", 248 DisplayName: "thedisplayname", 249 Type: "dropdown", 250 HelpText: "thehelptext", 251 RegenerateHelpText: "theregeneratehelptext", 252 Placeholder: "theplaceholder", 253 Options: []*PluginOption{ 254 &PluginOption{ 255 DisplayName: "theoptiondisplayname", 256 Value: "thevalue", 257 }, 258 }, 259 Default: "thedefault", 260 }, 261 }, 262 }, 263 } 264 265 sanitized := manifest.ClientManifest() 266 267 assert.NotEmpty(t, sanitized.Id) 268 assert.NotEmpty(t, sanitized.Version) 269 assert.NotEmpty(t, sanitized.Webapp) 270 assert.NotEmpty(t, sanitized.SettingsSchema) 271 assert.Empty(t, sanitized.Name) 272 assert.Empty(t, sanitized.Description) 273 assert.Empty(t, sanitized.Backend) 274 275 assert.NotEmpty(t, manifest.Id) 276 assert.NotEmpty(t, manifest.Version) 277 assert.NotEmpty(t, manifest.Webapp) 278 assert.NotEmpty(t, manifest.Name) 279 assert.NotEmpty(t, manifest.Description) 280 assert.NotEmpty(t, manifest.Backend) 281 assert.NotEmpty(t, manifest.SettingsSchema) 282 }