jahzielv/fix/13809 host filtering (#14099)

- fix: use function that returns params
- fix: missing integration test

fixes: #13809

# Checklist for submitter

If some of the following don't apply, delete the relevant line.

- [x] Changes file added for user-visible changes in `changes/` or
`orbit/changes/`.
See [Changes
files](https://fleetdm.com/docs/contributing/committing-changes#changes-files)
for more information.
- [x] Added/updated tests
- [x] Manual QA for all new/changed functionality
This commit is contained in:
Jahziel Villasana-Espinoza 2023-09-26 12:59:37 -04:00 committed by GitHub
parent 81a6c76bed
commit b2938d1d53
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 1 deletions

View file

@ -0,0 +1 @@
* Use the correct set of parameters for SQL statement when the `after` and `order_key` query params are passed to the `/api/latest/fleet/labels/{labelID}/hosts` endpoint. Fixes issue 13809.

View file

@ -584,7 +584,7 @@ func (ds *Datastore) applyHostLabelFilters(filter fleet.TeamFilter, lid uint, qu
query, params = filterHostsByMDMBootstrapPackageStatus(query, opt, params)
query, params = searchLike(query, params, opt.MatchQuery, hostSearchColumns...)
query = appendListOptionsToSQL(query, &opt.ListOptions)
query, params = appendListOptionsWithCursorToSQL(query, params, &opt.ListOptions)
return query, params
}

View file

@ -324,6 +324,10 @@ func testLabelsListHostsInLabel(t *testing.T, db *Datastore) {
listHostsInLabelCheckCount(t, db, filter, l1.ID, fleet.HostListOptions{}, 3)
hosts := listHostsInLabelCheckCount(t, db, filter, l1.ID, fleet.HostListOptions{LowDiskSpaceFilter: ptr.Int(35), ListOptions: fleet.ListOptions{OrderKey: "id", After: "1"}}, 2)
require.Equal(t, h2.ID, hosts[0].ID)
require.Equal(t, h3.ID, hosts[1].ID)
listHostsInLabelCheckCount(t, db, filter, l1.ID, fleet.HostListOptions{LowDiskSpaceFilter: ptr.Int(35)}, 3)
listHostsInLabelCheckCount(t, db, filter, l1.ID, fleet.HostListOptions{LowDiskSpaceFilter: ptr.Int(25)}, 2)
listHostsInLabelCheckCount(t, db, filter, l1.ID, fleet.HostListOptions{LowDiskSpaceFilter: ptr.Int(15)}, 1)

View file

@ -3089,6 +3089,11 @@ func (s *integrationTestSuite) TestLabels() {
s.DoJSON("GET", fmt.Sprintf("/api/latest/fleet/labels/%d/hosts", lbl2.ID), nil, http.StatusOK, &listHostsResp)
assert.Len(t, listHostsResp.Hosts, len(hosts))
s.DoJSON("GET", fmt.Sprintf("/api/latest/fleet/labels/%d/hosts", lbl2.ID), nil, http.StatusOK, &listHostsResp, "order_key", "id", "after", fmt.Sprintf("%d", hosts[0].ID))
assert.Len(t, listHostsResp.Hosts, 2)
assert.Equal(t, hosts[1].ID, listHostsResp.Hosts[0].ID)
assert.Equal(t, hosts[2].ID, listHostsResp.Hosts[1].ID)
// list hosts in label searching by display_name
s.DoJSON("GET", fmt.Sprintf("/api/latest/fleet/labels/%d/hosts", lbl2.ID), nil, http.StatusOK, &listHostsResp, "order_key", "display_name", "order_direction", "desc")
assert.Len(t, listHostsResp.Hosts, len(hosts))