Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add unit test for validationError and all regex related checker funcs #1325

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions backend/pkg/api/enums/enums.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ type AdInsertMode int
var _ EnumFactory[AdInsertMode] = AdInsertMode(0)

const (
AdInsertBefore AdInsertMode = iota
AdInsertAfter AdInsertMode = iota
AdInsertReplace AdInsertMode = iota
AdInsertInsert AdInsertMode = iota
AdInsertBefore AdInsertMode = iota
AdInsertAfter
AdInsertReplace
AdInsertInsert
)

func (c AdInsertMode) Int() int {
Expand Down
31 changes: 3 additions & 28 deletions backend/pkg/api/handlers/input_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/gobitfly/beaconchain/pkg/api/enums"
"github.com/gobitfly/beaconchain/pkg/api/types"
"github.com/gorilla/mux"
"github.com/invopop/jsonschema"
"github.com/shopspring/decimal"
"github.com/xeipuuv/gojsonschema"
Expand Down Expand Up @@ -253,6 +252,7 @@ func (v *validationError) checkAdConfigurationKeys(keysString string) []string {
}
var keys []string
for _, key := range splitParameters(keysString, ',') {
key = strings.TrimSpace(key)
keys = append(keys, v.checkRegex(reName, key, "keys"))
}
return keys
Expand All @@ -262,33 +262,6 @@ func (v *validationError) checkPrimaryDashboardId(param string) types.VDBIdPrima
return types.VDBIdPrimary(v.checkUint(param, "dashboard_id"))
}

// helper function to unify handling of block detail request validation
func (h *HandlerService) validateBlockRequest(r *http.Request, paramName string) (uint64, uint64, error) {
var v validationError
var err error
chainId := v.checkNetworkParameter(mux.Vars(r)["network"])
var value uint64
switch paramValue := mux.Vars(r)[paramName]; paramValue {
// possibly add other values like "genesis", "finalized", hardforks etc. later
case "latest":
ctx := r.Context()
if paramName == "block" {
value, err = h.daService.GetLatestBlock(ctx)
} else if paramName == "slot" {
value, err = h.daService.GetLatestSlot(ctx)
}
if err != nil {
return 0, 0, err
}
default:
value = v.checkUint(paramValue, paramName)
}
if v.hasErrors() {
return 0, 0, v
}
return chainId, value, nil
}

// checkGroupId validates the given group id and returns it as an int64.
// If the given group id is empty and allowEmpty is true, it returns -1 (all groups).
func (v *validationError) checkGroupId(param string, allowEmpty bool) int64 {
Expand All @@ -314,6 +287,7 @@ func splitParameters(params string, delim rune) []string {
func parseGroupIdList[T any](groupIds string, convert func(string, string) T) []T {
var ids []T
for _, id := range splitParameters(groupIds, ',') {
id = strings.TrimSpace(id)
ids = append(ids, convert(id, "group_ids"))
}
return ids
Expand Down Expand Up @@ -425,6 +399,7 @@ func (v *validationError) checkProtocolModes(protocolModes string) types.VDBProt
}
protocolsSlice := splitParameters(protocolModes, ',')
for _, protocolMode := range protocolsSlice {
protocolMode = strings.TrimSpace(protocolMode)
switch protocolMode {
case "rocket_pool":
modes.RocketPool = true
Expand Down
Loading
Loading