2020-05-22 14:29:12 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
2023-01-30 11:27:34 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2020-05-22 14:29:12 +00:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2023-01-30 11:27:34 +00:00
|
|
|
"github.com/photoprism/photoprism/pkg/sortby"
|
2020-05-22 14:29:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetFoldersOriginals(t *testing.T) {
|
|
|
|
t.Run("flat", func(t *testing.T) {
|
|
|
|
app, router, conf := NewApiTest()
|
2020-05-22 17:25:53 +00:00
|
|
|
_ = conf.CreateDirectories()
|
2020-07-16 13:43:23 +00:00
|
|
|
expected, err := fs.Dirs(conf.OriginalsPath(), false, true)
|
2020-05-22 14:29:12 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-11-26 13:28:50 +00:00
|
|
|
SearchFoldersOriginals(router)
|
2020-05-22 14:29:12 +00:00
|
|
|
r := PerformRequest(app, "GET", "/api/v1/folders/originals")
|
|
|
|
|
2020-05-24 20:16:06 +00:00
|
|
|
// t.Logf("RESPONSE: %s", r.Body.Bytes())
|
|
|
|
|
|
|
|
var resp FoldersResponse
|
|
|
|
err = json.Unmarshal(r.Body.Bytes(), &resp)
|
2020-05-22 14:29:12 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-05-24 20:16:06 +00:00
|
|
|
folders := resp.Folders
|
|
|
|
|
2020-05-23 18:58:58 +00:00
|
|
|
if len(folders) != len(expected) {
|
2020-05-22 14:29:12 +00:00
|
|
|
t.Fatalf("response contains %d folders", len(folders))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(folders) == 0 {
|
|
|
|
// There are no existing folders, that's ok.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, folder := range folders {
|
|
|
|
assert.Equal(t, "", folder.FolderDescription)
|
2022-04-13 20:17:59 +00:00
|
|
|
assert.Equal(t, entity.MediaUnknown, folder.FolderType)
|
2023-01-30 11:27:34 +00:00
|
|
|
assert.Equal(t, sortby.Name, folder.FolderOrder)
|
2020-06-07 08:09:35 +00:00
|
|
|
assert.Equal(t, entity.RootOriginals, folder.Root)
|
2020-05-23 18:58:58 +00:00
|
|
|
assert.IsType(t, "", folder.FolderUID)
|
2020-05-22 14:29:12 +00:00
|
|
|
assert.Equal(t, false, folder.FolderFavorite)
|
|
|
|
assert.Equal(t, false, folder.FolderIgnore)
|
|
|
|
assert.Equal(t, false, folder.FolderWatch)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("recursive", func(t *testing.T) {
|
|
|
|
app, router, conf := NewApiTest()
|
2020-05-22 17:25:53 +00:00
|
|
|
_ = conf.CreateDirectories()
|
2020-07-16 13:43:23 +00:00
|
|
|
expected, err := fs.Dirs(conf.OriginalsPath(), true, true)
|
2020-05-22 14:29:12 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-11-26 13:28:50 +00:00
|
|
|
SearchFoldersOriginals(router)
|
2020-05-22 14:29:12 +00:00
|
|
|
r := PerformRequest(app, "GET", "/api/v1/folders/originals?recursive=true")
|
2020-05-24 20:16:06 +00:00
|
|
|
|
|
|
|
// t.Logf("RESPONSE: %s", r.Body.Bytes())
|
|
|
|
|
|
|
|
var resp FoldersResponse
|
|
|
|
err = json.Unmarshal(r.Body.Bytes(), &resp)
|
2020-05-22 14:29:12 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-05-24 20:16:06 +00:00
|
|
|
folders := resp.Folders
|
|
|
|
|
2020-05-23 18:58:58 +00:00
|
|
|
if len(folders) != len(expected) {
|
2020-05-22 14:29:12 +00:00
|
|
|
t.Fatalf("response contains %d folders", len(folders))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, folder := range folders {
|
|
|
|
assert.Equal(t, "", folder.FolderDescription)
|
2022-04-13 20:17:59 +00:00
|
|
|
assert.Equal(t, entity.MediaUnknown, folder.FolderType)
|
2023-01-30 11:27:34 +00:00
|
|
|
assert.Equal(t, sortby.Name, folder.FolderOrder)
|
2020-06-07 08:09:35 +00:00
|
|
|
assert.Equal(t, entity.RootOriginals, folder.Root)
|
2020-05-23 18:58:58 +00:00
|
|
|
assert.IsType(t, "", folder.FolderUID)
|
2020-05-22 14:29:12 +00:00
|
|
|
assert.Equal(t, false, folder.FolderFavorite)
|
|
|
|
assert.Equal(t, false, folder.FolderIgnore)
|
|
|
|
assert.Equal(t, false, folder.FolderWatch)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetFoldersImport(t *testing.T) {
|
|
|
|
t.Run("flat", func(t *testing.T) {
|
|
|
|
app, router, conf := NewApiTest()
|
2020-05-22 17:25:53 +00:00
|
|
|
_ = conf.CreateDirectories()
|
2020-07-16 13:43:23 +00:00
|
|
|
expected, err := fs.Dirs(conf.ImportPath(), false, true)
|
2020-05-22 14:29:12 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-11-26 13:28:50 +00:00
|
|
|
SearchFoldersImport(router)
|
2020-05-22 14:29:12 +00:00
|
|
|
r := PerformRequest(app, "GET", "/api/v1/folders/import")
|
2020-05-24 20:16:06 +00:00
|
|
|
|
|
|
|
// t.Logf("RESPONSE: %s", r.Body.Bytes())
|
|
|
|
|
|
|
|
var resp FoldersResponse
|
|
|
|
err = json.Unmarshal(r.Body.Bytes(), &resp)
|
2020-05-22 14:29:12 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-05-24 20:16:06 +00:00
|
|
|
folders := resp.Folders
|
|
|
|
|
2020-05-23 18:58:58 +00:00
|
|
|
if len(folders) != len(expected) {
|
2020-05-22 14:29:12 +00:00
|
|
|
t.Fatalf("response contains %d folders", len(folders))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(folders) == 0 {
|
|
|
|
// There are no existing folders, that's ok.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, folder := range folders {
|
|
|
|
assert.Equal(t, "", folder.FolderDescription)
|
2022-04-13 20:17:59 +00:00
|
|
|
assert.Equal(t, entity.MediaUnknown, folder.FolderType)
|
2023-01-30 11:27:34 +00:00
|
|
|
assert.Equal(t, sortby.Name, folder.FolderOrder)
|
2020-05-24 20:16:06 +00:00
|
|
|
assert.Equal(t, entity.RootImport, folder.Root)
|
2020-05-23 18:58:58 +00:00
|
|
|
assert.IsType(t, "", folder.FolderUID)
|
2020-05-22 14:29:12 +00:00
|
|
|
assert.Equal(t, false, folder.FolderFavorite)
|
|
|
|
assert.Equal(t, false, folder.FolderIgnore)
|
|
|
|
assert.Equal(t, false, folder.FolderWatch)
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
t.Run("recursive", func(t *testing.T) {
|
|
|
|
app, router, conf := NewApiTest()
|
2020-05-22 17:25:53 +00:00
|
|
|
_ = conf.CreateDirectories()
|
2020-07-16 13:43:23 +00:00
|
|
|
expected, err := fs.Dirs(conf.ImportPath(), true, true)
|
2020-05-22 14:29:12 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-05-23 18:58:58 +00:00
|
|
|
|
2021-11-26 13:28:50 +00:00
|
|
|
SearchFoldersImport(router)
|
2020-05-22 14:29:12 +00:00
|
|
|
r := PerformRequest(app, "GET", "/api/v1/folders/import?recursive=true")
|
2020-05-24 20:16:06 +00:00
|
|
|
|
|
|
|
var resp FoldersResponse
|
|
|
|
err = json.Unmarshal(r.Body.Bytes(), &resp)
|
2020-05-22 14:29:12 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-05-24 20:16:06 +00:00
|
|
|
folders := resp.Folders
|
|
|
|
|
2020-05-23 18:58:58 +00:00
|
|
|
if len(folders) != len(expected) {
|
2020-05-22 14:29:12 +00:00
|
|
|
t.Fatalf("response contains %d folders", len(folders))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, folder := range folders {
|
|
|
|
assert.Equal(t, "", folder.FolderDescription)
|
2022-04-13 20:17:59 +00:00
|
|
|
assert.Equal(t, entity.MediaUnknown, folder.FolderType)
|
2023-01-30 11:27:34 +00:00
|
|
|
assert.Equal(t, sortby.Name, folder.FolderOrder)
|
2020-05-24 20:16:06 +00:00
|
|
|
assert.Equal(t, entity.RootImport, folder.Root)
|
2020-05-23 18:58:58 +00:00
|
|
|
assert.IsType(t, "", folder.FolderUID)
|
2020-05-22 14:29:12 +00:00
|
|
|
assert.Equal(t, false, folder.FolderFavorite)
|
|
|
|
assert.Equal(t, false, folder.FolderIgnore)
|
|
|
|
assert.Equal(t, false, folder.FolderWatch)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|