github.com/haalcala/mattermost-server-change-repo@v0.0.0-20210713015153-16753fbeee5f/plugin/helpers_plugin_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package plugin_test 5 6 import ( 7 "io/ioutil" 8 "net/http" 9 "net/http/httptest" 10 "path/filepath" 11 "testing" 12 13 "github.com/stretchr/testify/assert" 14 "github.com/stretchr/testify/require" 15 16 "github.com/mattermost/mattermost-server/v5/model" 17 "github.com/mattermost/mattermost-server/v5/plugin" 18 "github.com/mattermost/mattermost-server/v5/plugin/plugintest" 19 "github.com/mattermost/mattermost-server/v5/plugin/plugintest/mock" 20 "github.com/mattermost/mattermost-server/v5/utils/fileutils" 21 ) 22 23 func TestInstallPluginFromURL(t *testing.T) { 24 replace := true 25 26 t.Run("incompatible server version", func(t *testing.T) { 27 h := &plugin.HelpersImpl{} 28 api := &plugintest.API{} 29 api.On("GetServerVersion").Return("5.1.0") 30 h.API = api 31 32 _, err := h.InstallPluginFromURL("", true) 33 34 assert.Error(t, err) 35 assert.Equal(t, "incompatible server version for plugin, minimum required version: 5.18.0, current version: 5.1.0", err.Error()) 36 }) 37 38 t.Run("error while parsing the download url", func(t *testing.T) { 39 h := &plugin.HelpersImpl{} 40 api := &plugintest.API{} 41 api.On("GetServerVersion").Return("5.19.0") 42 h.API = api 43 _, err := h.InstallPluginFromURL("http://%41:8080/", replace) 44 45 assert.Error(t, err) 46 assert.Equal(t, "error while parsing url: parse \"http://%41:8080/\": invalid URL escape \"%41\"", err.Error()) 47 }) 48 49 t.Run("errors out while downloading file", func(t *testing.T) { 50 h := &plugin.HelpersImpl{} 51 api := &plugintest.API{} 52 api.On("GetServerVersion").Return("5.19.0") 53 h.API = api 54 testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { 55 res.WriteHeader(http.StatusInternalServerError) 56 })) 57 defer testServer.Close() 58 url := testServer.URL 59 60 _, err := h.InstallPluginFromURL(url, replace) 61 62 assert.Error(t, err) 63 assert.Equal(t, "received 500 status code while downloading plugin from server", err.Error()) 64 }) 65 66 t.Run("downloads the file successfully", func(t *testing.T) { 67 h := &plugin.HelpersImpl{} 68 api := &plugintest.API{} 69 api.On("GetServerVersion").Return("5.19.0") 70 h.API = api 71 path, _ := fileutils.FindDir("tests") 72 tarData, err := ioutil.ReadFile(filepath.Join(path, "testplugin.tar.gz")) 73 require.NoError(t, err) 74 expectedManifest := &model.Manifest{Id: "testplugin"} 75 api.On("InstallPlugin", mock.Anything, false).Return(expectedManifest, nil) 76 77 testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { 78 res.WriteHeader(http.StatusOK) 79 _, _ = res.Write(tarData) 80 })) 81 defer testServer.Close() 82 url := testServer.URL 83 84 manifest, err := h.InstallPluginFromURL(url, false) 85 86 assert.NoError(t, err) 87 assert.Equal(t, "testplugin", manifest.Id) 88 }) 89 90 t.Run("the url pointing to server is incorrect", func(t *testing.T) { 91 h := &plugin.HelpersImpl{} 92 api := &plugintest.API{} 93 api.On("GetServerVersion").Return("5.19.0") 94 h.API = api 95 testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { 96 res.WriteHeader(http.StatusNotFound) 97 })) 98 defer testServer.Close() 99 url := testServer.URL 100 101 _, err := h.InstallPluginFromURL(url, false) 102 103 assert.Error(t, err) 104 assert.Equal(t, "received 404 status code while downloading plugin from server", err.Error()) 105 }) 106 } 107 108 func TestGetPluginAssetURL(t *testing.T) { 109 siteURL := "https://mattermost.example.com" 110 api := &plugintest.API{} 111 api.On("GetConfig").Return(&model.Config{ServiceSettings: model.ServiceSettings{SiteURL: &siteURL}}) 112 113 p := &plugin.HelpersImpl{API: api} 114 115 t.Run("Valid asset directory was provided", func(t *testing.T) { 116 pluginID := "mattermost-1234" 117 dir := "assets" 118 wantedURL := "https://mattermost.example.com/mattermost-1234/assets" 119 gotURL, err := p.GetPluginAssetURL(pluginID, dir) 120 121 assert.Equalf(t, wantedURL, gotURL, "GetPluginAssetURL(%q, %q) got=%q; want=%v", pluginID, dir, gotURL, wantedURL) 122 assert.NoError(t, err) 123 }) 124 125 t.Run("Valid asset directory path was provided", func(t *testing.T) { 126 pluginID := "mattermost-1234" 127 dirPath := "/mattermost/assets" 128 wantedURL := "https://mattermost.example.com/mattermost-1234/mattermost/assets" 129 gotURL, err := p.GetPluginAssetURL(pluginID, dirPath) 130 131 assert.Equalf(t, wantedURL, gotURL, "GetPluginAssetURL(%q, %q) got=%q; want=%q", pluginID, dirPath, gotURL, wantedURL) 132 assert.NoError(t, err) 133 }) 134 135 t.Run("Valid pluginID was provided", func(t *testing.T) { 136 pluginID := "mattermost-1234" 137 dir := "assets" 138 wantedURL := "https://mattermost.example.com/mattermost-1234/assets" 139 gotURL, err := p.GetPluginAssetURL(pluginID, dir) 140 141 assert.Equalf(t, wantedURL, gotURL, "GetPluginAssetURL(%q, %q) got=%q; want=%q", pluginID, dir, gotURL, wantedURL) 142 assert.NoError(t, err) 143 }) 144 145 t.Run("Invalid asset directory name was provided", func(t *testing.T) { 146 pluginID := "mattermost-1234" 147 dir := "" 148 want := "" 149 gotURL, err := p.GetPluginAssetURL(pluginID, dir) 150 151 assert.Emptyf(t, gotURL, "GetPluginAssetURL(%q, %q) got=%s; want=%q", pluginID, dir, gotURL, want) 152 assert.Error(t, err) 153 }) 154 155 t.Run("Invalid pluginID was provided", func(t *testing.T) { 156 pluginID := "" 157 dir := "assets" 158 want := "" 159 gotURL, err := p.GetPluginAssetURL(pluginID, dir) 160 161 assert.Emptyf(t, gotURL, "GetPluginAssetURL(%q, %q) got=%q; want=%q", pluginID, dir, gotURL, want) 162 assert.Error(t, err) 163 }) 164 165 siteURL = "" 166 api.On("GetConfig").Return(&model.Config{ServiceSettings: model.ServiceSettings{SiteURL: &siteURL}}) 167 168 t.Run("Empty SiteURL was configured", func(t *testing.T) { 169 pluginID := "mattermost-1234" 170 dir := "assets" 171 want := "" 172 gotURL, err := p.GetPluginAssetURL(pluginID, dir) 173 174 assert.Emptyf(t, gotURL, "GetPluginAssetURL(%q, %q) got=%q; want=%q", pluginID, dir, gotURL, want) 175 assert.Error(t, err) 176 }) 177 }