Files
mostlymatter/server/public/plugin/environment_test.go
Jesse Hallam bb02b35048 Expose public/ API as submodule (#23345)
* model -> public/model

* plugin -> public/plugin

* public/model/utils -> public/utils

* platform/shared/mlog -> public/shared/mlog

* platform/shared/i18n -> public/shared/i18n

* platform/shared/markdown -> public/shared/markdown

* platform/services/timezones -> public/shared/timezones

* channels/einterfaces -> einterfaces

* expose public/ submodule

* go mod tidy

* .github: cache-dependency-path, setup-go-work

* modules-tidy for public/ too

* remove old gomodtidy
2023-05-10 13:07:02 -03:00

99 строки
2.6 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package plugin
import (
"encoding/json"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost-server/server/public/model"
"github.com/mattermost/mattermost-server/server/public/shared/mlog"
)
func TestAvailablePlugins(t *testing.T) {
dir, err1 := os.MkdirTemp("", "mm-plugin-test")
require.NoError(t, err1)
t.Cleanup(func() {
os.RemoveAll(dir)
})
testLogger, _ := mlog.NewLogger()
env := Environment{
pluginDir: dir,
logger: testLogger,
}
t.Run("Should be able to load available plugins", func(t *testing.T) {
bundle1 := model.BundleInfo{
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")
manifestJSON, jsonErr := json.Marshal(bundle1.Manifest)
require.NoError(t, jsonErr)
err = os.WriteFile(path, manifestJSON, 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 = os.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)
})
t.Run("Should not load bundles on blocklist", func(t *testing.T) {
bundle := model.BundleInfo{
Manifest: &model.Manifest{
Id: "playbooks",
Version: "1",
},
}
err := os.Mkdir(filepath.Join(dir, "plugin4"), 0700)
require.NoError(t, err)
defer os.RemoveAll(filepath.Join(dir, "plugin4"))
path := filepath.Join(dir, "plugin4", "plugin.json")
manifestJSON, jsonErr := json.Marshal(bundle.Manifest)
require.NoError(t, jsonErr)
err = os.WriteFile(path, manifestJSON, 0644)
require.NoError(t, err)
bundles, err := env.Available()
require.NoError(t, err)
require.Len(t, bundles, 0)
})
}