bringing up to date with master branch
This commit is contained in:
@@ -290,6 +290,16 @@ describe CanCan::Ability do
|
||||
@ability.should be_fully_authorized(:update, :ranges)
|
||||
end
|
||||
|
||||
it "should accept a set as a condition value" do
|
||||
object_with_foo_2 = Object.new
|
||||
object_with_foo_2.should_receive(:foo) { 2 }
|
||||
object_with_foo_3 = Object.new
|
||||
object_with_foo_3.should_receive(:foo) { 3 }
|
||||
@ability.can :read, :objects, :foo => [1, 2, 5].to_set
|
||||
@ability.can?(:read, object_with_foo_2).should be_true
|
||||
@ability.can?(:read, object_with_foo_3).should be_false
|
||||
end
|
||||
|
||||
it "does not match subjects return nil for methods that must match nested a nested conditions hash" do
|
||||
object_with_foo = Object.new
|
||||
object_with_foo.should_receive(:foo) { :bar }
|
||||
@@ -353,7 +363,6 @@ describe CanCan::Ability do
|
||||
@ability.can?(:update, :books, :author).should be_false
|
||||
end
|
||||
|
||||
|
||||
# Hash Association
|
||||
|
||||
it "checks permission through association when hash is passed as subject" do
|
||||
@@ -363,6 +372,15 @@ describe CanCan::Ability do
|
||||
@ability.can?(:read, 123 => :books).should be_true
|
||||
end
|
||||
|
||||
it "checks permissions on association hash with multiple rules" do
|
||||
@ability.can :read, :books, :range => {:begin => 3}
|
||||
@ability.can :read, :books, :range => {:end => 6}
|
||||
@ability.can?(:read, (1..4) => :books).should be_false
|
||||
@ability.can?(:read, (3..5) => :books).should be_true
|
||||
@ability.can?(:read, (1..6) => :books).should be_true
|
||||
@ability.can?(:read, 123 => :books).should be_true
|
||||
end
|
||||
|
||||
it "checks ability on hash subclass" do
|
||||
class Container < Hash; end
|
||||
@ability.can :read, :containers
|
||||
@@ -509,4 +527,15 @@ describe CanCan::Ability do
|
||||
# @ability.unauthorized_message(:update, ArgumentError).should == "update argument error"
|
||||
end
|
||||
end
|
||||
|
||||
it "merges the rules from another ability" do
|
||||
@ability.can :use, :tools
|
||||
another_ability = Object.new
|
||||
another_ability.extend(CanCan::Ability)
|
||||
another_ability.can :use, :search
|
||||
|
||||
@ability.merge(another_ability)
|
||||
@ability.can?(:use, :search).should be_true
|
||||
@ability.send(:rules).size.should == 2
|
||||
end
|
||||
end
|
||||
|
||||
@@ -32,7 +32,7 @@ describe CanCan::ControllerAdditions do
|
||||
@controller.cannot?(:foo, :bar).should be_true
|
||||
end
|
||||
|
||||
it "load_and_authorize_resource should setup a before filter which passes call to ControllerResource" do
|
||||
it "load_and_authorize_resource adds a before filter which passes call to ControllerResource" do
|
||||
controller_resource = double("controller_resource")
|
||||
controller_resource.should_receive(:process)
|
||||
CanCan::ControllerResource.stub(:new).with(@controller, nil, :load => true, :authorize => true, :foo => :bar) { controller_resource }
|
||||
@@ -40,7 +40,7 @@ describe CanCan::ControllerAdditions do
|
||||
@controller_class.load_and_authorize_resource :foo => :bar
|
||||
end
|
||||
|
||||
it "load_and_authorize_resource should properly pass first argument as the resource name" do
|
||||
it "load_and_authorize_resource passes first argument as the resource name" do
|
||||
controller_resource = double("controller_resource")
|
||||
controller_resource.should_receive(:process)
|
||||
CanCan::ControllerResource.stub(:new).with(@controller, :project, :load => true, :authorize => true, :foo => :bar) { controller_resource }
|
||||
@@ -48,7 +48,15 @@ describe CanCan::ControllerAdditions do
|
||||
@controller_class.load_and_authorize_resource :project, :foo => :bar
|
||||
end
|
||||
|
||||
it "load_and_authorize_resource with :prepend should prepend the before filter" do
|
||||
it "load_and_authorize_resource passes :only, :except, :if, :unless options to before filter" do
|
||||
controller_resource = double("controller_resource")
|
||||
controller_resource.should_receive(:process)
|
||||
CanCan::ControllerResource.stub(:new).with(@controller, nil, :load => true, :authorize => true) { controller_resource }
|
||||
@controller_class.should_receive(:before_filter).with(:only => 1, :except => 2, :if => 3, :unless => 4).and_yield(@controller)
|
||||
@controller_class.load_and_authorize_resource :only => 1, :except => 2, :if => 3, :unless => 4
|
||||
end
|
||||
|
||||
it "load_and_authorize_resource with :prepend prepends the before filter" do
|
||||
@controller_class.should_receive(:prepend_before_filter).with({})
|
||||
@controller_class.load_and_authorize_resource :foo => :bar, :prepend => true
|
||||
end
|
||||
|
||||
@@ -35,6 +35,26 @@ describe CanCan::ControllerResource do
|
||||
@controller.instance_variable_get(:@project).should == project
|
||||
end
|
||||
|
||||
it "attempts to load a resource with the same namespace as the controller when using :: for namespace" do
|
||||
module SomeEngine
|
||||
class Project < ::Project; end
|
||||
end
|
||||
project = SomeEngine::Project.create!
|
||||
@params.merge!(:controller => "SomeEngine::ProjectsController", :action => "show", :id => project.id)
|
||||
CanCan::ControllerResource.new(@controller, :load => true).process
|
||||
@controller.instance_variable_get(:@project).should == project
|
||||
end
|
||||
|
||||
# Rails includes namespace in params, see issue #349
|
||||
it "creates through the namespaced params" do
|
||||
module SomeEngine
|
||||
class Project < ::Project; end
|
||||
end
|
||||
@params.merge!(:controller => "SomeEngine::ProjectsController", :action => "create", :some_engine_project => {:name => "foobar"})
|
||||
CanCan::ControllerResource.new(@controller, :load => true).process
|
||||
@controller.instance_variable_get(:@project).name.should == "foobar"
|
||||
end
|
||||
|
||||
it "loads resource for namespaced controller when using '::' for namespace" do
|
||||
project = Project.create!
|
||||
@params.merge!(:controller => "Admin::ProjectsController", :action => "show", :id => project.id)
|
||||
@@ -48,6 +68,15 @@ describe CanCan::ControllerResource do
|
||||
@controller.instance_variable_get(:@project).name.should == "foobar"
|
||||
end
|
||||
|
||||
it "builds a new resource for namespaced model with hash if params[:id] is not specified" do
|
||||
module SomeEngine
|
||||
class Project < ::Project; end
|
||||
end
|
||||
@params.merge!(:action => "create", :some_engine_project => {:name => "foobar"})
|
||||
CanCan::ControllerResource.new(@controller, :load => true, :class => SomeEngine::Project).process
|
||||
@controller.instance_variable_get(:@project).name.should == "foobar"
|
||||
end
|
||||
|
||||
it "builds a new resource with attributes from current ability" do
|
||||
@params.merge!(:action => "new")
|
||||
@ability.can(:create, :projects, :name => "from conditions")
|
||||
@@ -169,6 +198,11 @@ describe CanCan::ControllerResource do
|
||||
resource.should_not be_parent
|
||||
end
|
||||
|
||||
it "has the specified resource_class if name is passed to load_resource" do
|
||||
resource = CanCan::ControllerResource.new(@controller, :category)
|
||||
resource.send(:resource_class).should == Category
|
||||
end
|
||||
|
||||
it "loads parent resource through proper id parameter" do
|
||||
project = Project.create!
|
||||
@params.merge!(:action => "index", :project_id => project.id)
|
||||
@@ -226,23 +260,18 @@ describe CanCan::ControllerResource do
|
||||
it "named resources should be loaded independently of the controller name" do
|
||||
category = Category.create!
|
||||
@params.merge!(:action => "new", :category_id => category.id)
|
||||
|
||||
CanCan::ControllerResource.new(@controller, :category, :load => true).process
|
||||
CanCan::ControllerResource.new(@controller, :project, :load => true, :through => :category).process
|
||||
|
||||
@controller.instance_variable_get(:@category).should eq(category)
|
||||
|
||||
project = @controller.instance_variable_get(:@project)
|
||||
project.category.should eq(category)
|
||||
end
|
||||
|
||||
|
||||
it "parent resources shouldn't be altered" do
|
||||
category = Category.create!
|
||||
@params.merge!(:action => "create", :category_id => category.id, :project => { :name => 'foo' })
|
||||
|
||||
CanCan::ControllerResource.new(@controller, :category, :load => true).process
|
||||
CanCan::ControllerResource.new(@controller, :project, :load => true, :through => :category).process
|
||||
|
||||
project = @controller.instance_variable_get(:@project)
|
||||
project.new_record?.should eq(true)
|
||||
project.name.should eq('foo')
|
||||
@@ -329,6 +358,16 @@ describe CanCan::ControllerResource do
|
||||
@controller.instance_variable_get(:@project).should == project
|
||||
end
|
||||
|
||||
it "loads the model using a custom namespaced class" do
|
||||
module SomeEngine
|
||||
class Project < ::Project; end
|
||||
end
|
||||
project = SomeEngine::Project.create!
|
||||
@params.merge!(:action => "show", :id => project.id)
|
||||
CanCan::ControllerResource.new(@controller, :load => true, :class => SomeEngine::Project).process
|
||||
@controller.instance_variable_get(:@project).should == project
|
||||
end
|
||||
|
||||
it "does not authorize based on resource name if class is false because we don't do class level authorization anymore" do
|
||||
@params.merge!(:action => "show", :id => 123)
|
||||
@controller.stub(:authorize!).with(:show, :projects) { raise CanCan::Unauthorized }
|
||||
|
||||
@@ -39,4 +39,20 @@ describe CanCan::InheritedResource do
|
||||
CanCan::InheritedResource.new(@controller, :load => true).process
|
||||
@controller.instance_variable_get(:@projects).should == :projects
|
||||
end
|
||||
|
||||
it "should build a new resource with attributes from current ability" do
|
||||
@params[:action] = "new"
|
||||
@ability.can(:create, :projects, :name => "from conditions")
|
||||
@controller.stub(:build_resource) { Struct.new(:name).new }
|
||||
CanCan::InheritedResource.new(@controller, :load => true).process
|
||||
@controller.instance_variable_get(:@project).name.should == "from conditions"
|
||||
end
|
||||
|
||||
it "should override initial attributes with params" do
|
||||
@params.merge!(:action => "new", :project => {:name => "from params"})
|
||||
@ability.can(:create, :projects, :name => "from conditions")
|
||||
@controller.stub(:build_resource) { Struct.new(:name).new }
|
||||
CanCan::ControllerResource.new(@controller, :load => true).process
|
||||
@controller.instance_variable_get(:@project).name.should == "from params"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -228,6 +228,17 @@ if ENV["MODEL_ADAPTER"].nil? || ENV["MODEL_ADAPTER"] == "active_record"
|
||||
@ability.should_not be_able_to(:read, article2)
|
||||
end
|
||||
|
||||
it "should merge MetaWhere and non-MetaWhere conditions" do
|
||||
pending
|
||||
@ability.can :read, Article, :priority.lt => 2
|
||||
@ability.can :read, Article, :priority => 1
|
||||
article1 = Article.create!(:priority => 1)
|
||||
article2 = Article.create!(:priority => 3)
|
||||
Article.accessible_by(@ability).should == [article1]
|
||||
@ability.should be_able_to(:read, article1)
|
||||
@ability.should_not be_able_to(:read, article2)
|
||||
end
|
||||
|
||||
it "matches any MetaWhere condition" do
|
||||
pending
|
||||
adapter = CanCan::ModelAdapters::ActiveRecordAdapter
|
||||
|
||||
@@ -71,6 +71,18 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
|
||||
MongoidProject.accessible_by(@ability, :read).entries.should == [sir]
|
||||
end
|
||||
|
||||
it "returns the correct records when a mix of can and cannot rules in defined ability" do
|
||||
pending "TODO figure out why this isn't working"
|
||||
@ability.can :manage, :mongoid_projects, :title => 'Sir'
|
||||
@ability.cannot :destroy, :mongoid_projects
|
||||
|
||||
sir = MongoidProject.create(:title => 'Sir')
|
||||
lord = MongoidProject.create(:title => 'Lord')
|
||||
dude = MongoidProject.create(:title => 'Dude')
|
||||
|
||||
MongoidProject.accessible_by(@ability, :destroy).entries.should == [sir]
|
||||
end
|
||||
|
||||
it "is able to mix empty conditions and hashes" do
|
||||
pending "TODO figure out why this isn't working"
|
||||
@ability.can :read, :mongoid_projects
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
require "spec_helper"
|
||||
require "ostruct" # for OpenStruct below
|
||||
|
||||
# Most of Rule functionality is tested in Ability specs
|
||||
describe CanCan::Rule do
|
||||
@@ -45,4 +46,10 @@ describe CanCan::Rule do
|
||||
CanCan::Rule.new(false, :read, :integers, :foo => :bar).specificity.should eq(4)
|
||||
CanCan::Rule.new(false, :read, :integers, :foo).specificity.should eq(4)
|
||||
end
|
||||
|
||||
it "should not be mergeable if conditions are not simple hashes" do
|
||||
meta_where = OpenStruct.new(:name => 'metawhere', :column => 'test')
|
||||
@conditions[meta_where] = :bar
|
||||
@rule.should be_unmergeable
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user