github.com/xzl8028/xenia-server@v0.0.0-20190809101854-18450a97da63/api4/plugin_test.go (about) 1 // Copyright (c) 2017-present Xenia, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package api4 5 6 import ( 7 "bytes" 8 "encoding/json" 9 "io/ioutil" 10 "net/http" 11 "net/http/httptest" 12 "os" 13 "path/filepath" 14 "testing" 15 16 "github.com/xzl8028/xenia-server/model" 17 "github.com/xzl8028/xenia-server/utils/fileutils" 18 "github.com/stretchr/testify/assert" 19 ) 20 21 func TestPlugin(t *testing.T) { 22 th := Setup().InitBasic() 23 defer th.TearDown() 24 25 statesJson, _ := json.Marshal(th.App.Config().PluginSettings.PluginStates) 26 states := map[string]*model.PluginState{} 27 json.Unmarshal(statesJson, &states) 28 th.App.UpdateConfig(func(cfg *model.Config) { 29 *cfg.PluginSettings.Enable = true 30 *cfg.PluginSettings.EnableUploads = true 31 *cfg.PluginSettings.AllowInsecureDownloadUrl = true 32 }) 33 34 path, _ := fileutils.FindDir("tests") 35 tarData, err := ioutil.ReadFile(filepath.Join(path, "testplugin.tar.gz")) 36 if err != nil { 37 t.Fatal(err) 38 } 39 40 // Install from URL 41 testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { 42 res.WriteHeader(http.StatusOK) 43 res.Write(tarData) 44 })) 45 defer func() { testServer.Close() }() 46 47 url := testServer.URL 48 49 manifest, resp := th.SystemAdminClient.InstallPluginFromUrl(url, false) 50 CheckNoError(t, resp) 51 assert.Equal(t, "testplugin", manifest.Id) 52 53 _, resp = th.SystemAdminClient.InstallPluginFromUrl(url, false) 54 CheckBadRequestStatus(t, resp) 55 56 manifest, resp = th.SystemAdminClient.InstallPluginFromUrl(url, true) 57 CheckNoError(t, resp) 58 assert.Equal(t, "testplugin", manifest.Id) 59 60 th.App.RemovePlugin(manifest.Id) 61 62 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.PluginSettings.Enable = false }) 63 64 _, resp = th.SystemAdminClient.InstallPluginFromUrl(url, false) 65 CheckNotImplementedStatus(t, resp) 66 67 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.PluginSettings.Enable = true }) 68 69 _, resp = th.Client.InstallPluginFromUrl(url, false) 70 CheckForbiddenStatus(t, resp) 71 72 _, resp = th.SystemAdminClient.InstallPluginFromUrl("http://nodata", false) 73 CheckBadRequestStatus(t, resp) 74 75 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.PluginSettings.AllowInsecureDownloadUrl = false }) 76 77 _, resp = th.SystemAdminClient.InstallPluginFromUrl(url, false) 78 CheckBadRequestStatus(t, resp) 79 80 // Successful upload 81 manifest, resp = th.SystemAdminClient.UploadPlugin(bytes.NewReader(tarData)) 82 CheckNoError(t, resp) 83 84 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.PluginSettings.EnableUploads = true }) 85 86 manifest, resp = th.SystemAdminClient.UploadPluginForced(bytes.NewReader(tarData)) 87 defer os.RemoveAll("plugins/testplugin") 88 CheckNoError(t, resp) 89 90 assert.Equal(t, "testplugin", manifest.Id) 91 92 // Upload error cases 93 _, resp = th.SystemAdminClient.UploadPlugin(bytes.NewReader([]byte("badfile"))) 94 CheckBadRequestStatus(t, resp) 95 96 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.PluginSettings.Enable = false }) 97 _, resp = th.SystemAdminClient.UploadPlugin(bytes.NewReader(tarData)) 98 CheckNotImplementedStatus(t, resp) 99 100 th.App.UpdateConfig(func(cfg *model.Config) { 101 *cfg.PluginSettings.Enable = true 102 *cfg.PluginSettings.EnableUploads = false 103 }) 104 _, resp = th.SystemAdminClient.UploadPlugin(bytes.NewReader(tarData)) 105 CheckNotImplementedStatus(t, resp) 106 107 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.PluginSettings.EnableUploads = true }) 108 _, resp = th.Client.UploadPlugin(bytes.NewReader(tarData)) 109 CheckForbiddenStatus(t, resp) 110 111 // Successful gets 112 pluginsResp, resp := th.SystemAdminClient.GetPlugins() 113 CheckNoError(t, resp) 114 115 found := false 116 for _, m := range pluginsResp.Inactive { 117 if m.Id == manifest.Id { 118 found = true 119 } 120 } 121 122 assert.True(t, found) 123 124 found = false 125 for _, m := range pluginsResp.Active { 126 if m.Id == manifest.Id { 127 found = true 128 } 129 } 130 131 assert.False(t, found) 132 133 // Successful activate 134 ok, resp := th.SystemAdminClient.EnablePlugin(manifest.Id) 135 CheckNoError(t, resp) 136 assert.True(t, ok) 137 138 pluginsResp, resp = th.SystemAdminClient.GetPlugins() 139 CheckNoError(t, resp) 140 141 found = false 142 for _, m := range pluginsResp.Active { 143 if m.Id == manifest.Id { 144 found = true 145 } 146 } 147 148 assert.True(t, found) 149 150 // Activate error case 151 ok, resp = th.SystemAdminClient.EnablePlugin("junk") 152 CheckBadRequestStatus(t, resp) 153 assert.False(t, ok) 154 155 ok, resp = th.SystemAdminClient.EnablePlugin("JUNK") 156 CheckBadRequestStatus(t, resp) 157 assert.False(t, ok) 158 159 // Successful deactivate 160 ok, resp = th.SystemAdminClient.DisablePlugin(manifest.Id) 161 CheckNoError(t, resp) 162 assert.True(t, ok) 163 164 pluginsResp, resp = th.SystemAdminClient.GetPlugins() 165 CheckNoError(t, resp) 166 167 found = false 168 for _, m := range pluginsResp.Inactive { 169 if m.Id == manifest.Id { 170 found = true 171 } 172 } 173 174 assert.True(t, found) 175 176 // Deactivate error case 177 ok, resp = th.SystemAdminClient.DisablePlugin("junk") 178 CheckBadRequestStatus(t, resp) 179 assert.False(t, ok) 180 181 // Get error cases 182 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.PluginSettings.Enable = false }) 183 _, resp = th.SystemAdminClient.GetPlugins() 184 CheckNotImplementedStatus(t, resp) 185 186 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.PluginSettings.Enable = true }) 187 _, resp = th.Client.GetPlugins() 188 CheckForbiddenStatus(t, resp) 189 190 // Successful webapp get 191 _, resp = th.SystemAdminClient.EnablePlugin(manifest.Id) 192 CheckNoError(t, resp) 193 194 manifests, resp := th.Client.GetWebappPlugins() 195 CheckNoError(t, resp) 196 197 found = false 198 for _, m := range manifests { 199 if m.Id == manifest.Id { 200 found = true 201 } 202 } 203 204 assert.True(t, found) 205 206 // Successful remove 207 ok, resp = th.SystemAdminClient.RemovePlugin(manifest.Id) 208 CheckNoError(t, resp) 209 assert.True(t, ok) 210 211 // Remove error cases 212 ok, resp = th.SystemAdminClient.RemovePlugin(manifest.Id) 213 CheckBadRequestStatus(t, resp) 214 assert.False(t, ok) 215 216 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.PluginSettings.Enable = false }) 217 _, resp = th.SystemAdminClient.RemovePlugin(manifest.Id) 218 CheckNotImplementedStatus(t, resp) 219 220 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.PluginSettings.Enable = true }) 221 _, resp = th.Client.RemovePlugin(manifest.Id) 222 CheckForbiddenStatus(t, resp) 223 224 _, resp = th.SystemAdminClient.RemovePlugin("bad.id") 225 CheckBadRequestStatus(t, resp) 226 }