gonextcloud/webdav_impl_test.go

111 lines
1.9 KiB
Go
Raw Normal View History

package gonextcloud
import (
"os"
"path/filepath"
2019-07-20 21:37:16 +00:00
"sort"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
2019-07-20 21:37:16 +00:00
dir string
wd WebDav
folders = []string{
2019-07-20 21:37:16 +00:00
"folder1",
"folder1/sub1",
"folder1/sub1/ssub1",
"folder1/sub2",
"folder1/sub2/ssub1",
"folder1/sub2/ssub2",
"folder2",
"folder2/sub2",
"folder2/sub3",
"folder2/sub3/ssub1",
"folder2/sub4",
}
wtests = []struct {
name string
test test
}{
{
name: "CreateFolder",
test: testCreateFolder,
},
2019-07-20 21:37:16 +00:00
{
name: "TestCreateSubFolders",
test: testCreateSubFolders,
},
{
name: "TestStat",
test: testStat,
},
{
name: "TestWalk",
test: testWalk,
},
{
name: "TestDelete",
test: testDelete,
},
}
)
func TestWebDav(t *testing.T) {
if err := initClient(); err != nil {
t.Fatal(err)
}
if config.NotExistingFolder == "" {
config.NotExistingFolder = "/not-existing-folder"
}
dir = config.NotExistingFolder
wd = c.WebDav()
for _, tt := range wtests {
t.Run(tt.name, tt.test)
}
}
2019-07-20 21:37:16 +00:00
func testCreateFolder(t *testing.T) {
err := wd.Mkdir(dir, 0777)
require.NoError(t, err)
}
func testStat(t *testing.T) {
i, err := wd.Stat(dir)
require.NoError(t, err)
assert.Equal(t, dir, i.Name())
assert.True(t, i.IsDir())
}
2019-07-20 21:37:16 +00:00
func testCreateSubFolders(t *testing.T) {
sort.Strings(folders)
d := strings.TrimRight(dir, "/")
var ds []string
ds = append(ds, d)
for _, f := range folders {
p := d + "/" + f
err := wd.MkdirAll(p, 0777)
assert.NoError(t, err)
ds = append(ds, p)
}
folders = ds
}
func testWalk(t *testing.T) {
2019-07-20 21:37:16 +00:00
err := wd.Walk(dir, func(path string, info os.FileInfo, err error) error {
assert.NoError(t, err)
assert.Equal(t, filepath.Base(path), info.Name())
2019-07-20 21:37:16 +00:00
assert.Contains(t, folders, path)
return nil
})
assert.NoError(t, err)
}
func testDelete(t *testing.T) {
err := wd.Remove(dir)
require.NoError(t, err)
}