From 019947ff0196cff142e4e631260491bc79bb2a7a Mon Sep 17 00:00:00 2001 From: sispeo <42068883+fperot74@users.noreply.github.com> Date: Tue, 26 Sep 2023 10:03:50 +0200 Subject: [PATCH] [CLOUDTRUST-5106] Fix bug in validation tools --- go.mod | 4 ++-- go.sum | 4 ++-- validation/validator.go | 2 +- validation/validator_test.go | 6 +++++- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 8e11180..440563e 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/cloudtrust/common-service/v2 go 1.17 require ( - github.com/IBM/sarama v1.41.1 + github.com/IBM/sarama v1.41.1 github.com/gbrlsnchs/jwt/v2 v2.0.0 github.com/getsentry/raven-go v0.2.0 github.com/go-kit/kit v0.12.0 @@ -13,7 +13,7 @@ require ( github.com/google/uuid v1.3.0 github.com/gorilla/mux v1.8.0 github.com/influxdata/influxdb1-client v0.0.0-20200827194710-b269163b24ab - github.com/nyaruka/phonenumbers v1.0.74 + github.com/nyaruka/phonenumbers v1.1.8 github.com/opentracing/opentracing-go v1.2.0 github.com/pkg/errors v0.9.1 github.com/stretchr/testify v1.8.4 diff --git a/go.sum b/go.sum index 455ec74..5aa6d38 100644 --- a/go.sum +++ b/go.sum @@ -357,8 +357,8 @@ github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uY github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU= -github.com/nyaruka/phonenumbers v1.0.74 h1:Eo3aIKM6zfaHd+mCs+BY7FYpmHobb5C/erLkgs5Wx8U= -github.com/nyaruka/phonenumbers v1.0.74/go.mod h1:3aiS+PS3DuYwkbK3xdcmRwMiPNECZ0oENH8qUT1lY7Q= +github.com/nyaruka/phonenumbers v1.1.8 h1:mjFu85FeoH2Wy18aOMUvxqi1GgAqiQSJsa/cCC5yu2s= +github.com/nyaruka/phonenumbers v1.1.8/go.mod h1:DC7jZd321FqUe+qWSNcHi10tyIyGNXGcNbfkPvdp1Vs= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= diff --git a/validation/validator.go b/validation/validator.go index 62c4a45..906d328 100644 --- a/validation/validator.go +++ b/validation/validator.go @@ -94,7 +94,7 @@ func (v *successValidator) ValidateParameterIn(prmName string, value *string, al return &failedValidator{err: cerrors.CreateMissingParameterError(prmName)} } } else { - if _, ok := allowedValues[*value]; !ok { + if allowed, ok := allowedValues[*value]; !ok || !allowed { return &failedValidator{err: cerrors.CreateBadRequestError(cerrors.MsgErrInvalidParam + "." + prmName)} } } diff --git a/validation/validator_test.go b/validation/validator_test.go index 75f401b..35d2484 100644 --- a/validation/validator_test.go +++ b/validation/validator_test.go @@ -111,9 +111,10 @@ func TestValidateParameterNotNil(t *testing.T) { } func TestValidateParameterIn(t *testing.T) { - var weekend = map[string]bool{"saturday": true, "sunday": true} + var weekend = map[string]bool{"saturday": true, "sunday": true, "wednesday": false} var validValue = "sunday" var invalidValue = "green" + var disallowedValue = "wednesday" t.Run("Nil value, not mandatory", func(t *testing.T) { assert.Nil(t, NewParameterValidator().ValidateParameterIn("param", nil, weekend, false).Status()) @@ -127,6 +128,9 @@ func TestValidateParameterIn(t *testing.T) { t.Run("Invalid value", func(t *testing.T) { assert.NotNil(t, NewParameterValidator().ValidateParameterIn("param", &invalidValue, weekend, true).Status()) }) + t.Run("Disallowed value", func(t *testing.T) { + assert.NotNil(t, NewParameterValidator().ValidateParameterIn("param", &disallowedValue, weekend, true).Status()) + }) t.Run("Valid check after failed validation", func(t *testing.T) { assert.NotNil(t, failingValidator().ValidateParameterIn("param", nil, weekend, false).Status()) })