photoprism/internal/share/webdav/webdav_test.go
Michael Mayer b86f68c3f3 Backend: Add WebDAV client and dummy test server #225
Signed-off-by: Michael Mayer <michael@liquidbytes.net>
2020-03-27 11:01:41 +01:00

115 lines
2 KiB
Go

package webdav
import (
"os"
"testing"
"github.com/photoprism/photoprism/pkg/fs"
"github.com/photoprism/photoprism/pkg/rnd"
"github.com/stretchr/testify/assert"
)
const (
testUrl = "http://webdav-dummy/"
testUser = "admin"
testPass = "photoprism"
)
func TestConnect(t *testing.T) {
c := Connect(testUrl, testUser, testPass)
assert.IsType(t, Client{}, c)
}
func TestClient_Files(t *testing.T) {
c := Connect(testUrl, testUser, testPass)
assert.IsType(t, Client{}, c)
files, err := c.Files("Photos")
if err != nil {
t.Fatal(err)
}
if len(files) == 0 {
t.Fatal("no files found")
}
}
func TestClient_Download(t *testing.T) {
c := Connect(testUrl, testUser, testPass)
assert.IsType(t, Client{}, c)
files, err := c.Files("Photos")
if err != nil {
t.Fatal(err)
}
tempDir := os.TempDir() + rnd.UUID()
tempFile := tempDir + "/foo.jpg"
if len(files) == 0 {
t.Fatal("no files to download")
}
if err := c.Download(files[0], tempFile); err != nil {
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) {
c := Connect(testUrl, testUser, testPass)
assert.IsType(t, Client{}, c)
t.Run("non-recursive", func(t *testing.T) {
tempDir := os.TempDir() + rnd.UUID()
if errs := c.DownloadDir("Photos", tempDir, false); len(errs) > 0 {
t.Fatal(errs)
}
if err := os.RemoveAll(tempDir); err != nil {
t.Fatal(err)
}
})
t.Run("recursive", func(t *testing.T) {
tempDir := os.TempDir() + rnd.UUID()
if errs := c.DownloadDir("Photos", tempDir, true); len(errs) > 0 {
t.Fatal(errs)
}
if err := os.RemoveAll(tempDir); err != nil {
t.Fatal(err)
}
})
}
func TestClient_UploadAndDelete(t *testing.T) {
c := Connect(testUrl, testUser, testPass)
assert.IsType(t, Client{}, c)
tempName := rnd.UUID() + ".jpg"
if err := c.Upload("testdata/example.jpg", tempName); err != nil {
t.Fatal(err)
}
if err := c.Delete(tempName); err != nil {
t.Fatal(err)
}
}