2014-01-23 18:07:36 +00:00
|
|
|
module Shoulda
|
2010-12-15 22:34:19 +00:00
|
|
|
module Matchers
|
2014-01-23 18:07:36 +00:00
|
|
|
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
|
|
|
#
|
2014-01-23 18:07:36 +00:00
|
|
|
# class MyApplication < Rails::Application
|
|
|
|
# config.filter_parameters << :secret_key
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# # RSpec
|
2016-06-16 00:00:51 +00:00
|
|
|
# RSpec.describe ApplicationController, type: :controller do
|
2014-01-23 18:07:36 +00:00
|
|
|
# it { should filter_param(:secret_key) }
|
|
|
|
# end
|
|
|
|
#
|
2015-09-30 19:15:23 +00:00
|
|
|
# # Minitest (Shoulda)
|
2014-01-23 18:07:36 +00:00
|
|
|
# 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
|
|
|
|
|
2014-01-23 18:07:36 +00:00
|
|
|
# @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
|
|
|
|
|
2020-09-06 13:09:31 +00:00
|
|
|
def matches?(_controller)
|
2010-12-15 22:34:19 +00:00
|
|
|
filters_key?
|
|
|
|
end
|
|
|
|
|
2013-12-24 11:24:27 +00:00
|
|
|
def failure_message
|
2020-11-03 05:56:49 +00:00
|
|
|
"Expected #{@key} to be filtered; filtered keys:"\
|
|
|
|
" #{filtered_keys.join(', ')}"
|
2010-12-15 22:34:19 +00:00
|
|
|
end
|
|
|
|
|
2013-12-24 11:24:27 +00:00
|
|
|
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
|