2020-03-27 11:01:41 +01:00
|
|
|
package webdav
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2020-10-04 11:23:01 +02:00
|
|
|
"path/filepath"
|
2020-03-27 11:01:41 +01:00
|
|
|
"testing"
|
|
|
|
|
2022-03-27 21:37:11 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2020-03-27 11:01:41 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
|
|
|
"github.com/photoprism/photoprism/pkg/rnd"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-11-09 16:55:16 +01:00
|
|
|
testUrl = "http://dummy-webdav/"
|
2020-03-27 11:01:41 +01:00
|
|
|
testUser = "admin"
|
|
|
|
testPass = "photoprism"
|
|
|
|
)
|
|
|
|
|
2023-03-28 20:37:42 +02:00
|
|
|
func TestClientUrl(t *testing.T) {
|
|
|
|
result, err := clientUrl(testUrl, testUser, testPass)
|
2020-03-27 11:01:41 +01:00
|
|
|
|
2023-03-28 20:37:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, "http://admin:photoprism@dummy-webdav/", result.String())
|
2020-03-27 11:01:41 +01:00
|
|
|
}
|
|
|
|
|
2023-03-28 20:37:42 +02:00
|
|
|
func TestNewClient(t *testing.T) {
|
|
|
|
c, err := NewClient(testUrl, testUser, testPass, TimeoutLow)
|
2020-03-27 11:01:41 +01:00
|
|
|
|
2023-03-28 20:37:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.IsType(t, &Client{}, c)
|
|
|
|
}
|
2020-03-27 11:01:41 +01:00
|
|
|
|
2023-03-28 20:37:42 +02:00
|
|
|
func TestClient_Files(t *testing.T) {
|
|
|
|
c, err := NewClient(testUrl, testUser, testPass, TimeoutLow)
|
2020-03-27 11:01:41 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-03-28 20:37:42 +02:00
|
|
|
assert.IsType(t, &Client{}, c)
|
|
|
|
|
|
|
|
t.Run("NonRecursive", func(t *testing.T) {
|
|
|
|
files, err := c.Files("Photos", false)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(files) == 0 {
|
|
|
|
t.Fatal("no files found")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("Recursive", func(t *testing.T) {
|
|
|
|
files, err := c.Files("Photos", true)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(files) == 0 {
|
|
|
|
t.Fatal("no files found")
|
|
|
|
}
|
|
|
|
})
|
2020-03-27 11:01:41 +01:00
|
|
|
}
|
|
|
|
|
2020-03-27 11:56:24 +01:00
|
|
|
func TestClient_Directories(t *testing.T) {
|
2023-03-28 20:37:42 +02:00
|
|
|
c, err := NewClient(testUrl, testUser, testPass, TimeoutLow)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-03-27 11:56:24 +01:00
|
|
|
|
2023-03-28 20:37:42 +02:00
|
|
|
assert.IsType(t, &Client{}, c)
|
2020-03-27 11:56:24 +01:00
|
|
|
|
2023-03-28 20:37:42 +02:00
|
|
|
t.Run("NonRecursive", func(t *testing.T) {
|
2022-03-27 21:37:11 +02:00
|
|
|
dirs, err := c.Directories("", false, MaxRequestDuration)
|
2020-03-27 11:56:24 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(dirs) == 0 {
|
|
|
|
t.Fatal("no directories found")
|
|
|
|
}
|
|
|
|
|
2020-04-02 18:17:07 +02:00
|
|
|
assert.IsType(t, fs.FileInfo{}, dirs[0])
|
|
|
|
assert.Equal(t, "Photos", dirs[0].Name)
|
|
|
|
assert.Equal(t, "/Photos", dirs[0].Abs)
|
|
|
|
assert.Equal(t, true, dirs[0].Dir)
|
|
|
|
assert.Equal(t, int64(0), dirs[0].Size)
|
2020-03-27 11:56:24 +01:00
|
|
|
})
|
2023-03-28 20:37:42 +02:00
|
|
|
t.Run("Recursive", func(t *testing.T) {
|
2022-03-27 21:37:11 +02:00
|
|
|
dirs, err := c.Directories("", true, 0)
|
2020-03-27 11:56:24 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(dirs) < 2 {
|
|
|
|
t.Fatal("at least 2 directories expected")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-03-28 20:37:42 +02:00
|
|
|
func TestClient_UploadAndDelete(t *testing.T) {
|
|
|
|
c, err := NewClient(testUrl, testUser, testPass, TimeoutLow)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.IsType(t, &Client{}, c)
|
|
|
|
|
|
|
|
tempName := rnd.UUID() + fs.ExtJPEG
|
|
|
|
|
|
|
|
if err := c.Upload(fs.Abs("testdata/example.jpg"), tempName); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.Delete(tempName); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 11:01:41 +01:00
|
|
|
func TestClient_Download(t *testing.T) {
|
2023-03-28 20:37:42 +02:00
|
|
|
c, err := NewClient(testUrl, testUser, testPass, TimeoutDefault)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-03-27 11:01:41 +01:00
|
|
|
|
2023-03-28 20:37:42 +02:00
|
|
|
assert.IsType(t, &Client{}, c)
|
2020-03-27 11:01:41 +01:00
|
|
|
|
2023-03-28 20:37:42 +02:00
|
|
|
files, err := c.Files("Photos", false)
|
2020-03-27 11:01:41 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-10-04 11:23:01 +02:00
|
|
|
tempDir := filepath.Join(os.TempDir(), rnd.UUID())
|
2020-03-27 11:01:41 +01:00
|
|
|
tempFile := tempDir + "/foo.jpg"
|
|
|
|
|
|
|
|
if len(files) == 0 {
|
|
|
|
t.Fatal("no files to download")
|
|
|
|
}
|
|
|
|
|
2020-04-06 16:34:29 +02:00
|
|
|
if err := c.Download(files[0].Abs, tempFile, false); err != nil {
|
2020-03-27 11:01:41 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !fs.FileExists(tempFile) {
|
|
|
|
t.Fatalf("%s does not exist", tempFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.RemoveAll(tempDir); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_DownloadDir(t *testing.T) {
|
2023-03-28 20:37:42 +02:00
|
|
|
c, err := NewClient(testUrl, testUser, testPass, TimeoutLow)
|
2020-03-27 11:01:41 +01:00
|
|
|
|
2023-03-28 20:37:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-03-27 11:01:41 +01:00
|
|
|
|
2023-03-28 20:37:42 +02:00
|
|
|
assert.IsType(t, &Client{}, c)
|
|
|
|
|
|
|
|
t.Run("NonRecursive", func(t *testing.T) {
|
2020-10-04 11:23:01 +02:00
|
|
|
tempDir := filepath.Join(os.TempDir(), rnd.UUID())
|
2020-03-27 11:01:41 +01:00
|
|
|
|
2023-03-28 20:37:42 +02:00
|
|
|
if err = os.RemoveAll(tempDir); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-04-06 16:34:29 +02:00
|
|
|
if errs := c.DownloadDir("Photos", tempDir, false, false); len(errs) > 0 {
|
2020-03-27 11:01:41 +01:00
|
|
|
t.Fatal(errs)
|
|
|
|
}
|
|
|
|
|
2023-03-28 20:37:42 +02:00
|
|
|
if err = os.RemoveAll(tempDir); err != nil {
|
2020-03-27 11:01:41 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
2023-03-28 20:37:42 +02:00
|
|
|
t.Run("Recursive", func(t *testing.T) {
|
2020-10-04 11:23:01 +02:00
|
|
|
tempDir := filepath.Join(os.TempDir(), rnd.UUID())
|
2020-03-27 11:01:41 +01:00
|
|
|
|
2023-03-28 20:37:42 +02:00
|
|
|
if err = os.RemoveAll(tempDir); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-04-06 16:34:29 +02:00
|
|
|
if errs := c.DownloadDir("Photos", tempDir, true, false); len(errs) > 0 {
|
2020-03-27 11:01:41 +01:00
|
|
|
t.Fatal(errs)
|
|
|
|
}
|
|
|
|
|
2023-03-28 20:37:42 +02:00
|
|
|
if err = os.RemoveAll(tempDir); err != nil {
|
2020-03-27 11:01:41 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|