spec/authority/controller_spec.rb in authority-2.8.1 vs spec/authority/controller_spec.rb in authority-2.9.0
- old
+ new
@@ -1,9 +1,10 @@
require 'spec_helper'
require 'support/example_classes'
require 'support/mock_rails'
require 'active_support/core_ext/proc'
+require 'set'
describe Authority::Controller do
class ExampleController
def self.rescue_from(*args) ; end
@@ -85,10 +86,12 @@
describe "class methods" do
describe "authorize_actions_for" do
+ let(:child_controller) { Class.new(controller_class) }
+
it "allows specifying the class of the model to protect" do
controller_class.authorize_actions_for(resource_class)
expect(controller_class.authority_resource).to eq(resource_class)
end
@@ -101,12 +104,18 @@
filter_options = {:only => [:show, :edit, :update]}
controller_class.should_receive(:before_filter).with(:run_authorization_check, filter_options)
controller_class.authorize_actions_for(resource_class, filter_options)
end
- it "passes the action hash to the `authority_action` method" do
- child_controller = Class.new(controller_class)
+ it "if :all_actions option is given, it overrides the action hash to use the action given" do
+ overridden_action_map = controller_class.authority_action_map
+ overridden_action_map.update(overridden_action_map) {|k,v| v = :annihilate}
+ child_controller.should_receive(:authority_actions).with(overridden_action_map)
+ child_controller.authorize_actions_for(resource_class, :all_actions => :annihilate)
+ end
+
+ it "passes the action hash to the `authority_actions` method" do
new_actions = {:synthesize => :create, :annihilate => 'delete'}
child_controller.should_receive(:authority_actions).with(new_actions)
child_controller.authorize_actions_for(resource_class, :actions => new_actions)
end
@@ -135,23 +144,56 @@
expect(controller_class.authority_resource).to eq(resource_class)
end
end
- describe "authority_action" do
+ describe "authority_actions" do
it "modifies this controller's authority action map" do
new_actions = {:show => :display, :synthesize => :create, :annihilate => 'delete'}
controller_class.authority_actions(new_actions)
expect(controller_class.authority_action_map).to eq(
Authority.configuration.controller_action_map.merge(new_actions)
)
end
+ it "forces to use a single method when :all_actions option is given" do
+ force_actions = {:all_actions => :utilize}
+ controller_class.authority_actions(force_actions)
+ expect(controller_class.authority_action_map.values.uniq).to eq([:utilize])
+ end
+
+ it "can be used multiple times; each usage appends methods to authority_action_map" do
+ controller_class.authority_actions({:all_actions => :utilize})
+ controller_class.authority_actions({:synthesize => :create})
+ controller_class.authority_actions({:transmogrify => :update})
+ expect(controller_class.authority_action_map.values.uniq.to_set).to eq([:create, :update, :utilize].to_set)
+ expect(controller_class.authority_action_map[:synthesize]).to eq(:create)
+ end
+
it "does not modify any other controller" do
child_controller = Class.new(controller_class)
child_controller.authority_actions(:smite => 'delete')
expect(controller_class.authority_action_map[:smite]).to eq(nil)
+ end
+
+ end
+
+ describe "overridden_actions" do
+
+ it "overrides authority action map if option :all_actions is present" do
+ options = { :all_actions => :display, :actions => {:show => :display, :synthesize => :create} }
+ expect(controller_class.overridden_actions(options).values.uniq).to eq([:display])
+ end
+
+ it "returns :actions hash if option :all_actions is not present" do
+ options = { :actions => {:show => :display, :synthesize => :create, :annihilate => 'delete'} }
+ expect(controller_class.overridden_actions(options)).to eq(options[:actions])
+ end
+
+ it "returns an empty hash if no :all_actions nor :actions options present" do
+ options = { :show => :display, :synthesize => :create, :annihilate => 'delete' }
+ expect(controller_class.overridden_actions(options)).to eq({})
end
end
describe "ensure_authorization_performed" do