github.com/haalcala/mattermost-server-change-repo@v0.0.0-20210713015153-16753fbeee5f/plugin/environment_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package plugin
     5  
     6  import (
     7  	"io/ioutil"
     8  	"os"
     9  	"path/filepath"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/require"
    13  
    14  	"github.com/mattermost/mattermost-server/v5/model"
    15  )
    16  
    17  func TestAvaliablePlugins(t *testing.T) {
    18  	dir, err1 := ioutil.TempDir("", "mm-plugin-test")
    19  	require.NoError(t, err1)
    20  	t.Cleanup(func() {
    21  		os.RemoveAll(dir)
    22  	})
    23  
    24  	env := Environment{
    25  		pluginDir: dir,
    26  	}
    27  
    28  	t.Run("Should be able to load available plugins", func(t *testing.T) {
    29  		bundle1 := model.BundleInfo{
    30  			ManifestPath: "",
    31  			Manifest: &model.Manifest{
    32  				Id:      "someid",
    33  				Version: "1",
    34  			},
    35  		}
    36  		err := os.Mkdir(filepath.Join(dir, "plugin1"), 0700)
    37  		require.NoError(t, err)
    38  		defer os.RemoveAll(filepath.Join(dir, "plugin1"))
    39  
    40  		path := filepath.Join(dir, "plugin1", "plugin.json")
    41  		err = ioutil.WriteFile(path, []byte(bundle1.Manifest.ToJson()), 0644)
    42  		require.NoError(t, err)
    43  
    44  		bundles, err := env.Available()
    45  		require.NoError(t, err)
    46  		require.Len(t, bundles, 1)
    47  	})
    48  
    49  	t.Run("Should not be able to load plugins without a valid manifest file", func(t *testing.T) {
    50  		err := os.Mkdir(filepath.Join(dir, "plugin2"), 0700)
    51  		require.NoError(t, err)
    52  		defer os.RemoveAll(filepath.Join(dir, "plugin2"))
    53  
    54  		path := filepath.Join(dir, "plugin2", "manifest.json")
    55  		err = ioutil.WriteFile(path, []byte("{}"), 0644)
    56  		require.NoError(t, err)
    57  
    58  		bundles, err := env.Available()
    59  		require.NoError(t, err)
    60  		require.Len(t, bundles, 0)
    61  	})
    62  
    63  	t.Run("Should not be able to load plugins without a manifest file", func(t *testing.T) {
    64  		err := os.Mkdir(filepath.Join(dir, "plugin3"), 0700)
    65  		require.NoError(t, err)
    66  		defer os.RemoveAll(filepath.Join(dir, "plugin3"))
    67  
    68  		bundles, err := env.Available()
    69  		require.NoError(t, err)
    70  		require.Len(t, bundles, 0)
    71  	})
    72  }