groupify 0.5.1 → 0.6.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,3 @@
1
- require 'spec_helper'
2
-
3
1
  RSpec.configure do |config|
4
2
  config.order = "random"
5
3
 
@@ -19,14 +17,19 @@ end
19
17
  require 'mongoid'
20
18
  puts "Mongoid version #{Mongoid::VERSION}"
21
19
 
22
- require 'mongoid-rspec'
23
- include Mongoid::Matchers
24
-
25
20
  # Load mongoid config
26
- Mongoid.load!('./spec/groupify/mongoid.yml', :test)
27
- #Moped.logger = Logger.new(STDOUT)
21
+ #Mongoid.load!('./spec/groupify/mongoid.yml', :test)
22
+ Mongoid::Config.sessions = {
23
+ default: {
24
+ database: 'groupify',
25
+ hosts: [ "localhost:27017" ]
26
+ }
27
+ }
28
+
29
+ if DEBUG
30
+ Moped.logger = Logger.new(STDOUT)
31
+ end
28
32
 
29
- require 'groupify'
30
33
  require 'groupify/adapter/mongoid'
31
34
 
32
35
  class MongoidUser
@@ -39,6 +42,11 @@ end
39
42
  class MongoidManager < MongoidUser
40
43
  end
41
44
 
45
+ class MongoidWidget
46
+ include Mongoid::Document
47
+ acts_as_group_member :class_name => 'MongoidGroup'
48
+ end
49
+
42
50
  class MongoidTask
43
51
  include Mongoid::Document
44
52
 
@@ -54,203 +62,543 @@ end
54
62
  class MongoidGroup
55
63
  include Mongoid::Document
56
64
 
57
- acts_as_group :members => [:mongoid_users, :mongoid_tasks], :default_members => :mongoid_users
65
+ acts_as_group :members => [:mongoid_users, :mongoid_tasks, :mongoid_widgets], :default_members => :mongoid_users
58
66
  alias_method :users, :mongoid_users
59
67
  alias_method :tasks, :mongoid_tasks
68
+ alias_method :widgets, :mongoid_widgets
60
69
  end
61
70
 
62
71
  class MongoidProject < MongoidGroup
72
+ acts_as_named_group_member
73
+
63
74
  has_members :mongoid_issues
64
75
  has_members :mongoid_managers
65
76
  alias_method :issues, :mongoid_issues
66
77
  alias_method :managers, :mongoid_managers
67
78
  end
68
79
 
69
- describe MongoidGroup do
70
- it { should respond_to :members}
71
- it { should respond_to :add }
72
- end
73
-
74
- describe MongoidUser do
75
- it { should respond_to :groups}
76
- it { should respond_to :in_group?}
77
- it { should respond_to :in_any_group?}
78
- it { should respond_to :in_all_groups?}
79
- it { should respond_to :shares_any_group?}
80
-
81
- # Mongoid specific
82
- it { should have_and_belong_to_many(:groups).of_type(MongoidGroup) }
83
- end
84
-
85
80
  describe Groupify::Mongoid do
86
81
  let!(:user) { MongoidUser.create! }
87
82
  let!(:group) { MongoidGroup.create! }
83
+ let(:widget) { MongoidWidget.create! }
88
84
  let(:task) { MongoidTask.create! }
89
85
  let(:issue) { MongoidIssue.create! }
90
86
  let(:manager) { MongoidManager.create! }
91
87
 
92
- it "members and groups are empty when initialized" do
93
- user.groups.should be_empty
94
- MongoidUser.new.groups.should be_empty
88
+ context 'when using groups' do
89
+ it "members and groups are empty when initialized" do
90
+ expect(user.groups).to be_empty
91
+ expect(MongoidUser.new.groups).to be_empty
95
92
 
96
- MongoidGroup.new.members.should be_empty
97
- group.members.should be_empty
98
- end
99
-
100
- it "adds a group to a member" do
101
- user.groups << group
102
- user.groups.should include(group)
103
- group.members.should include(user)
104
- group.users.should include(user)
105
- end
106
-
107
- it "adds a member to a group" do
108
- group.add user
109
- user.groups.should include(group)
110
- group.members.should include(user)
111
- end
93
+ expect(MongoidGroup.new.members).to be_empty
94
+ expect(group.members).to be_empty
95
+ end
96
+
97
+ it "adds a group to a member" do
98
+ user.groups << group
99
+ expect(user.groups).to include(group)
100
+ expect(group.members).to include(user)
101
+ expect(group.users).to include(user)
102
+ end
103
+
104
+ it "adds a member to a group" do
105
+ expect(user.groups).to be_empty
106
+ group.add user
107
+ expect(user.groups).to include(group)
108
+ expect(group.members).to include(user)
109
+ end
112
110
 
113
- it "adds multiple members to a group" do
114
- group.add(user, task)
115
- group.users.should include(user)
116
- group.tasks.should include(task)
111
+ it "adds multiple members to a group" do
112
+ group.add(user, task)
113
+ expect(group.users).to include(user)
114
+ expect(group.tasks).to include(task)
117
115
 
118
- users = [MongoidUser.create!, MongoidUser.create!]
119
- group.add(users)
120
- group.users.should include(*users)
121
- end
116
+ users = [MongoidUser.create!, MongoidUser.create!]
117
+ group.add(users)
118
+ expect(group.users).to include(*users)
119
+ end
122
120
 
123
- it 'lists which member classes can belong to this group' do
124
- group.class.member_classes.should include(MongoidUser, MongoidTask)
125
- group.member_classes.should include(MongoidUser, MongoidTask)
121
+ it 'lists which member classes can belong to this group' do
122
+ expect(group.class.member_classes).to include(MongoidUser, MongoidTask)
123
+ expect(group.member_classes).to include(MongoidUser, MongoidTask)
126
124
 
127
- MongoidProject.member_classes.should include(MongoidUser, MongoidTask, MongoidIssue)
128
- end
129
-
130
- it "finds members by group" do
131
- group.add user
125
+ expect(MongoidProject.member_classes).to include(MongoidUser, MongoidTask, MongoidIssue)
126
+ end
132
127
 
133
- MongoidUser.in_group(group).first.should eql(user)
134
- end
128
+ it "finds members by group" do
129
+ group.add user
130
+
131
+ expect(MongoidUser.in_group(group).first).to eql(user)
132
+ end
135
133
 
136
- it "finds the groups a member belongs to" do
137
- group.add user
138
-
139
- MongoidGroup.with_member(user).first.should == group
140
- end
134
+ it "finds the groups a member belongs to" do
135
+ group.add user
136
+
137
+ expect(MongoidGroup.with_member(user).first).to eq(group)
138
+ end
141
139
 
142
- it "removes the membership relation when a member is destroyed" do
143
- group.add user
144
- user.destroy
145
- group.should_not be_destroyed
146
- group.users.should_not include(user)
147
- end
140
+ context 'when removing' do
141
+ it "removes members from a group" do
142
+ group.add user
143
+ group.add widget
148
144
 
149
- it "removes the membership relations when a group is destroyed" do
150
- group.add user
151
- group.add task
152
- group.destroy
145
+ group.users.delete(user)
146
+ group.widgets.destroy(widget)
153
147
 
154
- user.should_not be_destroyed
155
- user.reload.groups.should be_empty
148
+ expect(group.widgets).to_not include(widget)
149
+ expect(group.users).to_not include(user)
156
150
 
157
- task.should_not be_destroyed
158
- task.reload.groups.should be_empty
159
- end
151
+ expect(widget.groups).to_not include(group)
152
+ expect(user.groups).to_not include(group)
153
+ end
160
154
 
161
- context 'when merging' do
162
- it "moves the members from source to destination and destroys the source" do
163
- source = MongoidGroup.create!
164
- destination = MongoidProject.create!
155
+ it "removes groups from a member" do
156
+ group.add widget
157
+ group.add user
165
158
 
166
- source.add(user)
167
- source.add(manager)
168
- destination.add(task)
159
+ user.groups.delete(group)
160
+ widget.groups.destroy(group)
169
161
 
170
- destination.merge!(source)
171
- source.destroyed?.should be_true
172
-
173
- destination.users.to_a.should include(user)
174
- destination.managers.to_a.should include(manager)
175
- destination.tasks.to_a.should include(task)
162
+ expect(group.widgets).to_not include(widget)
163
+ expect(group.users).to_not include(user)
164
+
165
+ expect(widget.groups).to_not include(group)
166
+ expect(user.groups).to_not include(group)
167
+ end
168
+
169
+ it "removes the membership relation when a member is destroyed" do
170
+ group.add user
171
+ user.destroy
172
+ expect(group).not_to be_destroyed
173
+ expect(group.users).not_to include(user)
174
+ end
175
+
176
+ it "removes the membership relations when a group is destroyed" do
177
+ group.add user
178
+ group.add task
179
+ group.destroy
180
+
181
+ expect(user).not_to be_destroyed
182
+ expect(user.reload.groups).to be_empty
183
+
184
+ expect(task).not_to be_destroyed
185
+ expect(task.reload.groups).to be_empty
186
+ end
187
+ end
188
+
189
+ context 'when checking group membership' do
190
+ it "members can check if they belong to any/all groups" do
191
+ user.groups << group
192
+ group2 = MongoidGroup.create!
193
+ user.groups << group2
194
+ group3 = MongoidGroup.create!
195
+
196
+ expect(user.groups).to include(group)
197
+ expect(user.groups).to include(group2)
198
+
199
+ expect(MongoidUser.in_group(group).first).to eql(user)
200
+ expect(MongoidUser.in_group(group2).first).to eql(user)
201
+ expect(user.in_group?(group)).to be true
202
+
203
+ expect(MongoidUser.in_any_group(group).first).to eql(user)
204
+ expect(MongoidUser.in_any_group(group3)).to be_empty
205
+ expect(user.in_any_group?(group2, group3)).to be true
206
+ expect(user.in_any_group?(group3)).to be false
207
+
208
+ expect(MongoidUser.in_all_groups(group, group2).first).to eql(user)
209
+ expect(MongoidUser.in_all_groups([group, group2]).first).to eql(user)
210
+ expect(user.in_all_groups?(group, group2)).to be true
211
+ expect(user.in_all_groups?(group, group3)).to be false
212
+ end
213
+
214
+ it "members can check if groups are shared" do
215
+ user.groups << group
216
+ widget.groups << group
217
+ user2 = MongoidUser.create!(:groups => [group])
218
+
219
+ expect(user.shares_any_group?(widget)).to be true
220
+ expect(MongoidWidget.shares_any_group(user).to_a).to include(widget)
221
+ expect(MongoidUser.shares_any_group(widget).to_a).to include(user, user2)
222
+
223
+ expect(user.shares_any_group?(user2)).to be true
224
+ expect(MongoidUser.shares_any_group(user).to_a).to include(user2)
225
+ end
176
226
  end
177
227
 
178
- it "fails to merge if the destination group cannot contain the source group's members" do
179
- source = MongoidProject.create!
180
- destination = MongoidGroup.create!
228
+ context 'when merging' do
229
+ it "moves the members from source to destination and destroys the source" do
230
+ source = MongoidGroup.create!
231
+ destination = MongoidProject.create!
232
+
233
+ source.add(user)
234
+ source.add(manager)
235
+ destination.add(task)
236
+
237
+ destination.merge!(source)
238
+ expect(source.destroyed?).to be true
239
+
240
+ expect(destination.users.to_a).to include(user)
241
+ expect(destination.managers.to_a).to include(manager)
242
+ expect(destination.tasks.to_a).to include(task)
243
+ end
244
+
245
+ it "fails to merge if the destination group cannot contain the source group's members" do
246
+ source = MongoidProject.create!
247
+ destination = MongoidGroup.create!
181
248
 
182
- source.add(issue)
183
- destination.add(user)
249
+ source.add(issue)
250
+ destination.add(user)
184
251
 
185
- # Issues cannot be members of a MongoidGroup
186
- expect {destination.merge!(source)}.to raise_error(ArgumentError)
252
+ # Issues cannot be members of a MongoidGroup
253
+ expect {destination.merge!(source)}.to raise_error(ArgumentError)
254
+ end
255
+
256
+ it "merges incompatible groups as long as all the source members can be moved to the destination" do
257
+ source = MongoidProject.create!
258
+ destination = MongoidGroup.create!
259
+
260
+ source.add(user)
261
+ destination.add(task)
262
+
263
+ expect {destination.merge!(source)}.to_not raise_error
264
+
265
+ expect(source.destroyed?).to be true
266
+ expect(destination.users.to_a).to include(user)
267
+ expect(destination.tasks.to_a).to include(task)
268
+ end
187
269
  end
188
270
 
189
- it "merges incompatible groups as long as all the source members can be moved to the destination" do
190
- source = MongoidProject.create!
191
- destination = MongoidGroup.create!
271
+ it "members can belong to many groups" do
272
+ user.groups << group
273
+ group2 = MongoidGroup.create!
274
+ user.groups << group2
192
275
 
193
- source.add(user)
194
- destination.add(task)
276
+ group3 = user.groups.create!
195
277
 
196
- expect {destination.merge!(source)}.to_not raise_error
278
+ user.save!
197
279
 
198
- source.destroyed?.should be_true
199
- destination.users.to_a.should include(user)
200
- destination.tasks.to_a.should include(task)
280
+ group4 = MongoidGroup.create!
281
+
282
+ expect(user.groups).to include(group, group2, group3)
283
+
284
+ expect(MongoidUser.in_group(group).first).to eql(user)
285
+ expect(MongoidUser.in_group(group2).first).to eql(user)
286
+ expect(user.in_group?(group)).to be true
287
+ expect(user.in_group?(group4)).to be false
288
+
289
+ expect(MongoidUser.in_any_group(group, group4).first).to eql(user)
290
+ expect(MongoidUser.in_any_group(group4)).to be_empty
291
+ expect(user.in_any_group?(group2, group4)).to be true
292
+ expect(user.in_any_group?(group4)).to be false
293
+
294
+ expect(MongoidUser.in_all_groups(group, group2).first).to eql(user)
295
+ expect(MongoidUser.in_all_groups([group, group3]).first).to eql(user)
296
+ expect(MongoidUser.in_all_groups([group2, group4])).to be_empty
297
+ expect(user.in_all_groups?(group, group3)).to be true
298
+ expect(user.in_all_groups?(group, group4)).to be false
299
+
300
+ expect(MongoidUser.in_only_groups(group, group2, group3).first).to eql(user)
301
+ expect(MongoidUser.in_only_groups(group, group2, group3, group4)).to be_empty
302
+ expect(MongoidUser.in_only_groups(group, group2)).to be_empty
303
+ expect(user.in_only_groups?(group, group2, group3)).to be true
304
+ expect(user.in_only_groups?(group, group2)).to be false
305
+ expect(user.in_only_groups?(group, group2, group3, group4)).to be false
201
306
  end
202
307
  end
203
308
 
204
- it "members can belong to many groups" do
205
- user.groups << group
206
- group2 = MongoidGroup.create!
207
- user.groups << group2
208
-
209
- user.groups.should include(group)
210
- user.groups.should include(group2)
211
-
212
- MongoidUser.in_group(group).first.should eql(user)
213
- MongoidUser.in_group(group2).first.should eql(user)
214
-
215
- MongoidUser.in_any_group(group).first.should eql(user)
216
- MongoidUser.in_all_groups(group, group2).first.should eql(user)
217
- MongoidUser.in_all_groups([group, group2]).first.should eql(user)
218
- end
219
-
220
- it "members can have named groups" do
221
- user.named_groups << :admin
222
- user.named_groups << :user
223
- user.save
224
- user.named_groups.should include(:admin)
225
-
226
- user.in_named_group?(:admin).should be_true
227
- user.in_any_named_group?(:admin, :user, :test).should be_true
228
- user.in_all_named_groups?(:admin, :user).should be_true
229
- user.in_all_named_groups?(:admin, :user, :test).should be_false
230
-
231
- MongoidUser.in_named_group(:admin).first.should eql(user)
232
- MongoidUser.in_any_named_group(:admin, :test).first.should eql(user)
233
- MongoidUser.in_all_named_groups(:admin, :user).first.should eql(user)
234
-
235
- # Uniqueness
236
- user.named_groups << :admin
237
- user.save
238
- user.named_groups.count{|g| g == :admin}.should == 1
239
- end
240
-
241
- it "members can check if groups are shared" do
242
- user.groups << group
243
- user2 = MongoidUser.create!(:groups => [group])
244
-
245
- user.shares_any_group?(user2).should be_true
246
- MongoidUser.shares_any_group(user).to_a.should include(user2)
309
+ context "when using membership types with groups" do
310
+ it 'adds groups to a member with a specific membership type' do
311
+ group.add(user, as: :admin)
312
+
313
+ expect(user.groups).to include(group)
314
+ expect(group.members).to include(user)
315
+ expect(group.users).to include(user)
316
+
317
+ expect(user.groups(:as => :admin)).to include(group)
318
+ expect(user.groups.as(:admin)).to include(group)
319
+ expect(group.members).to include(user)
320
+ expect(group.users).to include(user)
321
+ end
322
+
323
+ it 'adds members to a group with specific membership types' do
324
+ group.add(user, as: 'manager')
325
+ group.add(widget)
326
+
327
+ expect(user.groups).to include(group)
328
+ expect(group.members).to include(user)
329
+ expect(group.members.as(:manager)).to include(user)
330
+ expect(group.users).to include(user)
331
+
332
+ expect(user.groups(:as => :manager)).to include(group)
333
+ expect(group.members).to include(user)
334
+ expect(group.users).to include(user)
335
+ end
336
+
337
+ it "adds multiple members to a group with a specific membership type" do
338
+ manager = MongoidUser.create!
339
+ group.add(user, manager, as: :manager)
340
+
341
+ expect(group.users.as(:manager)).to include(user, manager)
342
+ expect(group.users(as: :manager)).to include(user, manager)
343
+ end
344
+
345
+ it "finds members by membership type" do
346
+ group.add user, as: 'manager'
347
+ expect(MongoidUser.as(:manager)).to include(user)
348
+ end
349
+
350
+ it "finds members by group with membership type" do
351
+ group.add user, as: 'employee'
352
+ manager = MongoidUser.create!
353
+ group.add(user, manager, as: :manager)
354
+
355
+ employees = MongoidUser.in_group(group).as('employee')
356
+ expect(employees).to include(user)
357
+ expect(employees).to_not include(manager)
358
+ end
359
+
360
+ it "finds the group a member belongs to with a membership type" do
361
+ group.add user, as: MongoidManager
362
+ user.groups.create!
363
+
364
+ expect(MongoidGroup.with_member(user).as(MongoidManager).to_a).to eq([group])
365
+ end
366
+
367
+ it "checks if members belong to any groups with a certain membership type" do
368
+ group2 = MongoidGroup.create!
369
+ group.add(user, as: 'employee')
370
+ group2.add(user)
371
+
372
+ expect(MongoidUser.in_any_group(group, group2).as('employee').first).to eql(user)
373
+ end
374
+
375
+ it "checks if members belong to all groups with a certain membership type" do
376
+ group2 = MongoidGroup.create!
377
+ group3 = MongoidGroup.create!
378
+ group.add(user, as: 'employee')
379
+ group2.add(user, as: 'employee')
380
+ group3.add(user, as: 'contractor')
381
+
382
+ expect(MongoidUser.in_all_groups(group, group2).as('employee').first).to eql(user)
383
+ expect(MongoidUser.in_all_groups([group, group2]).as('employee').first).to eql(user)
384
+ expect(MongoidUser.in_all_groups(group, group3).as('employee')).to be_empty
385
+
386
+ expect(user.in_all_groups?(group, group2, group3)).to be true
387
+ expect(user.in_all_groups?(group, group2, as: :employee)).to be true
388
+ expect(user.in_all_groups?(group, group3, as: 'employee')).to be false
389
+ end
390
+
391
+ it "checks if members belong to only groups with a certain membership type" do
392
+ group2 = MongoidGroup.create!
393
+ group3 = MongoidGroup.create!
394
+ group4 = MongoidGroup.create!
395
+
396
+ group.add(user, as: 'employee')
397
+ group2.add(user, as: 'employee')
398
+ group3.add(user, as: 'contractor')
399
+ group4.add(user, as: 'employee')
400
+
401
+ expect(MongoidUser.in_only_groups(group, group2, group4).as('employee').first).to eql(user)
402
+ expect(MongoidUser.in_only_groups([group, group2]).as('employee')).to be_empty
403
+ expect(MongoidUser.in_only_groups(group, group2, group3, group4).as('employee')).to be_empty
404
+
405
+ expect(user.in_only_groups?(group, group2, group3, group4))
406
+ expect(user.in_only_groups?(group, group2, group4, as: 'employee')).to be true
407
+ expect(user.in_only_groups?(group, group2, as: 'employee')).to be false
408
+ expect(user.in_only_groups?(group, group2, group3, group4, as: 'employee')).to be false
409
+ end
410
+
411
+ it "members can check if groups are shared with the same membership type" do
412
+ user2 = MongoidUser.create!
413
+ group.add(user, user2, widget, as: "Sub Group #1")
414
+
415
+ expect(user.shares_any_group?(widget, as: "Sub Group #1")).to be true
416
+ expect(MongoidWidget.shares_any_group(user).as("Sub Group #1").to_a).to include(widget)
417
+ expect(MongoidUser.shares_any_group(widget).as("Sub Group #1").to_a).to include(user, user2)
418
+
419
+ expect(user.shares_any_group?(user2, as: "Sub Group #1")).to be true
420
+ expect(MongoidUser.shares_any_group(user).as("Sub Group #1").to_a).to include(user2)
421
+ end
422
+
423
+ context "when removing" do
424
+ before(:each) do
425
+ group.add user, as: 'employee'
426
+ group.add user, as: 'manager'
427
+ end
428
+
429
+ it "removes all membership types when removing a member from a group" do
430
+ group.add user
431
+
432
+ group.users.destroy(user)
433
+
434
+ expect(user.groups).to_not include(group)
435
+ expect(user.groups.as('manager')).to_not include(group)
436
+ expect(user.groups.as('employee')).to_not include(group)
437
+ expect(group.users).to_not include(user)
438
+ end
439
+
440
+ it "removes a specific membership type from the member side" do
441
+ user.groups.destroy(group, as: 'manager')
442
+ expect(user.groups.as('manager')).to be_empty
443
+ expect(user.groups.as('employee')).to include(group)
444
+ expect(user.groups).to include(group)
445
+ end
446
+
447
+ it "removes a specific membership type from the group side" do
448
+ group.users.delete(user, as: :manager)
449
+ expect(user.groups.as('manager')).to be_empty
450
+ expect(user.groups.as('employee')).to include(group)
451
+ expect(user.groups).to include(group)
452
+ end
453
+
454
+ it "retains the member in the group if all membership types have been removed" do
455
+ user.groups.delete(group, as: 'manager')
456
+ user.groups.delete(group, as: 'employee')
457
+ expect(user.groups).to include(group)
458
+ end
459
+ end
247
460
  end
248
-
249
- it "members can check if named groups are shared" do
250
- user.named_groups << :admin
251
- user2 = MongoidUser.create!(:named_groups => [:admin])
252
-
253
- user.shares_any_named_group?(user2).should be_true
254
- MongoidUser.shares_any_named_group(user).to_a.should include(user2)
461
+
462
+ context 'when using named groups' do
463
+ before(:each) do
464
+ user.named_groups.concat :admin, :user, :poster
465
+ user.save!
466
+ end
467
+
468
+ it "enforces uniqueness" do
469
+ user.named_groups << :admin
470
+ user.save!
471
+ expect(user.named_groups.count{|g| g == :admin}).to eq(1)
472
+ end
473
+
474
+ it "queries named groups" do
475
+ expect(user.named_groups).to include(:user, :admin)
476
+ end
477
+
478
+ it "removes named groups" do
479
+ user.named_groups.delete(:admin, :poster)
480
+ expect(user.named_groups).to include(:user)
481
+ expect(user.named_groups).to_not include(:admin, :poster)
482
+
483
+ user.named_groups.destroy(:user)
484
+ expect(user.named_groups).to be_empty
485
+ end
486
+
487
+ it "removes all named groups" do
488
+ user.named_groups.destroy_all
489
+ expect(user.named_groups).to be_empty
490
+ end
491
+
492
+ it "checks if a member belongs to one named group" do
493
+ expect(user.in_named_group?(:admin)).to be true
494
+ expect(MongoidUser.in_named_group(:admin).first).to eql(user)
495
+ end
496
+
497
+ it "checks if a member belongs to any named group" do
498
+ expect(user.in_any_named_group?(:admin, :user, :test)).to be true
499
+ expect(user.in_any_named_group?(:foo, :bar)).to be false
500
+
501
+ expect(MongoidUser.in_any_named_group(:admin, :test).first).to eql(user)
502
+ expect(MongoidUser.in_any_named_group(:test)).to be_empty
503
+ end
504
+
505
+ it "checks if a member belongs to all named groups" do
506
+ expect(user.in_all_named_groups?(:admin, :user)).to be true
507
+ expect(user.in_all_named_groups?(:admin, :user, :test)).to be false
508
+ expect(MongoidUser.in_all_named_groups(:admin, :user).first).to eql(user)
509
+ end
510
+
511
+ it "checks if a member belongs to only certain named groups" do
512
+ expect(user.in_only_named_groups?(:admin, :user, :poster)).to be true
513
+ expect(user.in_only_named_groups?(:admin, :user, :poster, :foo)).to be false
514
+ expect(user.in_only_named_groups?(:admin, :user)).to be false
515
+ expect(user.in_only_named_groups?(:admin, :user, :test)).to be false
516
+
517
+ expect(MongoidUser.in_only_named_groups(:admin, :user, :poster).first).to eql(user)
518
+ expect(MongoidUser.in_only_named_groups(:admin, :user, :poster, :foo)).to be_empty
519
+ expect(MongoidUser.in_only_named_groups(:admin)).to be_empty
520
+ end
521
+
522
+ it "checks if named groups are shared" do
523
+ user2 = MongoidUser.create!(:named_groups => [:admin])
524
+
525
+ expect(user.shares_any_named_group?(user2)).to be true
526
+ expect(MongoidUser.shares_any_named_group(user).to_a).to include(user2)
527
+ end
528
+
529
+ context 'and using membership types with named groups' do
530
+ before(:each) do
531
+ user.named_groups.concat :team1, :team2, as: 'employee'
532
+ user.named_groups.push :team3, as: 'manager'
533
+ user.named_groups.push :team1, as: 'developer'
534
+ end
535
+
536
+ it "queries named groups, filtering by membership type" do
537
+ expect(user.named_groups).to include(:team1, :team2, :team3)
538
+ expect(user.named_groups.as('manager')).to eq([:team3])
539
+ end
540
+
541
+ it "enforces uniqueness of named groups" do
542
+ user.named_groups << :team1
543
+ expect(user.named_groups.count{|g| g == :team1}).to eq(1)
544
+ expect(user.named_groups.as('employee').count{|g| g == :team1}).to eq(1)
545
+ end
546
+
547
+ it "enforces uniqueness of group name and membership type for group memberships" do
548
+ user.named_groups.push :team1, as: 'employee'
549
+ expect(user.named_groups.count{|g| g == :team1}).to eq(1)
550
+ expect(user.named_groups.as('employee').count{|g| g == :team1}).to eq(1)
551
+ end
552
+
553
+ it "checks if a member belongs to one named group with a certain membership type" do
554
+ expect(user.in_named_group?(:team1, as: 'employee')).to be true
555
+ expect(MongoidUser.in_named_group(:team1).as('employee').first).to eql(user)
556
+ end
557
+
558
+ it "checks if a member belongs to any named group with a certain membership type" do
559
+ expect(user.in_any_named_group?(:team1, :team3, as: 'employee')).to be true
560
+ expect(MongoidUser.in_any_named_group(:team2, :team3).as('manager').first).to eql(user)
561
+ end
562
+
563
+ it "checks if a member belongs to all named groups with a certain membership type" do
564
+ expect(user.in_all_named_groups?(:team1, :team2, as: 'employee')).to be true
565
+ expect(user.in_all_named_groups?(:team1, :team3, as: 'employee')).to be false
566
+ expect(MongoidUser.in_all_named_groups(:team1, :team2).as('employee').first).to eql(user)
567
+ end
568
+
569
+ it "checks if a member belongs to only certain named groups with a certain membership type" do
570
+ expect(user.in_only_named_groups?(:team1, :team2, as: 'employee')).to be true
571
+ expect(user.in_only_named_groups?(:team1, as: 'employee')).to be false
572
+ expect(user.in_only_named_groups?(:team1, :team3, as: 'employee')).to be false
573
+ expect(user.in_only_named_groups?(:foo, as: 'employee')).to be false
574
+
575
+ expect(MongoidUser.in_only_named_groups(:team1, :team2).as('employee').first).to eql(user)
576
+ expect(MongoidUser.in_only_named_groups(:team1).as('employee')).to be_empty
577
+ expect(MongoidUser.in_only_named_groups(:foo).as('employee')).to be_empty
578
+ end
579
+
580
+ it "checks if a member shares any named groups with a certain membership type" do
581
+ project = MongoidProject.create!(:named_groups => [:team3])
582
+
583
+ expect(user.shares_any_named_group?(project, as: 'manager')).to be true
584
+ expect(MongoidUser.shares_any_named_group(project).as('manager').to_a).to include(user)
585
+ end
586
+
587
+ it "removes named groups with a certain membership type" do
588
+ user.named_groups.delete(:team1, as: :employee)
589
+ expect(user.named_groups.as(:employee)).to include(:team2)
590
+ expect(user.named_groups.as(:employee)).to_not include(:team1)
591
+ expect(user.named_groups.as(:developer)).to include(:team1)
592
+ expect(user.named_groups).to include(:team1)
593
+ end
594
+
595
+ it "removes all named group memberships if membership type is not specified" do
596
+ user.named_groups.destroy(:team1)
597
+ expect(user.named_groups).to_not include(:team1)
598
+ expect(user.named_groups.as(:employee)).to_not include(:team1)
599
+ expect(user.named_groups.as(:developer)).to_not include(:team1)
600
+ expect(user.named_groups.as(:employee)).to include(:team2)
601
+ end
602
+ end
255
603
  end
256
- end
604
+ end