File: test_associations.rb

package info (click to toggle)
ruby-activeldap 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 1,588 kB
  • sloc: ruby: 18,143; sh: 12; makefile: 5
file content (719 lines) | stat: -rw-r--r-- 26,011 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
require 'al-test-utils'

class TestAssociations < Test::Unit::TestCase
  include AlTestUtils

  priority :must
  def test_belongs_to_replace_with_string
    make_temporary_user do |user,|
      make_temporary_group do |group1|
        make_temporary_group do |group2|
          user.groups = [group1, group2]
          user.save!

          user.groups = [group2.cn]

          group1.reload
          group2.reload
          assert_equal([group2], user.groups.to_a)
          assert_equal([], group1.member_uid(true))
          assert_equal([user.id], group2.member_uid(true))
        end
      end
    end
  end

  priority :normal
  def test_has_many_of_self
    @user_class.has_many(:references,
                         :class_name => "User",
                         :primary_key => "dn",
                         :foreign_key => "seeAlso")
    @user_class.set_associated_class(:references, @user_class)
    make_temporary_user do |user1, password1|
      make_temporary_user(:see_also => user1.dn.to_s) do |user2, password2|
        make_temporary_user(:see_also => user2.dn.to_s) do |user3, password3|
          make_temporary_user(:see_also => user2.dn.to_s) do |user4, password4|
            make_temporary_user(:see_also => user1.dn.to_s) do |user5, password5|
              user1_references = user1.references.collect {|r| r.dn.to_s}
              user2_references = user2.references.collect {|r| r.dn.to_s}
              user1_expected_references = [user2, user5].collect {|r| r.dn.to_s}
              user2_expected_references = [user3, user4].collect {|r| r.dn.to_s}
              assert_equal(user1_expected_references, user1_references)
              assert_equal(user2_expected_references, user2_references)
            end
          end
        end
      end
    end
  end

  def test_belongs_to_add_with_string
    make_temporary_user do |user,|
      make_temporary_group do |group1|
        make_temporary_group do |group2|
          assert_equal([[], []],
                       [group1.members.collect(&:cn),
                        group2.members.collect(&:cn)])

          user.groups = [group1.cn, group2.cn]
          user.save!

          group1.reload
          group2.reload
          assert_equal([[user.cn], [user.cn]],
                       [group1.members.collect(&:cn),
                        group2.members.collect(&:cn)])
        end
      end
    end
  end

  def test_has_many_delete_required_attribute
    make_temporary_group do |group|
      make_temporary_user do |user,|
        user.primary_group = group
        assert_raise(ActiveLdap::RequiredAttributeMissed) do
          group.primary_members.delete(user)
        end
      end
    end
  end

  def test_to_xml
    make_temporary_user do |user,|
      make_temporary_group do |group1|
        make_temporary_group do |group2|
          user.groups = [group1, group2]
          assert_equal(<<-EOX, user.groups.to_xml(:root => "groups"))
<?xml version="1.0" encoding="UTF-8"?>
<groups type="array">
  <group>
    <dn>#{group1.dn}</dn>
    <cns type="array">
      <cn>#{group1.cn}</cn>
    </cns>
    <gidNumber>#{group1.gid_number}</gidNumber>
    <memberUids type="array">
      <memberUid>#{user.cn}</memberUid>
    </memberUids>
    <objectClasses type="array">
      <objectClass>posixGroup</objectClass>
    </objectClasses>
  </group>
  <group>
    <dn>#{group2.dn}</dn>
    <cns type="array">
      <cn>#{group2.cn}</cn>
    </cns>
    <gidNumber>#{group2.gid_number}</gidNumber>
    <memberUids type="array">
      <memberUid>#{user.cn}</memberUid>
    </memberUids>
    <objectClasses type="array">
      <objectClass>posixGroup</objectClass>
    </objectClasses>
  </group>
</groups>
EOX
        end
      end
    end
  end

  def test_belongs_to_with_invalid_dn_attribute_value
    make_temporary_user do |user,|
      make_temporary_group do |group|
        user.primary_group = group
        user.uid = "#"
        assert_nothing_raised do
          user.primary_group.reload
        end
      end
    end
  end

  def test_belongs_to_foreign_key_before_1_1_0
    ActiveSupport::Deprecation.silence do
      @group_class.belongs_to :related_users, :many => "seeAlso",
                              :foreign_key => "dn"
    end
    @group_class.set_associated_class(:related_users, @user_class)
    make_temporary_user do |user,|
      make_temporary_group do |group|
        user.see_also = group.dn
        user.save!

        group = @group_class.find(group.id)
        assert_equal([user.dn], group.related_users.collect(&:dn))
      end
    end
  end

  def test_has_many_wrap_with_nonexistent_entry
    @user_class.has_many :references, :wrap => "seeAlso", :primary_key => "dn"
    @user_class.set_associated_class(:references, @group_class)
    @group_class.belongs_to :related_users, :many => "seeAlso",
                            :primary_key => "dn"
    @group_class.set_associated_class(:related_users, @user_class)
    make_temporary_user do |user,|
      make_temporary_group do |group1|
        make_temporary_group do |group2|
          user.references = [group1, group2]
          group3_dn = group2.dn.to_s.sub(/cn=(.*?),/, "cn=\\1-nonexistent,")
          user.see_also += [group3_dn]
          user.save!

          group3_dn = dn(group3_dn)
          user = @user_class.find(user.dn)
          assert_equal([group1.dn, group2.dn, group3_dn],
                       user.see_also)
          assert_equal([group1.dn, group2.dn, group3_dn],
                       user.references.collect(&:dn))
          assert_equal([group1.gid_number, group2.gid_number, nil],
                       user.references.collect(&:gid_number))
          assert_equal([false, false, true],
                       user.references.collect(&:new_entry?))
        end
      end
    end
  end

  def test_has_many_wrap_with_dn_value
    @user_class.has_many :references, :wrap => "seeAlso", :primary_key => "dn"
    @user_class.set_associated_class(:references, @group_class)
    @group_class.belongs_to :related_users, :many => "seeAlso",
                            :primary_key => "dn"
    @group_class.set_associated_class(:related_users, @user_class)
    make_temporary_user do |user,|
      make_temporary_group do |group1|
        make_temporary_group do |group2|
          make_temporary_group do |group3|
            entries = [user, group1, group2, group3]

            user.references << group1
            user, group1, group2, group3 = reload_entries(*entries)
            assert_references([[group1]], [user])
            assert_related_users([user], group1)
            assert_related_users([], group2)
            assert_related_users([], group3)

            user.references = [group2, group3]
            user, group1, group2, group3 = reload_entries(*entries)
            assert_references([[group2, group3]], [user])
            assert_related_users([], group1)
            assert_related_users([user], group2)
            assert_related_users([user], group3)

            user.references.delete(group2)
            user, group1, group2, group3 = reload_entries(*entries)
            assert_references([[group3]], [user])
            assert_related_users([], group1)
            assert_related_users([], group2)
            assert_related_users([user], group3)
          end
        end
      end
    end
  end

  def test_belongs_to_many_with_dn_value
    @user_class.has_many :references, :wrap => "seeAlso", :primary_key => "dn"
    @user_class.set_associated_class(:references, @group_class)
    @group_class.belongs_to :related_users, :many => "seeAlso",
                            :primary_key => "dn"
    @group_class.set_associated_class(:related_users, @user_class)
    make_temporary_group do |group|
      make_temporary_user do |user1,|
        make_temporary_user do |user2,|
          make_temporary_user do |user3,|
            entries = [group, user1, user2, user3]

            group.related_users = [user1, user2]
            group, user1, user2, user3 = reload_entries(*entries)
            assert_references([[group], [group], []],
                              [user1, user2, user3])
            assert_related_users([user1, user2], group)

            group.related_users << user3
            group, user1, user2, user3 = reload_entries(*entries)
            assert_references([[group], [group], [group]],
                              [user1, user2, user3])
            assert_related_users([user1, user2, user3], group)

            group.related_users.delete(user1)
            group, user1, user2, user3 = reload_entries(*entries)
            assert_references([[], [group], [group]],
                              [user1, user2, user3])
            assert_related_users([user2, user3], group)

            group.related_users = []
            group, user1, user2, user3 = reload_entries(*entries)
            assert_references([[], [], []],
                              [user1, user2, user3])
            assert_related_users([], group)
          end
        end
      end
    end
  end

  def test_belongs_to_many_with_dn_key
    @user_class.belongs_to :dn_groups, :many => "memberUid", :primary_key => "dn"
    @user_class.set_associated_class(:dn_groups, @group_class)
    @group_class.has_many :dn_members, :wrap => "memberUid", :primary_key => "dn"
    @group_class.set_associated_class(:dn_members, @user_class)
    make_temporary_group do |group|
      make_temporary_user do |user1,|
        make_temporary_user do |user2,|
          make_temporary_user do |user3,|
            entries = [group, user1, user2, user3]

            user1.dn_groups << group
            group, user1, user2, user3 = reload_entries(*entries)
            assert_dn_groups([[group], [], []], [user1, user2, user3])
            assert_dn_members([user1], group)

            user2.dn_groups = [group]
            group, user1, user2, user3 = reload_entries(*entries)
            assert_dn_groups([[group], [group], []], [user1, user2, user3])
            assert_dn_members([user1, user2], group)

            user1.dn_groups = []
            group, user1, user2, user3 = reload_entries(*entries)
            assert_dn_groups([[], [group], []], [user1, user2, user3])
            assert_dn_members([user2], group)

            user2.dn_groups.delete(group)
            group, user1, user2, user3 = reload_entries(*entries)
            assert_dn_groups([[], [], []], [user1, user2, user3])
            assert_dn_members([], group)
          end
        end
      end
    end
  end

  def test_belongs_to_many_add_by_dn_attribute
    make_temporary_group do |group1|
      make_temporary_group do |group2|
        make_temporary_user do |user,|
          user.update_attribute(:cn, "new #{user.cn}")

          user.groups = [group1]
          assert_equal([group1.id].sort,
                       user.groups.collect {|g| g.id}.sort)

          user.groups << group2.id
          assert_equal([group1.id, group2.id].sort,
                       user.groups.collect {|g| g.id}.sort)
        end
      end
    end
  end

  def test_belongs_to_many_delete
    make_temporary_group do |group1|
      make_temporary_group do |group2|
        make_temporary_user do |user,|
          user.update_attribute(:cn, "new #{user.cn}")

          user.groups = [group1, group2]
          assert_equal([group1.id, group2.id].sort,
                       user.groups.collect {|g| g.id}.sort)
          assert_equal([user.id].sort, group1.member_uid(true))
          assert_equal([user.id].sort, group2.member_uid(true))

          user.groups = []
          assert_equal([], user.groups.to_a)
          assert_equal([], group1.member_uid(true))
          assert_equal([], group2.member_uid(true))
        end
      end
    end
  end

  def test_belongs_to_before_save
    make_temporary_group do |group1|
      make_temporary_group do |group2|
        ensure_delete_group(group2.cn.succ) do |group3_name|
          group3 = @group_class.new(group3_name)
          group3.gid_number = group2.gid_number.succ
          make_temporary_user(:gid_number => group1.gid_number) do |user,|
            assert_equal(group1.gid_number, user.primary_group.gid_number)
            assert_equal(group1.gid_number, user.gid_number)

            user.primary_group = group2
            assert_equal(group2.gid_number, user.primary_group.gid_number)
            assert_equal(group2.gid_number, user.gid_number)

            user_in_ldap = @user_class.find(user.id)
            assert_equal(group1.gid_number,
                         user_in_ldap.primary_group.gid_number)
            assert_equal(group1.gid_number, user_in_ldap.gid_number)

            assert(group3.new_entry?)
            user.primary_group = group3
            assert_equal(group3.gid_number, user.primary_group.gid_number)
            assert_equal(group2.gid_number, user.gid_number)

            assert(user.save)
            assert_equal(group3.gid_number, user.gid_number)

            user_in_ldap = @user_class.find(user.id)
            assert(!user_in_ldap.primary_group.exists?)

            assert(group3.save)
            assert(user_in_ldap.primary_group.exists?)
            assert_equal(group3.gid_number,
                         user_in_ldap.primary_group.gid_number)
          end
        end
      end
    end
  end

  def test_extend
    mod = Module.new
    mod.__send__(:mattr_accessor, :called)
    mod.__send__(:define_method, :replace) do |entries|
      super(entries)
      mod.called = true
    end
    mod.called = false

    @group_class.send(:undef_method, :members, :members=, :__make_members)
    @group_class.has_many :members, :wrap => "memberUid",
                          :extend => mod
    @group_class.set_associated_class(:members, @user_class)

    make_temporary_group do |group|
      gid_number1 = group.gid_number.to_i + 1
      make_temporary_user(:gid_number => gid_number1) do |user1, password1|
        user1.update_attribute(:cn, "new #{user1.cn}")

        assert(!mod.called)
        group.members = [user1]
        assert(mod.called)
      end
    end
  end

  def test_has_many_wrap_assign
    make_temporary_group do |group|
      gid_number1 = group.gid_number.to_i + 1
      gid_number2 = group.gid_number.to_i + 2
      make_temporary_user(:gid_number => gid_number1) do |user1, password1|
        make_temporary_user(:gid_number => gid_number2) do |user2, password2|
          user1.update_attribute(:cn, "new #{user1.cn}")
          user2.update_attribute(:cn, "new #{user2.cn}")

          assert_equal([], group.members.to_a)
          assert_equal([], group.member_uid(true))

          assert_equal(gid_number1, user1.gid_number.to_i)
          assert_equal(gid_number2, user2.gid_number.to_i)
          group.members = [user1, user2]
          assert_equal([user1.uid, user2.uid].sort,
                       group.members.collect {|x| x.uid}.sort)
          assert_equal([user1.uid, user2.uid].sort, group.member_uid.sort)
          assert_equal(gid_number2, user2.gid_number.to_i)

          group.members = [user1]
          assert_equal([user1.uid].sort,
                       group.members.collect {|x| x.uid}.sort)
          assert_equal(user1.uid, group.member_uid)
          assert_equal(gid_number1, user1.gid_number.to_i)
        end
      end
    end
  end

  def test_has_many_wrap_assign_new_entry
    make_temporary_user do |user1, |
      make_temporary_user do |user2, |
        ensure_delete_group('test_new_group') do |cn|
          group = @group_class.new(:cn => cn, :gid_number => default_gid)

          assert_equal([], group.members.to_a)
          assert_equal([], group.member_uid(true))

          group.members = [user1, user2]

          assert group.new_entry?
          assert_equal([user1.uid, user2.uid].sort, group.members.map {|x| x.uid }.sort)
          assert_equal([user1.uid, user2.uid].sort, group.member_uid(true).sort)

          group.members = [user2]

          assert group.new_entry?
          assert_equal([user2.uid], group.members.map {|x| x.uid })
          assert_equal(user2.uid, group.member_uid)

          group.members << user1

          assert group.new_entry?
          assert_equal([user1.uid, user2.uid].sort, group.members.map {|x| x.uid }.sort)
          assert_equal([user1.uid, user2.uid].sort, group.member_uid.sort)

          assert(group.save)

          group = @group_class.find(cn)
          assert_equal([user1.uid, user2.uid].sort, group.members.map {|x| x.uid }.sort)
          assert_equal([user1.uid, user2.uid].sort, group.member_uid.sort)
        end
      end
    end
  end

  def test_has_many_validation
    group_class = Class.new(ActiveLdap::Base)
    group_class.ldap_mapping :prefix => "ou=Groups",
                             :scope => :sub,
                             :classes => ["posixGroup"]
    assert_raises(ArgumentError) do
      group_class.has_many :users, :unknown_name => "value"
    end

    mod = Module.new
    assert_nothing_raised do
      group_class.has_many :users, :class_name => "User"
      group_class.has_many :members, :class => @user_class, :wrap => "memberUid",
                           :extend => mod
      group_class.has_many :primary_members, :class => @user_class,
                           :primary_key => "gidNumber",
                           :foreign_key => "gidNumber",
                           :extend => mod
    end
  end

  def test_belongs_to_validation
    user_class = Class.new(ActiveLdap::Base)
    user_class.ldap_mapping :dn_attribute => "uid",
                            :prefix => "ou=Users",
                            :scope => :sub,
                            :classes => ["posixAccount", "person"]
    assert_raises(ArgumentError) do
      user_class.belongs_to :groups, :unknown_name => "value"
    end

    mod = Module.new
    assert_nothing_raised do
      user_class.belongs_to :string_groups, :class_name => "Group"
      user_class.belongs_to :groups, :class => @group_class,
                            :many => "memberUid",
                            :extend => mod
      user_class.belongs_to :primary_group, :class => @group_class,
                            :foreign_key => "gidNumber",
                            :primary_key => "gidNumber",
                            :extend => mod
    end
  end

  def test_has_many_assign
    make_temporary_group do |group|
      gid_number1 = group.gid_number.to_i + 1
      gid_number2 = group.gid_number.to_i + 2
      make_temporary_user(:gid_number => gid_number1) do |user1, password1|
        make_temporary_user(:gid_number => gid_number2) do |user2, password2|
          assert_equal(gid_number1, user1.gid_number.to_i)
          group.primary_members = [user1]
          assert_equal([user1.uid].sort,
                       group.primary_members.collect {|x| x.uid}.sort)
          assert_equal(group.gid_number, user1.gid_number)

          assert_equal(gid_number2, user2.gid_number.to_i)
          group.primary_members = [user1, user2]
          assert_equal([user1.uid, user2.uid].sort,
                       group.primary_members.collect {|x| x.uid}.sort)
          assert_equal(group.gid_number, user2.gid_number)


          assert_raises(ActiveLdap::RequiredAttributeMissed) do
            group.primary_members = []
          end

          assert_raises(ActiveLdap::RequiredAttributeMissed) do
            group.primary_members = [user1]
          end

          assert_raises(ActiveLdap::RequiredAttributeMissed) do
            group.primary_members = [user2]
          end

          assert_nothing_raised do
            group.primary_members = [user1, user2]
          end
        end
      end
    end
  end

  def test_belongs_to_many
    make_temporary_group do |group1|
      make_temporary_group do |group2|
        make_temporary_user do |user,|
          user.update_attribute(:cn, "new #{user.cn}")

          assert_equal([], user.groups.to_a)
          assert_equal([], group1.member_uid(true))
          assert_equal([], group2.member_uid(true))

          user.groups << group1
          assert_equal([group1.id].sort, user.groups.collect {|g| g.id}.sort)
          assert_equal([user.id].sort, group1.member_uid(true))
          assert_equal([].sort, group2.member_uid(true))

          user.groups << group2
          assert_equal([group1.id, group2.id].sort,
                       user.groups.collect {|g| g.id}.sort)
          assert_equal([user.id].sort, group1.member_uid(true))
          assert_equal([user.id].sort, group2.member_uid(true))
        end
      end
    end
  end

  def test_belongs_to_many_non_exist
    make_temporary_group do |group|
      ensure_delete_user("temp-user1") do |user1,|
        options = {:uid => "temp-user2", :gid_number => group.gid_number.succ}
        make_temporary_user(options) do |user2,|
          ensure_delete_user("temp-user3") do |user3,|
            group.members << user2
            group.member_uid = [user1, group.member_uid, user3]
            assert(group.save)
            group.members.reload
            assert_equal([user1, user2.id, user3],
                         group.members.collect {|g| g.id})
            assert_equal([true, false, true],
                         group.members.collect {|g| g.new_entry?})
          end
        end
      end
    end
  end

  def test_belongs_to
    make_temporary_group do |group|
      gid_number = group.gid_number.to_i + 1
      make_temporary_user(:gid_number => group.gid_number) do |user_in_group,|
        make_temporary_user(:gid_number => gid_number) do |user_not_in_group,|
          assert(user_in_group.primary_group.reload)
          assert(user_in_group.primary_group.loaded?)
          assert_equal(group.gid_number, user_in_group.gid_number)
          assert_equal(group.gid_number, user_in_group.primary_group.gid_number)


          assert(!user_not_in_group.primary_group.loaded?)

          assert_equal(gid_number, user_not_in_group.gid_number.to_i)
          assert_not_equal(group.gid_number, user_not_in_group.gid_number)

          user_not_in_group.primary_group = group
          assert(user_not_in_group.primary_group.loaded?)
          assert(user_not_in_group.primary_group.updated?)
          assert_equal(group.gid_number, user_not_in_group.gid_number)
          assert_equal(group.gid_number,
                       user_not_in_group.primary_group.gid_number)
          assert_not_equal(gid_number, user_not_in_group.gid_number.to_i)

          assert_equal(group.gid_number, user_in_group.gid_number)
          assert_equal(group.gid_number, user_in_group.primary_group.gid_number)
        end
      end
    end
  end

  def test_has_many_wrap
    make_temporary_group do |group|
      gid_number1 = group.gid_number.to_i + 1
      gid_number2 = group.gid_number.to_i + 2
      make_temporary_user(:gid_number => gid_number1) do |user1, password1|
        make_temporary_user(:gid_number => gid_number2) do |user2, password2|
          user1.update_attribute(:cn, "new #{user1.cn}")
          user2.update_attribute(:cn, "new #{user2.cn}")

          assert_equal([], group.members.to_a)
          assert_equal([], group.member_uid(true))

          assert_equal(gid_number1, user1.gid_number.to_i)
          group.members << user1
          assert_equal([user1.uid].sort,
                       group.members.collect {|x| x.uid}.sort)
          assert_equal(user1.uid, group.member_uid)
          assert_equal(gid_number1, user1.gid_number.to_i)

          assert_equal(gid_number2, user2.gid_number.to_i)
          group.members << user2
          assert_equal([user1.uid, user2.uid].sort,
                       group.members.collect {|x| x.uid}.sort)
          assert_equal([user1.uid, user2.uid].sort, group.member_uid.sort)
          assert_equal(gid_number2, user2.gid_number.to_i)
        end
      end
    end
  end

  def test_has_many
    make_temporary_group do |group|
      gid_number1 = group.gid_number.to_i + 1
      gid_number2 = group.gid_number.to_i + 2
      make_temporary_user(:gid_number => gid_number1) do |user1, password1|
        make_temporary_user(:gid_number => gid_number2) do |user2, password2|
          assert_equal([], group.primary_members.to_a)

          assert_equal(gid_number1, user1.gid_number.to_i)
          group.primary_members << user1
          assert_equal([user1.uid].sort,
                       group.primary_members.collect {|x| x.uid}.sort)
          assert_equal(group.gid_number, user1.gid_number)

          assert_equal(gid_number2, user2.gid_number.to_i)
          group.primary_members << user2
          assert_equal([user1.uid, user2.uid].sort,
                       group.primary_members.collect {|x| x.uid}.sort)
          assert_equal(group.gid_number, user2.gid_number)
        end
      end
    end
  end

  private
  def reload_entries(*entries)
    entries.collect do |entry|
      entry.class.find(entry[entry.dn_attribute])
    end
  end

  def assert_groups_relation(expected_groups_values, entries, relation_name)
    expected_groups_values = expected_groups_values.collect do |groups|
      groups.collect(&:cn).sort
    end
    actual_groups_values = entries.collect do |entry|
      entry.send(relation_name).collect(&:cn).sort
    end
    assert_equal(expected_groups_values, actual_groups_values)
  end

  def assert_users_relation(expected_users, group, relation_name)
    assert_equal(expected_users.collect(&:cn).sort,
                 group.send(relation_name).collect(&:cn).sort)
  end

  def assert_references(expected_groups_values, users)
    assert_groups_relation(expected_groups_values, users, :references)
  end

  def assert_related_users(expected_users, group)
    assert_users_relation(expected_users, group, :related_users)
  end

  def assert_dn_groups(expected_groups_values, users)
    assert_groups_relation(expected_groups_values, users, :dn_groups)
  end

  def assert_dn_members(expected_users, group)
    assert_users_relation(expected_users, group, :dn_members)
  end
end