powerhome-activeldap 3.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +6 -0
  3. data/COPYING +340 -0
  4. data/Gemfile +12 -0
  5. data/LICENSE +59 -0
  6. data/README.textile +140 -0
  7. data/TODO +32 -0
  8. data/benchmark/README.md +64 -0
  9. data/benchmark/bench-backend.rb +247 -0
  10. data/benchmark/bench-instantiate.rb +98 -0
  11. data/benchmark/config.yaml.sample +5 -0
  12. data/doc/text/development.textile +54 -0
  13. data/doc/text/news.textile +811 -0
  14. data/doc/text/rails.textile +144 -0
  15. data/doc/text/tutorial.textile +1010 -0
  16. data/examples/config.yaml.example +5 -0
  17. data/examples/example.der +0 -0
  18. data/examples/example.jpg +0 -0
  19. data/examples/groupadd +41 -0
  20. data/examples/groupdel +35 -0
  21. data/examples/groupls +49 -0
  22. data/examples/groupmod +42 -0
  23. data/examples/lpasswd +55 -0
  24. data/examples/objects/group.rb +13 -0
  25. data/examples/objects/ou.rb +4 -0
  26. data/examples/objects/user.rb +20 -0
  27. data/examples/ouadd +38 -0
  28. data/examples/useradd +45 -0
  29. data/examples/useradd-binary +53 -0
  30. data/examples/userdel +34 -0
  31. data/examples/userls +50 -0
  32. data/examples/usermod +42 -0
  33. data/examples/usermod-binary-add +50 -0
  34. data/examples/usermod-binary-add-time +54 -0
  35. data/examples/usermod-binary-del +48 -0
  36. data/examples/usermod-lang-add +43 -0
  37. data/lib/active_ldap.rb +85 -0
  38. data/lib/active_ldap/action_controller/ldap_benchmarking.rb +55 -0
  39. data/lib/active_ldap/acts/tree.rb +78 -0
  40. data/lib/active_ldap/adapter/base.rb +707 -0
  41. data/lib/active_ldap/adapter/jndi.rb +184 -0
  42. data/lib/active_ldap/adapter/jndi_connection.rb +185 -0
  43. data/lib/active_ldap/adapter/ldap.rb +290 -0
  44. data/lib/active_ldap/adapter/ldap_ext.rb +105 -0
  45. data/lib/active_ldap/adapter/net_ldap.rb +309 -0
  46. data/lib/active_ldap/adapter/net_ldap_ext.rb +23 -0
  47. data/lib/active_ldap/association/belongs_to.rb +47 -0
  48. data/lib/active_ldap/association/belongs_to_many.rb +58 -0
  49. data/lib/active_ldap/association/children.rb +21 -0
  50. data/lib/active_ldap/association/collection.rb +105 -0
  51. data/lib/active_ldap/association/has_many.rb +31 -0
  52. data/lib/active_ldap/association/has_many_utils.rb +44 -0
  53. data/lib/active_ldap/association/has_many_wrap.rb +75 -0
  54. data/lib/active_ldap/association/proxy.rb +107 -0
  55. data/lib/active_ldap/associations.rb +205 -0
  56. data/lib/active_ldap/attribute_methods.rb +23 -0
  57. data/lib/active_ldap/attribute_methods/before_type_cast.rb +24 -0
  58. data/lib/active_ldap/attribute_methods/dirty.rb +43 -0
  59. data/lib/active_ldap/attribute_methods/query.rb +31 -0
  60. data/lib/active_ldap/attribute_methods/read.rb +44 -0
  61. data/lib/active_ldap/attribute_methods/write.rb +38 -0
  62. data/lib/active_ldap/attributes.rb +176 -0
  63. data/lib/active_ldap/base.rb +1410 -0
  64. data/lib/active_ldap/callbacks.rb +71 -0
  65. data/lib/active_ldap/command.rb +49 -0
  66. data/lib/active_ldap/compatible.rb +44 -0
  67. data/lib/active_ldap/configuration.rb +147 -0
  68. data/lib/active_ldap/connection.rb +299 -0
  69. data/lib/active_ldap/distinguished_name.rb +291 -0
  70. data/lib/active_ldap/entry_attribute.rb +78 -0
  71. data/lib/active_ldap/escape.rb +12 -0
  72. data/lib/active_ldap/get_text.rb +20 -0
  73. data/lib/active_ldap/get_text/parser.rb +161 -0
  74. data/lib/active_ldap/helper.rb +92 -0
  75. data/lib/active_ldap/human_readable.rb +133 -0
  76. data/lib/active_ldap/ldap_error.rb +74 -0
  77. data/lib/active_ldap/ldif.rb +930 -0
  78. data/lib/active_ldap/log_subscriber.rb +50 -0
  79. data/lib/active_ldap/object_class.rb +95 -0
  80. data/lib/active_ldap/operations.rb +624 -0
  81. data/lib/active_ldap/persistence.rb +100 -0
  82. data/lib/active_ldap/populate.rb +53 -0
  83. data/lib/active_ldap/railtie.rb +43 -0
  84. data/lib/active_ldap/railties/controller_runtime.rb +48 -0
  85. data/lib/active_ldap/schema.rb +701 -0
  86. data/lib/active_ldap/schema/syntaxes.rb +422 -0
  87. data/lib/active_ldap/timeout.rb +75 -0
  88. data/lib/active_ldap/timeout_stub.rb +17 -0
  89. data/lib/active_ldap/user_password.rb +99 -0
  90. data/lib/active_ldap/validations.rb +200 -0
  91. data/lib/active_ldap/version.rb +3 -0
  92. data/lib/active_ldap/xml.rb +139 -0
  93. data/lib/rails/generators/active_ldap/model/USAGE +18 -0
  94. data/lib/rails/generators/active_ldap/model/model_generator.rb +47 -0
  95. data/lib/rails/generators/active_ldap/model/templates/model_active_ldap.rb +3 -0
  96. data/lib/rails/generators/active_ldap/scaffold/scaffold_generator.rb +14 -0
  97. data/lib/rails/generators/active_ldap/scaffold/templates/ldap.yml +19 -0
  98. data/po/en/active-ldap.po +4029 -0
  99. data/po/ja/active-ldap.po +4060 -0
  100. data/test/add-phonetic-attribute-options-to-slapd.ldif +10 -0
  101. data/test/al-test-utils.rb +428 -0
  102. data/test/command.rb +111 -0
  103. data/test/config.yaml.sample +6 -0
  104. data/test/fixtures/lower_case_object_class_schema.rb +802 -0
  105. data/test/run-test.rb +34 -0
  106. data/test/test_acts_as_tree.rb +60 -0
  107. data/test/test_adapter.rb +121 -0
  108. data/test/test_associations.rb +701 -0
  109. data/test/test_attributes.rb +117 -0
  110. data/test/test_base.rb +1214 -0
  111. data/test/test_base_per_instance.rb +61 -0
  112. data/test/test_bind.rb +62 -0
  113. data/test/test_callback.rb +31 -0
  114. data/test/test_configuration.rb +40 -0
  115. data/test/test_connection.rb +82 -0
  116. data/test/test_connection_per_class.rb +112 -0
  117. data/test/test_connection_per_dn.rb +112 -0
  118. data/test/test_dirty.rb +98 -0
  119. data/test/test_dn.rb +172 -0
  120. data/test/test_find.rb +176 -0
  121. data/test/test_groupadd.rb +50 -0
  122. data/test/test_groupdel.rb +46 -0
  123. data/test/test_groupls.rb +107 -0
  124. data/test/test_groupmod.rb +51 -0
  125. data/test/test_ldif.rb +1890 -0
  126. data/test/test_load.rb +133 -0
  127. data/test/test_lpasswd.rb +75 -0
  128. data/test/test_object_class.rb +74 -0
  129. data/test/test_persistence.rb +131 -0
  130. data/test/test_reflection.rb +175 -0
  131. data/test/test_schema.rb +559 -0
  132. data/test/test_syntax.rb +444 -0
  133. data/test/test_user.rb +217 -0
  134. data/test/test_user_password.rb +108 -0
  135. data/test/test_useradd-binary.rb +62 -0
  136. data/test/test_useradd.rb +57 -0
  137. data/test/test_userdel.rb +48 -0
  138. data/test/test_userls.rb +91 -0
  139. data/test/test_usermod-binary-add-time.rb +65 -0
  140. data/test/test_usermod-binary-add.rb +64 -0
  141. data/test/test_usermod-binary-del.rb +66 -0
  142. data/test/test_usermod-lang-add.rb +59 -0
  143. data/test/test_usermod.rb +58 -0
  144. data/test/test_validation.rb +274 -0
  145. metadata +379 -0
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $VERBOSE = true
4
+
5
+ $KCODE = 'u' if RUBY_VERSION < "1.9"
6
+
7
+ base_dir = File.expand_path(File.dirname(__FILE__))
8
+ top_dir = File.expand_path(File.join(base_dir, ".."))
9
+ $LOAD_PATH.unshift(File.join(top_dir))
10
+ $LOAD_PATH.unshift(File.join(top_dir, "lib"))
11
+ $LOAD_PATH.unshift(File.join(top_dir, "test"))
12
+
13
+ require "rubygems"
14
+ require "bundler/setup"
15
+
16
+ require "test/unit"
17
+ require "test/unit/notify"
18
+ Test::Unit::Priority.enable
19
+
20
+ succeeded = true
21
+ target_adapters = [ENV["ACTIVE_LDAP_TEST_ADAPTER"]]
22
+ # target_adapters << "ldap"
23
+ # target_adapters << "net-ldap"
24
+ # target_adapters << "jndi"
25
+ target_adapters.each do |adapter|
26
+ ENV["ACTIVE_LDAP_TEST_ADAPTER"] = adapter
27
+ puts "using adapter: #{adapter ? adapter : 'default'}"
28
+ unless Test::Unit::AutoRunner.run(true, File.dirname($0), ARGV.dup)
29
+ succeeded = false
30
+ end
31
+ puts
32
+ end
33
+
34
+ exit(succeeded)
@@ -0,0 +1,60 @@
1
+ require 'al-test-utils'
2
+
3
+ class TestActsAsTree < Test::Unit::TestCase
4
+ include AlTestUtils
5
+
6
+ priority :must
7
+
8
+ priority :normal
9
+ def test_children
10
+ users = ou_class.find("Users")
11
+ assert_equal([], users.children.collect(&:ou))
12
+
13
+ sub_users = users.class.new("SubUsers")
14
+ users.children << sub_users
15
+ assert_equal(["SubUsers"], users.children.collect(&:ou))
16
+
17
+ users = ou_class.find("Users")
18
+ assert_equal(["SubUsers"], users.children.collect(&:ou))
19
+
20
+ assert_equal(dn("ou=SubUsers,#{users.dn}"),
21
+ sub_users.dn)
22
+
23
+ assert(ou_class.exists?("SubUsers"))
24
+ users.children.replace([])
25
+ assert(!ou_class.exists?("SubUsers"))
26
+ assert_equal([], users.children.collect(&:ou))
27
+ end
28
+
29
+ def test_parent
30
+ users = ou_class.find("Users")
31
+ assert_equal([], users.children.collect(&:ou))
32
+
33
+ sub_users = users.class.new("SubUsers")
34
+ sub_users.parent = users
35
+ assert_equal(dn("ou=SubUsers,#{users.dn}"),
36
+ sub_users.dn)
37
+ assert_equal(["SubUsers"], users.children.collect(&:ou))
38
+
39
+ sub_users = ou_class.find("SubUsers")
40
+ assert_equal(users.dn, sub_users.parent.dn)
41
+
42
+ assert_raises(ArgumentError) do
43
+ sub_users.parent = nil
44
+ end
45
+
46
+ make_ou("OtherUsers")
47
+ other_users = ou_class.find("OtherUsers")
48
+ assert_equal([], other_users.children.collect(&:ou))
49
+
50
+ sub_users.parent = other_users.dn
51
+ assert_equal(dn("ou=SubUsers,#{other_users.dn}"),
52
+ sub_users.dn)
53
+
54
+ other_users.clear_association_cache
55
+ assert_equal(["SubUsers"], other_users.children.collect(&:ou))
56
+
57
+ users.clear_association_cache
58
+ assert_equal([], users.children.collect(&:ou))
59
+ end
60
+ end
@@ -0,0 +1,121 @@
1
+ require 'al-test-utils'
2
+
3
+ class TestAdapter < Test::Unit::TestCase
4
+ include AlTestUtils
5
+
6
+ def setup
7
+ end
8
+
9
+ def teardown
10
+ end
11
+
12
+ priority :must
13
+ def test_operator
14
+ assert_parse_filter("(uid=Alice)", ["uid", "=", "Alice"])
15
+ assert_parse_filter("(uid~=Alice)", ["uid", "~=", "Alice"])
16
+ assert_parse_filter("(uidNumber>=1000)", ["uidNumber", ">=", "1000"])
17
+ assert_parse_filter("(uidNumber<=1000)", ["uidNumber", "<=", "1000"])
18
+ assert_parse_filter("(&(uid~=Alice)(uid~=Bob))",
19
+ ["uid", "~=", "Alice", "Bob"])
20
+ assert_parse_filter("(uid~=Alice)", [["uid", "~=", "Alice"]])
21
+ assert_parse_filter("(|(uid~=Alice)(uid~=Bob))",
22
+ [:or,
23
+ ["uid", "~=", "Alice"],
24
+ ["uid", "~=", "Bob"]])
25
+ assert_parse_filter("(|(uid~=Alice)(uid~=Bob))",
26
+ [:or,
27
+ ["uid", "~=", "Alice", "Bob"]])
28
+ end
29
+
30
+ priority :normal
31
+ def test_filter_with_escaped_character
32
+ assert_parse_filter("(uid=Alice\\28Bob)", {:uid => "Alice(Bob"})
33
+ assert_parse_filter("(uid=Alice\\29Bob)", {:uid => "Alice)Bob"})
34
+ assert_parse_filter("(uid=Alice\\29Bob\\28)", {:uid => "Alice)Bob("})
35
+ assert_parse_filter("(uid=Alice\\28\\29Bob)", {:uid => "Alice()Bob"})
36
+ assert_parse_filter("(uid=Alice*Bob)", {:uid => "Alice*Bob"})
37
+ assert_parse_filter("(uid=Alice\\2ABob)", {:uid => "Alice**Bob"})
38
+ assert_parse_filter("(uid=Alice\\2A*\\5CBob)", {:uid => "Alice***\\Bob"})
39
+ assert_parse_filter("(uid=Alice\\5C\\2A*Bob)", {:uid => "Alice\\***Bob"})
40
+ assert_parse_filter("(uid=Alice\\3ABob)", {:uid => "Alice:Bob"})
41
+ end
42
+
43
+ def test_empty_filter
44
+ assert_parse_filter(nil, nil)
45
+ assert_parse_filter(nil, "")
46
+ assert_parse_filter(nil, " ")
47
+ end
48
+
49
+ def test_simple_filter
50
+ assert_parse_filter("(objectClass=*)", "objectClass=*")
51
+ assert_parse_filter("(objectClass=*)", "(objectClass=*)")
52
+ assert_parse_filter("(&(uid=bob)(objectClass=*))",
53
+ "(&(uid=bob)(objectClass=*))")
54
+
55
+ assert_parse_filter("(objectClass=*)", {:objectClass => "*"})
56
+ assert_parse_filter("(&(objectClass=*)(uid=bob))",
57
+ {:uid => "bob", :objectClass => "*"})
58
+
59
+ assert_parse_filter("(&(uid=bob)(objectClass=*))",
60
+ [:and, "uid=bob", "objectClass=*"])
61
+ assert_parse_filter("(&(uid=bob)(objectClass=*))",
62
+ [:&, "uid=bob", "objectClass=*"])
63
+ assert_parse_filter("(|(uid=bob)(objectClass=*))",
64
+ [:or, "uid=bob", "objectClass=*"])
65
+ assert_parse_filter("(|(uid=bob)(objectClass=*))",
66
+ [:|, "uid=bob", "objectClass=*"])
67
+ end
68
+
69
+ def test_multi_value_filter
70
+ assert_parse_filter("(&(objectClass=top)(objectClass=posixAccount))",
71
+ {:objectClass => ["top", "posixAccount"]})
72
+
73
+ assert_parse_filter("(&(objectClass=top)(objectClass=posixAccount))",
74
+ [[:objectClass, "top"],
75
+ [:objectClass, "posixAccount"]])
76
+ assert_parse_filter("(&(objectClass=top)(objectClass=posixAccount))",
77
+ [[:objectClass, ["top", "posixAccount"]]])
78
+ end
79
+
80
+ def test_nested_filter
81
+ assert_parse_filter("(&(objectClass=*)(uid=bob))",
82
+ [:and, {:uid => "bob", :objectClass => "*"}])
83
+ assert_parse_filter("(&(objectClass=*)(|(uid=bob)(uid=alice)))",
84
+ [:and, {:objectClass => "*"},
85
+ [:or, [:uid, "bob"], [:uid, "alice"]]])
86
+ assert_parse_filter("(&(objectClass=*)(|(uid=bob)(uid=alice)))",
87
+ [:and,
88
+ {:objectClass => "*",
89
+ :uid => [:or, "bob", "alice"]}])
90
+ assert_parse_filter("(&(gidNumber=100001)" +
91
+ "(|(uid=temp-user1)(uid=temp-user2)))",
92
+ [:and,
93
+ [:and, {"gidNumber" => ["100001"]}],
94
+ [:or, {"uid" => ["temp-user1", "temp-user2"]}]])
95
+ assert_parse_filter("(&(gidNumber=100001)" +
96
+ "(objectClass=person)(objectClass=posixAccount))",
97
+ [:and,
98
+ [:or, ["gidNumber", "100001"]],
99
+ ["objectClass", "person"],
100
+ ["objectClass", "posixAccount"]])
101
+ assert_parse_filter("(&(!(|(gidNumber=100001)(gidNumber=100002)))" +
102
+ "(objectClass=person)(!(objectClass=posixAccount)))",
103
+ [:and,
104
+ [:not, [:or, ["gidNumber", "100001", "100002"]]],
105
+ ["objectClass", "person"],
106
+ [:not, ["objectClass", "posixAccount"]]])
107
+ end
108
+
109
+ def test_invalid_operator
110
+ assert_raises(ArgumentError) do
111
+ assert_parse_filter("(&(objectClass=*)(uid=bob))",
112
+ [:xxx, {:uid => "bob", :objectClass => "*"}])
113
+ end
114
+ end
115
+
116
+ private
117
+ def assert_parse_filter(expected, filter)
118
+ adapter = ActiveLdap::Adapter::Base.new
119
+ assert_equal(expected, adapter.send(:parse_filter, filter))
120
+ end
121
+ end
@@ -0,0 +1,701 @@
1
+ require 'al-test-utils'
2
+
3
+ class TestAssociations < Test::Unit::TestCase
4
+ include AlTestUtils
5
+
6
+ priority :must
7
+ def test_belongs_to_replace_with_string
8
+ make_temporary_user do |user,|
9
+ make_temporary_group do |group1|
10
+ make_temporary_group do |group2|
11
+ user.groups = [group1, group2]
12
+ user.save!
13
+
14
+ user.groups = [group2.cn]
15
+
16
+ group1.reload
17
+ group2.reload
18
+ assert_equal([group2], user.groups.to_a)
19
+ assert_equal([], group1.member_uid(true))
20
+ assert_equal([user.id], group2.member_uid(true))
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ priority :normal
27
+ def test_has_many_of_self
28
+ @user_class.has_many(:references,
29
+ :class_name => "User",
30
+ :primary_key => "dn",
31
+ :foreign_key => "seeAlso")
32
+ @user_class.set_associated_class(:references, @user_class)
33
+ make_temporary_user do |user1, password1|
34
+ make_temporary_user(:see_also => user1.dn.to_s) do |user2, password2|
35
+ make_temporary_user(:see_also => user2.dn.to_s) do |user3, password3|
36
+ make_temporary_user(:see_also => user2.dn.to_s) do |user4, password4|
37
+ make_temporary_user(:see_also => user1.dn.to_s) do |user5, password5|
38
+ user1_references = user1.references.collect {|r| r.dn.to_s}
39
+ user2_references = user2.references.collect {|r| r.dn.to_s}
40
+ user1_expected_references = [user2, user5].collect {|r| r.dn.to_s}
41
+ user2_expected_references = [user3, user4].collect {|r| r.dn.to_s}
42
+ assert_equal(user1_expected_references, user1_references)
43
+ assert_equal(user2_expected_references, user2_references)
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ def test_belongs_to_add_with_string
52
+ make_temporary_user do |user,|
53
+ make_temporary_group do |group1|
54
+ make_temporary_group do |group2|
55
+ assert_equal([[], []],
56
+ [group1.members.collect(&:cn),
57
+ group2.members.collect(&:cn)])
58
+
59
+ user.groups = [group1.cn, group2.cn]
60
+ user.save!
61
+
62
+ group1.reload
63
+ group2.reload
64
+ assert_equal([[user.cn], [user.cn]],
65
+ [group1.members.collect(&:cn),
66
+ group2.members.collect(&:cn)])
67
+ end
68
+ end
69
+ end
70
+ end
71
+
72
+ def test_has_many_delete_required_attribute
73
+ make_temporary_group do |group|
74
+ make_temporary_user do |user,|
75
+ user.primary_group = group
76
+ assert_raise(ActiveLdap::RequiredAttributeMissed) do
77
+ group.primary_members.delete(user)
78
+ end
79
+ end
80
+ end
81
+ end
82
+
83
+ def test_to_xml
84
+ make_temporary_user do |user,|
85
+ make_temporary_group do |group1|
86
+ make_temporary_group do |group2|
87
+ user.groups = [group1, group2]
88
+ assert_equal(<<-EOX, user.groups.to_xml(:root => "groups"))
89
+ <?xml version="1.0" encoding="UTF-8"?>
90
+ <groups type="array">
91
+ <group>
92
+ <dn>#{group1.dn}</dn>
93
+ <cns type="array">
94
+ <cn>#{group1.cn}</cn>
95
+ </cns>
96
+ <gidNumber>#{group1.gid_number}</gidNumber>
97
+ <memberUids type="array">
98
+ <memberUid>#{user.cn}</memberUid>
99
+ </memberUids>
100
+ <objectClasses type="array">
101
+ <objectClass>posixGroup</objectClass>
102
+ </objectClasses>
103
+ </group>
104
+ <group>
105
+ <dn>#{group2.dn}</dn>
106
+ <cns type="array">
107
+ <cn>#{group2.cn}</cn>
108
+ </cns>
109
+ <gidNumber>#{group2.gid_number}</gidNumber>
110
+ <memberUids type="array">
111
+ <memberUid>#{user.cn}</memberUid>
112
+ </memberUids>
113
+ <objectClasses type="array">
114
+ <objectClass>posixGroup</objectClass>
115
+ </objectClasses>
116
+ </group>
117
+ </groups>
118
+ EOX
119
+ end
120
+ end
121
+ end
122
+ end
123
+
124
+ def test_belongs_to_with_invalid_dn_attribute_value
125
+ make_temporary_user do |user,|
126
+ make_temporary_group do |group|
127
+ user.primary_group = group
128
+ user.uid = "#"
129
+ assert_nothing_raised do
130
+ user.primary_group.reload
131
+ end
132
+ end
133
+ end
134
+ end
135
+
136
+ def test_belongs_to_foreign_key_before_1_1_0
137
+ ActiveSupport::Deprecation.silence do
138
+ @group_class.belongs_to :related_users, :many => "seeAlso",
139
+ :foreign_key => "dn"
140
+ end
141
+ @group_class.set_associated_class(:related_users, @user_class)
142
+ make_temporary_user do |user,|
143
+ make_temporary_group do |group|
144
+ user.see_also = group.dn
145
+ user.save!
146
+
147
+ group = @group_class.find(group.id)
148
+ assert_equal([user.dn], group.related_users.collect(&:dn))
149
+ end
150
+ end
151
+ end
152
+
153
+ def test_has_many_wrap_with_nonexistent_entry
154
+ @user_class.has_many :references, :wrap => "seeAlso", :primary_key => "dn"
155
+ @user_class.set_associated_class(:references, @group_class)
156
+ @group_class.belongs_to :related_users, :many => "seeAlso",
157
+ :primary_key => "dn"
158
+ @group_class.set_associated_class(:related_users, @user_class)
159
+ make_temporary_user do |user,|
160
+ make_temporary_group do |group1|
161
+ make_temporary_group do |group2|
162
+ user.references = [group1, group2]
163
+ group3_dn = group2.dn.to_s.sub(/cn=(.*?),/, "cn=\\1-nonexistent,")
164
+ user.see_also += [group3_dn]
165
+ user.save!
166
+
167
+ group3_dn = dn(group3_dn)
168
+ user = @user_class.find(user.dn)
169
+ assert_equal([group1.dn, group2.dn, group3_dn],
170
+ user.see_also)
171
+ assert_equal([group1.dn, group2.dn, group3_dn],
172
+ user.references.collect(&:dn))
173
+ assert_equal([group1.gid_number, group2.gid_number, nil],
174
+ user.references.collect(&:gid_number))
175
+ assert_equal([false, false, true],
176
+ user.references.collect(&:new_entry?))
177
+ end
178
+ end
179
+ end
180
+ end
181
+
182
+ def test_has_many_wrap_with_dn_value
183
+ @user_class.has_many :references, :wrap => "seeAlso", :primary_key => "dn"
184
+ @user_class.set_associated_class(:references, @group_class)
185
+ @group_class.belongs_to :related_users, :many => "seeAlso",
186
+ :primary_key => "dn"
187
+ @group_class.set_associated_class(:related_users, @user_class)
188
+ make_temporary_user do |user,|
189
+ make_temporary_group do |group1|
190
+ make_temporary_group do |group2|
191
+ make_temporary_group do |group3|
192
+ entries = [user, group1, group2, group3]
193
+
194
+ user.references << group1
195
+ user, group1, group2, group3 = reload_entries(*entries)
196
+ assert_references([[group1]], [user])
197
+ assert_related_users([user], group1)
198
+ assert_related_users([], group2)
199
+ assert_related_users([], group3)
200
+
201
+ user.references = [group2, group3]
202
+ user, group1, group2, group3 = reload_entries(*entries)
203
+ assert_references([[group2, group3]], [user])
204
+ assert_related_users([], group1)
205
+ assert_related_users([user], group2)
206
+ assert_related_users([user], group3)
207
+
208
+ user.references.delete(group2)
209
+ user, group1, group2, group3 = reload_entries(*entries)
210
+ assert_references([[group3]], [user])
211
+ assert_related_users([], group1)
212
+ assert_related_users([], group2)
213
+ assert_related_users([user], group3)
214
+ end
215
+ end
216
+ end
217
+ end
218
+ end
219
+
220
+ def test_belongs_to_many_with_dn_value
221
+ @user_class.has_many :references, :wrap => "seeAlso", :primary_key => "dn"
222
+ @user_class.set_associated_class(:references, @group_class)
223
+ @group_class.belongs_to :related_users, :many => "seeAlso",
224
+ :primary_key => "dn"
225
+ @group_class.set_associated_class(:related_users, @user_class)
226
+ make_temporary_group do |group|
227
+ make_temporary_user do |user1,|
228
+ make_temporary_user do |user2,|
229
+ make_temporary_user do |user3,|
230
+ entries = [group, user1, user2, user3]
231
+
232
+ group.related_users = [user1, user2]
233
+ group, user1, user2, user3 = reload_entries(*entries)
234
+ assert_references([[group], [group], []],
235
+ [user1, user2, user3])
236
+ assert_related_users([user1, user2], group)
237
+
238
+ group.related_users << user3
239
+ group, user1, user2, user3 = reload_entries(*entries)
240
+ assert_references([[group], [group], [group]],
241
+ [user1, user2, user3])
242
+ assert_related_users([user1, user2, user3], group)
243
+
244
+ group.related_users.delete(user1)
245
+ group, user1, user2, user3 = reload_entries(*entries)
246
+ assert_references([[], [group], [group]],
247
+ [user1, user2, user3])
248
+ assert_related_users([user2, user3], group)
249
+
250
+ group.related_users = []
251
+ group, user1, user2, user3 = reload_entries(*entries)
252
+ assert_references([[], [], []],
253
+ [user1, user2, user3])
254
+ assert_related_users([], group)
255
+ end
256
+ end
257
+ end
258
+ end
259
+ end
260
+
261
+ def test_belongs_to_many_with_dn_key
262
+ @user_class.belongs_to :dn_groups, :many => "memberUid", :primary_key => "dn"
263
+ @user_class.set_associated_class(:dn_groups, @group_class)
264
+ @group_class.has_many :dn_members, :wrap => "memberUid", :primary_key => "dn"
265
+ @group_class.set_associated_class(:dn_members, @user_class)
266
+ make_temporary_group do |group|
267
+ make_temporary_user do |user1,|
268
+ make_temporary_user do |user2,|
269
+ make_temporary_user do |user3,|
270
+ entries = [group, user1, user2, user3]
271
+
272
+ user1.dn_groups << group
273
+ group, user1, user2, user3 = reload_entries(*entries)
274
+ assert_dn_groups([[group], [], []], [user1, user2, user3])
275
+ assert_dn_members([user1], group)
276
+
277
+ user2.dn_groups = [group]
278
+ group, user1, user2, user3 = reload_entries(*entries)
279
+ assert_dn_groups([[group], [group], []], [user1, user2, user3])
280
+ assert_dn_members([user1, user2], group)
281
+
282
+ user1.dn_groups = []
283
+ group, user1, user2, user3 = reload_entries(*entries)
284
+ assert_dn_groups([[], [group], []], [user1, user2, user3])
285
+ assert_dn_members([user2], group)
286
+
287
+ user2.dn_groups.delete(group)
288
+ group, user1, user2, user3 = reload_entries(*entries)
289
+ assert_dn_groups([[], [], []], [user1, user2, user3])
290
+ assert_dn_members([], group)
291
+ end
292
+ end
293
+ end
294
+ end
295
+ end
296
+
297
+ def test_belongs_to_many_delete
298
+ make_temporary_group do |group1|
299
+ make_temporary_group do |group2|
300
+ make_temporary_user do |user,|
301
+ user.update_attribute(:cn, "new #{user.cn}")
302
+
303
+ user.groups = [group1, group2]
304
+ assert_equal([group1.id, group2.id].sort,
305
+ user.groups.collect {|g| g.id}.sort)
306
+ assert_equal([user.id].sort, group1.member_uid(true))
307
+ assert_equal([user.id].sort, group2.member_uid(true))
308
+
309
+ user.groups = []
310
+ assert_equal([], user.groups.to_a)
311
+ assert_equal([], group1.member_uid(true))
312
+ assert_equal([], group2.member_uid(true))
313
+ end
314
+ end
315
+ end
316
+ end
317
+
318
+ def test_belongs_to_before_save
319
+ make_temporary_group do |group1|
320
+ make_temporary_group do |group2|
321
+ ensure_delete_group(group2.cn.succ) do |group3_name|
322
+ group3 = @group_class.new(group3_name)
323
+ group3.gid_number = group2.gid_number.succ
324
+ make_temporary_user(:gid_number => group1.gid_number) do |user,|
325
+ assert_equal(group1.gid_number, user.primary_group.gid_number)
326
+ assert_equal(group1.gid_number, user.gid_number)
327
+
328
+ user.primary_group = group2
329
+ assert_equal(group2.gid_number, user.primary_group.gid_number)
330
+ assert_equal(group2.gid_number, user.gid_number)
331
+
332
+ user_in_ldap = @user_class.find(user.id)
333
+ assert_equal(group1.gid_number,
334
+ user_in_ldap.primary_group.gid_number)
335
+ assert_equal(group1.gid_number, user_in_ldap.gid_number)
336
+
337
+ assert(group3.new_entry?)
338
+ user.primary_group = group3
339
+ assert_equal(group3.gid_number, user.primary_group.gid_number)
340
+ assert_equal(group2.gid_number, user.gid_number)
341
+
342
+ assert(user.save)
343
+ assert_equal(group3.gid_number, user.gid_number)
344
+
345
+ user_in_ldap = @user_class.find(user.id)
346
+ assert(!user_in_ldap.primary_group.exists?)
347
+
348
+ assert(group3.save)
349
+ assert(user_in_ldap.primary_group.exists?)
350
+ assert_equal(group3.gid_number,
351
+ user_in_ldap.primary_group.gid_number)
352
+ end
353
+ end
354
+ end
355
+ end
356
+ end
357
+
358
+ def test_extend
359
+ mod = Module.new
360
+ mod.__send__(:mattr_accessor, :called)
361
+ mod.__send__(:define_method, :replace) do |entries|
362
+ super(entries)
363
+ mod.called = true
364
+ end
365
+ mod.called = false
366
+
367
+ @group_class.send(:undef_method, :members, :members=, :__make_members)
368
+ @group_class.has_many :members, :wrap => "memberUid",
369
+ :extend => mod
370
+ @group_class.set_associated_class(:members, @user_class)
371
+
372
+ make_temporary_group do |group|
373
+ gid_number1 = group.gid_number.to_i + 1
374
+ make_temporary_user(:gid_number => gid_number1) do |user1, password1|
375
+ user1.update_attribute(:cn, "new #{user1.cn}")
376
+
377
+ assert(!mod.called)
378
+ group.members = [user1]
379
+ assert(mod.called)
380
+ end
381
+ end
382
+ end
383
+
384
+ def test_has_many_wrap_assign
385
+ make_temporary_group do |group|
386
+ gid_number1 = group.gid_number.to_i + 1
387
+ gid_number2 = group.gid_number.to_i + 2
388
+ make_temporary_user(:gid_number => gid_number1) do |user1, password1|
389
+ make_temporary_user(:gid_number => gid_number2) do |user2, password2|
390
+ user1.update_attribute(:cn, "new #{user1.cn}")
391
+ user2.update_attribute(:cn, "new #{user2.cn}")
392
+
393
+ assert_equal([], group.members.to_a)
394
+ assert_equal([], group.member_uid(true))
395
+
396
+ assert_equal(gid_number1, user1.gid_number.to_i)
397
+ assert_equal(gid_number2, user2.gid_number.to_i)
398
+ group.members = [user1, user2]
399
+ assert_equal([user1.uid, user2.uid].sort,
400
+ group.members.collect {|x| x.uid}.sort)
401
+ assert_equal([user1.uid, user2.uid].sort, group.member_uid.sort)
402
+ assert_equal(gid_number2, user2.gid_number.to_i)
403
+
404
+ group.members = [user1]
405
+ assert_equal([user1.uid].sort,
406
+ group.members.collect {|x| x.uid}.sort)
407
+ assert_equal(user1.uid, group.member_uid)
408
+ assert_equal(gid_number1, user1.gid_number.to_i)
409
+ end
410
+ end
411
+ end
412
+ end
413
+
414
+ def test_has_many_wrap_assign_new_entry
415
+ make_temporary_user do |user1, |
416
+ make_temporary_user do |user2, |
417
+ ensure_delete_group('test_new_group') do |cn|
418
+ group = @group_class.new(:cn => cn, :gid_number => default_gid)
419
+
420
+ assert_equal([], group.members.to_a)
421
+ assert_equal([], group.member_uid(true))
422
+
423
+ group.members = [user1, user2]
424
+
425
+ assert group.new_entry?
426
+ assert_equal([user1.uid, user2.uid].sort, group.members.map {|x| x.uid }.sort)
427
+ assert_equal([user1.uid, user2.uid].sort, group.member_uid(true).sort)
428
+
429
+ group.members = [user2]
430
+
431
+ assert group.new_entry?
432
+ assert_equal([user2.uid], group.members.map {|x| x.uid })
433
+ assert_equal(user2.uid, group.member_uid)
434
+
435
+ group.members << user1
436
+
437
+ assert group.new_entry?
438
+ assert_equal([user1.uid, user2.uid].sort, group.members.map {|x| x.uid }.sort)
439
+ assert_equal([user1.uid, user2.uid].sort, group.member_uid.sort)
440
+
441
+ assert(group.save)
442
+
443
+ group = @group_class.find(cn)
444
+ assert_equal([user1.uid, user2.uid].sort, group.members.map {|x| x.uid }.sort)
445
+ assert_equal([user1.uid, user2.uid].sort, group.member_uid.sort)
446
+ end
447
+ end
448
+ end
449
+ end
450
+
451
+ def test_has_many_validation
452
+ group_class = Class.new(ActiveLdap::Base)
453
+ group_class.ldap_mapping :prefix => "ou=Groups",
454
+ :scope => :sub,
455
+ :classes => ["posixGroup"]
456
+ assert_raises(ArgumentError) do
457
+ group_class.has_many :users, :unknown_name => "value"
458
+ end
459
+
460
+ mod = Module.new
461
+ assert_nothing_raised do
462
+ group_class.has_many :users, :class_name => "User"
463
+ group_class.has_many :members, :class => @user_class, :wrap => "memberUid",
464
+ :extend => mod
465
+ group_class.has_many :primary_members, :class => @user_class,
466
+ :primary_key => "gidNumber",
467
+ :foreign_key => "gidNumber",
468
+ :extend => mod
469
+ end
470
+ end
471
+
472
+ def test_belongs_to_validation
473
+ user_class = Class.new(ActiveLdap::Base)
474
+ user_class.ldap_mapping :dn_attribute => "uid",
475
+ :prefix => "ou=Users",
476
+ :scope => :sub,
477
+ :classes => ["posixAccount", "person"]
478
+ assert_raises(ArgumentError) do
479
+ user_class.belongs_to :groups, :unknown_name => "value"
480
+ end
481
+
482
+ mod = Module.new
483
+ assert_nothing_raised do
484
+ user_class.belongs_to :string_groups, :class_name => "Group"
485
+ user_class.belongs_to :groups, :class => @group_class,
486
+ :many => "memberUid",
487
+ :extend => mod
488
+ user_class.belongs_to :primary_group, :class => @group_class,
489
+ :foreign_key => "gidNumber",
490
+ :primary_key => "gidNumber",
491
+ :extend => mod
492
+ end
493
+ end
494
+
495
+ def test_has_many_assign
496
+ make_temporary_group do |group|
497
+ gid_number1 = group.gid_number.to_i + 1
498
+ gid_number2 = group.gid_number.to_i + 2
499
+ make_temporary_user(:gid_number => gid_number1) do |user1, password1|
500
+ make_temporary_user(:gid_number => gid_number2) do |user2, password2|
501
+ assert_equal(gid_number1, user1.gid_number.to_i)
502
+ group.primary_members = [user1]
503
+ assert_equal([user1.uid].sort,
504
+ group.primary_members.collect {|x| x.uid}.sort)
505
+ assert_equal(group.gid_number, user1.gid_number)
506
+
507
+ assert_equal(gid_number2, user2.gid_number.to_i)
508
+ group.primary_members = [user1, user2]
509
+ assert_equal([user1.uid, user2.uid].sort,
510
+ group.primary_members.collect {|x| x.uid}.sort)
511
+ assert_equal(group.gid_number, user2.gid_number)
512
+
513
+
514
+ assert_raises(ActiveLdap::RequiredAttributeMissed) do
515
+ group.primary_members = []
516
+ end
517
+
518
+ assert_raises(ActiveLdap::RequiredAttributeMissed) do
519
+ group.primary_members = [user1]
520
+ end
521
+
522
+ assert_raises(ActiveLdap::RequiredAttributeMissed) do
523
+ group.primary_members = [user2]
524
+ end
525
+
526
+ assert_nothing_raised do
527
+ group.primary_members = [user1, user2]
528
+ end
529
+ end
530
+ end
531
+ end
532
+ end
533
+
534
+ def test_belongs_to_many
535
+ make_temporary_group do |group1|
536
+ make_temporary_group do |group2|
537
+ make_temporary_user do |user,|
538
+ user.update_attribute(:cn, "new #{user.cn}")
539
+
540
+ assert_equal([], user.groups.to_a)
541
+ assert_equal([], group1.member_uid(true))
542
+ assert_equal([], group2.member_uid(true))
543
+
544
+ user.groups << group1
545
+ assert_equal([group1.id].sort, user.groups.collect {|g| g.id}.sort)
546
+ assert_equal([user.id].sort, group1.member_uid(true))
547
+ assert_equal([].sort, group2.member_uid(true))
548
+
549
+ user.groups << group2
550
+ assert_equal([group1.id, group2.id].sort,
551
+ user.groups.collect {|g| g.id}.sort)
552
+ assert_equal([user.id].sort, group1.member_uid(true))
553
+ assert_equal([user.id].sort, group2.member_uid(true))
554
+ end
555
+ end
556
+ end
557
+ end
558
+
559
+ def test_belongs_to_many_non_exist
560
+ make_temporary_group do |group|
561
+ ensure_delete_user("temp-user1") do |user1,|
562
+ options = {:uid => "temp-user2", :gid_number => group.gid_number.succ}
563
+ make_temporary_user(options) do |user2,|
564
+ ensure_delete_user("temp-user3") do |user3,|
565
+ group.members << user2
566
+ group.member_uid = [user1, group.member_uid, user3]
567
+ assert(group.save)
568
+ group.members.reload
569
+ assert_equal([user1, user2.id, user3],
570
+ group.members.collect {|g| g.id})
571
+ assert_equal([true, false, true],
572
+ group.members.collect {|g| g.new_entry?})
573
+ end
574
+ end
575
+ end
576
+ end
577
+ end
578
+
579
+ def test_belongs_to
580
+ make_temporary_group do |group|
581
+ gid_number = group.gid_number.to_i + 1
582
+ make_temporary_user(:gid_number => group.gid_number) do |user_in_group,|
583
+ make_temporary_user(:gid_number => gid_number) do |user_not_in_group,|
584
+ assert(user_in_group.primary_group.reload)
585
+ assert(user_in_group.primary_group.loaded?)
586
+ assert_equal(group.gid_number, user_in_group.gid_number)
587
+ assert_equal(group.gid_number, user_in_group.primary_group.gid_number)
588
+
589
+
590
+ assert(!user_not_in_group.primary_group.loaded?)
591
+
592
+ assert_equal(gid_number, user_not_in_group.gid_number.to_i)
593
+ assert_not_equal(group.gid_number, user_not_in_group.gid_number)
594
+
595
+ user_not_in_group.primary_group = group
596
+ assert(user_not_in_group.primary_group.loaded?)
597
+ assert(user_not_in_group.primary_group.updated?)
598
+ assert_equal(group.gid_number, user_not_in_group.gid_number)
599
+ assert_equal(group.gid_number,
600
+ user_not_in_group.primary_group.gid_number)
601
+ assert_not_equal(gid_number, user_not_in_group.gid_number.to_i)
602
+
603
+ assert_equal(group.gid_number, user_in_group.gid_number)
604
+ assert_equal(group.gid_number, user_in_group.primary_group.gid_number)
605
+ end
606
+ end
607
+ end
608
+ end
609
+
610
+ def test_has_many_wrap
611
+ make_temporary_group do |group|
612
+ gid_number1 = group.gid_number.to_i + 1
613
+ gid_number2 = group.gid_number.to_i + 2
614
+ make_temporary_user(:gid_number => gid_number1) do |user1, password1|
615
+ make_temporary_user(:gid_number => gid_number2) do |user2, password2|
616
+ user1.update_attribute(:cn, "new #{user1.cn}")
617
+ user2.update_attribute(:cn, "new #{user2.cn}")
618
+
619
+ assert_equal([], group.members.to_a)
620
+ assert_equal([], group.member_uid(true))
621
+
622
+ assert_equal(gid_number1, user1.gid_number.to_i)
623
+ group.members << user1
624
+ assert_equal([user1.uid].sort,
625
+ group.members.collect {|x| x.uid}.sort)
626
+ assert_equal(user1.uid, group.member_uid)
627
+ assert_equal(gid_number1, user1.gid_number.to_i)
628
+
629
+ assert_equal(gid_number2, user2.gid_number.to_i)
630
+ group.members << user2
631
+ assert_equal([user1.uid, user2.uid].sort,
632
+ group.members.collect {|x| x.uid}.sort)
633
+ assert_equal([user1.uid, user2.uid].sort, group.member_uid.sort)
634
+ assert_equal(gid_number2, user2.gid_number.to_i)
635
+ end
636
+ end
637
+ end
638
+ end
639
+
640
+ def test_has_many
641
+ make_temporary_group do |group|
642
+ gid_number1 = group.gid_number.to_i + 1
643
+ gid_number2 = group.gid_number.to_i + 2
644
+ make_temporary_user(:gid_number => gid_number1) do |user1, password1|
645
+ make_temporary_user(:gid_number => gid_number2) do |user2, password2|
646
+ assert_equal([], group.primary_members.to_a)
647
+
648
+ assert_equal(gid_number1, user1.gid_number.to_i)
649
+ group.primary_members << user1
650
+ assert_equal([user1.uid].sort,
651
+ group.primary_members.collect {|x| x.uid}.sort)
652
+ assert_equal(group.gid_number, user1.gid_number)
653
+
654
+ assert_equal(gid_number2, user2.gid_number.to_i)
655
+ group.primary_members << user2
656
+ assert_equal([user1.uid, user2.uid].sort,
657
+ group.primary_members.collect {|x| x.uid}.sort)
658
+ assert_equal(group.gid_number, user2.gid_number)
659
+ end
660
+ end
661
+ end
662
+ end
663
+
664
+ private
665
+ def reload_entries(*entries)
666
+ entries.collect do |entry|
667
+ entry.class.find(entry[entry.dn_attribute])
668
+ end
669
+ end
670
+
671
+ def assert_groups_relation(expected_groups_values, entries, relation_name)
672
+ expected_groups_values = expected_groups_values.collect do |groups|
673
+ groups.collect(&:cn).sort
674
+ end
675
+ actual_groups_values = entries.collect do |entry|
676
+ entry.send(relation_name).collect(&:cn).sort
677
+ end
678
+ assert_equal(expected_groups_values, actual_groups_values)
679
+ end
680
+
681
+ def assert_users_relation(expected_users, group, relation_name)
682
+ assert_equal(expected_users.collect(&:cn).sort,
683
+ group.send(relation_name).collect(&:cn).sort)
684
+ end
685
+
686
+ def assert_references(expected_groups_values, users)
687
+ assert_groups_relation(expected_groups_values, users, :references)
688
+ end
689
+
690
+ def assert_related_users(expected_users, group)
691
+ assert_users_relation(expected_users, group, :related_users)
692
+ end
693
+
694
+ def assert_dn_groups(expected_groups_values, users)
695
+ assert_groups_relation(expected_groups_values, users, :dn_groups)
696
+ end
697
+
698
+ def assert_dn_members(expected_users, group)
699
+ assert_users_relation(expected_users, group, :dn_members)
700
+ end
701
+ end