Skip to content
This repository
Newer
Older
100644 228 lines (183 sloc) 8.936 kb
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
1 if ENV["MODEL_ADAPTER"] == "mongoid"
2 require "spec_helper"
3
4 class MongoidCategory
5 include Mongoid::Document
6
7 references_many :mongoid_projects
8 end
9
10 class MongoidProject
11 include Mongoid::Document
8628aa00 »
2010-12-30 cleanup whitespace
12
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
13 referenced_in :mongoid_category
14 end
15
16 Mongoid.configure do |config|
4c5ba09f »
2010-12-29 adding model adapter files in proper location with loading behavior
17 config.master = Mongo::Connection.new('127.0.0.1', 27017).db("cancan_mongoid_spec")
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
18 end
19
f7a494dc »
2010-12-30 switching mongoid over to new adapter
20 describe CanCan::ModelAdapters::MongoidAdapter do
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
21 context "Mongoid defined" do
22 before(:each) do
23 @ability = Object.new
24 @ability.extend(CanCan::Ability)
25 end
26
27 after(:each) do
28 Mongoid.master.collections.select do |collection|
29 collection.name !~ /system/
8628aa00 »
2010-12-30 cleanup whitespace
30 end.each(&:drop)
31 end
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
32
f7a494dc »
2010-12-30 switching mongoid over to new adapter
33 it "should be for only Mongoid classes" do
34 CanCan::ModelAdapters::MongoidAdapter.should_not be_for_class(Object)
cef6c212 »
2011-01-04 allow model adapter to override condition hash matching in Rule, also…
35 CanCan::ModelAdapters::MongoidAdapter.should be_for_class(MongoidProject)
36 CanCan::ModelAdapters::AbstractAdapter.adapter_class(MongoidProject).should == CanCan::ModelAdapters::MongoidAdapter
f7a494dc »
2010-12-30 switching mongoid over to new adapter
37 end
38
613ab1c1 »
2011-05-21 delegating ControllerResource find to model adapter, uses 'get' for D…
39 it "should find record" do
40 project = MongoidProject.create
41 CanCan::ModelAdapters::MongoidAdapter.find(MongoidProject, project.id).should == project
42 end
43
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
44 it "should compare properties on mongoid documents with the conditions hash" do
cef6c212 »
2011-01-04 allow model adapter to override condition hash matching in Rule, also…
45 model = MongoidProject.new
46 @ability.can :read, MongoidProject, :id => model.id
47 @ability.should be_able_to(:read, model)
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
48 end
49
17c52a79 »
2011-04-27 Augments Mongoid adapter by handling case where attribute is an array
50 it "should be able to read hashes when field is array" do
51 one_to_three = MongoidProject.create(:numbers => ['one', 'two', 'three'])
52 two_to_five = MongoidProject.create(:numbers => ['two', 'three', 'four', 'five'])
53
54 @ability.can :foo, MongoidProject, :numbers => 'one'
55 @ability.should be_able_to(:foo, one_to_three)
56 @ability.should_not be_able_to(:foo, two_to_five)
57 end
58
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
59 it "should return [] when no ability is defined so no records are found" do
cef6c212 »
2011-01-04 allow model adapter to override condition hash matching in Rule, also…
60 MongoidProject.create(:title => 'Sir')
61 MongoidProject.create(:title => 'Lord')
62 MongoidProject.create(:title => 'Dude')
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
63
cef6c212 »
2011-01-04 allow model adapter to override condition hash matching in Rule, also…
64 MongoidProject.accessible_by(@ability, :read).entries.should == []
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
65 end
66
67 it "should return the correct records based on the defined ability" do
cef6c212 »
2011-01-04 allow model adapter to override condition hash matching in Rule, also…
68 @ability.can :read, MongoidProject, :title => "Sir"
69 sir = MongoidProject.create(:title => 'Sir')
70 lord = MongoidProject.create(:title => 'Lord')
71 dude = MongoidProject.create(:title => 'Dude')
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
72
55c8a504 »
2011-01-18 added cannot support and multiple can support
73 MongoidProject.accessible_by(@ability, :read).entries.should == [sir]
8628aa00 »
2010-12-30 cleanup whitespace
74 end
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
75
da663aae »
2012-06-10 Fix for issue-644 to allow users to specify a mix of can and cannot r…
76 it "should return the correct records when a mix of can and cannot rules in defined ability" do
77 @ability.can :manage, MongoidProject, :title => 'Sir'
78 @ability.cannot :destroy, MongoidProject
79
80 sir = MongoidProject.create(:title => 'Sir')
81 lord = MongoidProject.create(:title => 'Lord')
82 dude = MongoidProject.create(:title => 'Dude')
83
84 MongoidProject.accessible_by(@ability, :destroy).entries.should == [sir]
85 end
86
ad62d60b »
2011-05-10 Fixes bug in mongoid_adapter with empty conditions hash
87 it "should be able to mix empty conditions and hashes" do
88 @ability.can :read, MongoidProject
89 @ability.can :read, MongoidProject, :title => 'Sir'
90 sir = MongoidProject.create(:title => 'Sir')
91 lord = MongoidProject.create(:title => 'Lord')
92
93 MongoidProject.accessible_by(@ability, :read).count.should == 2
94 end
95
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
96 it "should return everything when the defined ability is manage all" do
97 @ability.can :manage, :all
cef6c212 »
2011-01-04 allow model adapter to override condition hash matching in Rule, also…
98 sir = MongoidProject.create(:title => 'Sir')
99 lord = MongoidProject.create(:title => 'Lord')
100 dude = MongoidProject.create(:title => 'Dude')
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
101
cef6c212 »
2011-01-04 allow model adapter to override condition hash matching in Rule, also…
102 MongoidProject.accessible_by(@ability, :read).entries.should == [sir, lord, dude]
8628aa00 »
2010-12-30 cleanup whitespace
103 end
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
104
2b620411 »
2011-04-15 Adds ability to use Scope query with Mongoid
105 it "should allow a scope for conditions" do
106 @ability.can :read, MongoidProject, MongoidProject.where(:title => 'Sir')
107 sir = MongoidProject.create(:title => 'Sir')
108 lord = MongoidProject.create(:title => 'Lord')
109 dude = MongoidProject.create(:title => 'Dude')
110
111 MongoidProject.accessible_by(@ability, :read).entries.should == [sir]
112 end
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
113
114 describe "Mongoid::Criteria where clause Symbol extensions using MongoDB expressions" do
115 it "should handle :field.in" do
cef6c212 »
2011-01-04 allow model adapter to override condition hash matching in Rule, also…
116 obj = MongoidProject.create(:title => 'Sir')
117 @ability.can :read, MongoidProject, :title.in => ["Sir", "Madam"]
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
118 @ability.can?(:read, obj).should == true
cef6c212 »
2011-01-04 allow model adapter to override condition hash matching in Rule, also…
119 MongoidProject.accessible_by(@ability, :read).should == [obj]
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
120
cef6c212 »
2011-01-04 allow model adapter to override condition hash matching in Rule, also…
121 obj2 = MongoidProject.create(:title => 'Lord')
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
122 @ability.can?(:read, obj2).should == false
123 end
124
125 describe "activates only when there are Criteria in the hash" do
126 it "Calls where on the model class when there are criteria" do
cef6c212 »
2011-01-04 allow model adapter to override condition hash matching in Rule, also…
127 obj = MongoidProject.create(:title => 'Bird')
8628aa00 »
2010-12-30 cleanup whitespace
128 @conditions = {:title.nin => ["Fork", "Spoon"]}
12b0bff0 »
2011-01-05 Use `Mongoid::Matchers#matches?` instead of a database query in `Mong…
129
cef6c212 »
2011-01-04 allow model adapter to override condition hash matching in Rule, also…
130 @ability.can :read, MongoidProject, @conditions
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
131 @ability.should be_able_to(:read, obj)
132 end
133 it "Calls the base version if there are no mongoid criteria" do
cef6c212 »
2011-01-04 allow model adapter to override condition hash matching in Rule, also…
134 obj = MongoidProject.new(:title => 'Bird')
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
135 @conditions = {:id => obj.id}
cef6c212 »
2011-01-04 allow model adapter to override condition hash matching in Rule, also…
136 @ability.can :read, MongoidProject, @conditions
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
137 @ability.should be_able_to(:read, obj)
138 end
139 end
140
141 it "should handle :field.nin" do
cef6c212 »
2011-01-04 allow model adapter to override condition hash matching in Rule, also…
142 obj = MongoidProject.create(:title => 'Sir')
143 @ability.can :read, MongoidProject, :title.nin => ["Lord", "Madam"]
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
144 @ability.can?(:read, obj).should == true
cef6c212 »
2011-01-04 allow model adapter to override condition hash matching in Rule, also…
145 MongoidProject.accessible_by(@ability, :read).should == [obj]
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
146
cef6c212 »
2011-01-04 allow model adapter to override condition hash matching in Rule, also…
147 obj2 = MongoidProject.create(:title => 'Lord')
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
148 @ability.can?(:read, obj2).should == false
149 end
150
151 it "should handle :field.size" do
cef6c212 »
2011-01-04 allow model adapter to override condition hash matching in Rule, also…
152 obj = MongoidProject.create(:titles => ['Palatin', 'Margrave'])
153 @ability.can :read, MongoidProject, :titles.size => 2
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
154 @ability.can?(:read, obj).should == true
cef6c212 »
2011-01-04 allow model adapter to override condition hash matching in Rule, also…
155 MongoidProject.accessible_by(@ability, :read).should == [obj]
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
156
cef6c212 »
2011-01-04 allow model adapter to override condition hash matching in Rule, also…
157 obj2 = MongoidProject.create(:titles => ['Palatin', 'Margrave', 'Marquis'])
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
158 @ability.can?(:read, obj2).should == false
8628aa00 »
2010-12-30 cleanup whitespace
159 end
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
160
161 it "should handle :field.exists" do
cef6c212 »
2011-01-04 allow model adapter to override condition hash matching in Rule, also…
162 obj = MongoidProject.create(:titles => ['Palatin', 'Margrave'])
163 @ability.can :read, MongoidProject, :titles.exists => true
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
164 @ability.can?(:read, obj).should == true
cef6c212 »
2011-01-04 allow model adapter to override condition hash matching in Rule, also…
165 MongoidProject.accessible_by(@ability, :read).should == [obj]
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
166
cef6c212 »
2011-01-04 allow model adapter to override condition hash matching in Rule, also…
167 obj2 = MongoidProject.create
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
168 @ability.can?(:read, obj2).should == false
169 end
170
171 it "should handle :field.gt" do
cef6c212 »
2011-01-04 allow model adapter to override condition hash matching in Rule, also…
172 obj = MongoidProject.create(:age => 50)
173 @ability.can :read, MongoidProject, :age.gt => 45
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
174 @ability.can?(:read, obj).should == true
cef6c212 »
2011-01-04 allow model adapter to override condition hash matching in Rule, also…
175 MongoidProject.accessible_by(@ability, :read).should == [obj]
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
176
cef6c212 »
2011-01-04 allow model adapter to override condition hash matching in Rule, also…
177 obj2 = MongoidProject.create(:age => 40)
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
178 @ability.can?(:read, obj2).should == false
8628aa00 »
2010-12-30 cleanup whitespace
179 end
3885f469 »
2011-01-08 updating changelog
180
9a14c706 »
2011-01-06 Add MongoidAdapter specs for unsaved instances
181 it "should handle instance not saved to database" do
182 obj = MongoidProject.new(:title => 'Sir')
183 @ability.can :read, MongoidProject, :title.in => ["Sir", "Madam"]
184 @ability.can?(:read, obj).should == true
3885f469 »
2011-01-08 updating changelog
185
9a14c706 »
2011-01-06 Add MongoidAdapter specs for unsaved instances
186 # accessible_by only returns saved records
187 MongoidProject.accessible_by(@ability, :read).entries.should == []
3885f469 »
2011-01-08 updating changelog
188
9a14c706 »
2011-01-06 Add MongoidAdapter specs for unsaved instances
189 obj2 = MongoidProject.new(:title => 'Lord')
190 @ability.can?(:read, obj2).should == false
3885f469 »
2011-01-08 updating changelog
191 end
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
192 end
193
194 it "should call where with matching ability conditions" do
cef6c212 »
2011-01-04 allow model adapter to override condition hash matching in Rule, also…
195 obj = MongoidProject.create(:foo => {:bar => 1})
196 @ability.can :read, MongoidProject, :foo => {:bar => 1}
197 MongoidProject.accessible_by(@ability, :read).entries.first.should == obj
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
198 end
da663aae »
2012-06-10 Fix for issue-644 to allow users to specify a mix of can and cannot r…
199
55c8a504 »
2011-01-18 added cannot support and multiple can support
200 it "should exclude from the result if set to cannot" do
201 obj = MongoidProject.create(:bar => 1)
202 obj2 = MongoidProject.create(:bar => 2)
203 @ability.can :read, MongoidProject
204 @ability.cannot :read, MongoidProject, :bar => 2
cff92291 »
2011-01-18 improved test assertion
205 MongoidProject.accessible_by(@ability, :read).entries.should == [obj]
55c8a504 »
2011-01-18 added cannot support and multiple can support
206 end
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
207
55c8a504 »
2011-01-18 added cannot support and multiple can support
208 it "should combine the rules" do
209 obj = MongoidProject.create(:bar => 1)
210 obj2 = MongoidProject.create(:bar => 2)
211 obj3 = MongoidProject.create(:bar => 3)
212 @ability.can :read, MongoidProject, :bar => 1
213 @ability.can :read, MongoidProject, :bar => 2
214 MongoidProject.accessible_by(@ability, :read).entries.should =~ [obj, obj2]
215 end
da663aae »
2012-06-10 Fix for issue-644 to allow users to specify a mix of can and cannot r…
216
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
217 it "should not allow to fetch records when ability with just block present" do
cef6c212 »
2011-01-04 allow model adapter to override condition hash matching in Rule, also…
218 @ability.can :read, MongoidProject do
219 false
220 end
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
221 lambda {
cef6c212 »
2011-01-04 allow model adapter to override condition hash matching in Rule, also…
222 MongoidProject.accessible_by(@ability)
f9a498d2 »
2010-12-29 moving model adapter specs into their own directory with MODEL_ADAPTE…
223 }.should raise_error(CanCan::Error)
224 end
225 end
226 end
227 end
Something went wrong with that request. Please try again.