fleet/server/service/transport_test.go
Jacob Shandling 49b04ba4a5
For requests with invalid list options, return 400 instead of 500 (#11632)
## Addresses #11272 

- For requests with invalid list options (`page`, `per_page`,
`order_key`, `order_direction`), return `400` instead of `500`
<img width="957" alt="Screenshot 2023-05-10 at 2 28 56 PM"
src="https://github.com/fleetdm/fleet/assets/61553566/d4400a92-b158-4a41-9d00-9ba5170d48f6">

# Checklist for submitter

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

- [x] Manual QA for all new/changed functionality

---------

Co-authored-by: Jacob Shandling <jacob@fleetdm.com>
2023-05-17 13:41:30 -07:00

117 lines
2.7 KiB
Go

package service
import (
"net/http"
"net/url"
"testing"
"github.com/fleetdm/fleet/v4/server/fleet"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestListOptionsFromRequest(t *testing.T) {
var listOptionsTests = []struct {
// url string to parse
url string
// expected list options
listOptions fleet.ListOptions
// should cause a BadRequest error
shouldErr400 bool
}{
// both params provided
{
url: "/foo?page=1&per_page=10",
listOptions: fleet.ListOptions{Page: 1, PerPage: 10},
},
// only per_page (page should default to 0)
{
url: "/foo?per_page=10",
listOptions: fleet.ListOptions{Page: 0, PerPage: 10},
},
// only page (per_page should default to defaultPerPage
{
url: "/foo?page=10",
listOptions: fleet.ListOptions{Page: 10, PerPage: defaultPerPage},
},
// no params provided (defaults to empty ListOptions indicating
// unlimited)
{
url: "/foo?unrelated=foo",
listOptions: fleet.ListOptions{},
},
// Both order params provided
{
url: "/foo?order_key=foo&order_direction=desc",
listOptions: fleet.ListOptions{OrderKey: "foo", OrderDirection: fleet.OrderDescending},
},
// Both order params provided (asc)
{
url: "/foo?order_key=bar&order_direction=asc",
listOptions: fleet.ListOptions{OrderKey: "bar", OrderDirection: fleet.OrderAscending},
},
// Default order direction
{
url: "/foo?order_key=foo",
listOptions: fleet.ListOptions{OrderKey: "foo", OrderDirection: fleet.OrderAscending},
},
// All params defined
{
url: "/foo?order_key=foo&order_direction=desc&page=1&per_page=100",
listOptions: fleet.ListOptions{
OrderKey: "foo",
OrderDirection: fleet.OrderDescending,
Page: 1,
PerPage: 100,
},
},
// various 400 error cases
{
url: "/foo?page=foo&per_page=10",
shouldErr400: true,
},
{
url: "/foo?page=1&per_page=foo",
shouldErr400: true,
},
{
url: "/foo?page=-1",
shouldErr400: true,
},
{
url: "/foo?page=-1&per_page=-10",
shouldErr400: true,
},
{
url: "/foo?page=1&order_direction=desc",
shouldErr400: true,
},
{
url: "/foo?&order_direction=foo&order_key=",
shouldErr400: true,
},
}
for _, tt := range listOptionsTests {
t.Run(tt.url, func(t *testing.T) {
url, _ := url.Parse(tt.url)
req := &http.Request{URL: url}
opt, err := listOptionsFromRequest(req)
if tt.shouldErr400 {
assert.NotNil(t, err)
var be *fleet.BadRequestError
require.ErrorAs(t, err, &be)
return
}
assert.Nil(t, err)
assert.Equal(t, tt.listOptions, opt)
})
}
}