Skip to content

Commit

Permalink
set default validator when nil (#92)
Browse files Browse the repository at this point in the history
* set default validator when nil

* updated changelog

* added unit tests

* prep for release
  • Loading branch information
kristinapathak authored Nov 29, 2021
1 parent 052e0d4 commit a8af7ce
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [v0.3.5]
- Changed errorEncoder to log errors. [#90](https://github.com/xmidt-org/ancla/pull/90)
- Fixed webhook request decoder panic; added default validator when none given. [#92](https://github.com/xmidt-org/ancla/pull/92)

## [v0.3.4]
- Added more details to some validation errors. [#91](https://github.com/xmidt-org/ancla/pull/91)
Expand Down Expand Up @@ -84,7 +87,8 @@ internalWebhooks. [#80](https://github.com/xmidt-org/ancla/pull/80)
## [v0.1.0]
- Initial release

[Unreleased]: https://github.com/xmidt-org/ancla/compare/v0.3.4...HEAD
[Unreleased]: https://github.com/xmidt-org/ancla/compare/v0.3.5...HEAD
[v0.3.5]: https://github.com/xmidt-org/ancla/compare/0.3.4...v0.3.5
[v0.3.4]: https://github.com/xmidt-org/ancla/compare/0.3.3...v0.3.4
[v0.3.3]: https://github.com/xmidt-org/ancla/compare/0.3.2...v0.3.3
[v0.3.2]: https://github.com/xmidt-org/ancla/compare/0.3.1...v0.3.2
Expand Down
5 changes: 5 additions & 0 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ func addWebhookRequestDecoder(config transportConfig) kithttp.DecodeRequestFunc
config.basicPartnerIDsHeader = DefaultBasicPartnerIDsHeader
}

// if no validators are given, we accept anything.
if config.v == nil {
config.v = AlwaysValid()
}

return func(c context.Context, r *http.Request) (request interface{}, err error) {
requestPayload, err := ioutil.ReadAll(r.Body)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,12 @@ func TestAddWebhookRequestDecoder(t *testing.T) {
Validator: Validators{},
Auth: "jwt",
},
{
Description: "No validator provided",
InputPayload: addWebhookDecoderInput(),
ExpectedDecodedRequest: addWebhookDecoderOutput(true),
Auth: "jwt",
},
{
Description: "Do not check PartnerIDs",
InputPayload: addWebhookDecoderInput(),
Expand Down
7 changes: 7 additions & 0 deletions webhookValidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ func (vf ValidatorFunc) Validate(w Webhook) error {
return vf(w)
}

// AlwaysValid doesn't check anything in the webhook and never returns an error.
func AlwaysValid() ValidatorFunc {
return func(w Webhook) error {
return nil
}
}

// CheckEvents makes sure there is at least one value in Events and ensures that
// all values should parse into regex.
func CheckEvents() ValidatorFunc {
Expand Down
5 changes: 5 additions & 0 deletions webhookValidator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ var (
negativeThreeDuration = -3 * time.Minute
)

func TestAlwaysValid(t *testing.T) {
err := AlwaysValid()(Webhook{})
assert.NoError(t, err)
}

func TestCheckEvents(t *testing.T) {
tcs := []struct {
desc string
Expand Down

0 comments on commit a8af7ce

Please sign in to comment.