From 1bf4373f3f558906b8cfc74f693eb21123a12388 Mon Sep 17 00:00:00 2001 From: Ibrahim Serdar Acikgoz Date: Wed, 7 Oct 2020 10:23:59 +0300 Subject: [PATCH] [MM-28885] app/plugin: check if plugin manifest has errors (#15630) * app/plugin: dont sync plugins with errors * reflect review comments * update test * Update plugin/environment_test.go Co-authored-by: Jesse Hallam * reflect review comments * reflect review comments Co-authored-by: Jesse Hallam --- plugin/environment.go | 3 +- plugin/environment_test.go | 71 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 plugin/environment_test.go diff --git a/plugin/environment.go b/plugin/environment.go index e0f968f9cf..ff80117ffb 100644 --- a/plugin/environment.go +++ b/plugin/environment.go @@ -86,7 +86,8 @@ func scanSearchPath(path string) ([]*model.BundleInfo, error) { if !file.IsDir() || file.Name()[0] == '.' { continue } - if info := model.BundleInfoForPath(filepath.Join(path, file.Name())); info.ManifestPath != "" { + info := model.BundleInfoForPath(filepath.Join(path, file.Name())) + if info.Manifest != nil { ret = append(ret, info) } } diff --git a/plugin/environment_test.go b/plugin/environment_test.go new file mode 100644 index 0000000000..3b996dc52c --- /dev/null +++ b/plugin/environment_test.go @@ -0,0 +1,71 @@ +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. +// See LICENSE.txt for license information. + +package plugin + +import ( + "io/ioutil" + "os" + "path/filepath" + "testing" + + "github.com/mattermost/mattermost-server/v5/model" + "github.com/stretchr/testify/require" +) + +func TestAvaliablePlugins(t *testing.T) { + dir, err1 := ioutil.TempDir("", "mm-plugin-test") + require.NoError(t, err1) + t.Cleanup(func() { + os.RemoveAll(dir) + }) + + env := Environment{ + pluginDir: dir, + } + + t.Run("Should be able to load available plugins", func(t *testing.T) { + bundle1 := model.BundleInfo{ + ManifestPath: "", + Manifest: &model.Manifest{ + Id: "someid", + Version: "1", + }, + } + err := os.Mkdir(filepath.Join(dir, "plugin1"), 0700) + require.NoError(t, err) + defer os.RemoveAll(filepath.Join(dir, "plugin1")) + + path := filepath.Join(dir, "plugin1", "plugin.json") + err = ioutil.WriteFile(path, []byte(bundle1.Manifest.ToJson()), 0644) + require.NoError(t, err) + + bundles, err := env.Available() + require.NoError(t, err) + require.Len(t, bundles, 1) + }) + + t.Run("Should not be able to load plugins without a valid manifest file", func(t *testing.T) { + err := os.Mkdir(filepath.Join(dir, "plugin2"), 0700) + require.NoError(t, err) + defer os.RemoveAll(filepath.Join(dir, "plugin2")) + + path := filepath.Join(dir, "plugin2", "manifest.json") + err = ioutil.WriteFile(path, []byte("{}"), 0644) + require.NoError(t, err) + + bundles, err := env.Available() + require.NoError(t, err) + require.Len(t, bundles, 0) + }) + + t.Run("Should not be able to load plugins without a manifest file", func(t *testing.T) { + err := os.Mkdir(filepath.Join(dir, "plugin3"), 0700) + require.NoError(t, err) + defer os.RemoveAll(filepath.Join(dir, "plugin3")) + + bundles, err := env.Available() + require.NoError(t, err) + require.Len(t, bundles, 0) + }) +}