Add support and tests for datamapper.

This broke some of the mongoid tests and I don't know how to fix them.  Both packages
  define Symbol#in, and when you load them both things don't behave properly.  Hopefully
  someone more versed in mongoid can rewrite the spec to not depend on the Symbol extensions.
This commit is contained in:
Nate Mueller
2010-12-29 06:07:20 +08:00
committed by Ryan Bates
parent 2d31cbdf60
commit d315e22e7a
3 changed files with 130 additions and 1 deletions

View File

@@ -0,0 +1,59 @@
require "spec_helper"
require 'cancan/data_mapper_additions'
describe CanCan::DataMapperAdditions do
before(:each) do
@model_class = Class.new
@model_class.class_eval do
include DataMapper::Resource
end
stub(@model_class).all(:conditions => ['true=false']) { 'no-match:' }
@ability = Object.new
@ability.extend(CanCan::Ability)
end
it "should return no records when no ability is defined so no records are found" do
@model_class.accessible_by(@ability, :read).should == 'no-match:'
end
it "should call all with matching ability conditions" do
@ability.can :read, @model_class, :foo => {:bar => 1}
stub(@model_class).all(:conditions => {:foo => {:bar => 1}}) { 'found-records:' }
@model_class.accessible_by(@ability, :read).should == 'no-match:found-records:'
end
it "should merge association joins and sanitize conditions" do
@ability.can :read, @model_class, :foo => {:bar => 1}
@ability.can :read, @model_class, :too => {:car => 1, :far => {:bar => 1}}
stub(@model_class).all(:conditions => {:foo => {:bar => 1}}) { 'foo:' }
stub(@model_class).all(:conditions => {:too => {:car => 1, :far => {:bar => 1}}}) { 'too:' }
@model_class.accessible_by(@ability).should == 'no-match:too:foo:'
end
it "should allow to define sql conditions by not hash" do
@ability.can :read, @model_class, :foo => 1
@ability.can :read, @model_class, ['bar = ?', 1]
stub(@model_class).all(:conditions => {:foo => 1}) { 'foo:' }
stub(@model_class).all(:conditions => ['bar = ?', 1]) { 'bar:' }
@model_class.accessible_by(@ability).should == 'no-match:bar:foo:'
end
it "should not allow to fetch records when ability with just block present" do
@ability.can :read, @model_class do false end
lambda {
@model_class.accessible_by(@ability)
}.should raise_error(CanCan::Error)
end
it "should not allow to check ability on object when nonhash sql ability definition without block present" do
@ability.can :read, @model_class, ['bar = ?', 1]
lambda {
@ability.can? :read, @model_class.new
}.should raise_error(CanCan::Error)
end
end