- Add unit tests in internal/services/library_service_test.go - Test path traversal protection - Test non-existent path handling - Test file vs directory validation - Test successful directory listing - Add integration tests in cmd/server/tests/library_browse_test.go - Use setupTestServer() helper from test_helpers.go - Test no authentication returns 401 - Test regular user returns 403 forbidden - Test admin can browse directories - Test path traversal blocking - All tests use table-driven approach with t.Run() Fixes: Issue 2 (tests)
39 lines
1.1 KiB
Go
39 lines
1.1 KiB
Go
package services
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestBrowseDirectories(t *testing.T) {
|
|
service := &LibraryService{}
|
|
|
|
t.Run("blocks path traversal with ..", func(t *testing.T) {
|
|
_, _, _, err := service.BrowseDirectories(context.Background(), "/etc/../root")
|
|
assert.Error(t, err)
|
|
assert.Contains(t, err.Error(), "path traversal not allowed")
|
|
})
|
|
|
|
t.Run("returns error for non-existent path", func(t *testing.T) {
|
|
_, _, _, err := service.BrowseDirectories(context.Background(), "/nonexistent/path")
|
|
assert.Error(t, err)
|
|
assert.Contains(t, err.Error(), "path does not exist")
|
|
})
|
|
|
|
t.Run("returns error when path is a file", func(t *testing.T) {
|
|
_, _, _, err := service.BrowseDirectories(context.Background(), "/etc/passwd")
|
|
assert.Error(t, err)
|
|
assert.Contains(t, err.Error(), "not a directory")
|
|
})
|
|
|
|
t.Run("returns subdirectories for valid path", func(t *testing.T) {
|
|
dirs, currentPath, parentPath, err := service.BrowseDirectories(context.Background(), "/tmp")
|
|
assert.NoError(t, err)
|
|
assert.NotEmpty(t, dirs)
|
|
assert.Equal(t, "/tmp", currentPath)
|
|
assert.Equal(t, "/", parentPath)
|
|
})
|
|
}
|