Fix sorting on multiple ticket fields (#412)

This commit is contained in:
Jonas Plum
2022-08-21 22:23:27 +02:00
committed by GitHub
parent 2b7be7c212
commit fd8e793361
4 changed files with 130 additions and 34 deletions

View File

@@ -7,6 +7,7 @@ import (
"fmt"
"net/http"
"strconv"
"strings"
"github.com/go-chi/chi"
"github.com/xeipuuv/gojsonschema"
@@ -58,30 +59,12 @@ func parseQueryBool(r *http.Request, s string) (bool, error) {
}
func parseQueryStringArray(r *http.Request, key string) ([]string, error) {
stringArray, ok := r.URL.Query()[key]
if !ok {
return nil, nil
}
return removeEmpty(stringArray), nil
}
func removeEmpty(l []string) []string {
var stringArray []string
for _, s := range l {
if s == "" {
continue
}
stringArray = append(stringArray, s)
}
return stringArray
return parseQueryArray(r, key), nil
}
func parseQueryBoolArray(r *http.Request, key string) ([]bool, error) {
stringArray, ok := r.URL.Query()[key]
if !ok {
return nil, nil
}
stringArray := parseQueryArray(r, key)
var boolArray []bool
for _, s := range stringArray {
if s == "" {
@@ -97,6 +80,33 @@ func parseQueryBoolArray(r *http.Request, key string) ([]bool, error) {
return boolArray, nil
}
func parseQueryArray(r *http.Request, key string) []string {
stringArray, ok := r.URL.Query()[key]
if !ok {
return nil
}
if len(stringArray) == 0 {
return nil
}
stringArray = strings.Split(stringArray[0], ",")
return removeEmpty(stringArray)
}
func removeEmpty(l []string) []string {
var stringArray []string
for _, s := range l {
if s == "" {
continue
}
stringArray = append(stringArray, s)
}
return stringArray
}
func parseQueryOptionalInt(r *http.Request, key string) (*int, error) {
s := r.URL.Query().Get(key)
if s == "" {

86
generated/api/api_test.go Normal file
View File

@@ -0,0 +1,86 @@
package api
import (
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func Test_parseQueryOptionalBoolArray(t *testing.T) {
type args struct {
r *http.Request
key string
}
tests := []struct {
name string
args args
want []bool
wantErr bool
}{
{
name: "bool array",
args: args{
r: httptest.NewRequest(
http.MethodGet,
"https://try.catalyst-soar.com/api/tickets?type=alert&offset=0&count=10&sort=status%2Cowner%2Ccreated&desc=true%2Cfalse%2Cfalse&query=status+%3D%3D+%27open%27+AND+%28owner+%3D%3D+%27eve%27+OR+%21owner%29",
nil,
),
key: "desc",
},
want: []bool{true, false, false},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := parseQueryOptionalBoolArray(tt.args.r, tt.args.key)
if (err != nil) != tt.wantErr {
t.Errorf("parseQueryOptionalBoolArray() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("parseQueryOptionalBoolArray() got = %v, want %v", got, tt.want)
}
})
}
}
func Test_parseQueryOptionalStringArray(t *testing.T) {
type args struct {
r *http.Request
key string
}
tests := []struct {
name string
args args
want []string
wantErr bool
}{
{
name: "string array",
args: args{
r: httptest.NewRequest(
http.MethodGet,
"https://try.catalyst-soar.com/api/tickets?type=alert&offset=0&count=10&sort=status%2Cowner%2Ccreated&desc=true%2Cfalse%2Cfalse&query=status+%3D%3D+%27open%27+AND+%28owner+%3D%3D+%27eve%27+OR+%21owner%29",
nil,
),
key: "sort",
},
want: []string{"status", "owner", "created"},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := parseQueryOptionalStringArray(tt.args.r, tt.args.key)
if (err != nil) != tt.wantErr {
t.Errorf("parseQueryOptionalStringArray() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("parseQueryOptionalStringArray() got = %v, want %v", got, tt.want)
}
})
}
}