changing should spec wording

This commit is contained in:
Ryan Bates 2012-04-22 15:15:09 -07:00
parent b37f2d083e
commit ec36137aa6
9 changed files with 134 additions and 134 deletions

View File

@ -290,7 +290,7 @@ describe CanCan::Ability do
@ability.should be_fully_authorized(:update, :ranges) @ability.should be_fully_authorized(:update, :ranges)
end end
it "should not match subjects return nil for methods that must match nested a nested conditions hash" do 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 = Object.new
object_with_foo.should_receive(:foo) { :bar } object_with_foo.should_receive(:foo) { :bar }
@ability.can :read, :arrays, :first => { :foo => :bar } @ability.can :read, :arrays, :first => { :foo => :bar }
@ -395,14 +395,14 @@ describe CanCan::Ability do
end end
end end
it "should not raise access denied exception if ability is authorized to perform an action and return subject" do it "does not raise access denied exception if ability is authorized to perform an action and return subject" do
@ability.can :read, :foo @ability.can :read, :foo
lambda { lambda {
@ability.authorize!(:read, :foo).should == :foo @ability.authorize!(:read, :foo).should == :foo
}.should_not raise_error }.should_not raise_error
end end
it "should know when block is used in conditions" do it "knows when block is used in conditions" do
@ability.can :read, :foo @ability.can :read, :foo
@ability.should_not have_block(:read, :foo) @ability.should_not have_block(:read, :foo)
@ability.can :read, :foo do |foo| @ability.can :read, :foo do |foo|
@ -411,14 +411,14 @@ describe CanCan::Ability do
@ability.should have_block(:read, :foo) @ability.should have_block(:read, :foo)
end end
it "should know when raw sql is used in conditions" do it "knows when raw sql is used in conditions" do
@ability.can :read, :foo @ability.can :read, :foo
@ability.should_not have_raw_sql(:read, :foo) @ability.should_not have_raw_sql(:read, :foo)
@ability.can :read, :foo, 'false' @ability.can :read, :foo, 'false'
@ability.should have_raw_sql(:read, :foo) @ability.should have_raw_sql(:read, :foo)
end end
it "should raise access denied exception with default message if not specified" do it "raises access denied exception with default message if not specified" do
begin begin
@ability.authorize! :read, :books @ability.authorize! :read, :books
rescue CanCan::Unauthorized => e rescue CanCan::Unauthorized => e

View File

@ -11,7 +11,7 @@ describe CanCan::ControllerAdditions do
@controller_class.send(:include, CanCan::ControllerAdditions) @controller_class.send(:include, CanCan::ControllerAdditions)
end end
it "should raise ImplementationRemoved when attempting to call load/authorize/skip/check calls on a controller" do it "raises ImplementationRemoved when attempting to call load/authorize/skip/check calls on a controller" do
lambda { @controller_class.load_resource }.should raise_error(CanCan::ImplementationRemoved) lambda { @controller_class.load_resource }.should raise_error(CanCan::ImplementationRemoved)
lambda { @controller_class.authorize_resource }.should raise_error(CanCan::ImplementationRemoved) lambda { @controller_class.authorize_resource }.should raise_error(CanCan::ImplementationRemoved)
lambda { @controller_class.skip_load_resource }.should raise_error(CanCan::ImplementationRemoved) lambda { @controller_class.skip_load_resource }.should raise_error(CanCan::ImplementationRemoved)
@ -26,7 +26,7 @@ describe CanCan::ControllerAdditions do
@controller.authorize!(:foo, :bar) @controller.authorize!(:foo, :bar)
end end
it "should provide a can? and cannot? methods which go through the current ability" do it "provides a can? and cannot? methods which go through the current ability" do
@controller.current_ability.should be_kind_of(Ability) @controller.current_ability.should be_kind_of(Ability)
@controller.can?(:foo, :bar).should be_false @controller.can?(:foo, :bar).should be_false
@controller.cannot?(:foo, :bar).should be_true @controller.cannot?(:foo, :bar).should be_true

View File

@ -12,55 +12,55 @@ describe CanCan::ControllerResource do
# @controller_class.stub(:cancan_skipper) { {:authorize => {}, :load => {}} } # @controller_class.stub(:cancan_skipper) { {:authorize => {}, :load => {}} }
end end
it "should load the resource into an instance variable if params[:id] is specified" do it "loads the resource into an instance variable if params[:id] is specified" do
project = Project.create! project = Project.create!
@params.merge!(:action => "show", :id => project.id) @params.merge!(:action => "show", :id => project.id)
CanCan::ControllerResource.new(@controller, :load => true).process CanCan::ControllerResource.new(@controller, :load => true).process
@controller.instance_variable_get(:@project).should == project @controller.instance_variable_get(:@project).should == project
end end
it "should not load resource into an instance variable if already set" do it "does not load resource into an instance variable if already set" do
@params.merge!(:action => "show", :id => 123) @params.merge!(:action => "show", :id => 123)
@controller.instance_variable_set(:@project, :some_project) @controller.instance_variable_set(:@project, :some_project)
CanCan::ControllerResource.new(@controller, :load => true).process CanCan::ControllerResource.new(@controller, :load => true).process
@controller.instance_variable_get(:@project).should == :some_project @controller.instance_variable_get(:@project).should == :some_project
end end
it "should properly load resource for namespaced controller" do it "loads resource for namespaced controller" do
project = Project.create! project = Project.create!
@params.merge!(:controller => "admin/projects", :action => "show", :id => project.id) @params.merge!(:controller => "admin/projects", :action => "show", :id => project.id)
CanCan::ControllerResource.new(@controller, :load => true).process CanCan::ControllerResource.new(@controller, :load => true).process
@controller.instance_variable_get(:@project).should == project @controller.instance_variable_get(:@project).should == project
end end
it "should properly load resource for namespaced controller when using '::' for namespace" do it "loads resource for namespaced controller when using '::' for namespace" do
project = Project.create! project = Project.create!
@params.merge!(:controller => "Admin::ProjectsController", :action => "show", :id => project.id) @params.merge!(:controller => "Admin::ProjectsController", :action => "show", :id => project.id)
CanCan::ControllerResource.new(@controller, :load => true).process CanCan::ControllerResource.new(@controller, :load => true).process
@controller.instance_variable_get(:@project).should == project @controller.instance_variable_get(:@project).should == project
end end
it "should build a new resource with hash if params[:id] is not specified and authorize on each attribute" do it "builds a new resource with hash if params[:id] is not specified and authorize on each attribute" do
@params.merge!(:action => "create", :project => {:name => "foobar"}) @params.merge!(:action => "create", :project => {:name => "foobar"})
CanCan::ControllerResource.new(@controller, :load => true).process CanCan::ControllerResource.new(@controller, :load => true).process
@controller.instance_variable_get(:@project).name.should == "foobar" @controller.instance_variable_get(:@project).name.should == "foobar"
end end
it "should build a new resource with attributes from current ability" do it "builds a new resource with attributes from current ability" do
@params.merge!(:action => "new") @params.merge!(:action => "new")
@ability.can(:create, :projects, :name => "from conditions") @ability.can(:create, :projects, :name => "from conditions")
CanCan::ControllerResource.new(@controller, :load => true).process CanCan::ControllerResource.new(@controller, :load => true).process
@controller.instance_variable_get(:@project).name.should == "from conditions" @controller.instance_variable_get(:@project).name.should == "from conditions"
end end
it "should override initial attributes with params" do it "overrides initial attributes with params" do
@params.merge!(:action => "new", :project => {:name => "from params"}) @params.merge!(:action => "new", :project => {:name => "from params"})
@ability.can(:create, :projects, :name => "from conditions") @ability.can(:create, :projects, :name => "from conditions")
CanCan::ControllerResource.new(@controller, :load => true).process CanCan::ControllerResource.new(@controller, :load => true).process
@controller.instance_variable_get(:@project).name.should == "from params" @controller.instance_variable_get(:@project).name.should == "from params"
end end
it "should build a collection when on index action when class responds to accessible_by and mark ability as fully authorized" do it "builds a collection when on index action when class responds to accessible_by and mark ability as fully authorized" do
Project.stub(:accessible_by).with(@ability, :index) { :found_projects } Project.stub(:accessible_by).with(@ability, :index) { :found_projects }
@params[:action] = "index" @params[:action] = "index"
CanCan::ControllerResource.new(@controller, :project, :load => true).process CanCan::ControllerResource.new(@controller, :project, :load => true).process
@ -69,7 +69,7 @@ describe CanCan::ControllerResource do
@ability.should be_fully_authorized(:index, :projects) @ability.should be_fully_authorized(:index, :projects)
end end
it "should not build a collection when on index action when class does not respond to accessible_by and not mark ability as fully authorized" do it "does not build a collection when on index action when class does not respond to accessible_by and not mark ability as fully authorized" do
@params[:action] = "index" @params[:action] = "index"
CanCan::ControllerResource.new(@controller, :load => true).process CanCan::ControllerResource.new(@controller, :load => true).process
@controller.instance_variable_get(:@project).should be_nil @controller.instance_variable_get(:@project).should be_nil
@ -77,7 +77,7 @@ describe CanCan::ControllerResource do
@ability.should_not be_fully_authorized(:index, :projects) @ability.should_not be_fully_authorized(:index, :projects)
end end
it "should not use accessible_by when defining abilities through a block" do it "does not use accessible_by when defining abilities through a block" do
Project.stub(:accessible_by).with(@ability) { :found_projects } Project.stub(:accessible_by).with(@ability) { :found_projects }
@params[:action] = "index" @params[:action] = "index"
@ability.can(:read, :projects) { |p| false } @ability.can(:read, :projects) { |p| false }
@ -86,7 +86,7 @@ describe CanCan::ControllerResource do
@controller.instance_variable_defined?(:@projects).should be_false @controller.instance_variable_defined?(:@projects).should be_false
end end
it "should not authorize resource in collection action" do it "does not authorize resource in collection action" do
@params[:action] = "index" @params[:action] = "index"
@controller.instance_variable_set(:@project, :some_project) @controller.instance_variable_set(:@project, :some_project)
@controller.stub(:authorize!).with(:index, :projects) { raise CanCan::Unauthorized } @controller.stub(:authorize!).with(:index, :projects) { raise CanCan::Unauthorized }
@ -94,7 +94,7 @@ describe CanCan::ControllerResource do
lambda { resource.process }.should_not raise_error(CanCan::Unauthorized) lambda { resource.process }.should_not raise_error(CanCan::Unauthorized)
end end
it "should authorize parent resource in collection action" do it "authorizes parent resource in collection action" do
@params[:action] = "index" @params[:action] = "index"
@controller.instance_variable_set(:@category, :some_category) @controller.instance_variable_set(:@category, :some_category)
@controller.stub(:authorize!).with(:show, :some_category) { raise CanCan::Unauthorized } @controller.stub(:authorize!).with(:show, :some_category) { raise CanCan::Unauthorized }
@ -102,7 +102,7 @@ describe CanCan::ControllerResource do
lambda { resource.process }.should raise_error(CanCan::Unauthorized) lambda { resource.process }.should raise_error(CanCan::Unauthorized)
end end
it "should perform authorization using controller action and loaded model" do it "performs authorization using controller action and loaded model" do
@params.merge!(:action => "show", :id => 123) @params.merge!(:action => "show", :id => 123)
@controller.instance_variable_set(:@project, :some_project) @controller.instance_variable_set(:@project, :some_project)
@controller.stub(:authorize!).with(:show, :some_project) { raise CanCan::Unauthorized } @controller.stub(:authorize!).with(:show, :some_project) { raise CanCan::Unauthorized }
@ -110,20 +110,20 @@ describe CanCan::ControllerResource do
lambda { resource.process }.should raise_error(CanCan::Unauthorized) lambda { resource.process }.should raise_error(CanCan::Unauthorized)
end end
it "should not perform authorization using controller action when no loaded model" do it "does not perform authorization using controller action when no loaded model" do
@params.merge!(:action => "show", :id => 123) @params.merge!(:action => "show", :id => 123)
@controller.stub(:authorize!).with(:show, :projects) { raise CanCan::Unauthorized } @controller.stub(:authorize!).with(:show, :projects) { raise CanCan::Unauthorized }
resource = CanCan::ControllerResource.new(@controller, :authorize => true) resource = CanCan::ControllerResource.new(@controller, :authorize => true)
lambda { resource.process }.should_not raise_error(CanCan::Unauthorized) lambda { resource.process }.should_not raise_error(CanCan::Unauthorized)
end end
it "should not build a single resource when on custom collection action even with id" do it "does not build a single resource when on custom collection action even with id" do
@params.merge!(:action => "sort", :id => 123) @params.merge!(:action => "sort", :id => 123)
CanCan::ControllerResource.new(@controller, :load => true, :collection => [:sort, :list]).process CanCan::ControllerResource.new(@controller, :load => true, :collection => [:sort, :list]).process
@controller.instance_variable_get(:@project).should be_nil @controller.instance_variable_get(:@project).should be_nil
end end
it "should load a collection resource when on custom action with no id param" do it "loads a collection resource when on custom action with no id param" do
Project.stub(:accessible_by).with(@ability, :sort) { :found_projects } Project.stub(:accessible_by).with(@ability, :sort) { :found_projects }
@params[:action] = "sort" @params[:action] = "sort"
CanCan::ControllerResource.new(@controller, :load => true).process CanCan::ControllerResource.new(@controller, :load => true).process
@ -131,47 +131,47 @@ describe CanCan::ControllerResource do
@controller.instance_variable_get(:@projects).should == :found_projects @controller.instance_variable_get(:@projects).should == :found_projects
end end
it "should build a resource when on custom new action even when params[:id] exists" do it "builds a resource when on custom new action even when params[:id] exists" do
@params.merge!(:action => "build", :id => 123) @params.merge!(:action => "build", :id => 123)
Project.stub(:new) { :some_project } Project.stub(:new) { :some_project }
CanCan::ControllerResource.new(@controller, :load => true, :new => :build).process CanCan::ControllerResource.new(@controller, :load => true, :new => :build).process
@controller.instance_variable_get(:@project).should == :some_project @controller.instance_variable_get(:@project).should == :some_project
end end
it "should not try to load resource for other action if params[:id] is undefined" do it "does not try to load resource for other action if params[:id] is undefined" do
@params[:action] = "list" @params[:action] = "list"
CanCan::ControllerResource.new(@controller, :load => true).process CanCan::ControllerResource.new(@controller, :load => true).process
@controller.instance_variable_get(:@project).should be_nil @controller.instance_variable_get(:@project).should be_nil
end end
it "should be a parent resource when name is provided which doesn't match controller" do it "is a parent resource when name is provided which doesn't match controller" do
resource = CanCan::ControllerResource.new(@controller, :category) resource = CanCan::ControllerResource.new(@controller, :category)
resource.should be_parent resource.should be_parent
end end
it "should not be a parent resource when name is provided which matches controller" do it "does not be a parent resource when name is provided which matches controller" do
resource = CanCan::ControllerResource.new(@controller, :project) resource = CanCan::ControllerResource.new(@controller, :project)
resource.should_not be_parent resource.should_not be_parent
end end
it "should be parent if specified in options" do it "is parent if specified in options" do
resource = CanCan::ControllerResource.new(@controller, :project, {:parent => true}) resource = CanCan::ControllerResource.new(@controller, :project, {:parent => true})
resource.should be_parent resource.should be_parent
end end
it "should not be parent if specified in options" do it "does not be parent if specified in options" do
resource = CanCan::ControllerResource.new(@controller, :category, {:parent => false}) resource = CanCan::ControllerResource.new(@controller, :category, {:parent => false})
resource.should_not be_parent resource.should_not be_parent
end end
it "should load parent resource through proper id parameter" do it "loads parent resource through proper id parameter" do
project = Project.create! project = Project.create!
@params.merge!(:action => "index", :project_id => project.id) @params.merge!(:action => "index", :project_id => project.id)
CanCan::ControllerResource.new(@controller, :project, :load => true, :parent => true).process CanCan::ControllerResource.new(@controller, :project, :load => true, :parent => true).process
@controller.instance_variable_get(:@project).should == project @controller.instance_variable_get(:@project).should == project
end end
it "should load resource through the association of another parent resource using instance variable" do it "loads resource through the association of another parent resource using instance variable" do
@params.merge!(:action => "show", :id => 123) @params.merge!(:action => "show", :id => 123)
category = double("category", :projects => double("projects")) category = double("category", :projects => double("projects"))
category.projects.stub(:find).with(123) { :some_project } category.projects.stub(:find).with(123) { :some_project }
@ -180,7 +180,7 @@ describe CanCan::ControllerResource do
@controller.instance_variable_get(:@project).should == :some_project @controller.instance_variable_get(:@project).should == :some_project
end end
it "should load resource through the custom association name" do it "loads resource through the custom association name" do
@params.merge!(:action => "show", :id => 123) @params.merge!(:action => "show", :id => 123)
category = double("category", :custom_projects => double("custom_projects")) category = double("category", :custom_projects => double("custom_projects"))
category.custom_projects.stub(:find).with(123) { :some_project } category.custom_projects.stub(:find).with(123) { :some_project }
@ -189,7 +189,7 @@ describe CanCan::ControllerResource do
@controller.instance_variable_get(:@project).should == :some_project @controller.instance_variable_get(:@project).should == :some_project
end end
it "should load resource through the association of another parent resource using method" do it "loads resource through the association of another parent resource using method" do
@params.merge!(:action => "show", :id => 123) @params.merge!(:action => "show", :id => 123)
category = double("category", :projects => double("projects")) category = double("category", :projects => double("projects"))
@controller.stub(:category) { category } @controller.stub(:category) { category }
@ -198,14 +198,14 @@ describe CanCan::ControllerResource do
@controller.instance_variable_get(:@project).should == :some_project @controller.instance_variable_get(:@project).should == :some_project
end end
it "should not load through parent resource if instance isn't loaded when shallow" do it "does not load through parent resource if instance isn't loaded when shallow" do
project = Project.create! project = Project.create!
@params.merge!(:action => "show", :id => project.id) @params.merge!(:action => "show", :id => project.id)
CanCan::ControllerResource.new(@controller, :load => true, :through => :category, :shallow => true).process CanCan::ControllerResource.new(@controller, :load => true, :through => :category, :shallow => true).process
@controller.instance_variable_get(:@project).should == project @controller.instance_variable_get(:@project).should == project
end end
it "should raise Unauthorized when attempting to load resource through nil" do it "raises Unauthorized when attempting to load resource through nil" do
project = Project.create! project = Project.create!
@params.merge!(:action => "show", :id => project.id) @params.merge!(:action => "show", :id => project.id)
resource = CanCan::ControllerResource.new(@controller, :load => true, :through => :category) resource = CanCan::ControllerResource.new(@controller, :load => true, :through => :category)
@ -218,7 +218,7 @@ describe CanCan::ControllerResource do
@controller.instance_variable_get(:@project).should be_nil @controller.instance_variable_get(:@project).should be_nil
end end
it "should authorize nested resource through parent association on index action" do it "authorizes nested resource through parent association on index action" do
pending pending
@params.merge!(:action => "index") @params.merge!(:action => "index")
category = Object.new category = Object.new
@ -228,7 +228,7 @@ describe CanCan::ControllerResource do
lambda { resource.process }.should raise_error(CanCan::Unauthorized) lambda { resource.process }.should raise_error(CanCan::Unauthorized)
end end
it "should load through first matching if multiple are given" do it "loads through first matching if multiple are given" do
@params.merge!(:action => "show", :id => 123) @params.merge!(:action => "show", :id => 123)
category = double("category", :projects => double("projects")) category = double("category", :projects => double("projects"))
category.projects.stub(:find).with(123) { :some_project } category.projects.stub(:find).with(123) { :some_project }
@ -237,7 +237,7 @@ describe CanCan::ControllerResource do
@controller.instance_variable_get(:@project).should == :some_project @controller.instance_variable_get(:@project).should == :some_project
end end
it "should find record through has_one association with :singleton option without id param" do it "finds record through has_one association with :singleton option without id param" do
@params.merge!(:action => "show", :id => nil) @params.merge!(:action => "show", :id => nil)
category = Object.new category = Object.new
@controller.instance_variable_set(:@category, category) @controller.instance_variable_set(:@category, category)
@ -246,7 +246,7 @@ describe CanCan::ControllerResource do
@controller.instance_variable_get(:@project).should == :some_project @controller.instance_variable_get(:@project).should == :some_project
end end
it "should not build record through has_one association with :singleton option because it can cause it to delete it in the database" do it "does not build record through has_one association with :singleton option because it can cause it to delete it in the database" do
@params.merge!(:action => "create", :project => {:name => "foobar"}) @params.merge!(:action => "create", :project => {:name => "foobar"})
category = Category.new category = Category.new
@controller.instance_variable_set(:@category, category) @controller.instance_variable_set(:@category, category)
@ -255,20 +255,20 @@ describe CanCan::ControllerResource do
@controller.instance_variable_get(:@project).category.should == category @controller.instance_variable_get(:@project).category.should == category
end end
it "should find record through has_one association with :singleton and :shallow options" do it "finds record through has_one association with :singleton and :shallow options" do
project = Project.create! project = Project.create!
@params.merge!(:action => "show", :id => project.id) @params.merge!(:action => "show", :id => project.id)
CanCan::ControllerResource.new(@controller, :load => true, :through => :category, :singleton => true, :shallow => true).process CanCan::ControllerResource.new(@controller, :load => true, :through => :category, :singleton => true, :shallow => true).process
@controller.instance_variable_get(:@project).should == project @controller.instance_variable_get(:@project).should == project
end end
it "should build record through has_one association with :singleton and :shallow options" do it "builds record through has_one association with :singleton and :shallow options" do
@params.merge!(:action => "create", :project => {:name => "foobar"}) @params.merge!(:action => "create", :project => {:name => "foobar"})
CanCan::ControllerResource.new(@controller, :load => true, :through => :category, :singleton => true, :shallow => true).process CanCan::ControllerResource.new(@controller, :load => true, :through => :category, :singleton => true, :shallow => true).process
@controller.instance_variable_get(:@project).name.should == "foobar" @controller.instance_variable_get(:@project).name.should == "foobar"
end end
it "should only authorize :show action on parent resource" do it "only authorizes :show action on parent resource" do
project = Project.create! project = Project.create!
@params.merge!(:action => "new", :project_id => project.id) @params.merge!(:action => "new", :project_id => project.id)
@controller.stub(:authorize!).with(:show, project) { raise CanCan::Unauthorized } @controller.stub(:authorize!).with(:show, project) { raise CanCan::Unauthorized }
@ -276,7 +276,7 @@ describe CanCan::ControllerResource do
lambda { resource.process }.should raise_error(CanCan::Unauthorized) lambda { resource.process }.should raise_error(CanCan::Unauthorized)
end end
it "should authorize update action before setting attributes" do it "authorizes update action before setting attributes" do
@ability.can :update, :projects, :name => "bar" @ability.can :update, :projects, :name => "bar"
project = Project.create!(:name => "foo") project = Project.create!(:name => "foo")
@params.merge!(:action => "update", :id => project.id, :project => {:name => "bar"}) @params.merge!(:action => "update", :id => project.id, :project => {:name => "bar"})
@ -284,7 +284,7 @@ describe CanCan::ControllerResource do
lambda { resource.process }.should raise_error(CanCan::Unauthorized) lambda { resource.process }.should raise_error(CanCan::Unauthorized)
end end
it "should authorize update action after setting attributes" do it "authorizes update action after setting attributes" do
@ability.can :update, :projects, :name => "foo" @ability.can :update, :projects, :name => "foo"
project = Project.create!(:name => "foo") project = Project.create!(:name => "foo")
@params.merge!(:action => "update", :id => project.id, :project => {:name => "bar"}) @params.merge!(:action => "update", :id => project.id, :project => {:name => "bar"})
@ -292,21 +292,21 @@ describe CanCan::ControllerResource do
lambda { resource.process }.should raise_error(CanCan::Unauthorized) lambda { resource.process }.should raise_error(CanCan::Unauthorized)
end end
it "should load the model using a custom class" do it "loads the model using a custom class" do
project = Project.create! project = Project.create!
@params.merge!(:action => "show", :id => project.id) @params.merge!(:action => "show", :id => project.id)
CanCan::ControllerResource.new(@controller, :load => true, :class => Project).process CanCan::ControllerResource.new(@controller, :load => true, :class => Project).process
@controller.instance_variable_get(:@project).should == project @controller.instance_variable_get(:@project).should == project
end end
it "should not authorize based on resource name if class is false because we don't do class level authorization anymore" do 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) @params.merge!(:action => "show", :id => 123)
@controller.stub(:authorize!).with(:show, :projects) { raise CanCan::Unauthorized } @controller.stub(:authorize!).with(:show, :projects) { raise CanCan::Unauthorized }
resource = CanCan::ControllerResource.new(@controller, :authorize => true, :class => false) resource = CanCan::ControllerResource.new(@controller, :authorize => true, :class => false)
lambda { resource.process }.should_not raise_error(CanCan::Unauthorized) lambda { resource.process }.should_not raise_error(CanCan::Unauthorized)
end end
it "should load and authorize using custom instance name" do it "loads and authorize using custom instance name" do
project = Project.create! project = Project.create!
@params.merge!(:action => "show", :id => project.id) @params.merge!(:action => "show", :id => project.id)
@controller.stub(:authorize!).with(:show, project) { raise CanCan::Unauthorized } @controller.stub(:authorize!).with(:show, project) { raise CanCan::Unauthorized }
@ -315,7 +315,7 @@ describe CanCan::ControllerResource do
@controller.instance_variable_get(:@custom_project).should == project @controller.instance_variable_get(:@custom_project).should == project
end end
it "should load resource using custom ID param" do it "loads resource using custom ID param" do
project = Project.create! project = Project.create!
@params.merge!(:action => "show", :the_project => project.id) @params.merge!(:action => "show", :the_project => project.id)
resource = CanCan::ControllerResource.new(@controller, :id_param => :the_project, :load => true) resource = CanCan::ControllerResource.new(@controller, :id_param => :the_project, :load => true)
@ -323,35 +323,35 @@ describe CanCan::ControllerResource do
@controller.instance_variable_get(:@project).should == project @controller.instance_variable_get(:@project).should == project
end end
it "should load resource using custom find_by attribute" do it "loads resource using custom find_by attribute" do
project = Project.create!(:name => "foo") project = Project.create!(:name => "foo")
@params.merge!(:action => "show", :id => "foo") @params.merge!(:action => "show", :id => "foo")
CanCan::ControllerResource.new(@controller, :load => true, :find_by => :name).process CanCan::ControllerResource.new(@controller, :load => true, :find_by => :name).process
@controller.instance_variable_get(:@project).should == project @controller.instance_variable_get(:@project).should == project
end end
it "should authorize each new attribute in the create action" do it "authorizes each new attribute in the create action" do
@params.merge!(:action => "create", :project => {:name => "foo"}) @params.merge!(:action => "create", :project => {:name => "foo"})
@controller.instance_variable_set(:@project, :some_project) @controller.instance_variable_set(:@project, :some_project)
@ability.should_receive(:authorize!).with(:create, :some_project, :name) @ability.should_receive(:authorize!).with(:create, :some_project, :name)
CanCan::ControllerResource.new(@controller, :authorize => true).process CanCan::ControllerResource.new(@controller, :authorize => true).process
end end
it "should allow full find method to be passed into find_by option" do it "allows full find method to be passed into find_by option" do
project = Project.create!(:name => "foo") project = Project.create!(:name => "foo")
@params.merge!(:action => "show", :id => "foo") @params.merge!(:action => "show", :id => "foo")
CanCan::ControllerResource.new(@controller, :find_by => :find_by_name, :load => true).process CanCan::ControllerResource.new(@controller, :find_by => :find_by_name, :load => true).process
@controller.instance_variable_get(:@project).should == project @controller.instance_variable_get(:@project).should == project
end end
it "should authorize each new attribute in the update action" do it "authorizes each new attribute in the update action" do
@params.merge!(:action => "update", :id => 123, :project => {:name => "foo"}) @params.merge!(:action => "update", :id => 123, :project => {:name => "foo"})
@controller.instance_variable_set(:@project, :some_project) @controller.instance_variable_set(:@project, :some_project)
@ability.should_receive(:authorize!).with(:update, :some_project, :name) @ability.should_receive(:authorize!).with(:update, :some_project, :name)
CanCan::ControllerResource.new(@controller, :authorize => true).process CanCan::ControllerResource.new(@controller, :authorize => true).process
end end
it "should fetch member through method when instance variable is not provided" do it "fetches member through method when instance variable is not provided" do
@controller.stub(:project) { :some_project } @controller.stub(:project) { :some_project }
@params.merge!(:action => "show", :id => 123) @params.merge!(:action => "show", :id => 123)
@controller.stub(:authorize!).with(:show, :some_project) { raise CanCan::Unauthorized } @controller.stub(:authorize!).with(:show, :some_project) { raise CanCan::Unauthorized }
@ -359,7 +359,7 @@ describe CanCan::ControllerResource do
lambda { resource.process }.should raise_error(CanCan::Unauthorized) lambda { resource.process }.should raise_error(CanCan::Unauthorized)
end end
it "should attempt to load a resource with the same namespace as the controller when using :: for namespace" do it "attempts to load a resource with the same namespace as the controller when using :: for namespace" do
module Namespaced module Namespaced
class Project < ::Project; end class Project < ::Project; end
end end
@ -370,7 +370,7 @@ describe CanCan::ControllerResource do
end end
# Rails includes namespace in params, see issue #349 # Rails includes namespace in params, see issue #349
it "should create through namespaced params" do it "creates through namespaced params" do
module Namespaced module Namespaced
class Project < ::Project; end class Project < ::Project; end
end end
@ -379,25 +379,25 @@ describe CanCan::ControllerResource do
@controller.instance_variable_get(:@project).name.should == "foobar" @controller.instance_variable_get(:@project).name.should == "foobar"
end end
# it "should raise ImplementationRemoved when adding :name option" do # it "raises ImplementationRemoved when adding :name option" do
# lambda { # lambda {
# CanCan::ControllerResource.new(@controller, :name => :foo) # CanCan::ControllerResource.new(@controller, :name => :foo)
# }.should raise_error(CanCan::ImplementationRemoved) # }.should raise_error(CanCan::ImplementationRemoved)
# end # end
# #
# it "should raise ImplementationRemoved exception when specifying :resource option since it is no longer used" do # it "raises ImplementationRemoved exception when specifying :resource option since it is no longer used" do
# lambda { # lambda {
# CanCan::ControllerResource.new(@controller, :resource => Project) # CanCan::ControllerResource.new(@controller, :resource => Project)
# }.should raise_error(CanCan::ImplementationRemoved) # }.should raise_error(CanCan::ImplementationRemoved)
# end # end
# #
# it "should raise ImplementationRemoved exception when passing :nested option" do # it "raises ImplementationRemoved exception when passing :nested option" do
# lambda { # lambda {
# CanCan::ControllerResource.new(@controller, :nested => :project) # CanCan::ControllerResource.new(@controller, :nested => :project)
# }.should raise_error(CanCan::ImplementationRemoved) # }.should raise_error(CanCan::ImplementationRemoved)
# end # end
# it "should skip resource behavior for :only actions in array" do # it "skips resource behavior for :only actions in array" do
# @controller_class.stub(:cancan_skipper) { {:load => {nil => {:only => [:index, :show]}}} } # @controller_class.stub(:cancan_skipper) { {:load => {nil => {:only => [:index, :show]}}} }
# @params.merge!(:action => "index") # @params.merge!(:action => "index")
# CanCan::ControllerResource.new(@controller).skip?(:load).should be_true # CanCan::ControllerResource.new(@controller).skip?(:load).should be_true
@ -408,7 +408,7 @@ describe CanCan::ControllerResource do
# CanCan::ControllerResource.new(@controller).skip?(:load).should be_false # CanCan::ControllerResource.new(@controller).skip?(:load).should be_false
# end # end
# #
# it "should skip resource behavior for :only one action on resource" do # it "skips resource behavior for :only one action on resource" do
# @controller_class.stub(:cancan_skipper) { {:authorize => {:project => {:only => :index}}} } # @controller_class.stub(:cancan_skipper) { {:authorize => {:project => {:only => :index}}} }
# @params.merge!(:action => "index") # @params.merge!(:action => "index")
# CanCan::ControllerResource.new(@controller).skip?(:authorize).should be_false # CanCan::ControllerResource.new(@controller).skip?(:authorize).should be_false
@ -417,7 +417,7 @@ describe CanCan::ControllerResource do
# CanCan::ControllerResource.new(@controller, :project).skip?(:authorize).should be_false # CanCan::ControllerResource.new(@controller, :project).skip?(:authorize).should be_false
# end # end
# #
# it "should skip resource behavior :except actions in array" do # it "skips resource behavior :except actions in array" do
# @controller_class.stub(:cancan_skipper) { {:load => {nil => {:except => [:index, :show]}}} } # @controller_class.stub(:cancan_skipper) { {:load => {nil => {:except => [:index, :show]}}} }
# @params.merge!(:action => "index") # @params.merge!(:action => "index")
# CanCan::ControllerResource.new(@controller).skip?(:load).should be_false # CanCan::ControllerResource.new(@controller).skip?(:load).should be_false
@ -428,7 +428,7 @@ describe CanCan::ControllerResource do
# CanCan::ControllerResource.new(@controller, :some_resource).skip?(:load).should be_false # CanCan::ControllerResource.new(@controller, :some_resource).skip?(:load).should be_false
# end # end
# #
# it "should skip resource behavior :except one action on resource" do # it "skips resource behavior :except one action on resource" do
# @controller_class.stub(:cancan_skipper) { {:authorize => {:project => {:except => :index}}} } # @controller_class.stub(:cancan_skipper) { {:authorize => {:project => {:except => :index}}} }
# @params.merge!(:action => "index") # @params.merge!(:action => "index")
# CanCan::ControllerResource.new(@controller, :project).skip?(:authorize).should be_false # CanCan::ControllerResource.new(@controller, :project).skip?(:authorize).should be_false
@ -437,7 +437,7 @@ describe CanCan::ControllerResource do
# CanCan::ControllerResource.new(@controller, :project).skip?(:authorize).should be_true # CanCan::ControllerResource.new(@controller, :project).skip?(:authorize).should be_true
# end # end
# #
# it "should skip loading and authorization" do # it "skips loading and authorization" do
# @controller_class.stub(:cancan_skipper) { {:authorize => {nil => {}}, :load => {nil => {}}} } # @controller_class.stub(:cancan_skipper) { {:authorize => {nil => {}}, :load => {nil => {}}} }
# @params.merge!(:action => "new") # @params.merge!(:action => "new")
# resource = CanCan::ControllerResource.new(@controller) # resource = CanCan::ControllerResource.new(@controller)

View File

@ -6,12 +6,12 @@ describe CanCan::Unauthorized do
@exception = CanCan::Unauthorized.new(nil, :some_action, :some_subject) @exception = CanCan::Unauthorized.new(nil, :some_action, :some_subject)
end end
it "should have action and subject accessors" do it "has action and subject accessors" do
@exception.action.should == :some_action @exception.action.should == :some_action
@exception.subject.should == :some_subject @exception.subject.should == :some_subject
end end
it "should have a changable default message" do it "has a changable default message" do
@exception.message.should == "You are not authorized to access this page." @exception.message.should == "You are not authorized to access this page."
@exception.default_message = "Unauthorized!" @exception.default_message = "Unauthorized!"
@exception.message.should == "Unauthorized!" @exception.message.should == "Unauthorized!"
@ -23,12 +23,12 @@ describe CanCan::Unauthorized do
@exception = CanCan::Unauthorized.new("Access denied!") @exception = CanCan::Unauthorized.new("Access denied!")
end end
it "should have nil action and subject" do it "has nil action and subject" do
@exception.action.should be_nil @exception.action.should be_nil
@exception.subject.should be_nil @exception.subject.should be_nil
end end
it "should have passed message" do it "has passed message" do
@exception.message.should == "Access denied!" @exception.message.should == "Access denied!"
end end
end end

View File

@ -46,36 +46,36 @@ if ENV["MODEL_ADAPTER"].nil? || ENV["MODEL_ADAPTER"] == "active_record"
@comment_table = Comment.table_name @comment_table = Comment.table_name
end end
it "should be for only active record classes" do it "is for only active record classes" do
CanCan::ModelAdapters::ActiveRecordAdapter.should_not be_for_class(Object) CanCan::ModelAdapters::ActiveRecordAdapter.should_not be_for_class(Object)
CanCan::ModelAdapters::ActiveRecordAdapter.should be_for_class(Article) CanCan::ModelAdapters::ActiveRecordAdapter.should be_for_class(Article)
CanCan::ModelAdapters::AbstractAdapter.adapter_class(Article).should == CanCan::ModelAdapters::ActiveRecordAdapter CanCan::ModelAdapters::AbstractAdapter.adapter_class(Article).should == CanCan::ModelAdapters::ActiveRecordAdapter
end end
it "should find record" do it "finds record" do
article = Article.create! article = Article.create!
CanCan::ModelAdapters::ActiveRecordAdapter.find(Article, article.id).should == article CanCan::ModelAdapters::ActiveRecordAdapter.find(Article, article.id).should == article
end end
it "should not fetch any records when no abilities are defined" do it "does not fetch any records when no abilities are defined" do
Article.create! Article.create!
Article.accessible_by(@ability).should be_empty Article.accessible_by(@ability).should be_empty
end end
it "should fetch all articles when one can read all" do it "fetches all articles when one can read all" do
@ability.can :read, :articles @ability.can :read, :articles
article = Article.create! article = Article.create!
Article.accessible_by(@ability).should == [article] Article.accessible_by(@ability).should == [article]
end end
it "should fetch only the articles that are published" do it "fetches only the articles that are published" do
@ability.can :read, :articles, :published => true @ability.can :read, :articles, :published => true
article1 = Article.create!(:published => true) article1 = Article.create!(:published => true)
article2 = Article.create!(:published => false) article2 = Article.create!(:published => false)
Article.accessible_by(@ability).should == [article1] Article.accessible_by(@ability).should == [article1]
end end
it "should fetch any articles which are published or secret" do it "fetches any articles which are published or secret" do
@ability.can :read, :articles, :published => true @ability.can :read, :articles, :published => true
@ability.can :read, :articles, :secret => true @ability.can :read, :articles, :secret => true
article1 = Article.create!(:published => true, :secret => false) article1 = Article.create!(:published => true, :secret => false)
@ -85,7 +85,7 @@ if ENV["MODEL_ADAPTER"].nil? || ENV["MODEL_ADAPTER"] == "active_record"
Article.accessible_by(@ability).should == [article1, article2, article3] Article.accessible_by(@ability).should == [article1, article2, article3]
end end
it "should fetch only the articles that are published and not secret" do it "fetches only the articles that are published and not secret" do
@ability.can :read, :articles, :published => true @ability.can :read, :articles, :published => true
@ability.cannot :read, :articles, :secret => true @ability.cannot :read, :articles, :secret => true
article1 = Article.create!(:published => true, :secret => false) article1 = Article.create!(:published => true, :secret => false)
@ -95,21 +95,21 @@ if ENV["MODEL_ADAPTER"].nil? || ENV["MODEL_ADAPTER"] == "active_record"
Article.accessible_by(@ability).should == [article1] Article.accessible_by(@ability).should == [article1]
end end
it "should only read comments for articles which are published" do it "only reads comments for articles which are published" do
@ability.can :read, :comments, :article => { :published => true } @ability.can :read, :comments, :article => { :published => true }
comment1 = Comment.create!(:article => Article.create!(:published => true)) comment1 = Comment.create!(:article => Article.create!(:published => true))
comment2 = Comment.create!(:article => Article.create!(:published => false)) comment2 = Comment.create!(:article => Article.create!(:published => false))
Comment.accessible_by(@ability).should == [comment1] Comment.accessible_by(@ability).should == [comment1]
end end
it "should only read comments for visible categories through articles" do it "only reads comments for visible categories through articles" do
@ability.can :read, :comments, :article => { :category => { :visible => true } } @ability.can :read, :comments, :article => { :category => { :visible => true } }
comment1 = Comment.create!(:article => Article.create!(:category => Category.create!(:visible => true))) comment1 = Comment.create!(:article => Article.create!(:category => Category.create!(:visible => true)))
comment2 = Comment.create!(:article => Article.create!(:category => Category.create!(:visible => false))) comment2 = Comment.create!(:article => Article.create!(:category => Category.create!(:visible => false)))
Comment.accessible_by(@ability).should == [comment1] Comment.accessible_by(@ability).should == [comment1]
end end
it "should allow conditions in SQL and merge with hash conditions" do it "allows conditions in SQL and merge with hash conditions" do
@ability.can :read, :articles, :published => true @ability.can :read, :articles, :published => true
@ability.can :read, :articles, ["secret=?", true] @ability.can :read, :articles, ["secret=?", true]
article1 = Article.create!(:published => true, :secret => false) article1 = Article.create!(:published => true, :secret => false)
@ -119,14 +119,14 @@ if ENV["MODEL_ADAPTER"].nil? || ENV["MODEL_ADAPTER"] == "active_record"
Article.accessible_by(@ability).should == [article1, article2, article3] Article.accessible_by(@ability).should == [article1, article2, article3]
end end
it "should allow a scope for conditions" do it "allows a scope for conditions" do
@ability.can :read, :articles, Article.where(:secret => true) @ability.can :read, :articles, Article.where(:secret => true)
article1 = Article.create!(:secret => true) article1 = Article.create!(:secret => true)
article2 = Article.create!(:secret => false) article2 = Article.create!(:secret => false)
Article.accessible_by(@ability).should == [article1] Article.accessible_by(@ability).should == [article1]
end end
it "should fetch only associated records when using with a scope for conditions" do it "fetches only associated records when using with a scope for conditions" do
@ability.can :read, :articles, Article.where(:secret => true) @ability.can :read, :articles, Article.where(:secret => true)
category1 = Category.create!(:visible => false) category1 = Category.create!(:visible => false)
category2 = Category.create!(:visible => true) category2 = Category.create!(:visible => true)
@ -136,58 +136,58 @@ if ENV["MODEL_ADAPTER"].nil? || ENV["MODEL_ADAPTER"] == "active_record"
category1.articles.accessible_by(@ability).map(&:id).should == [article1.id] category1.articles.accessible_by(@ability).map(&:id).should == [article1.id]
end end
it "should raise an exception when trying to merge scope with other conditions" do it "raises an exception when trying to merge scope with other conditions" do
@ability.can :read, :articles, :published => true @ability.can :read, :articles, :published => true
@ability.can :read, :articles, Article.where(:secret => true) @ability.can :read, :articles, Article.where(:secret => true)
lambda { Article.accessible_by(@ability) }.should raise_error(CanCan::Error, "Unable to merge an Active Record scope with other conditions. Instead use a hash or SQL for read articles ability.") lambda { Article.accessible_by(@ability) }.should raise_error(CanCan::Error, "Unable to merge an Active Record scope with other conditions. Instead use a hash or SQL for read articles ability.")
end end
it "should not allow to fetch records when ability with just block present" do it "does not allow to fetch records when ability with just block present" do
@ability.can :read, :articles do @ability.can :read, :articles do
false false
end end
lambda { Article.accessible_by(@ability) }.should raise_error(CanCan::Error) lambda { Article.accessible_by(@ability) }.should raise_error(CanCan::Error)
end end
it "should not allow to check ability on object against SQL conditions without block" do it "does not allow to check ability on object against SQL conditions without block" do
@ability.can :read, :articles, ["secret=?", true] @ability.can :read, :articles, ["secret=?", true]
lambda { @ability.can? :read, Article.new }.should raise_error(CanCan::Error) lambda { @ability.can? :read, Article.new }.should raise_error(CanCan::Error)
end end
it "should have false conditions if no abilities match" do it "has false conditions if no abilities match" do
@ability.model_adapter(Article, :read).conditions.should == "'t'='f'" @ability.model_adapter(Article, :read).conditions.should == "'t'='f'"
end end
it "should return false conditions for cannot clause" do it "returns false conditions for cannot clause" do
@ability.cannot :read, :articles @ability.cannot :read, :articles
@ability.model_adapter(Article, :read).conditions.should == "'t'='f'" @ability.model_adapter(Article, :read).conditions.should == "'t'='f'"
end end
it "should return SQL for single `can` definition in front of default `cannot` condition" do it "returns SQL for single `can` definition in front of default `cannot` condition" do
@ability.cannot :read, :articles @ability.cannot :read, :articles
@ability.can :read, :articles, :published => false, :secret => true @ability.can :read, :articles, :published => false, :secret => true
@ability.model_adapter(Article, :read).conditions.should orderlessly_match(%Q["#{@article_table}"."published" = 'f' AND "#{@article_table}"."secret" = 't']) @ability.model_adapter(Article, :read).conditions.should orderlessly_match(%Q["#{@article_table}"."published" = 'f' AND "#{@article_table}"."secret" = 't'])
end end
it "should return true condition for single `can` definition in front of default `can` condition" do it "returns true condition for single `can` definition in front of default `can` condition" do
@ability.can :read, :articles @ability.can :read, :articles
@ability.can :read, :articles, :published => false, :secret => true @ability.can :read, :articles, :published => false, :secret => true
@ability.model_adapter(Article, :read).conditions.should eq(:secret => true, :published => false) @ability.model_adapter(Article, :read).conditions.should eq(:secret => true, :published => false)
end end
it "should return `false condition` for single `cannot` definition in front of default `cannot` condition" do it "returns `false condition` for single `cannot` definition in front of default `cannot` condition" do
@ability.cannot :read, :articles @ability.cannot :read, :articles
@ability.cannot :read, :articles, :published => false, :secret => true @ability.cannot :read, :articles, :published => false, :secret => true
@ability.model_adapter(Article, :read).conditions.should == "'t'='f'" @ability.model_adapter(Article, :read).conditions.should == "'t'='f'"
end end
it "should return `not (sql)` for single `cannot` definition in front of default `can` condition" do it "returns `not (sql)` for single `cannot` definition in front of default `can` condition" do
@ability.can :read, :articles @ability.can :read, :articles
@ability.cannot :read, :articles, :published => false, :secret => true @ability.cannot :read, :articles, :published => false, :secret => true
@ability.model_adapter(Article, :read).conditions.should orderlessly_match(%Q["not (#{@article_table}"."published" = 'f' AND "#{@article_table}"."secret" = 't')]) @ability.model_adapter(Article, :read).conditions.should orderlessly_match(%Q["not (#{@article_table}"."published" = 'f' AND "#{@article_table}"."secret" = 't')])
end end
it "should return appropriate sql conditions in complex case" do it "returns appropriate sql conditions in complex case" do
@ability.can :read, :articles @ability.can :read, :articles
@ability.can :access, :articles, :id => 1 @ability.can :access, :articles, :id => 1
@ability.can :update, :articles, :published => true @ability.can :update, :articles, :published => true
@ -197,7 +197,7 @@ if ENV["MODEL_ADAPTER"].nil? || ENV["MODEL_ADAPTER"] == "active_record"
@ability.model_adapter(Article, :read).conditions.should == {:id => 1} # used to be "t=t" but changed with new specificity rule (issue #321) @ability.model_adapter(Article, :read).conditions.should == {:id => 1} # used to be "t=t" but changed with new specificity rule (issue #321)
end end
it "should not forget conditions when calling with SQL string" do it "does not forget conditions when calling with SQL string" do
@ability.can :read, :articles, :published => true @ability.can :read, :articles, :published => true
@ability.can :read, :articles, ['secret=?', false] @ability.can :read, :articles, ['secret=?', false]
adapter = @ability.model_adapter(Article, :read) adapter = @ability.model_adapter(Article, :read)
@ -206,29 +206,29 @@ if ENV["MODEL_ADAPTER"].nil? || ENV["MODEL_ADAPTER"] == "active_record"
end end
end end
it "should have nil joins if no rules" do it "has nil joins if no rules" do
@ability.model_adapter(Article, :read).joins.should be_nil @ability.model_adapter(Article, :read).joins.should be_nil
end end
it "should have nil joins if no nested hashes specified in conditions" do it "has nil joins if no nested hashes specified in conditions" do
@ability.can :read, :articles, :published => false @ability.can :read, :articles, :published => false
@ability.can :read, :articles, :secret => true @ability.can :read, :articles, :secret => true
@ability.model_adapter(Article, :read).joins.should be_nil @ability.model_adapter(Article, :read).joins.should be_nil
end end
it "should merge separate joins into a single array" do it "merges separate joins into a single array" do
@ability.can :read, :articles, :project => { :blocked => false } @ability.can :read, :articles, :project => { :blocked => false }
@ability.can :read, :articles, :company => { :admin => true } @ability.can :read, :articles, :company => { :admin => true }
@ability.model_adapter(Article, :read).joins.inspect.should orderlessly_match([:company, :project].inspect) @ability.model_adapter(Article, :read).joins.inspect.should orderlessly_match([:company, :project].inspect)
end end
it "should merge same joins into a single array" do it "merges same joins into a single array" do
@ability.can :read, :articles, :project => { :blocked => false } @ability.can :read, :articles, :project => { :blocked => false }
@ability.can :read, :articles, :project => { :admin => true } @ability.can :read, :articles, :project => { :admin => true }
@ability.model_adapter(Article, :read).joins.should == [:project] @ability.model_adapter(Article, :read).joins.should == [:project]
end end
it "should restrict articles given a MetaWhere condition" do it "restricts articles given a MetaWhere condition" do
@ability.can :read, :articles, :priority.lt => 2 @ability.can :read, :articles, :priority.lt => 2
article1 = Article.create!(:priority => 1) article1 = Article.create!(:priority => 1)
article2 = Article.create!(:priority => 3) article2 = Article.create!(:priority => 3)
@ -237,7 +237,7 @@ if ENV["MODEL_ADAPTER"].nil? || ENV["MODEL_ADAPTER"] == "active_record"
@ability.should_not be_able_to(:read, article2) @ability.should_not be_able_to(:read, article2)
end end
it "should match any MetaWhere condition" do it "matches any MetaWhere condition" do
adapter = CanCan::ModelAdapters::ActiveRecordAdapter adapter = CanCan::ModelAdapters::ActiveRecordAdapter
article1 = Article.new(:priority => 1, :name => "Hello World") article1 = Article.new(:priority => 1, :name => "Hello World")
adapter.matches_condition?(article1, :priority.eq, 1).should be_true adapter.matches_condition?(article1, :priority.eq, 1).should be_true

View File

@ -30,36 +30,36 @@ if ENV["MODEL_ADAPTER"] == "data_mapper"
@ability.extend(CanCan::Ability) @ability.extend(CanCan::Ability)
end end
it "should be for only data mapper classes" do it "is for only data mapper classes" do
CanCan::ModelAdapters::DataMapperAdapter.should_not be_for_class(Object) CanCan::ModelAdapters::DataMapperAdapter.should_not be_for_class(Object)
CanCan::ModelAdapters::DataMapperAdapter.should be_for_class(Article) CanCan::ModelAdapters::DataMapperAdapter.should be_for_class(Article)
CanCan::ModelAdapters::AbstractAdapter.adapter_class(Article).should == CanCan::ModelAdapters::DataMapperAdapter CanCan::ModelAdapters::AbstractAdapter.adapter_class(Article).should == CanCan::ModelAdapters::DataMapperAdapter
end end
it "should find record" do it "finds record" do
article = Article.create article = Article.create
CanCan::ModelAdapters::DataMapperAdapter.find(Article, article.id).should == article CanCan::ModelAdapters::DataMapperAdapter.find(Article, article.id).should == article
end end
it "should not fetch any records when no abilities are defined" do it "does not fetch any records when no abilities are defined" do
Article.create Article.create
Article.accessible_by(@ability).should be_empty Article.accessible_by(@ability).should be_empty
end end
it "should fetch all articles when one can read all" do it "fetches all articles when one can read all" do
@ability.can :read, :articles @ability.can :read, :articles
article = Article.create article = Article.create
Article.accessible_by(@ability).should == [article] Article.accessible_by(@ability).should == [article]
end end
it "should fetch only the articles that are published" do it "fetches only the articles that are published" do
@ability.can :read, :articles, :published => true @ability.can :read, :articles, :published => true
article1 = Article.create(:published => true) article1 = Article.create(:published => true)
article2 = Article.create(:published => false) article2 = Article.create(:published => false)
Article.accessible_by(@ability).should == [article1] Article.accessible_by(@ability).should == [article1]
end end
it "should fetch any articles which are published or secret" do it "fetches any articles which are published or secret" do
@ability.can :read, :articles, :published => true @ability.can :read, :articles, :published => true
@ability.can :read, :articles, :secret => true @ability.can :read, :articles, :secret => true
article1 = Article.create(:published => true, :secret => false) article1 = Article.create(:published => true, :secret => false)
@ -69,7 +69,7 @@ if ENV["MODEL_ADAPTER"] == "data_mapper"
Article.accessible_by(@ability).should == [article1, article2, article3] Article.accessible_by(@ability).should == [article1, article2, article3]
end end
it "should fetch only the articles that are published and not secret" do it "fetches only the articles that are published and not secret" do
pending "the `cannot` may require some custom SQL, maybe abstract out from Active Record adapter" pending "the `cannot` may require some custom SQL, maybe abstract out from Active Record adapter"
@ability.can :read, :articles, :published => true @ability.can :read, :articles, :published => true
@ability.cannot :read, :articles, :secret => true @ability.cannot :read, :articles, :secret => true
@ -80,14 +80,14 @@ if ENV["MODEL_ADAPTER"] == "data_mapper"
Article.accessible_by(@ability).should == [article1] Article.accessible_by(@ability).should == [article1]
end end
it "should only read comments for articles which are published" do it "only reads comments for articles which are published" do
@ability.can :read, :comments, :article => { :published => true } @ability.can :read, :comments, :article => { :published => true }
comment1 = Comment.create(:article => Article.create!(:published => true)) comment1 = Comment.create(:article => Article.create!(:published => true))
comment2 = Comment.create(:article => Article.create!(:published => false)) comment2 = Comment.create(:article => Article.create!(:published => false))
Comment.accessible_by(@ability).should == [comment1] Comment.accessible_by(@ability).should == [comment1]
end end
it "should allow conditions in SQL and merge with hash conditions" do it "allows conditions in SQL and merge with hash conditions" do
@ability.can :read, :articles, :published => true @ability.can :read, :articles, :published => true
@ability.can :read, :articles, ["secret=?", true] @ability.can :read, :articles, ["secret=?", true]
article1 = Article.create(:published => true, :secret => false) article1 = Article.create(:published => true, :secret => false)
@ -95,7 +95,7 @@ if ENV["MODEL_ADAPTER"] == "data_mapper"
Article.accessible_by(@ability).should == [article1] Article.accessible_by(@ability).should == [article1]
end end
it "should match gt comparison" do it "matches gt comparison" do
@ability.can :read, :articles, :priority.gt => 3 @ability.can :read, :articles, :priority.gt => 3
article1 = Article.create(:priority => 4) article1 = Article.create(:priority => 4)
article2 = Article.create(:priority => 3) article2 = Article.create(:priority => 3)
@ -104,7 +104,7 @@ if ENV["MODEL_ADAPTER"] == "data_mapper"
@ability.should_not be_able_to(:read, article2) @ability.should_not be_able_to(:read, article2)
end end
it "should match gte comparison" do it "matches gte comparison" do
@ability.can :read, :articles, :priority.gte => 3 @ability.can :read, :articles, :priority.gte => 3
article1 = Article.create(:priority => 4) article1 = Article.create(:priority => 4)
article2 = Article.create(:priority => 3) article2 = Article.create(:priority => 3)

View File

@ -1,7 +1,7 @@
require "spec_helper" require "spec_helper"
describe CanCan::ModelAdapters::DefaultAdapter do describe CanCan::ModelAdapters::DefaultAdapter do
it "should be default for generic classes" do it "is default for generic classes" do
CanCan::ModelAdapters::AbstractAdapter.adapter_class(Object).should == CanCan::ModelAdapters::DefaultAdapter CanCan::ModelAdapters::AbstractAdapter.adapter_class(Object).should == CanCan::ModelAdapters::DefaultAdapter
end end
end end

View File

@ -30,24 +30,24 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
end.each(&:drop) end.each(&:drop)
end end
it "should be for only Mongoid classes" do it "is for only Mongoid classes" do
CanCan::ModelAdapters::MongoidAdapter.should_not be_for_class(Object) CanCan::ModelAdapters::MongoidAdapter.should_not be_for_class(Object)
CanCan::ModelAdapters::MongoidAdapter.should be_for_class(MongoidProject) CanCan::ModelAdapters::MongoidAdapter.should be_for_class(MongoidProject)
CanCan::ModelAdapters::AbstractAdapter.adapter_class(MongoidProject).should == CanCan::ModelAdapters::MongoidAdapter CanCan::ModelAdapters::AbstractAdapter.adapter_class(MongoidProject).should == CanCan::ModelAdapters::MongoidAdapter
end end
it "should find record" do it "finds record" do
project = MongoidProject.create project = MongoidProject.create
CanCan::ModelAdapters::MongoidAdapter.find(MongoidProject, project.id).should == project CanCan::ModelAdapters::MongoidAdapter.find(MongoidProject, project.id).should == project
end end
it "should compare properties on mongoid documents with the conditions hash" do it "compares properties on mongoid documents with the conditions hash" do
model = MongoidProject.new model = MongoidProject.new
@ability.can :read, :mongoid_projects, :id => model.id @ability.can :read, :mongoid_projects, :id => model.id
@ability.should be_able_to(:read, model) @ability.should be_able_to(:read, model)
end end
it "should be able to read hashes when field is array" do it "is able to read hashes when field is array" do
one_to_three = MongoidProject.create(:numbers => ['one', 'two', 'three']) one_to_three = MongoidProject.create(:numbers => ['one', 'two', 'three'])
two_to_five = MongoidProject.create(:numbers => ['two', 'three', 'four', 'five']) two_to_five = MongoidProject.create(:numbers => ['two', 'three', 'four', 'five'])
@ -56,7 +56,7 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
@ability.should_not be_able_to(:foo, two_to_five) @ability.should_not be_able_to(:foo, two_to_five)
end end
it "should return [] when no ability is defined so no records are found" do it "returns [] when no ability is defined so no records are found" do
MongoidProject.create(:title => 'Sir') MongoidProject.create(:title => 'Sir')
MongoidProject.create(:title => 'Lord') MongoidProject.create(:title => 'Lord')
MongoidProject.create(:title => 'Dude') MongoidProject.create(:title => 'Dude')
@ -64,7 +64,7 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
MongoidProject.accessible_by(@ability, :read).entries.should == [] MongoidProject.accessible_by(@ability, :read).entries.should == []
end end
it "should return the correct records based on the defined ability" do it "returns the correct records based on the defined ability" do
@ability.can :read, :mongoid_projects, :title => "Sir" @ability.can :read, :mongoid_projects, :title => "Sir"
sir = MongoidProject.create(:title => 'Sir') sir = MongoidProject.create(:title => 'Sir')
lord = MongoidProject.create(:title => 'Lord') lord = MongoidProject.create(:title => 'Lord')
@ -73,7 +73,7 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
MongoidProject.accessible_by(@ability, :read).entries.should == [sir] MongoidProject.accessible_by(@ability, :read).entries.should == [sir]
end end
it "should be able to mix empty conditions and hashes" do it "is able to mix empty conditions and hashes" do
@ability.can :read, :mongoid_projects @ability.can :read, :mongoid_projects
@ability.can :read, :mongoid_projects, :title => 'Sir' @ability.can :read, :mongoid_projects, :title => 'Sir'
sir = MongoidProject.create(:title => 'Sir') sir = MongoidProject.create(:title => 'Sir')
@ -82,7 +82,7 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
MongoidProject.accessible_by(@ability, :read).count.should == 2 MongoidProject.accessible_by(@ability, :read).count.should == 2
end end
it "should return everything when the defined ability is access all" do it "returns everything when the defined ability is access all" do
@ability.can :access, :all @ability.can :access, :all
sir = MongoidProject.create(:title => 'Sir') sir = MongoidProject.create(:title => 'Sir')
lord = MongoidProject.create(:title => 'Lord') lord = MongoidProject.create(:title => 'Lord')
@ -91,7 +91,7 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
MongoidProject.accessible_by(@ability, :read).entries.should == [sir, lord, dude] MongoidProject.accessible_by(@ability, :read).entries.should == [sir, lord, dude]
end end
it "should allow a scope for conditions" do it "allows a scope for conditions" do
@ability.can :read, :mongoid_projects, MongoidProject.where(:title => 'Sir') @ability.can :read, :mongoid_projects, MongoidProject.where(:title => 'Sir')
sir = MongoidProject.create(:title => 'Sir') sir = MongoidProject.create(:title => 'Sir')
lord = MongoidProject.create(:title => 'Lord') lord = MongoidProject.create(:title => 'Lord')
@ -101,7 +101,7 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
end end
describe "Mongoid::Criteria where clause Symbol extensions using MongoDB expressions" do describe "Mongoid::Criteria where clause Symbol extensions using MongoDB expressions" do
it "should handle :field.in" do it "handles :field.in" do
obj = MongoidProject.create(:title => 'Sir') obj = MongoidProject.create(:title => 'Sir')
@ability.can :read, :mongoid_projects, :title.in => ["Sir", "Madam"] @ability.can :read, :mongoid_projects, :title.in => ["Sir", "Madam"]
@ability.can?(:read, obj).should == true @ability.can?(:read, obj).should == true
@ -127,7 +127,7 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
end end
end end
it "should handle :field.nin" do it "handles :field.nin" do
obj = MongoidProject.create(:title => 'Sir') obj = MongoidProject.create(:title => 'Sir')
@ability.can :read, :mongoid_projects, :title.nin => ["Lord", "Madam"] @ability.can :read, :mongoid_projects, :title.nin => ["Lord", "Madam"]
@ability.can?(:read, obj).should == true @ability.can?(:read, obj).should == true
@ -137,7 +137,7 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
@ability.can?(:read, obj2).should == false @ability.can?(:read, obj2).should == false
end end
it "should handle :field.size" do it "handles :field.size" do
obj = MongoidProject.create(:titles => ['Palatin', 'Margrave']) obj = MongoidProject.create(:titles => ['Palatin', 'Margrave'])
@ability.can :read, :mongoid_projects, :titles.size => 2 @ability.can :read, :mongoid_projects, :titles.size => 2
@ability.can?(:read, obj).should == true @ability.can?(:read, obj).should == true
@ -147,7 +147,7 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
@ability.can?(:read, obj2).should == false @ability.can?(:read, obj2).should == false
end end
it "should handle :field.exists" do it "handles :field.exists" do
obj = MongoidProject.create(:titles => ['Palatin', 'Margrave']) obj = MongoidProject.create(:titles => ['Palatin', 'Margrave'])
@ability.can :read, :mongoid_projects, :titles.exists => true @ability.can :read, :mongoid_projects, :titles.exists => true
@ability.can?(:read, obj).should == true @ability.can?(:read, obj).should == true
@ -157,7 +157,7 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
@ability.can?(:read, obj2).should == false @ability.can?(:read, obj2).should == false
end end
it "should handle :field.gt" do it "handles :field.gt" do
obj = MongoidProject.create(:age => 50) obj = MongoidProject.create(:age => 50)
@ability.can :read, :mongoid_projects, :age.gt => 45 @ability.can :read, :mongoid_projects, :age.gt => 45
@ability.can?(:read, obj).should == true @ability.can?(:read, obj).should == true
@ -167,7 +167,7 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
@ability.can?(:read, obj2).should == false @ability.can?(:read, obj2).should == false
end end
it "should handle instance not saved to database" do it "handles instance not saved to database" do
obj = MongoidProject.new(:title => 'Sir') obj = MongoidProject.new(:title => 'Sir')
@ability.can :read, :mongoid_projects, :title.in => ["Sir", "Madam"] @ability.can :read, :mongoid_projects, :title.in => ["Sir", "Madam"]
@ability.can?(:read, obj).should == true @ability.can?(:read, obj).should == true
@ -180,13 +180,13 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
end end
end end
it "should call where with matching ability conditions" do it "calls where with matching ability conditions" do
obj = MongoidProject.create(:foo => {:bar => 1}) obj = MongoidProject.create(:foo => {:bar => 1})
@ability.can :read, :mongoid_projects, :foo => {:bar => 1} @ability.can :read, :mongoid_projects, :foo => {:bar => 1}
MongoidProject.accessible_by(@ability, :read).entries.first.should == obj MongoidProject.accessible_by(@ability, :read).entries.first.should == obj
end end
it "should exclude from the result if set to cannot" do it "excludes from the result if set to cannot" do
obj = MongoidProject.create(:bar => 1) obj = MongoidProject.create(:bar => 1)
obj2 = MongoidProject.create(:bar => 2) obj2 = MongoidProject.create(:bar => 2)
@ability.can :read, :mongoid_projects @ability.can :read, :mongoid_projects
@ -194,7 +194,7 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
MongoidProject.accessible_by(@ability, :read).entries.should == [obj] MongoidProject.accessible_by(@ability, :read).entries.should == [obj]
end end
it "should combine the rules" do it "combines the rules" do
obj = MongoidProject.create(:bar => 1) obj = MongoidProject.create(:bar => 1)
obj2 = MongoidProject.create(:bar => 2) obj2 = MongoidProject.create(:bar => 2)
obj3 = MongoidProject.create(:bar => 3) obj3 = MongoidProject.create(:bar => 3)
@ -203,7 +203,7 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
MongoidProject.accessible_by(@ability, :read).entries.should =~ [obj, obj2] MongoidProject.accessible_by(@ability, :read).entries.should =~ [obj, obj2]
end end
it "should not allow to fetch records when ability with just block present" do it "does not allow to fetch records when ability with just block present" do
@ability.can :read, :mongoid_projects do @ability.can :read, :mongoid_projects do
false false
end end

View File

@ -7,37 +7,37 @@ describe CanCan::Rule do
@rule = CanCan::Rule.new(true, :read, :integers, @conditions) @rule = CanCan::Rule.new(true, :read, :integers, @conditions)
end end
it "should return no association joins if none exist" do it "returns no association joins if none exist" do
@rule.associations_hash.should == {} @rule.associations_hash.should == {}
end end
it "should return no association for joins if just attributes" do it "returns no association for joins if just attributes" do
@conditions[:foo] = :bar @conditions[:foo] = :bar
@rule.associations_hash.should == {} @rule.associations_hash.should == {}
end end
it "should return single association for joins" do it "returns single association for joins" do
@conditions[:foo] = {:bar => 1} @conditions[:foo] = {:bar => 1}
@rule.associations_hash.should == {:foo => {}} @rule.associations_hash.should == {:foo => {}}
end end
it "should return multiple associations for joins" do it "returns multiple associations for joins" do
@conditions[:foo] = {:bar => 1} @conditions[:foo] = {:bar => 1}
@conditions[:test] = {1 => 2} @conditions[:test] = {1 => 2}
@rule.associations_hash.should == {:foo => {}, :test => {}} @rule.associations_hash.should == {:foo => {}, :test => {}}
end end
it "should return nested associations for joins" do it "returns nested associations for joins" do
@conditions[:foo] = {:bar => {1 => 2}} @conditions[:foo] = {:bar => {1 => 2}}
@rule.associations_hash.should == {:foo => {:bar => {}}} @rule.associations_hash.should == {:foo => {:bar => {}}}
end end
it "should return no association joins if conditions is nil" do it "returns no association joins if conditions is nil" do
rule = CanCan::Rule.new(true, :read, :integers) rule = CanCan::Rule.new(true, :read, :integers)
rule.associations_hash.should == {} rule.associations_hash.should == {}
end end
it "should have higher specificity for attributes/conditions" do it "has higher specificity for attributes/conditions" do
CanCan::Rule.new(true, :read, :integers).specificity.should eq(1) CanCan::Rule.new(true, :read, :integers).specificity.should eq(1)
CanCan::Rule.new(true, :read, :integers, :foo => :bar).specificity.should eq(2) CanCan::Rule.new(true, :read, :integers, :foo => :bar).specificity.should eq(2)
CanCan::Rule.new(true, :read, :integers, :foo).specificity.should eq(2) CanCan::Rule.new(true, :read, :integers, :foo).specificity.should eq(2)