Implement restrictions on default column searching for #268 (#274)

* Implement restrictions on default column searching for #268

* Add better docs for config-set excluded-default-search-columns

* Enable debugging

* Clean up server binaries to avoid wasting disk space

* Add tests

* Swap from configuring excluded columns to configuring included columns, to prep for future changes where we may add support for other default columns

* Reduce gotestsum re-runs since tests are less flaky nowadays

* Fix bug in lib.where(...) function that failed to trim the args list and caused DB query correctness issues

* Disable tmate debugging

* Update goldens
This commit is contained in:
David Dworken
2025-01-18 12:25:07 -07:00
committed by GitHub
parent 23ed840aa3
commit 65d1ebfd07
15 changed files with 220 additions and 17 deletions

View File

@ -12,6 +12,7 @@ import (
"github.com/ddworken/hishtory/shared/testutils"
"github.com/stretchr/testify/require"
"gorm.io/gorm"
)
func TestMain(m *testing.M) {
@ -345,3 +346,63 @@ func TestSplitEscaped(t *testing.T) {
}
}
}
func TestParseNonAtomizedToken(t *testing.T) {
defer testutils.BackupAndRestore(t)()
require.NoError(t, hctx.InitConfig())
ctx := hctx.MakeContext()
// Default
q, v1, v2, v3, err := parseNonAtomizedToken(ctx, "echo hello")
require.NoError(t, err)
require.Equal(t, "(false OR command LIKE ? OR hostname LIKE ? OR current_working_directory LIKE ? )", q)
require.Equal(t, v1, "%echo hello%")
require.Equal(t, v2, "%echo hello%")
require.Equal(t, v3, "%echo hello%")
// Skipping cwd
config := hctx.GetConf(ctx)
config.DefaultSearchColumns = []string{"hostname", "command"}
q, v1, v2, v3, err = parseNonAtomizedToken(ctx, "echo hello")
require.NoError(t, err)
require.Equal(t, "(false OR command LIKE ? OR hostname LIKE ? )", q)
require.Equal(t, v1, "%echo hello%")
require.Equal(t, v2, "%echo hello%")
require.Nil(t, v3)
// Skipping cwd and hostname
config.DefaultSearchColumns = []string{"command"}
q, v1, v2, v3, err = parseNonAtomizedToken(ctx, "echo hello")
require.NoError(t, err)
require.Equal(t, "(false OR command LIKE ? )", q)
require.Equal(t, v1, "%echo hello%")
require.Nil(t, v2)
require.Nil(t, v3)
}
func TestWhere(t *testing.T) {
defer testutils.BackupAndRestore(t)()
require.NoError(t, hctx.InitConfig())
ctx := hctx.MakeContext()
db := hctx.GetDb(ctx)
testcases := []struct {
in_query string
in_args []any
expected_query string
}{
{"exit_code = ?", []any{1}, "SELECT * FROM `history_entries` WHERE exit_code = 1"},
{"exit_code = ?", []any{1, nil, nil}, "SELECT * FROM `history_entries` WHERE exit_code = 1"},
{"exit_code = ? OR exit_code = ?", []any{1, 2, nil}, "SELECT * FROM `history_entries` WHERE exit_code = 1 OR exit_code = 2"},
}
for _, tc := range testcases {
tx := where(db, tc.in_query, tc.in_args...)
queryString := tx.ToSQL(func(tx *gorm.DB) *gorm.DB {
var entries []data.HistoryEntry
return tx.Find(&entries)
})
require.Equal(t, tc.expected_query, queryString)
}
}