From 7e7fb14677fac1e151c93becc3636e7f6731cf51 Mon Sep 17 00:00:00 2001 From: Melissa Xie Date: Fri, 15 Mar 2013 16:54:16 -0400 Subject: [PATCH] Remove 'strong_parameters' matcher * See issue #252 * Also changes bourne dependency into a development dependency --- Gemfile.lock | 2 +- gemfiles/3.0.gemfile.lock | 2 +- gemfiles/3.1.gemfile.lock | 2 +- gemfiles/3.2.gemfile.lock | 2 +- lib/shoulda/matchers/action_controller.rb | 1 - .../strong_parameters_matcher.rb | 121 --------------- shoulda-matchers.gemspec | 2 +- .../strong_parameters_matcher_spec.rb | 142 ------------------ spec/support/controller_builder.rb | 24 --- 9 files changed, 5 insertions(+), 293 deletions(-) delete mode 100644 lib/shoulda/matchers/action_controller/strong_parameters_matcher.rb delete mode 100644 spec/shoulda/matchers/action_controller/strong_parameters_matcher_spec.rb diff --git a/Gemfile.lock b/Gemfile.lock index dab103a0..f22596e0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -3,7 +3,6 @@ PATH specs: shoulda-matchers (1.5.6) activesupport (>= 3.0.0) - bourne (~> 1.3) GEM remote: https://rubygems.org/ @@ -134,6 +133,7 @@ DEPENDENCIES activerecord-jdbcsqlite3-adapter appraisal (~> 0.4) aruba + bourne (~> 1.3) bundler (~> 1.1) cucumber (~> 1.1) jdbc-sqlite3 diff --git a/gemfiles/3.0.gemfile.lock b/gemfiles/3.0.gemfile.lock index 56878410..c30cd652 100644 --- a/gemfiles/3.0.gemfile.lock +++ b/gemfiles/3.0.gemfile.lock @@ -3,7 +3,6 @@ PATH specs: shoulda-matchers (1.5.6) activesupport (>= 3.0.0) - bourne (~> 1.3) GEM remote: https://rubygems.org/ @@ -125,6 +124,7 @@ DEPENDENCIES activerecord-jdbcsqlite3-adapter appraisal (~> 0.4) aruba + bourne (~> 1.3) bundler (~> 1.1) cucumber (~> 1.1) jdbc-sqlite3 diff --git a/gemfiles/3.1.gemfile.lock b/gemfiles/3.1.gemfile.lock index 9b3b01c7..1bde73b8 100644 --- a/gemfiles/3.1.gemfile.lock +++ b/gemfiles/3.1.gemfile.lock @@ -3,7 +3,6 @@ PATH specs: shoulda-matchers (1.5.6) activesupport (>= 3.0.0) - bourne (~> 1.3) GEM remote: https://rubygems.org/ @@ -144,6 +143,7 @@ DEPENDENCIES activerecord-jdbcsqlite3-adapter appraisal (~> 0.4) aruba + bourne (~> 1.3) bundler (~> 1.1) cucumber (~> 1.1) jdbc-sqlite3 diff --git a/gemfiles/3.2.gemfile.lock b/gemfiles/3.2.gemfile.lock index 33344fbe..6f5bea68 100644 --- a/gemfiles/3.2.gemfile.lock +++ b/gemfiles/3.2.gemfile.lock @@ -3,7 +3,6 @@ PATH specs: shoulda-matchers (1.5.6) activesupport (>= 3.0.0) - bourne (~> 1.3) GEM remote: https://rubygems.org/ @@ -142,6 +141,7 @@ DEPENDENCIES activerecord-jdbcsqlite3-adapter appraisal (~> 0.4) aruba + bourne (~> 1.3) bundler (~> 1.1) cucumber (~> 1.1) jdbc-sqlite3 diff --git a/lib/shoulda/matchers/action_controller.rb b/lib/shoulda/matchers/action_controller.rb index 999c20b9..e2069c39 100644 --- a/lib/shoulda/matchers/action_controller.rb +++ b/lib/shoulda/matchers/action_controller.rb @@ -8,7 +8,6 @@ require 'shoulda/matchers/action_controller/set_session_matcher' require 'shoulda/matchers/action_controller/route_matcher' require 'shoulda/matchers/action_controller/redirect_to_matcher' require 'shoulda/matchers/action_controller/render_template_matcher' -require 'shoulda/matchers/action_controller/strong_parameters_matcher' module Shoulda module Matchers diff --git a/lib/shoulda/matchers/action_controller/strong_parameters_matcher.rb b/lib/shoulda/matchers/action_controller/strong_parameters_matcher.rb deleted file mode 100644 index 19b440ec..00000000 --- a/lib/shoulda/matchers/action_controller/strong_parameters_matcher.rb +++ /dev/null @@ -1,121 +0,0 @@ -require 'bourne' -require 'active_support/deprecation' -begin - require 'strong_parameters' -rescue LoadError -end - -module Shoulda - module Matchers - module ActionController - def permit(*attributes) - attributes_and_context = attributes + [self] - StrongParametersMatcher.new(*attributes_and_context) - end - - class StrongParametersMatcher - def initialize(*attributes_and_context) - ActiveSupport::Deprecation.warn 'The strong_parameters matcher is deprecated and will be removed in 2.0' - @attributes = attributes_and_context[0...-1] - @context = attributes_and_context.last - @permitted_params = [] - end - - def for(action, options = {}) - @action = action - @verb = options[:verb] || verb_for_action - self - end - - def in_context(context) - @context = context - self - end - - def matches?(controller = nil) - simulate_controller_action && parameters_difference.empty? - end - - def does_not_match?(controller = nil) - simulate_controller_action && parameters_difference.present? - end - - def failure_message - "Expected controller to permit #{parameters_difference.to_sentence}, but it did not." - end - - def negative_failure_message - "Expected controller not to permit #{parameters_difference.to_sentence}, but it did." - end - - private - attr_reader :verb, :action, :attributes, :context - attr_accessor :permitted_params - - def simulate_controller_action - ensure_action_and_verb_present! - model_attrs = stubbed_model_attributes - - context.send(verb, action) - - verify_permit_call(model_attrs) - end - - def verify_permit_call(model_attrs) - matcher = Mocha::API::HaveReceived.new(:permit).with do |*params| - self.permitted_params = params - end - - matcher.matches?(model_attrs) - rescue Mocha::ExpectationError - false - end - - def parameters_difference - attributes - permitted_params - end - - def stubbed_model_attributes - extend Mocha::API - - model_attrs = ::ActionController::Parameters.new(arbitrary_attributes) - model_attrs.stubs(:permit) - ::ActionController::Parameters.any_instance.stubs(:[]).returns(model_attrs) - - model_attrs - end - - def ensure_action_and_verb_present! - if action.blank? - raise ActionNotDefinedError - end - if verb.blank? - raise VerbNotDefinedError - end - end - - def arbitrary_attributes - {:any_key => 'any_value'} - end - - def verb_for_action - verb_lookup = { :create => :post, :update => :put } - verb_lookup[action] - end - end - - class StrongParametersMatcher::ActionNotDefinedError < StandardError - def message - 'You must specify the controller action using the #for method.' - end - end - - class StrongParametersMatcher::VerbNotDefinedError < StandardError - def message - 'You must specify an HTTP verb when using a non-RESTful action.' + - ' e.g. for(:authorize, :verb => :post)' - end - end - end - end -end diff --git a/shoulda-matchers.gemspec b/shoulda-matchers.gemspec index b7aca17d..a455a04f 100644 --- a/shoulda-matchers.gemspec +++ b/shoulda-matchers.gemspec @@ -19,10 +19,10 @@ Gem::Specification.new do |s| s.require_paths = ["lib"] s.add_dependency('activesupport', '>= 3.0.0') - s.add_dependency('bourne', '~> 1.3') s.add_development_dependency('appraisal', '~> 0.4') s.add_development_dependency('aruba') + s.add_development_dependency('bourne', '~> 1.3') s.add_development_dependency('bundler', '~> 1.1') s.add_development_dependency('cucumber', '~> 1.1') s.add_development_dependency('rails', '~> 3.0') diff --git a/spec/shoulda/matchers/action_controller/strong_parameters_matcher_spec.rb b/spec/shoulda/matchers/action_controller/strong_parameters_matcher_spec.rb deleted file mode 100644 index af1d8264..00000000 --- a/spec/shoulda/matchers/action_controller/strong_parameters_matcher_spec.rb +++ /dev/null @@ -1,142 +0,0 @@ -require 'spec_helper' - -describe Shoulda::Matchers::ActionController do - describe ".permit" do - it "is true when the sent parameter is allowed" do - controller_class = controller_for_resource_with_strong_parameters do - params.require(:user).permit(:name) - end - - controller_class.should permit(:name).for(:create) - end - - it "is false when the sent parameter is not allowed" do - controller_class = controller_for_resource_with_strong_parameters do - params.require(:user).permit(:name) - end - - controller_class.should_not permit(:admin).for(:create) - end - - it "allows multiple attributes" do - controller_class = controller_for_resource_with_strong_parameters do - params.require(:user).permit(:name, :age) - end - - controller_class.should permit(:name, :age).for(:create) - end - end -end - -describe Shoulda::Matchers::ActionController::StrongParametersMatcher do - before do - controller_for_resource_with_strong_parameters do - params.require(:user).permit(:name, :age) - end - end - - describe "#matches?" do - it "is true for a subset of the allowable attributes" do - matcher = Shoulda::Matchers::ActionController::StrongParametersMatcher.new(:name, self).for(:create) - matcher.matches?.should be_true - end - - it "is true for all the allowable attributes" do - matcher = Shoulda::Matchers::ActionController::StrongParametersMatcher.new(:name, :age, self).for(:create) - matcher.matches?.should be_true - end - - it "is false when any attributes are not allowed" do - matcher = Shoulda::Matchers::ActionController::StrongParametersMatcher.new(:name, :admin, self).for(:create) - matcher.matches?.should be_false - end - - it "is false when permit is not called" do - matcher = Shoulda::Matchers::ActionController::StrongParametersMatcher.new(:name, self).for(:new, :verb => :get) - matcher.matches?.should be_false - end - - it "requires an action" do - matcher = Shoulda::Matchers::ActionController::StrongParametersMatcher.new(:name, self) - expect{ matcher.matches? }.to raise_error(Shoulda::Matchers::ActionController::StrongParametersMatcher::ActionNotDefinedError) - end - - it "requires a verb for non-restful action" do - matcher = Shoulda::Matchers::ActionController::StrongParametersMatcher.new(:name, self).for(:authorize) - expect{ matcher.matches? }.to raise_error(Shoulda::Matchers::ActionController::StrongParametersMatcher::VerbNotDefinedError) - end - end - - describe "#does_not_match?" do - it "it is true if any of the given attributes are allowed" do - matcher = Shoulda::Matchers::ActionController::StrongParametersMatcher.new(:name, :admin, self).for(:create) - matcher.does_not_match?.should be_true - end - - it "it is false if all of the given attribtues are allowed" do - matcher = Shoulda::Matchers::ActionController::StrongParametersMatcher.new(:name, :age, self).for(:create) - matcher.does_not_match?.should be_false - end - end - - describe "#failure_message" do - it "includes all missing attributes" do - matcher = Shoulda::Matchers::ActionController::StrongParametersMatcher.new(:name, :age, :city, :country, self).for(:create) - matcher.matches? - - matcher.failure_message.should eq("Expected controller to permit city and country, but it did not.") - end - end - - describe "#negative_failure_message" do - it "includes all attributes that should not have been allowed but were" do - matcher = Shoulda::Matchers::ActionController::StrongParametersMatcher.new(:name, :age, :city, :country, self).for(:create) - matcher.does_not_match?.should be_true - - matcher.negative_failure_message.should eq("Expected controller not to permit city and country, but it did.") - end - end - - describe "#for" do - context "when given :create" do - it "posts to the controller" do - context = stub('context', :post => nil) - matcher = Shoulda::Matchers::ActionController::StrongParametersMatcher.new(:name, context).for(:create) - - matcher.matches? - context.should have_received(:post).with(:create) - end - end - - context "when given :update" do - it "puts to the controller" do - context = stub('context', :put => nil) - matcher = Shoulda::Matchers::ActionController::StrongParametersMatcher.new(:name, context).for(:update) - - matcher.matches? - context.should have_received(:put).with(:update) - end - end - - context "when given a custom action and verb" do - it "puts to the controller" do - context = stub('context', :delete => nil) - matcher = Shoulda::Matchers::ActionController::StrongParametersMatcher.new(:name, context).for(:hide, :verb => :delete) - - matcher.matches? - context.should have_received(:delete).with(:hide) - end - end - end - - describe "#in_context" do - it 'sets the object the controller action is sent to' do - context = stub('context', :post => nil) - matcher = Shoulda::Matchers::ActionController::StrongParametersMatcher.new(:name, nil).for(:create).in_context(context) - - matcher.matches? - - context.should have_received(:post).with(:create) - end - end -end diff --git a/spec/support/controller_builder.rb b/spec/support/controller_builder.rb index 9b6186f4..5c145302 100644 --- a/spec/support/controller_builder.rb +++ b/spec/support/controller_builder.rb @@ -64,30 +64,6 @@ module ControllerBuilder File.open(full_path, 'w') { |file| file.write(contents) } end - def controller_for_resource_with_strong_parameters(&block) - define_model "User" - controller_class = define_controller "Users" do - def new - @user = User.new - render :nothing => true - end - - def create - @user = User.create(user_params) - render :nothing => true - end - - private - define_method :user_params, &block - end - - setup_rails_controller_test(controller_class) - - define_routes { resources :users } - - controller_class - end - private def delete_temporary_views