rclone/fs/rc/webgui/rc_test.go
Eng Zer Jun 8cf76f5e11 test: use T.TempDir to create temporary test directory
The directory created by `T.TempDir` is automatically removed when the
test and all its subtests complete.

Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2022-02-01 11:47:04 +00:00

157 lines
3.7 KiB
Go

package webgui
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
"github.com/rclone/rclone/fs/rc"
"github.com/rclone/rclone/fs/rc/rcflags"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const testPluginName = "rclone-test-plugin"
const testPluginAuthor = "rclone"
const testPluginKey = testPluginAuthor + "/" + testPluginName
const testPluginURL = "https://github.com/" + testPluginAuthor + "/" + testPluginName + "/"
func init() {
rcflags.Opt.WebUI = true
}
func setCacheDir(t *testing.T) string {
cacheDir := t.TempDir()
PluginsPath = filepath.Join(cacheDir, "plugins")
pluginsConfigPath = filepath.Join(cacheDir, "config")
loadedPlugins = newPlugins(availablePluginsJSONPath)
err := loadedPlugins.readFromFile()
assert.Nil(t, err)
return cacheDir
}
func cleanCacheDir(t *testing.T, cacheDir string) {
_ = os.RemoveAll(cacheDir)
}
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)
if err != nil && strings.Contains(err.Error(), "bad HTTP status") {
t.Skipf("skipping test as plugin download failed: %v", err)
}
require.Nil(t, err)
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) {
cacheDir := setCacheDir(t)
defer cleanCacheDir(t, cacheDir)
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) {
cacheDir := setCacheDir(t)
defer cleanCacheDir(t, cacheDir)
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) {
cacheDir := setCacheDir(t)
defer cleanCacheDir(t, cacheDir)
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)
}