2020-08-20 20:01:38 +02:00
|
|
|
package webgui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"path/filepath"
|
2020-08-31 13:38:46 +02:00
|
|
|
"strings"
|
2020-08-20 20:01:38 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/rclone/rclone/fs/rc"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-08-31 13:38:46 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-08-20 20:01:38 +02:00
|
|
|
)
|
|
|
|
|
2020-08-30 01:05:37 +02:00
|
|
|
const testPluginName = "rclone-test-plugin"
|
2020-08-20 20:01:38 +02:00
|
|
|
const testPluginAuthor = "rclone"
|
|
|
|
const testPluginKey = testPluginAuthor + "/" + testPluginName
|
|
|
|
const testPluginURL = "https://github.com/" + testPluginAuthor + "/" + testPluginName + "/"
|
|
|
|
|
2020-09-17 21:06:51 +02:00
|
|
|
func init() {
|
2024-07-04 11:47:05 +02:00
|
|
|
rc.Opt.WebUI = true
|
2020-09-17 21:06:51 +02:00
|
|
|
}
|
|
|
|
|
2022-12-08 13:43:53 +01:00
|
|
|
func setCacheDir(t *testing.T) {
|
2022-01-29 17:24:56 +01:00
|
|
|
cacheDir := t.TempDir()
|
2020-08-20 20:01:38 +02:00
|
|
|
PluginsPath = filepath.Join(cacheDir, "plugins")
|
|
|
|
pluginsConfigPath = filepath.Join(cacheDir, "config")
|
|
|
|
|
|
|
|
loadedPlugins = newPlugins(availablePluginsJSONPath)
|
2022-01-29 17:24:56 +01:00
|
|
|
err := loadedPlugins.readFromFile()
|
2020-08-20 20:01:38 +02:00
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func addPlugin(t *testing.T) {
|
|
|
|
addPlugin := rc.Calls.Get("pluginsctl/addPlugin")
|
|
|
|
assert.NotNil(t, addPlugin)
|
|
|
|
in := rc.Params{
|
|
|
|
"url": testPluginURL,
|
|
|
|
}
|
|
|
|
out, err := addPlugin.Fn(context.Background(), in)
|
2020-08-31 13:38:46 +02:00
|
|
|
if err != nil && strings.Contains(err.Error(), "bad HTTP status") {
|
|
|
|
t.Skipf("skipping test as plugin download failed: %v", err)
|
|
|
|
}
|
|
|
|
require.Nil(t, err)
|
2020-08-20 20:01:38 +02:00
|
|
|
assert.Nil(t, out)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func removePlugin(t *testing.T) {
|
|
|
|
addPlugin := rc.Calls.Get("pluginsctl/removePlugin")
|
|
|
|
assert.NotNil(t, addPlugin)
|
|
|
|
|
|
|
|
in := rc.Params{
|
|
|
|
"name": testPluginKey,
|
|
|
|
}
|
|
|
|
out, err := addPlugin.Fn(context.Background(), in)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.Nil(t, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
//func TestListTestPlugins(t *testing.T) {
|
|
|
|
// addPlugin := rc.Calls.Get("pluginsctl/listTestPlugins")
|
|
|
|
// assert.NotNil(t, addPlugin)
|
|
|
|
// in := rc.Params{}
|
|
|
|
// out, err := addPlugin.Fn(context.Background(), in)
|
|
|
|
// assert.Nil(t, err)
|
|
|
|
// expected := rc.Params{
|
|
|
|
// "loadedTestPlugins": map[string]PackageJSON{},
|
|
|
|
// }
|
|
|
|
// assert.Equal(t, expected, out)
|
|
|
|
//}
|
|
|
|
|
|
|
|
//func TestRemoveTestPlugin(t *testing.T) {
|
|
|
|
// addPlugin := rc.Calls.Get("pluginsctl/removeTestPlugin")
|
|
|
|
// assert.NotNil(t, addPlugin)
|
|
|
|
// in := rc.Params{
|
|
|
|
// "name": "",
|
|
|
|
// }
|
|
|
|
// out, err := addPlugin.Fn(context.Background(), in)
|
|
|
|
// assert.NotNil(t, err)
|
|
|
|
// assert.Nil(t, out)
|
|
|
|
//}
|
|
|
|
|
|
|
|
func TestAddPlugin(t *testing.T) {
|
2022-12-08 13:43:53 +01:00
|
|
|
setCacheDir(t)
|
2020-08-20 20:01:38 +02:00
|
|
|
|
|
|
|
addPlugin(t)
|
|
|
|
_, ok := loadedPlugins.LoadedPlugins[testPluginKey]
|
|
|
|
assert.True(t, ok)
|
|
|
|
|
|
|
|
//removePlugin(t)
|
|
|
|
//_, ok = loadedPlugins.LoadedPlugins[testPluginKey]
|
|
|
|
//assert.False(t, ok)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListPlugins(t *testing.T) {
|
2022-12-08 13:43:53 +01:00
|
|
|
setCacheDir(t)
|
2020-08-20 20:01:38 +02:00
|
|
|
|
|
|
|
addPlugin := rc.Calls.Get("pluginsctl/listPlugins")
|
|
|
|
assert.NotNil(t, addPlugin)
|
|
|
|
in := rc.Params{}
|
|
|
|
out, err := addPlugin.Fn(context.Background(), in)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
expected := rc.Params{
|
|
|
|
"loadedPlugins": map[string]PackageJSON{},
|
|
|
|
"loadedTestPlugins": map[string]PackageJSON{},
|
|
|
|
}
|
|
|
|
assert.Equal(t, expected, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemovePlugin(t *testing.T) {
|
2022-12-08 13:43:53 +01:00
|
|
|
setCacheDir(t)
|
2020-08-20 20:01:38 +02:00
|
|
|
|
|
|
|
addPlugin(t)
|
|
|
|
removePluginCall := rc.Calls.Get("pluginsctl/removePlugin")
|
|
|
|
assert.NotNil(t, removePlugin)
|
|
|
|
|
|
|
|
in := rc.Params{
|
|
|
|
"name": testPluginKey,
|
|
|
|
}
|
|
|
|
out, err := removePluginCall.Fn(context.Background(), in)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Nil(t, out)
|
|
|
|
removePlugin(t)
|
|
|
|
assert.Equal(t, len(loadedPlugins.LoadedPlugins), 0)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPluginsForType(t *testing.T) {
|
|
|
|
addPlugin := rc.Calls.Get("pluginsctl/getPluginsForType")
|
|
|
|
assert.NotNil(t, addPlugin)
|
|
|
|
in := rc.Params{
|
|
|
|
"type": "",
|
|
|
|
"pluginType": "FileHandler",
|
|
|
|
}
|
|
|
|
out, err := addPlugin.Fn(context.Background(), in)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotNil(t, out)
|
|
|
|
|
|
|
|
in = rc.Params{
|
|
|
|
"type": "video/mp4",
|
|
|
|
"pluginType": "",
|
|
|
|
}
|
|
|
|
_, err = addPlugin.Fn(context.Background(), in)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotNil(t, out)
|
|
|
|
}
|