292 lines
13 KiB
Ruby
292 lines
13 KiB
Ruby
require "spec_helper"
|
|
|
|
describe CanCan::ControllerResource do
|
|
before(:each) do
|
|
@params = HashWithIndifferentAccess.new(:controller => "projects")
|
|
@controller = Object.new # simple stub for now
|
|
@ability = Ability.new(nil)
|
|
stub(@controller).params { @params }
|
|
stub(@controller).current_ability { @ability }
|
|
end
|
|
|
|
it "should load the resource into an instance variable if params[:id] is specified" do
|
|
@params.merge!(:action => "show", :id => 123)
|
|
stub(Project).find(123) { :some_project }
|
|
resource = CanCan::ControllerResource.new(@controller)
|
|
resource.load_resource
|
|
@controller.instance_variable_get(:@project).should == :some_project
|
|
end
|
|
|
|
it "should not load resource into an instance variable if already set" do
|
|
@params.merge!(:action => "show", :id => 123)
|
|
@controller.instance_variable_set(:@project, :some_project)
|
|
resource = CanCan::ControllerResource.new(@controller)
|
|
resource.load_resource
|
|
@controller.instance_variable_get(:@project).should == :some_project
|
|
end
|
|
|
|
it "should properly load resource for namespaced controller" do
|
|
@params.merge!(:controller => "admin/projects", :action => "show", :id => 123)
|
|
stub(Project).find(123) { :some_project }
|
|
resource = CanCan::ControllerResource.new(@controller)
|
|
resource.load_resource
|
|
@controller.instance_variable_get(:@project).should == :some_project
|
|
end
|
|
|
|
it "should properly load resource for namespaced controller when using '::' for namespace" do
|
|
@params.merge!(:controller => "Admin::ProjectsController", :action => "show", :id => 123)
|
|
stub(Project).find(123) { :some_project }
|
|
resource = CanCan::ControllerResource.new(@controller)
|
|
resource.load_resource
|
|
@controller.instance_variable_get(:@project).should == :some_project
|
|
end
|
|
|
|
it "should build a new resource with hash if params[:id] is not specified" do
|
|
@params.merge!(:action => "create", :project => {:name => "foobar"})
|
|
resource = CanCan::ControllerResource.new(@controller)
|
|
resource.load_resource
|
|
@controller.instance_variable_get(:@project).name.should == "foobar"
|
|
end
|
|
|
|
it "should build a new resource with attributes from current ability" do
|
|
@params.merge!(:action => "new")
|
|
@ability.can(:create, Project, :name => "from conditions")
|
|
resource = CanCan::ControllerResource.new(@controller)
|
|
resource.load_resource
|
|
@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, Project, :name => "from conditions")
|
|
resource = CanCan::ControllerResource.new(@controller)
|
|
resource.load_resource
|
|
@controller.instance_variable_get(:@project).name.should == "from params"
|
|
end
|
|
|
|
it "should build a collection when on index action when class responds to accessible_by" do
|
|
stub(Project).accessible_by(@ability) { :found_projects }
|
|
@params[:action] = "index"
|
|
resource = CanCan::ControllerResource.new(@controller, :project)
|
|
resource.load_resource
|
|
@controller.instance_variable_get(:@project).should be_nil
|
|
@controller.instance_variable_get(:@projects).should == :found_projects
|
|
end
|
|
|
|
it "should not build a collection when on index action when class does not respond to accessible_by" do
|
|
@params[:action] = "index"
|
|
resource = CanCan::ControllerResource.new(@controller)
|
|
resource.load_resource
|
|
@controller.instance_variable_get(:@project).should be_nil
|
|
@controller.instance_variable_defined?(:@projects).should be_false
|
|
end
|
|
|
|
it "should not use accessible_by when defining abilities through a block" do
|
|
stub(Project).accessible_by(@ability) { :found_projects }
|
|
@params[:action] = "index"
|
|
@ability.can(:read, Project) { |p| false }
|
|
resource = CanCan::ControllerResource.new(@controller)
|
|
resource.load_resource
|
|
@controller.instance_variable_get(:@project).should be_nil
|
|
@controller.instance_variable_defined?(:@projects).should be_false
|
|
end
|
|
|
|
it "should perform authorization using controller action and loaded model" do
|
|
@params[:action] = "show"
|
|
@controller.instance_variable_set(:@project, :some_project)
|
|
stub(@controller).authorize!(:show, :some_project) { raise CanCan::AccessDenied }
|
|
resource = CanCan::ControllerResource.new(@controller)
|
|
lambda { resource.authorize_resource }.should raise_error(CanCan::AccessDenied)
|
|
end
|
|
|
|
it "should perform authorization using controller action and non loaded model" do
|
|
@params[:action] = "show"
|
|
stub(@controller).authorize!(:show, Project) { raise CanCan::AccessDenied }
|
|
resource = CanCan::ControllerResource.new(@controller)
|
|
lambda { resource.authorize_resource }.should raise_error(CanCan::AccessDenied)
|
|
end
|
|
|
|
it "should call load_resource and authorize_resource for load_and_authorize_resource" do
|
|
@params[:action] = "show"
|
|
resource = CanCan::ControllerResource.new(@controller)
|
|
mock(resource).load_resource
|
|
mock(resource).authorize_resource
|
|
resource.load_and_authorize_resource
|
|
end
|
|
|
|
it "should not build a resource when on custom collection action" do
|
|
@params[:action] = "sort"
|
|
resource = CanCan::ControllerResource.new(@controller, :collection => [:sort, :list])
|
|
resource.load_resource
|
|
@controller.instance_variable_get(:@project).should be_nil
|
|
end
|
|
|
|
it "should build a resource when on custom new action even when params[:id] exists" do
|
|
@params.merge!(:action => "build", :id => 123)
|
|
stub(Project).new { :some_project }
|
|
resource = CanCan::ControllerResource.new(@controller, :new => :build)
|
|
resource.load_resource
|
|
@controller.instance_variable_get(:@project).should == :some_project
|
|
end
|
|
|
|
it "should not try to load resource for other action if params[:id] is undefined" do
|
|
@params[:action] = "list"
|
|
resource = CanCan::ControllerResource.new(@controller)
|
|
resource.load_resource
|
|
@controller.instance_variable_get(:@project).should be_nil
|
|
end
|
|
|
|
it "should be a parent resource when name is provided which doesn't match controller" do
|
|
resource = CanCan::ControllerResource.new(@controller, :category)
|
|
resource.should be_parent
|
|
end
|
|
|
|
it "should not be a parent resource when name is provided which matches controller" do
|
|
resource = CanCan::ControllerResource.new(@controller, :project)
|
|
resource.should_not be_parent
|
|
end
|
|
|
|
it "should be parent if specified in options" do
|
|
resource = CanCan::ControllerResource.new(@controller, :project, {:parent => true})
|
|
resource.should be_parent
|
|
end
|
|
|
|
it "should not be parent if specified in options" do
|
|
resource = CanCan::ControllerResource.new(@controller, :category, {:parent => false})
|
|
resource.should_not be_parent
|
|
end
|
|
|
|
it "should load parent resource through proper id parameter" do
|
|
@params.merge!(:action => "index", :project_id => 123)
|
|
stub(Project).find(123) { :some_project }
|
|
resource = CanCan::ControllerResource.new(@controller, :project, :parent => true)
|
|
resource.load_resource
|
|
@controller.instance_variable_get(:@project).should == :some_project
|
|
end
|
|
|
|
it "should load resource through the association of another parent resource" do
|
|
@params.merge!(:action => "show", :id => 123)
|
|
category = Object.new
|
|
@controller.instance_variable_set(:@category, category)
|
|
stub(category).projects.stub!.find(123) { :some_project }
|
|
resource = CanCan::ControllerResource.new(@controller, :through => :category)
|
|
resource.load_resource
|
|
@controller.instance_variable_get(:@project).should == :some_project
|
|
end
|
|
|
|
it "should not load through parent resource if instance isn't loaded when shallow" do
|
|
@params.merge!(:action => "show", :id => 123)
|
|
stub(Project).find(123) { :some_project }
|
|
resource = CanCan::ControllerResource.new(@controller, :through => :category, :shallow => true)
|
|
resource.load_resource
|
|
@controller.instance_variable_get(:@project).should == :some_project
|
|
end
|
|
|
|
it "should raise AccessDenied when attempting to load resource through nil" do
|
|
@params.merge!(:action => "show", :id => 123)
|
|
stub(Project).find(123) { :some_project }
|
|
resource = CanCan::ControllerResource.new(@controller, :through => :category)
|
|
lambda {
|
|
resource.load_resource
|
|
}.should raise_error(CanCan::AccessDenied)
|
|
@controller.instance_variable_get(:@project).should be_nil
|
|
end
|
|
|
|
it "should authorize nested resource through parent association on index action" do
|
|
@params.merge!(:action => "index")
|
|
category = Object.new
|
|
@controller.instance_variable_set(:@category, category)
|
|
stub(@controller).authorize!(:index, category => Project) { raise CanCan::AccessDenied }
|
|
resource = CanCan::ControllerResource.new(@controller, :through => :category)
|
|
lambda { resource.authorize_resource }.should raise_error(CanCan::AccessDenied)
|
|
end
|
|
|
|
it "should load through first matching if multiple are given" do
|
|
@params.merge!(:action => "show", :id => 123)
|
|
category = Object.new
|
|
@controller.instance_variable_set(:@category, category)
|
|
stub(category).projects.stub!.find(123) { :some_project }
|
|
resource = CanCan::ControllerResource.new(@controller, :through => [:category, :user])
|
|
resource.load_resource
|
|
@controller.instance_variable_get(:@project).should == :some_project
|
|
end
|
|
|
|
it "should find record through has_one association with :singleton option" do
|
|
@params.merge!(:action => "show")
|
|
category = Object.new
|
|
@controller.instance_variable_set(:@category, category)
|
|
stub(category).project { :some_project }
|
|
resource = CanCan::ControllerResource.new(@controller, :through => :category, :singleton => true)
|
|
resource.load_resource
|
|
@controller.instance_variable_get(:@project).should == :some_project
|
|
end
|
|
|
|
it "should build record through has_one association with :singleton option" do
|
|
@params.merge!(:action => "create", :project => {:name => "foobar"})
|
|
category = Object.new
|
|
@controller.instance_variable_set(:@category, category)
|
|
stub(category).build_project { Project.new }
|
|
resource = CanCan::ControllerResource.new(@controller, :through => :category, :singleton => true)
|
|
resource.load_resource
|
|
@controller.instance_variable_get(:@project).name.should == "foobar"
|
|
end
|
|
|
|
it "should only authorize :read action on parent resource" do
|
|
@params.merge!(:action => "new", :project_id => 123)
|
|
stub(Project).find(123) { :some_project }
|
|
stub(@controller).authorize!(:read, :some_project) { raise CanCan::AccessDenied }
|
|
resource = CanCan::ControllerResource.new(@controller, :project, :parent => true)
|
|
lambda { resource.load_and_authorize_resource }.should raise_error(CanCan::AccessDenied)
|
|
end
|
|
|
|
it "should load the model using a custom class" do
|
|
@params.merge!(:action => "show", :id => 123)
|
|
stub(Project).find(123) { :some_project }
|
|
resource = CanCan::ControllerResource.new(@controller, :class => Project)
|
|
resource.load_resource
|
|
@controller.instance_variable_get(:@project).should == :some_project
|
|
end
|
|
|
|
it "should authorize based on resource name if class is false" do
|
|
@params.merge!(:action => "show", :id => 123)
|
|
stub(@controller).authorize!(:show, :project) { raise CanCan::AccessDenied }
|
|
resource = CanCan::ControllerResource.new(@controller, :class => false)
|
|
lambda { resource.authorize_resource }.should raise_error(CanCan::AccessDenied)
|
|
end
|
|
|
|
it "should load and authorize using custom instance name" do
|
|
@params.merge!(:action => "show", :id => 123)
|
|
stub(Project).find(123) { :some_project }
|
|
stub(@controller).authorize!(:show, :some_project) { raise CanCan::AccessDenied }
|
|
resource = CanCan::ControllerResource.new(@controller, :instance_name => :custom_project)
|
|
lambda { resource.load_and_authorize_resource }.should raise_error(CanCan::AccessDenied)
|
|
@controller.instance_variable_get(:@custom_project).should == :some_project
|
|
end
|
|
|
|
it "should load resource using custom find_by attribute" do
|
|
@params.merge!(:action => "show", :id => 123)
|
|
stub(Project).find_by_name!(123) { :some_project }
|
|
resource = CanCan::ControllerResource.new(@controller, :find_by => :name)
|
|
resource.load_resource
|
|
@controller.instance_variable_get(:@project).should == :some_project
|
|
end
|
|
|
|
it "should raise ImplementationRemoved when adding :name option" do
|
|
lambda {
|
|
CanCan::ControllerResource.new(@controller, :name => :foo)
|
|
}.should raise_error(CanCan::ImplementationRemoved)
|
|
end
|
|
|
|
it "should raise ImplementationRemoved exception when specifying :resource option since it is no longer used" do
|
|
lambda {
|
|
CanCan::ControllerResource.new(@controller, :resource => Project)
|
|
}.should raise_error(CanCan::ImplementationRemoved)
|
|
end
|
|
|
|
it "should raise ImplementationRemoved exception when passing :nested option" do
|
|
lambda {
|
|
CanCan::ControllerResource.new(@controller, :nested => :project)
|
|
}.should raise_error(CanCan::ImplementationRemoved)
|
|
end
|
|
end
|