WebDAV: Improve service discovery tests #3579

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer 2023-08-15 13:01:50 +02:00
parent a287830d1f
commit 8ed9f3f409
2 changed files with 50 additions and 25 deletions

View file

@ -8,6 +8,7 @@ import (
"github.com/photoprism/photoprism/pkg/txt"
)
// Account represents remote service details.
type Account struct {
AccName string
AccURL string
@ -17,6 +18,7 @@ type Account struct {
AccPass string
}
// Discover performs a service lookup based on the URL and credentials provided and returns an Account if successful.
func Discover(rawUrl, user, pass string) (result Account, err error) {
if rawUrl == "" {
return result, errors.New("service URL is empty")

View file

@ -7,7 +7,27 @@ import (
)
func TestDiscover(t *testing.T) {
t.Run("webdav", func(t *testing.T) {
t.Run("Empty", func(t *testing.T) {
r, err := Discover("", "", "")
assert.Equal(t, err.Error(), "service URL is empty")
assert.Equal(t, "", r.AccName)
assert.Equal(t, "", r.AccType)
assert.Equal(t, "", r.AccURL)
assert.Equal(t, "", r.AccUser)
assert.Equal(t, "", r.AccPass)
})
t.Run("Invalid", func(t *testing.T) {
r, err := Discover("xxx", "", "")
assert.Equal(t, err.Error(), "could not connect")
assert.Equal(t, "", r.AccName)
assert.Equal(t, "", r.AccType)
assert.Equal(t, "", r.AccURL)
assert.Equal(t, "", r.AccUser)
assert.Equal(t, "", r.AccPass)
})
t.Run("WebDAV", func(t *testing.T) {
r, err := Discover("http://admin:photoprism@dummy-webdav/", "", "")
if err != nil {
@ -20,8 +40,33 @@ func TestDiscover(t *testing.T) {
assert.Equal(t, "admin", r.AccUser)
assert.Equal(t, "photoprism", r.AccPass)
})
t.Run("WebDAVWithPort", func(t *testing.T) {
r, err := Discover("http://admin:photoprism@dummy-webdav:80/", "", "")
t.Run("webdav password", func(t *testing.T) {
if err != nil {
t.Fatal(err)
}
assert.Equal(t, "Dummy-Webdav", r.AccName)
assert.Equal(t, "webdav", r.AccType)
assert.Equal(t, "http://dummy-webdav:80/", r.AccURL)
assert.Equal(t, "admin", r.AccUser)
assert.Equal(t, "photoprism", r.AccPass)
})
t.Run("WebDAVWithPath", func(t *testing.T) {
r, err := Discover("http://dummy-webdav:80/Photos/", "admin", "photoprism")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, "Dummy-Webdav", r.AccName)
assert.Equal(t, "webdav", r.AccType)
assert.Equal(t, "http://dummy-webdav:80/Photos/", r.AccURL)
assert.Equal(t, "admin", r.AccUser)
assert.Equal(t, "photoprism", r.AccPass)
})
t.Run("WebDAVNoPassword", func(t *testing.T) {
r, err := Discover("http://admin@dummy-webdav/", "", "photoprism")
if err != nil {
@ -34,8 +79,7 @@ func TestDiscover(t *testing.T) {
assert.Equal(t, "admin", r.AccUser)
assert.Equal(t, "photoprism", r.AccPass)
})
t.Run("facebook", func(t *testing.T) {
t.Run("Facebook", func(t *testing.T) {
r, err := Discover("https://www.facebook.com/terms", "test", "")
if err != nil {
@ -48,25 +92,4 @@ func TestDiscover(t *testing.T) {
assert.Equal(t, "test", r.AccUser)
assert.Equal(t, "", r.AccPass)
})
t.Run("empty raw url", func(t *testing.T) {
r, err := Discover("", "", "")
assert.Equal(t, err.Error(), "service URL is empty")
assert.Equal(t, "", r.AccName)
assert.Equal(t, "", r.AccType)
assert.Equal(t, "", r.AccURL)
assert.Equal(t, "", r.AccUser)
assert.Equal(t, "", r.AccPass)
})
t.Run("invalid raw url", func(t *testing.T) {
r, err := Discover("xxx", "", "")
assert.Equal(t, err.Error(), "could not connect")
assert.Equal(t, "", r.AccName)
assert.Equal(t, "", r.AccType)
assert.Equal(t, "", r.AccURL)
assert.Equal(t, "", r.AccUser)
assert.Equal(t, "", r.AccPass)
})
}