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