- fix(migration): roots that differ only in case (/Music and /music are two directories on Linux) used to make migration 0005 fail on the unique name; the later ones now get a numeric suffix - feat(library): at startup, log a warning for every library whose directory cannot be read or that overlaps another, because migrated rows skip validation and may still hold roots that were removed from the old config - fix(library): creating or updating a library is now serialised, so two concurrent requests cannot both pass the overlap check - fix(library): changing a library's directory or deleting it is refused with 409 scan_running while a scan runs; the scan works from the list it started with and would write under a stale path - fix(library): library names clash ignoring case and accents the same way on SQLite and PostgreSQL, checked in Go - fix(library): changing only the path no longer revalidates an untouched name, which can be longer than the limit after a migration - test: roots differing in case on both databases; concurrent creates with a slow store, verified to fail without the lock; the busy guard; Problems
207 lines
7.7 KiB
Go
207 lines
7.7 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"testing"
|
|
|
|
"codeberg.org/squidd/sonda/internal/library"
|
|
)
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
// busyLibraries is a libraryService whose directory changes are refused.
|
|
type busyLibraries struct{}
|
|
|
|
func (busyLibraries) List(context.Context) ([]library.Library, error) { return nil, nil }
|
|
func (busyLibraries) Create(context.Context, string, string) (library.Library, error) {
|
|
return library.Library{}, nil
|
|
}
|
|
func (busyLibraries) Update(context.Context, int64, *string, *string) (library.Library, error) {
|
|
return library.Library{}, library.ErrScanRunning
|
|
}
|
|
func (busyLibraries) Delete(context.Context, int64) error { return library.ErrScanRunning }
|
|
|
|
func TestLibraryChangesDuringAScanAreConflicts(t *testing.T) {
|
|
h := newTestAPIWith(t, func(d *Deps) { d.Libraries = busyLibraries{} })
|
|
admin := registerAndLogin(t, h)
|
|
|
|
r := do(t, h, "PATCH", "/api/v1/libraries/1", admin, map[string]string{"path": "/tmp"})
|
|
if r.Code != http.StatusConflict || errCode(r) != "scan_running" {
|
|
t.Errorf("PATCH: %d %v", r.Code, r.Body)
|
|
}
|
|
r = do(t, h, "DELETE", "/api/v1/libraries/1", admin, nil)
|
|
if r.Code != http.StatusConflict || errCode(r) != "scan_running" {
|
|
t.Errorf("DELETE: %d %v", r.Code, r.Body)
|
|
}
|
|
}
|