Permalink
Browse files

Refactored should_filter_params to use a matcher

  • Loading branch information...
1 parent 3ed10a4 commit f07f56e9a8dc61164c830ebeb5e48a04022c5ad9 @jferris jferris committed Feb 3, 2009
View
8 controller_progress.txt
@@ -1,17 +1,15 @@
Unfinished:
-should_filter_params
-should_not_set_the_flash
-should_render_with_layout
-should_render_without_layout
+should_not_set_the_flash should_set_the_flash_to
+should_render_with_layout should_render_without_layout
should_respond_with
should_respond_with_content_type
should_return_from_session
should_route
-should_set_the_flash_to
Finished:
should_assign_to
should_not_assign_to
+should_filter_params
Won't implement:
should_be_restful
View
9 lib/shoulda/controller/macros.rb
@@ -110,12 +110,9 @@ def should_not_set_the_flash
# should_filter_params :password, :ssn
def should_filter_params(*keys)
keys.each do |key|
- should "filter #{key}" do
- assert @controller.respond_to?(:filter_parameters),
- "The key #{key} is not filtered"
- filtered = @controller.send(:filter_parameters, {key.to_s => key.to_s})
- assert_equal '[FILTERED]', filtered[key.to_s],
- "The key #{key} is not filtered"
+ matcher = filter_param(key)
+ should matcher.description do
+ assert_accepts matcher, @controller
end
end
end
View
1 lib/shoulda/controller/matchers.rb
@@ -1,4 +1,5 @@
require 'shoulda/controller/matchers/assign_to_matcher'
+require 'shoulda/controller/matchers/filter_param_matcher'
module Shoulda # :nodoc:
module Controller # :nodoc:
View
57 lib/shoulda/controller/matchers/filter_param_matcher.rb
@@ -0,0 +1,57 @@
+module Shoulda # :nodoc:
+ module Controller # :nodoc:
+ module Matchers
+
+ # Ensures that filter_parameter_logging is set for the specified key.
+ #
+ # Example:
+ #
+ # it { should filter_param(:password) }
+ def filter_param(key)
+ FilterParamMatcher.new(key)
+ end
+
+ class FilterParamMatcher # :nodoc:
+
+ def initialize(key)
+ @key = key.to_s
+ end
+
+ def matches?(controller)
+ @controller = controller
+ filters_params? && filters_key?
+ end
+
+ def failure_message
+ "Expected #{@key} to be filtered"
+ end
+
+ def negative_failure_message
+ "Did not expect #{@key} to be filtered"
+ end
+
+ def description
+ "filter #{@key}"
+ end
+
+ private
+
+ def filters_params?
+ @controller.respond_to?(:filter_parameters)
+ end
+
+ def filters_key?
+ filtered_value == '[FILTERED]'
+ end
+
+ def filtered_value
+ filtered = @controller.send(:filter_parameters,
+ @key.to_s => @key.to_s)
+ filtered[@key.to_s]
+ end
+
+ end
+
+ end
+ end
+end
View
32 test/matchers/controller/filter_param_matcher_test.rb
@@ -0,0 +1,32 @@
+require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
+
+class FilterParamMatcherTest < Test::Unit::TestCase # :nodoc:
+
+ context "a controller that filters no parameters" do
+ setup do
+ @controller = define_controller(:examples).new
+ end
+
+ should "reject filtering any parameter" do
+ assert_rejects filter_param(:any), @controller
+ end
+ end
+
+ context "a controller that filters a parameter" do
+ setup do
+ @controller = define_controller :examples do
+ filter_parameter_logging :password
+ end.new
+ end
+
+ should "accept filtering that parameter" do
+ assert_accepts filter_param(:password), @controller
+ end
+
+ should "reject filtering another parameter" do
+ assert_rejects filter_param(:other), @controller
+ end
+ end
+
+end
+
View
4 test/model_builder.rb
@@ -15,6 +15,8 @@ def create_table(table_name, &block)
end
def define_constant(class_name, base, &block)
+ class_name = class_name.to_s.camelize
+
klass = Class.new(base)
Object.const_set(class_name, klass)
@@ -44,6 +46,8 @@ def define_model(name, columns = {}, &block)
end
def define_controller(class_name, &block)
+ class_name = class_name.to_s
+ class_name << 'Controller' unless class_name =~ /Controller$/
define_constant(class_name, ActionController::Base, &block)
end

0 comments on commit f07f56e

Please sign in to comment.