- feat(storage): libraries table (name, path); audio_files points at a library
by id instead of repeating the root path; migration 0005 for both drivers
turns every existing root into a library and keeps file ids, tracks and the
missing flag
- feat(library): Libraries service validates names and paths: absolute, an
existing directory, never the filesystem root, never inside or around
another library (checked on real paths, so symlinks cannot hide an overlap)
- feat(api): admin-only GET/POST /libraries, PATCH/DELETE /libraries/{id} and
POST /libraries/{id}/scan; deleting a library drops its index entries and
keeps tracks and listening history
- feat(library): the scanner and the scan manager work on libraries; scanning
with no libraries answers 409 no_libraries
- feat(streaming): the library directory comes from the database instead of
a configured list of roots
- breaking(config): library.roots is removed; an old config fails with a hint
to use POST /api/v1/libraries
- test: CRUD and duplicates on both databases, path change keeps files,
upgrade of a database written before libraries existed
178 lines
6.6 KiB
Go
178 lines
6.6 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"testing"
|
|
)
|
|
|
|
func libPath(id float64) string {
|
|
return "/api/v1/libraries/" + strconv.FormatInt(int64(id), 10)
|
|
}
|
|
|
|
func mkdirT(t *testing.T, path string) string {
|
|
t.Helper()
|
|
if err := os.MkdirAll(path, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return path
|
|
}
|
|
|
|
func TestLibraryRoutesAreAdminOnly(t *testing.T) {
|
|
h := newTestAPIWithScan(t, &fakeScan{})
|
|
admin := registerAndLogin(t, h)
|
|
_, bob := addUser(t, h, admin, "bob", false)
|
|
|
|
calls := []struct{ method, path string }{
|
|
{"GET", "/api/v1/libraries"},
|
|
{"POST", "/api/v1/libraries"},
|
|
{"PATCH", "/api/v1/libraries/1"},
|
|
{"DELETE", "/api/v1/libraries/1"},
|
|
{"POST", "/api/v1/libraries/1/scan"},
|
|
}
|
|
for _, c := range calls {
|
|
if r := do(t, h, c.method, c.path, "", nil); r.Code != http.StatusUnauthorized {
|
|
t.Errorf("%s %s without token: %d", c.method, c.path, r.Code)
|
|
}
|
|
r := do(t, h, c.method, c.path, bob, map[string]any{"name": "x", "path": "/tmp"})
|
|
if r.Code != http.StatusForbidden || errCode(r) != "admin_required" {
|
|
t.Errorf("%s %s as non-admin: %d %v", c.method, c.path, r.Code, r.Body)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLibraryLifecycle(t *testing.T) {
|
|
h := newTestAPI(t)
|
|
admin := registerAndLogin(t, h)
|
|
base := t.TempDir()
|
|
jazz := mkdirT(t, filepath.Join(base, "jazz"))
|
|
rock := mkdirT(t, filepath.Join(base, "rock"))
|
|
|
|
r := do(t, h, "POST", "/api/v1/libraries", admin, map[string]string{"name": " Jazz ", "path": jazz})
|
|
if r.Code != http.StatusCreated || r.Body["name"] != "Jazz" || r.Body["path"] != jazz || r.Body["id"] == nil {
|
|
t.Fatalf("create: %d %v", r.Code, r.Body)
|
|
}
|
|
id, _ := r.Body["id"].(float64)
|
|
|
|
r = do(t, h, "GET", "/api/v1/libraries", admin, nil)
|
|
libs, _ := r.Body["libraries"].([]any)
|
|
if r.Code != http.StatusOK || len(libs) != 1 {
|
|
t.Fatalf("list: %d %v", r.Code, r.Body)
|
|
}
|
|
if first := libs[0].(map[string]any); first["created_at"] == nil || first["name"] != "Jazz" {
|
|
t.Errorf("listed library = %v", first)
|
|
}
|
|
|
|
// Rename only, then move only, then both.
|
|
r = do(t, h, "PATCH", libPath(id), admin, map[string]string{"name": "Jazz Collection"})
|
|
if r.Code != http.StatusOK || r.Body["name"] != "Jazz Collection" || r.Body["path"] != jazz {
|
|
t.Fatalf("rename: %d %v", r.Code, r.Body)
|
|
}
|
|
r = do(t, h, "PATCH", libPath(id), admin, map[string]string{"path": rock})
|
|
if r.Code != http.StatusOK || r.Body["path"] != rock || r.Body["name"] != "Jazz Collection" {
|
|
t.Fatalf("move: %d %v", r.Code, r.Body)
|
|
}
|
|
|
|
if r := do(t, h, "DELETE", libPath(id), admin, nil); r.Code != http.StatusNoContent {
|
|
t.Fatalf("delete: %d %v", r.Code, r.Body)
|
|
}
|
|
if r := do(t, h, "DELETE", libPath(id), admin, nil); r.Code != http.StatusNotFound || errCode(r) != "library_not_found" {
|
|
t.Errorf("delete twice: %d %v", r.Code, r.Body)
|
|
}
|
|
r = do(t, h, "GET", "/api/v1/libraries", admin, nil)
|
|
if libs, _ := r.Body["libraries"].([]any); len(libs) != 0 {
|
|
t.Errorf("libraries after delete: %v", libs)
|
|
}
|
|
}
|
|
|
|
func TestLibraryValidationErrors(t *testing.T) {
|
|
h := newTestAPI(t)
|
|
admin := registerAndLogin(t, h)
|
|
base := t.TempDir()
|
|
parent := mkdirT(t, filepath.Join(base, "music"))
|
|
child := mkdirT(t, filepath.Join(parent, "jazz"))
|
|
other := mkdirT(t, filepath.Join(base, "other"))
|
|
file := filepath.Join(base, "file.txt")
|
|
if err := os.WriteFile(file, []byte("x"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if r := do(t, h, "POST", "/api/v1/libraries", admin, map[string]string{"name": "Music", "path": parent}); r.Code != http.StatusCreated {
|
|
t.Fatal(r.Code, r.Body)
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
body any
|
|
status int
|
|
code string
|
|
}{
|
|
{"empty name", map[string]string{"name": " ", "path": other}, 400, "invalid_input"},
|
|
{"relative path", map[string]string{"name": "x", "path": "music/jazz"}, 400, "invalid_input"},
|
|
{"filesystem root", map[string]string{"name": "x", "path": "/"}, 400, "invalid_input"},
|
|
{"missing directory", map[string]string{"name": "x", "path": filepath.Join(base, "nope")}, 400, "invalid_input"},
|
|
{"a file", map[string]string{"name": "x", "path": file}, 400, "invalid_input"},
|
|
{"inside another library", map[string]string{"name": "x", "path": child}, 400, "invalid_input"},
|
|
{"around another library", map[string]string{"name": "x", "path": base}, 400, "invalid_input"},
|
|
{"same name", map[string]string{"name": "MUSIC", "path": other}, 409, "library_exists"},
|
|
{"same path", map[string]string{"name": "x", "path": parent}, 409, "library_exists"},
|
|
{"malformed json", "{oops", 400, "invalid_input"},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
r := do(t, h, "POST", "/api/v1/libraries", admin, tt.body)
|
|
if r.Code != tt.status || errCode(r) != tt.code {
|
|
t.Fatalf("%d %v, want %d %s", r.Code, r.Body, tt.status, tt.code)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLibraryPatchValidation(t *testing.T) {
|
|
h := newTestAPI(t)
|
|
admin := registerAndLogin(t, h)
|
|
base := t.TempDir()
|
|
a := mkdirT(t, filepath.Join(base, "a"))
|
|
|
|
r := do(t, h, "POST", "/api/v1/libraries", admin, map[string]string{"name": "A", "path": a})
|
|
id, _ := r.Body["id"].(float64)
|
|
|
|
if r := do(t, h, "PATCH", libPath(id), admin, map[string]any{}); r.Code != http.StatusBadRequest {
|
|
t.Errorf("empty patch: %d", r.Code)
|
|
}
|
|
if r := do(t, h, "PATCH", libPath(id), admin, map[string]string{"name": ""}); r.Code != http.StatusBadRequest {
|
|
t.Errorf("empty name: %d", r.Code)
|
|
}
|
|
if r := do(t, h, "PATCH", "/api/v1/libraries/999", admin, map[string]string{"name": "x"}); r.Code != http.StatusNotFound || errCode(r) != "library_not_found" {
|
|
t.Errorf("unknown library: %d %v", r.Code, r.Body)
|
|
}
|
|
if r := do(t, h, "PATCH", "/api/v1/libraries/abc", admin, map[string]string{"name": "x"}); r.Code != http.StatusBadRequest {
|
|
t.Errorf("bad id: %d", r.Code)
|
|
}
|
|
}
|
|
|
|
func TestScanOneLibrary(t *testing.T) {
|
|
scan := &fakeScan{}
|
|
h := newTestAPIWithScan(t, scan)
|
|
admin := registerAndLogin(t, h)
|
|
|
|
if r := do(t, h, "POST", "/api/v1/libraries/7/scan", admin, nil); r.Code != http.StatusAccepted || scan.lastOnly != 7 {
|
|
t.Fatalf("scan one: %d %v (library %d)", r.Code, r.Body, scan.lastOnly)
|
|
}
|
|
if r := do(t, h, "POST", "/api/v1/libraries/7/scan", admin, nil); r.Code != http.StatusConflict || errCode(r) != "scan_running" {
|
|
t.Errorf("second scan: %d %v", r.Code, r.Body)
|
|
}
|
|
if r := do(t, h, "POST", "/api/v1/libraries/999/scan", admin, nil); r.Code != http.StatusNotFound || errCode(r) != "library_not_found" {
|
|
t.Errorf("unknown library: %d %v", r.Code, r.Body)
|
|
}
|
|
}
|
|
|
|
func TestScanAllWithoutLibraries(t *testing.T) {
|
|
h := newTestAPIWithScan(t, &fakeScan{noLibs: true})
|
|
admin := registerAndLogin(t, h)
|
|
if r := do(t, h, "POST", "/api/v1/library/scan", admin, nil); r.Code != http.StatusConflict || errCode(r) != "no_libraries" {
|
|
t.Fatalf("%d %v", r.Code, r.Body)
|
|
}
|
|
}
|