diff --git a/internal/remote/discover.go b/internal/remote/discover.go index 85a9d833d..dda3c3a5e 100644 --- a/internal/remote/discover.go +++ b/internal/remote/discover.go @@ -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") diff --git a/internal/remote/discover_test.go b/internal/remote/discover_test.go index e37da4aea..29cdce335 100644 --- a/internal/remote/discover_test.go +++ b/internal/remote/discover_test.go @@ -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) - }) }