diff --git a/NEWS.md b/NEWS.md index e7ab1eba..793c7ba9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -12,6 +12,9 @@ session variable in question was set to nil (previously this actually did nothing). +* Fix `filter_param` so that it works when `config.filter_parameters` contains + regexes. + ### Improvements * `have_and_belongs_to_many` now checks to make sure that the join table diff --git a/lib/shoulda/matchers/action_controller/filter_param_matcher.rb b/lib/shoulda/matchers/action_controller/filter_param_matcher.rb index 652f5bcf..13b81be0 100644 --- a/lib/shoulda/matchers/action_controller/filter_param_matcher.rb +++ b/lib/shoulda/matchers/action_controller/filter_param_matcher.rb @@ -28,7 +28,7 @@ module Shoulda # @private class FilterParamMatcher def initialize(key) - @key = key.to_s + @key = key end def matches?(controller) @@ -52,11 +52,18 @@ module Shoulda private def filters_key? - filtered_keys.include?(@key) + filtered_keys.any? do |filter| + case filter + when Regexp + filter =~ @key + else + filter == @key + end + end end def filtered_keys - Rails.application.config.filter_parameters.map(&:to_s) + Rails.application.config.filter_parameters end end end diff --git a/spec/shoulda/matchers/action_controller/filter_param_matcher_spec.rb b/spec/shoulda/matchers/action_controller/filter_param_matcher_spec.rb index dc0a16f1..4d30146d 100644 --- a/spec/shoulda/matchers/action_controller/filter_param_matcher_spec.rb +++ b/spec/shoulda/matchers/action_controller/filter_param_matcher_spec.rb @@ -7,6 +7,12 @@ describe Shoulda::Matchers::ActionController::FilterParamMatcher do expect(nil).to filter_param(:secret) end + it 'accepts filtering a parameter matching a filtered regex' do + filter(/(?!tip)pin(?!g)/) + + expect(nil).to filter_param(:pin) + end + it 'rejects filtering an unfiltered parameter' do filter(:secret) matcher = filter_param(:other)