Files
bookhoard/cmd/server/tests/library_browse_test.go
T
john-okeefe b700f64624 fix: remove redundant defer setup.Close() calls to enable library cleanup
Problem:
Tests were calling `defer setup.Close()` which was interfering with the
library cleanup added in the previous commit. The execution order was:

1. setupTestServer() registers t.Cleanup() with library deletion code
2. Test calls defer setup.Close()
3. Test finishes:
   - defer setup.Close() runs FIRST → closes DB pool
   - t.Cleanup() runs SECOND → tries to delete libraries but DB is closed!

This prevented "Job Status Test Library" and other test libraries from
being cleaned up, leaving residual data in the database after tests.

Root Cause:
The setupTestServer() function already handles cleanup via t.Cleanup(),
which calls setup.Close() at the end. The explicit defer calls were
redundant and caused the database pool to close before library cleanup
could execute.

Solution:
Removed all 17 occurrences of `defer setup.Close()` from test files:
- worker_test.go: 4 tests
- jobs_test.go: 7 tests
- scan_settings_integration_test.go: 3 tests
- library_browse_test.go: 1 test
- goroutine_leak_test.go: 1 test
- fsnotify_integration_test.go: 1 test

Now setupTestServer()'s t.Cleanup() function properly:
1. Deletes "test" libraries (while DB is still connected)
2. Then calls setup.Close() to close connections

This ensures all test libraries are cleaned up, leaving a clean database
after `make test-integration` completes.

Files changed:
- cmd/server/tests/worker_test.go: Removed 4 defer calls
- cmd/server/tests/jobs_test.go: Removed 7 defer calls
- cmd/server/tests/scan_settings_integration_test.go: Removed 3 defer calls
- cmd/server/tests/library_browse_test.go: Removed 1 defer call
- cmd/server/tests/goroutine_leak_test.go: Removed 1 defer call
- cmd/server/tests/fsnotify_integration_test.go: Removed 1 defer call
2026-03-24 20:55:37 -04:00

62 lines
2.0 KiB
Go

package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestBrowseLibraryFoldersEndpoint(t *testing.T) {
setup := setupTestServer(t)
t.Run("GET /api/libraries/browse - no authentication returns 401", func(t *testing.T) {
req := httptest.NewRequest("GET", "/api/libraries/browse?path=/tmp", nil)
rec := httptest.NewRecorder()
setup.Server.Config.Handler.ServeHTTP(rec, req)
assert.Equal(t, http.StatusUnauthorized, rec.Code)
})
t.Run("GET /api/libraries/browse - regular user returns 403 forbidden", func(t *testing.T) {
req := httptest.NewRequest("GET", "/api/libraries/browse?path=/tmp", nil)
req.Header.Set("Authorization", "Bearer "+setup.RegularToken)
rec := httptest.NewRecorder()
setup.Server.Config.Handler.ServeHTTP(rec, req)
assert.Equal(t, http.StatusForbidden, rec.Code)
})
t.Run("GET /api/libraries/browse - admin can browse directories", func(t *testing.T) {
req := httptest.NewRequest("GET", "/api/libraries/browse?path=/tmp", nil)
req.Header.Set("Authorization", "Bearer "+setup.Token)
rec := httptest.NewRecorder()
setup.Server.Config.Handler.ServeHTTP(rec, req)
assert.Equal(t, http.StatusOK, rec.Code)
var data struct {
CurrentPath string `json:"current_path"`
ParentPath string `json:"parent_path"`
Directories []string `json:"directories"`
}
err := json.Unmarshal(rec.Body.Bytes(), &data)
assert.NoError(t, err)
assert.Equal(t, "/tmp", data.CurrentPath)
assert.NotEmpty(t, data.Directories)
})
t.Run("GET /api/libraries/browse - blocks path traversal attempts", func(t *testing.T) {
req := httptest.NewRequest("GET", "/api/libraries/browse?path=/etc/../root", nil)
req.Header.Set("Authorization", "Bearer "+setup.Token)
rec := httptest.NewRecorder()
setup.Server.Config.Handler.ServeHTTP(rec, req)
assert.Equal(t, http.StatusBadRequest, rec.Code)
var errResp struct {
Error string `json:"error"`
}
json.Unmarshal(rec.Body.Bytes(), &errResp)
assert.Contains(t, errResp.Error, "path traversal not allowed")
})
}