package storageagent import ( "encoding/json" "net/http" "net/http/httptest" "os" "path/filepath" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestInternalRequiresToken(t *testing.T) { h := NewRouter("secret", t.TempDir()) for _, auth := range []string{"", "Bearer wrong", "secret"} { rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/internal/mounts", nil) if auth != "" { req.Header.Set("Authorization", auth) } h.ServeHTTP(rec, req) assert.Equal(t, http.StatusUnauthorized, rec.Code, "auth=%q", auth) } } func TestListMounts(t *testing.T) { root := t.TempDir() require.NoError(t, os.Mkdir(filepath.Join(root, "src-1"), 0o755)) require.NoError(t, os.WriteFile(filepath.Join(root, "stray.txt"), nil, 0o644)) rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/internal/mounts", nil) req.Header.Set("Authorization", "Bearer secret") NewRouter("secret", root).ServeHTTP(rec, req) require.Equal(t, http.StatusOK, rec.Code) var body struct{ Mounts []Mount } require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &body)) assert.Equal(t, []Mount{{SourceID: "src-1", Path: filepath.Join(root, "src-1")}}, body.Mounts) } func TestListMountsMissingRoot(t *testing.T) { mounts, err := listMounts(filepath.Join(t.TempDir(), "absent")) require.NoError(t, err) assert.Empty(t, mounts) }