diff --git a/internal/clientcache/internal/daemon/search_handler.go b/internal/clientcache/internal/daemon/search_handler.go index 9ced1c4522..cd955f84f9 100644 --- a/internal/clientcache/internal/daemon/search_handler.go +++ b/internal/clientcache/internal/daemon/search_handler.go @@ -265,7 +265,7 @@ func parseSortDirection(sd string) (cache.SortDirection, bool) { case "asc", "ascending": return cache.Ascending, true case "desc", "descending": - return cache.Ascending, true + return cache.Descending, true case "": return cache.SortDirectionDefault, true default: diff --git a/internal/clientcache/internal/daemon/search_handler_test.go b/internal/clientcache/internal/daemon/search_handler_test.go new file mode 100644 index 0000000000..14409eb505 --- /dev/null +++ b/internal/clientcache/internal/daemon/search_handler_test.go @@ -0,0 +1,57 @@ +package daemon + +import ( + "testing" + + "github.com/hashicorp/boundary/internal/clientcache/internal/cache" + "github.com/stretchr/testify/assert" +) + +func TestParseSortBy(t *testing.T) { + testCases := []struct { + inputSb string + inputSr cache.SearchableResource + expectedValid bool + expectedSortBy cache.SortBy + }{ + {"name", cache.Targets, true, cache.SortByName}, + {"name", cache.Sessions, false, cache.SortByDefault}, + {"created_at", cache.Targets, false, cache.SortByDefault}, + {"created_at", cache.Sessions, true, cache.SortByCreatedAt}, + {"", cache.Targets, true, cache.SortByDefault}, + {"", cache.Sessions, true, cache.SortByDefault}, + {"ljkdhnsfg", cache.Targets, false, cache.SortByDefault}, + {"xcvbxcvb", cache.Sessions, false, cache.SortByDefault}, + {"name ", cache.Targets, false, cache.SortByDefault}, // Unicode no break space + {"name‮", cache.Targets, false, cache.SortByDefault}, // Unicode RtL override + {"‮name", cache.Targets, false, cache.SortByDefault}, // Unicode RtL override + } + for _, tc := range testCases { + actualSortBy, actualValid := parseSortBy(tc.inputSb, tc.inputSr) + assert.Equal(t, tc.expectedSortBy, actualSortBy) + assert.Equal(t, tc.expectedValid, actualValid) + } +} + +func TestParseSortDirection(t *testing.T) { + testCases := []struct { + inputSd string + expectedValid bool + expectedSortDirection cache.SortDirection + }{ + {"asc", true, cache.Ascending}, + {"ascending", true, cache.Ascending}, + {"desc", true, cache.Descending}, + {"descending", true, cache.Descending}, + {"", true, cache.SortDirectionDefault}, + {"asdasd", false, cache.SortDirectionDefault}, + {"asc ", false, cache.SortDirectionDefault}, + {"name‮", false, cache.SortDirectionDefault}, + {"‮name", false, cache.SortDirectionDefault}, + } + for _, tc := range testCases { + actualSortDirection, actualValid := parseSortDirection(tc.inputSd) + assert.Equal(t, tc.expectedSortDirection, actualSortDirection) + assert.Equal(t, tc.expectedValid, actualValid) + } +}