@@ -11,6 +11,8 @@ import (
11
11
"image/color"
12
12
"image/png"
13
13
"io/ioutil"
14
+ "net/http"
15
+ "net/http/httptest"
14
16
"os"
15
17
"path/filepath"
16
18
"strings"
@@ -731,6 +733,128 @@ func TestPluginAPIInstallPlugin(t *testing.T) {
731
733
assert .True (t , found )
732
734
}
733
735
736
+ func TestInstallPlugin (t * testing.T ) {
737
+ // TODO(ilgooz): remove this setup func to use existent setupPluginApiTest().
738
+ // following setupTest() func is a modified version of setupPluginApiTest().
739
+ // we need a modified version of setupPluginApiTest() because it wasn't possible to use it directly here
740
+ // since it removes plugin dirs right after it returns, does not update App configs with the plugin
741
+ // dirs and this behavior tends to break this test as a result.
742
+ setupTest := func (t * testing.T , pluginCode string , pluginManifest string , pluginID string , app * App ) (func (), string ) {
743
+ pluginDir , err := ioutil .TempDir ("" , "" )
744
+ require .NoError (t , err )
745
+ webappPluginDir , err := ioutil .TempDir ("" , "" )
746
+ require .NoError (t , err )
747
+
748
+ app .UpdateConfig (func (cfg * model.Config ) {
749
+ * cfg .PluginSettings .Directory = pluginDir
750
+ * cfg .PluginSettings .ClientDirectory = webappPluginDir
751
+ })
752
+
753
+ env , err := plugin .NewEnvironment (app .NewPluginAPI , pluginDir , webappPluginDir , app .Log )
754
+ require .NoError (t , err )
755
+
756
+ app .SetPluginsEnvironment (env )
757
+
758
+ backend := filepath .Join (pluginDir , pluginID , "backend.exe" )
759
+ utils .CompileGo (t , pluginCode , backend )
760
+
761
+ ioutil .WriteFile (filepath .Join (pluginDir , pluginID , "plugin.json" ), []byte (pluginManifest ), 0600 )
762
+ manifest , activated , reterr := env .Activate (pluginID )
763
+ require .Nil (t , reterr )
764
+ require .NotNil (t , manifest )
765
+ require .True (t , activated )
766
+
767
+ return func () {
768
+ os .RemoveAll (pluginDir )
769
+ os .RemoveAll (webappPluginDir )
770
+ }, pluginDir
771
+ }
772
+
773
+ th := Setup (t ).InitBasic ()
774
+ defer th .TearDown ()
775
+
776
+ // start an http server to serve plugin's tarball to the test.
777
+ path , _ := fileutils .FindDir ("tests" )
778
+ ts := httptest .NewServer (http .FileServer (http .Dir (path )))
779
+ defer ts .Close ()
780
+
781
+ th .App .UpdateConfig (func (cfg * model.Config ) {
782
+ * cfg .PluginSettings .Enable = true
783
+ * cfg .PluginSettings .EnableUploads = true
784
+ cfg .PluginSettings .Plugins ["testinstallplugin" ] = map [string ]interface {}{
785
+ "DownloadURL" : ts .URL + "/testplugin.tar.gz" ,
786
+ }
787
+ })
788
+
789
+ tearDown , _ := setupTest (t ,
790
+ `
791
+ package main
792
+
793
+ import (
794
+ "net/http"
795
+
796
+ "github.com/pkg/errors"
797
+
798
+ "github.com/mattermost/mattermost-server/v5/plugin"
799
+ )
800
+
801
+ type configuration struct {
802
+ DownloadURL string
803
+ }
804
+
805
+ type Plugin struct {
806
+ plugin.MattermostPlugin
807
+
808
+ configuration configuration
809
+ }
810
+
811
+ func (p *Plugin) OnConfigurationChange() error {
812
+ if err := p.API.LoadPluginConfiguration(&p.configuration); err != nil {
813
+ return err
814
+ }
815
+ return nil
816
+ }
817
+
818
+ func (p *Plugin) OnActivate() error {
819
+ resp, err := http.Get(p.configuration.DownloadURL)
820
+ if err != nil {
821
+ return err
822
+ }
823
+ defer resp.Body.Close()
824
+ _, aerr := p.API.InstallPlugin(resp.Body, true)
825
+ if aerr != nil {
826
+ return errors.Wrap(aerr, "cannot install plugin")
827
+ }
828
+ return nil
829
+ }
830
+
831
+ func main() {
832
+ plugin.ClientMain(&Plugin{})
833
+ }
834
+
835
+ ` ,
836
+ `{"id": "testinstallplugin", "backend": {"executable": "backend.exe"}, "settings_schema": {
837
+ "settings": [
838
+ {
839
+ "key": "DownloadURL",
840
+ "type": "text"
841
+ }
842
+ ]
843
+ }}` , "testinstallplugin" , th .App )
844
+ defer tearDown ()
845
+
846
+ hooks , err := th .App .GetPluginsEnvironment ().HooksForPlugin ("testinstallplugin" )
847
+ require .NoError (t , err )
848
+
849
+ err = hooks .OnActivate ()
850
+ require .NoError (t , err )
851
+
852
+ plugins , aerr := th .App .GetPlugins ()
853
+ require .Nil (t , aerr )
854
+ require .Len (t , plugins .Inactive , 1 )
855
+ require .Equal (t , "testplugin" , plugins .Inactive [0 ].Id )
856
+ }
857
+
734
858
func TestPluginAPIGetTeamIcon (t * testing.T ) {
735
859
th := Setup (t ).InitBasic ()
736
860
defer th .TearDown ()
0 commit comments