· 7 years ago · Mar 09, 2018, 09:54 PM
1class ActionController::TestCase
2 def self.should_have_before_filters(*filters)
3 should "have before filters for #{filters.join(',')}" do
4 chain = controller.class.filter_chain.select {|filter| filter.class == ActionController::Filters::BeforeFilter}
5 methods = chain.map{|filter| filter.method}
6 filters.each{|filter| assert methods.include?(filter)}
7 end
8 end
9
10 def self.should_protect_from_forgery
11 should_have_before_filters :verify_authenticity_token
12 should "set the authenticity token" do
13 assert_equal :authenticity_token, controller.class.request_forgery_protection_token
14 end
15 end
16
17 def self.should_filter_parameter_logging(options)
18 keys = options[:for].map{|k| k.to_s}
19 keys.each {|key|
20 should "filter #{key} parameter values from the log" do
21 begin
22 assert_equal '[FILTERED]', controller.send(:filter_parameters, key => 'secret')[key]
23 rescue
24 flunk "Could not filter parameters"
25 end
26 end
27 }
28 end
29end