Remove 'strong_parameters' matcher
* See issue #252 * Also changes bourne dependency into a development dependency
This commit is contained in:
parent
d3525f129e
commit
7e7fb14677
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
|
@ -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')
|
||||
|
|
|
@ -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
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue