thoughtbot--shoulda-matchers/lib/shoulda/matchers/action_controller/filter_param_matcher.rb

71 lines
1.7 KiB
Ruby
Raw Permalink Normal View History

module Shoulda
2010-12-15 22:34:19 +00:00
module Matchers
module ActionController
# The `filter_param` matcher is used to test parameter filtering
# configuration. Specifically, it asserts that the given parameter is
# present in `config.filter_parameters`.
2010-12-15 22:34:19 +00:00
#
# class MyApplication < Rails::Application
# config.filter_parameters << :secret_key
# end
#
# # RSpec
# RSpec.describe ApplicationController, type: :controller do
# it { should filter_param(:secret_key) }
# end
#
# # Minitest (Shoulda)
# class ApplicationControllerTest < ActionController::TestCase
# should filter_param(:secret_key)
# end
#
# @return [FilterParamMatcher]
2010-12-15 22:34:19 +00:00
#
def filter_param(key)
FilterParamMatcher.new(key)
end
# @private
class FilterParamMatcher
2010-12-15 22:34:19 +00:00
def initialize(key)
2014-08-07 22:06:29 +00:00
@key = key
2010-12-15 22:34:19 +00:00
end
def matches?(_controller)
2010-12-15 22:34:19 +00:00
filters_key?
end
def failure_message
"Expected #{@key} to be filtered; filtered keys:"\
" #{filtered_keys.join(', ')}"
2010-12-15 22:34:19 +00:00
end
def failure_message_when_negated
2010-12-15 22:34:19 +00:00
"Did not expect #{@key} to be filtered"
end
def description
"filter #{@key}"
end
private
def filters_key?
2014-08-07 22:06:29 +00:00
filtered_keys.any? do |filter|
case filter
when Regexp
filter =~ @key
else
filter == @key
end
end
2010-12-15 22:34:19 +00:00
end
def filtered_keys
2014-08-07 22:06:29 +00:00
Rails.application.config.filter_parameters
2010-12-15 22:34:19 +00:00
end
end
end
end
end