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,117 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'al-test-utils'
4
+
5
+ class TestAttributes < Test::Unit::TestCase
6
+ include AlTestUtils
7
+
8
+ priority :must
9
+
10
+ priority :normal
11
+ def test_to_real_attribute_name
12
+ user = @user_class.new("user")
13
+ assert_equal("objectClass",
14
+ user.__send__(:to_real_attribute_name, "objectclass"))
15
+ assert_equal("objectClass",
16
+ user.__send__(:to_real_attribute_name, "objectclass", true))
17
+ assert_nil(user.__send__(:to_real_attribute_name, "objectclass", false))
18
+ end
19
+
20
+ def test_protect_object_class_from_mass_assignment
21
+ classes = @user_class.required_classes + ["inetOrgPerson"]
22
+ user = @user_class.new(:uid => "XXX", :object_class => classes)
23
+ assert_equal(["inetOrgPerson"],
24
+ user.classes - @user_class.required_classes)
25
+
26
+ user = @user_class.new(:uid => "XXX", :object_class => ['inetOrgPerson'])
27
+ assert_equal(["inetOrgPerson"],
28
+ user.classes - @user_class.required_classes)
29
+
30
+ user = @user_class.new("XXX")
31
+ assert_equal([], user.classes - @user_class.required_classes)
32
+ user.attributes = {:object_class => classes}
33
+ assert_equal([], user.classes - @user_class.required_classes)
34
+ end
35
+
36
+ def test_normalize_attribute
37
+ assert_normalize_attribute(["usercertificate", [{"binary" => []}]],
38
+ "userCertificate",
39
+ [])
40
+ assert_normalize_attribute(["usercertificate", [{"binary" => []}]],
41
+ "userCertificate",
42
+ nil)
43
+ assert_normalize_attribute(["usercertificate",
44
+ [{"binary" => "BINARY DATA"}]],
45
+ "userCertificate",
46
+ "BINARY DATA")
47
+ assert_normalize_attribute(["usercertificate",
48
+ [{"binary" => ["BINARY DATA"]}]],
49
+ "userCertificate",
50
+ {"binary" => ["BINARY DATA"]})
51
+ end
52
+
53
+ def test_unnormalize_attribute
54
+ assert_unnormalize_attribute({"sn" => ["Surname"]},
55
+ "sn",
56
+ ["Surname"])
57
+ assert_unnormalize_attribute({"userCertificate;binary" => []},
58
+ "userCertificate",
59
+ [{"binary" => []}])
60
+ assert_unnormalize_attribute({"userCertificate;binary" => ["BINARY DATA"]},
61
+ "userCertificate",
62
+ [{"binary" => ["BINARY DATA"]}])
63
+ assert_unnormalize_attribute({
64
+ "sn" => ["Yamada"],
65
+ "sn;lang-ja" => ["山田"],
66
+ "sn;lang-ja;phonetic" => ["やまだ"]
67
+ },
68
+ "sn",
69
+ ["Yamada",
70
+ {"lang-ja" => ["山田",
71
+ {"phonetic" => ["やまだ"]}]}])
72
+ end
73
+
74
+ def test_attr_protected
75
+ user = @user_class.new(:uid => "XXX")
76
+ assert_equal("XXX", user.uid)
77
+ user.attributes = {:uid => "ZZZ"}
78
+ assert_equal("XXX", user.uid)
79
+
80
+ user = @user_class.new(:sn => "ZZZ")
81
+ assert_equal("ZZZ", user.sn)
82
+
83
+ user = @user_class.new(:uid => "XXX", :sn => "ZZZ")
84
+ assert_equal("XXX", user.uid)
85
+ assert_equal("ZZZ", user.sn)
86
+
87
+ @user_class.attr_protected :sn
88
+ user = @user_class.new(:sn => "ZZZ")
89
+ assert_nil(user.sn)
90
+
91
+ sub_user_class = Class.new(@user_class)
92
+ sub_user_class.ldap_mapping :dn_attribute => "uid"
93
+ user = sub_user_class.new(:uid => "XXX", :sn => "ZZZ")
94
+ assert_equal("XXX", user.uid)
95
+ assert_nil(user.sn)
96
+
97
+ sub_user_class.attr_protected :cn
98
+ user = sub_user_class.new(:uid => "XXX", :sn => "ZZZ", :cn => "Common Name")
99
+ assert_equal("XXX", user.uid)
100
+ assert_nil(user.sn)
101
+ assert_nil(user.cn)
102
+
103
+ user = @user_class.new(:uid => "XXX", :sn => "ZZZ", :cn => "Common Name")
104
+ assert_equal("XXX", user.uid)
105
+ assert_nil(user.sn)
106
+ assert_equal("Common Name", user.cn)
107
+ end
108
+
109
+ private
110
+ def assert_normalize_attribute(expected, name, value)
111
+ assert_equal(expected, ActiveLdap::Base.normalize_attribute(name, value))
112
+ end
113
+
114
+ def assert_unnormalize_attribute(expected, name, value)
115
+ assert_equal(expected, ActiveLdap::Base.unnormalize_attribute(name, value))
116
+ end
117
+ end
@@ -0,0 +1,1214 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'al-test-utils'
4
+
5
+ class TestBase < Test::Unit::TestCase
6
+ include AlTestUtils
7
+
8
+ priority :must
9
+ def test_search_colon_value
10
+ make_temporary_group(:cn => "temp:group") do |group|
11
+ assert_equal("temp:group", group.cn)
12
+ assert_not_nil(@group_class.find("temp:group"))
13
+ end
14
+ end
15
+
16
+ priority :normal
17
+ def test_lower_case_object_class
18
+ fixture_file = fixture("lower_case_object_class_schema.rb")
19
+ schema_entries = eval(File.read(fixture_file))
20
+ schema = ActiveLdap::Schema.new(schema_entries)
21
+ target_class = Class.new(ActiveLdap::Base) do
22
+ ldap_mapping :dn_attribute => "umpn",
23
+ :prefix => "cn=site",
24
+ :classes => ['top', 'umphone', 'umphonenumber']
25
+ end
26
+ target_class.connection.instance_variable_set("@schema", schema)
27
+ target_class.connection.instance_variable_set("@entry_attributes", {})
28
+ target = target_class.send(:instantiate,
29
+ [
30
+ "umpn=1.555.5551234,#{target_class.base}",
31
+ {
32
+ "umpn" => "1.555.5551234",
33
+ "objectclass" => ["top",
34
+ "umphone",
35
+ "umphonenumber"],
36
+ }
37
+ ])
38
+ assert_equal("1.555.5551234", target.umpn)
39
+ end
40
+
41
+ def test_set_and_get_false
42
+ user = @user_class.new
43
+ user.sn = false
44
+ assert_equal(false, user.sn)
45
+ end
46
+
47
+ def test_modify_entry_with_attribute_with_nested_options
48
+ make_temporary_user(:simple => true) do |user,|
49
+ user.sn = ["Yamada",
50
+ {"lang-ja" => ["山田",
51
+ {"phonetic" => ["やまだ"]}]}]
52
+ assert_nothing_raised do
53
+ user.save!
54
+ end
55
+ end
56
+ end
57
+
58
+ def test_add_entry_with_attribute_with_nested_options
59
+ ensure_delete_user("temp-user") do |uid,|
60
+ user = @user_class.new
61
+ user.cn = uid
62
+ user.uid = uid
63
+ user.uid_number = 1000
64
+ user.gid_number = 1000
65
+ user.home_directory = "/home/#{uid}"
66
+
67
+ assert_not_predicate(user, :valid?)
68
+ user.sn = ["Yamada",
69
+ {"lang-ja" => ["山田",
70
+ {"phonetic" => ["やまだ"]}]}]
71
+ assert_predicate(user, :valid?)
72
+ assert_nothing_raised do
73
+ user.save!
74
+ end
75
+ end
76
+ end
77
+
78
+ def test_not_rename_by_mass_update
79
+ make_temporary_user(:simple => true) do |user,|
80
+ original_id = user.id
81
+ assert_true(user.update_attributes(:id => "user2"))
82
+ assert_equal(original_id, user.id)
83
+ end
84
+ end
85
+
86
+ def test_attributes
87
+ make_temporary_group do |group|
88
+ assert_equal({
89
+ "cn" => group.cn,
90
+ "gidNumber" => group.gidNumber,
91
+ "objectClass" => group.classes,
92
+ },
93
+ group.attributes)
94
+ end
95
+ end
96
+
97
+ def test_rename_with_superior
98
+ make_ou("sub,ou=users")
99
+ make_temporary_user(:simple => true) do |user,|
100
+ user.id = "user2,ou=sub,#{@user_class.base}"
101
+ case user.connection.class.to_s.demodulize
102
+ when "Jndi"
103
+ assert_true(user.save)
104
+
105
+ found_user = nil
106
+ assert_nothing_raised do
107
+ found_user = @user_class.find("user2")
108
+ end
109
+ base = @user_class.base
110
+ assert_equal("#{@user_class.dn_attribute}=user2,ou=sub,#{base}",
111
+ found_user.dn.to_s)
112
+ else
113
+ assert_raise(ActiveLdap::NotImplemented) do
114
+ user.save
115
+ end
116
+ end
117
+ end
118
+ end
119
+
120
+ def test_rename
121
+ make_temporary_user(:simple => true) do |user,|
122
+ assert_not_equal("user2", user.id)
123
+ assert_raise(ActiveLdap::EntryNotFound) do
124
+ @user_class.find("user2")
125
+ end
126
+ user.id = "user2"
127
+ assert_true(user.save)
128
+ assert_equal("user2", user.id)
129
+
130
+ found_user = nil
131
+ assert_nothing_raised do
132
+ found_user = @user_class.find("user2")
133
+ end
134
+ assert_equal("user2", found_user.id)
135
+ end
136
+ end
137
+
138
+ def test_operational_attributes
139
+ make_temporary_group do |group|
140
+ dn, attributes = @group_class.search(:attributes => ["*"])[0]
141
+ normal_attributes = attributes.keys
142
+ dn, attributes = @group_class.search(:attributes => ["*", "+"])[0]
143
+ operational_attributes = attributes.keys - normal_attributes
144
+ operational_attribute = operational_attributes[0]
145
+
146
+ group = @group_class.find(:first, :attributes => ["*", "+"])
147
+ operational_attribute_value = group[operational_attribute]
148
+ assert_not_nil(operational_attribute_value)
149
+ group.save!
150
+ assert_equal(operational_attribute_value, group[operational_attribute])
151
+ end
152
+ end
153
+
154
+ def test_destroy_mixed_tree_by_instance
155
+ make_ou("base")
156
+ _entry_class = entry_class("ou=base")
157
+ _ou_class = ou_class("ou=base")
158
+ _dc_class = dc_class("ou=base")
159
+
160
+ root1 = _ou_class.create("root1")
161
+ _ou_class.create(:ou => "child1", :parent => root1)
162
+ _ou_class.create(:ou => "child2", :parent => root1)
163
+ _dc_class.create(:dc => "domain", :o => "domain", :parent => root1)
164
+ _ou_class.create(:ou => "child3", :parent => root1)
165
+ _ou_class.create("root2")
166
+ assert_equal(["base",
167
+ "root1", "child1", "child2", "domain", "child3",
168
+ "root2"],
169
+ _entry_class.find(:all).collect(&:id))
170
+ assert_raise(ActiveLdap::DeleteError) do
171
+ root1.destroy_all
172
+ end
173
+ assert_equal(["base", "root1", "domain", "root2"],
174
+ _entry_class.find(:all).collect(&:id))
175
+ end
176
+
177
+ def test_delete_mixed_tree_by_instance
178
+ make_ou("base")
179
+ _entry_class = entry_class("ou=base")
180
+ _ou_class = ou_class("ou=base")
181
+ _dc_class = dc_class("ou=base")
182
+
183
+ root1 = _ou_class.create("root1")
184
+ _ou_class.create(:ou => "child1", :parent => root1)
185
+ _ou_class.create(:ou => "child2", :parent => root1)
186
+ _dc_class.create(:dc => "domain", :o => "domain", :parent => root1)
187
+ _ou_class.create(:ou => "child3", :parent => root1)
188
+ _ou_class.create("root2")
189
+ assert_equal(["base",
190
+ "root1", "child1", "child2", "domain", "child3",
191
+ "root2"],
192
+ _entry_class.find(:all).collect(&:id))
193
+ assert_raise(ActiveLdap::DeleteError) do
194
+ root1.delete_all
195
+ end
196
+ assert_equal(["base", "root1", "domain", "root2"],
197
+ _entry_class.find(:all).collect(&:id))
198
+ end
199
+
200
+ def test_delete_tree
201
+ make_ou("base")
202
+ _ou_class = ou_class("ou=base")
203
+ root1 = _ou_class.create("root1")
204
+ _ou_class.create(:ou => "child1", :parent => root1)
205
+ _ou_class.create(:ou => "child2", :parent => root1)
206
+ _ou_class.create("root2")
207
+ assert_equal(["base", "root1", "child1", "child2", "root2"],
208
+ _ou_class.find(:all).collect(&:ou))
209
+ _ou_class.delete_all(:base => root1.dn)
210
+ assert_equal(["base", "root2"],
211
+ _ou_class.find(:all).collect(&:ou))
212
+ end
213
+
214
+ def test_delete_mixed_tree
215
+ make_ou("base")
216
+ _ou_class = ou_class("ou=base")
217
+ domain_class = Class.new(ActiveLdap::Base)
218
+ domain_class.ldap_mapping :dn_attribute => "dc",
219
+ :prefix => "",
220
+ :classes => ['domain']
221
+
222
+ root1 = _ou_class.create("root1")
223
+ child1 = _ou_class.create(:ou => "child1", :parent => root1)
224
+ domain_class.create(:dc => "domain1", :parent => child1)
225
+ _ou_class.create(:ou => "grandchild1", :parent => child1)
226
+ child2 = _ou_class.create(:ou => "child2", :parent => root1)
227
+ domain_class.create(:dc => "domain2", :parent => child2)
228
+ _ou_class.create("root2")
229
+
230
+ entry_class = Class.new(ActiveLdap::Base)
231
+ entry_class.ldap_mapping :prefix => "ou=base",
232
+ :classes => ["top"]
233
+ entry_class.dn_attribute = nil
234
+ assert_equal(["base", "root1", "child1", "domain1", "grandchild1",
235
+ "child2", "domain2", "root2"],
236
+ entry_class.find(:all).collect(&:id))
237
+ entry_class.delete_all(nil, :base => child2.dn)
238
+ assert_equal(["base", "root1", "child1", "domain1", "grandchild1", "root2"],
239
+ entry_class.find(:all).collect(&:id))
240
+ end
241
+
242
+ def test_first
243
+ make_temporary_user(:simple => true) do |user1,|
244
+ make_temporary_user(:simple => true) do |user2,|
245
+ assert_equal(user1, @user_class.find(:first))
246
+ assert_equal(user2, @user_class.find(:first, user2.cn))
247
+ end
248
+ end
249
+ end
250
+
251
+ def test_last
252
+ make_temporary_user(:simple => true) do |user1,|
253
+ make_temporary_user(:simple => true) do |user2,|
254
+ assert_equal(user2, @user_class.find(:last))
255
+ assert_equal(user1, @user_class.find(:last, user1.cn))
256
+ end
257
+ end
258
+ end
259
+
260
+ def test_convenient_operation_methods
261
+ make_temporary_user(:simple => true) do |user1,|
262
+ make_temporary_user(:simple => true) do |user2,|
263
+ assert_equal(user1, @user_class.first)
264
+ assert_equal(user2, @user_class.last)
265
+ assert_equal([user1, user2], @user_class.all)
266
+ end
267
+ end
268
+ end
269
+
270
+ def test_set_attributes_with_a_blank_value_in_values
271
+ make_temporary_user(:simple => true) do |user,|
272
+ user.attributes = {"description" => ["a", "b", ""]}
273
+ assert(user.save)
274
+ end
275
+ end
276
+
277
+ def test_set_attributes_with_a_blank_value
278
+ make_temporary_user(:simple => true) do |user,|
279
+ user.attributes = {"description" => [""]}
280
+ assert(user.save)
281
+ end
282
+ end
283
+
284
+ def test_create_invalid
285
+ user = @user_class.create
286
+ assert_not_predicate(user.errors, :empty?)
287
+ end
288
+
289
+ def test_id_with_invalid_dn_attribute_value
290
+ user = @user_class.new("#")
291
+ assert_equal("#", user.uid)
292
+ assert_equal("#", user.id)
293
+ end
294
+
295
+ def test_non_string_dn_attribute_value
296
+ user = @user_class.new("uidNumber=10110")
297
+ user.uid = user.cn = user.sn = "test-user"
298
+ user.gid_number = 10000
299
+ user.home_directory = "/home/test-user"
300
+ assert_nothing_raised do
301
+ user.save!
302
+ end
303
+ end
304
+
305
+ def test_set_dn_with_unnormalized_dn_attribute
306
+ make_temporary_user do |user,|
307
+ assert_not_equal("ZZZ", user.cn)
308
+ user.dn = "CN=ZZZ"
309
+ assert_equal("ZZZ", user.cn)
310
+ end
311
+ end
312
+
313
+ def test_destroy_with_empty_base_and_prefix_of_class
314
+ make_temporary_user do |user,|
315
+ base = user.class.base
316
+ prefix = user.class.prefix
317
+ begin
318
+ user.class.base = ""
319
+ user.class.prefix = ""
320
+ user.base = base
321
+ user.destroy
322
+ ensure
323
+ user.class.base = base
324
+ user.class.prefix = prefix
325
+ end
326
+ end
327
+ end
328
+
329
+ def test_empty_base_of_class
330
+ make_temporary_user do |user,|
331
+ user.class.prefix = ""
332
+ user.class.base = ""
333
+ user.base = "dc=net"
334
+ assert_equal("dc=net", user.base)
335
+ end
336
+ end
337
+
338
+ def test_search_value_with_no_dn_attribute
339
+ make_temporary_user do |user1,|
340
+ make_temporary_user do |user2,|
341
+ options = {:attribute => "seeAlso", :value => user2.dn}
342
+ assert_equal([],
343
+ user1.class.find(:all, options).collect(&:dn))
344
+
345
+ user1.see_also = user2.dn
346
+ user1.save!
347
+
348
+ assert_equal([user1.dn],
349
+ user1.class.find(:all, options).collect(&:dn))
350
+ end
351
+ end
352
+ end
353
+
354
+ def test_to_s
355
+ make_temporary_group do |group|
356
+ assert_equal(group.to_s, group.to_ldif)
357
+ end
358
+ end
359
+
360
+ def test_to_ldif
361
+ make_temporary_group do |group|
362
+ assert_to_ldif(group)
363
+
364
+ group.gidNumber += 1
365
+ group.description = ["Description", {"en" => "Description(en)"}]
366
+ assert_to_ldif(group)
367
+ end
368
+ end
369
+
370
+ def test_save_with_changes
371
+ make_temporary_user do |user, password|
372
+ user.cn += "!!!"
373
+ assert_true(detect_modify(user) {user.save})
374
+ end
375
+ end
376
+
377
+ def test_save_without_changes
378
+ make_temporary_user do |user, password|
379
+ assert_false(detect_modify(user) {user.save})
380
+ end
381
+ end
382
+
383
+ def test_normalize_dn_attribute
384
+ make_ou("Ous")
385
+ ou_class = Class.new(ActiveLdap::Base)
386
+ ou_class.ldap_mapping(:dn_attribute => "OU",
387
+ :prefix => "ou=OUS",
388
+ :classes => ["top", "organizationalUnit"])
389
+ ou_class.new("ou1").save!
390
+ ou_class.new("ou2").save!
391
+
392
+ ou1 = ou_class.find("ou1")
393
+ assert_equal("ou1", ou1.ou)
394
+ assert_equal("ou=ou1,#{ou_class.base}", ou1.dn)
395
+ ou2 = ou_class.find("ou2")
396
+ assert_equal("ou2", ou2.ou)
397
+ assert_equal("ou=ou2,#{ou_class.base}", ou2.dn)
398
+ end
399
+
400
+ def test_excluded_classes
401
+ mapping = {:classes => ["person"]}
402
+ person_class = Class.new(@user_class)
403
+ person_class.ldap_mapping(mapping)
404
+ person_class.prefix = nil
405
+
406
+ no_organizational_person_class = Class.new(@user_class)
407
+ no_organizational_person_mapping =
408
+ mapping.merge(:excluded_classes => ["organizationalPerson"])
409
+ no_organizational_person_class.ldap_mapping(no_organizational_person_mapping)
410
+ no_organizational_person_class.prefix = nil
411
+
412
+ no_simple_person_class = Class.new(@user_class)
413
+ no_simple_person_mapping =
414
+ mapping.merge(:excluded_classes => ['shadowAccount', 'inetOrgPerson',
415
+ "organizationalPerson"])
416
+ no_simple_person_class.ldap_mapping(no_simple_person_mapping)
417
+ no_simple_person_class.prefix = nil
418
+
419
+ make_temporary_user do |user1,|
420
+ make_temporary_user(:simple => true) do |user2,|
421
+ assert_equal([user1.dn, user2.dn].sort,
422
+ person_class.find(:all).collect(&:dn).sort)
423
+
424
+ no_organizational_people = no_organizational_person_class.find(:all)
425
+ assert_equal([user2.dn].sort,
426
+ no_organizational_people.collect(&:dn).sort)
427
+
428
+ assert_equal([user2.dn].sort,
429
+ no_simple_person_class.find(:all).collect(&:dn).sort)
430
+ end
431
+ end
432
+ end
433
+
434
+ def test_new_with_dn
435
+ cn = "XXX"
436
+ dn = "cn=#{cn},#{@user_class.base}"
437
+ user = @user_class.new(ActiveLdap::DN.parse(dn))
438
+ assert_equal(cn, user.cn)
439
+ assert_equal(dn, user.dn)
440
+ end
441
+
442
+ def test_dn_attribute_per_instance_with_invalid_value
443
+ user = @user_class.new
444
+ assert_equal("uid", user.dn_attribute)
445
+
446
+ user.dn = nil
447
+ assert_equal("uid", user.dn_attribute)
448
+ assert_nil(user.uid)
449
+
450
+ user.dn = ""
451
+ assert_equal("uid", user.dn_attribute)
452
+ assert_nil(user.uid)
453
+ end
454
+
455
+ def test_dn_attribute_per_instance
456
+ user = @user_class.new
457
+ assert_equal("uid", user.dn_attribute)
458
+ assert_nil(user.uid)
459
+
460
+ user.dn = "cn=xxx"
461
+ assert_equal("cn", user.dn_attribute)
462
+ assert_nil(user.uid)
463
+ assert_equal("xxx", user.cn)
464
+ assert_equal("cn=xxx,#{@user_class.base}", user.dn)
465
+
466
+ assert_equal("uid", @user_class.new.dn_attribute)
467
+
468
+ user.dn = "ZZZ"
469
+ assert_equal("cn", user.dn_attribute)
470
+ assert_nil(user.uid)
471
+ assert_equal("ZZZ", user.cn)
472
+ assert_equal("cn=ZZZ,#{@user_class.base}", user.dn)
473
+
474
+ user.dn = "uid=aaa"
475
+ assert_equal("uid", user.dn_attribute)
476
+ assert_equal("aaa", user.uid)
477
+ assert_equal("ZZZ", user.cn)
478
+ assert_equal("uid=aaa,#{@user_class.base}", user.dn)
479
+ end
480
+
481
+ def test_case_insensitive_nested_ou
482
+ ou_class("ou=Users").new("Sub").save!
483
+ make_temporary_user(:uid => "test-user,ou=SUB") do |user, password|
484
+ sub_user_class = Class.new(@user_class)
485
+ sub_user_class.ldap_mapping :prefix => "ou=sub"
486
+ assert_equal(dn("uid=test-user,ou=sub,#{@user_class.base}"),
487
+ sub_user_class.find(user.uid).dn)
488
+ end
489
+ end
490
+
491
+ def test_nested_ou
492
+ make_ou("units")
493
+ units = ou_class("ou=units")
494
+ units.new("one").save!
495
+ units.new("two").save!
496
+ units.new("three").save!
497
+
498
+ ous = units.find(:all, :scope => :sub).collect {|unit| unit.ou}
499
+ assert_equal(["one", "two", "three", "units"].sort, ous.sort)
500
+
501
+ ous = units.find(:all, :scope => :base).collect {|unit| unit.ou}
502
+ assert_equal(["units"].sort, ous.sort)
503
+
504
+ ous = units.find(:all, :scope => :one).collect {|unit| unit.ou}
505
+ assert_equal(["one", "two", "three"].sort, ous.sort)
506
+ end
507
+
508
+ def test_initialize_with_recommended_classes
509
+ mapping = {
510
+ :dn_attribute => "cn",
511
+ :prefix => "",
512
+ :scope => :one,
513
+ :classes => ["person"],
514
+ }
515
+ person_class = Class.new(ActiveLdap::Base)
516
+ person_class.ldap_mapping mapping
517
+
518
+ person_with_uid_class = Class.new(ActiveLdap::Base)
519
+ person_with_uid_mapping =
520
+ mapping.merge(:recommended_classes => ["uidObject"])
521
+ person_with_uid_class.ldap_mapping person_with_uid_mapping
522
+
523
+ name = "sample"
524
+ name_with_uid = "sample-with-uid"
525
+ uid = "1000"
526
+
527
+ person = person_class.new(name)
528
+ person.sn = name
529
+ assert(person.save)
530
+ assert_equal([name, name], [person.cn, person.sn])
531
+
532
+ person_with_uid = person_with_uid_class.new(name_with_uid)
533
+ person_with_uid.sn = name_with_uid
534
+ assert(!person_with_uid.save)
535
+ person_with_uid.uid = uid
536
+ assert(person_with_uid.save)
537
+ assert_equal([name_with_uid, name_with_uid],
538
+ [person_with_uid.cn, person_with_uid.sn])
539
+ assert_equal(uid, person_with_uid.uid)
540
+
541
+ assert_equal([person.dn, person_with_uid.dn],
542
+ person_class.search.collect {|dn, attrs| dn})
543
+ person_class.required_classes += ["uidObject"]
544
+ assert_equal([person_with_uid.dn],
545
+ person_class.search.collect {|dn, attrs| dn})
546
+
547
+ assert_equal([person.dn, person_with_uid.dn],
548
+ person_with_uid_class.search.collect {|dn, attrs| dn})
549
+ end
550
+
551
+ def test_search_with_object_class
552
+ ou_class = Class.new(ActiveLdap::Base)
553
+ ou_class.ldap_mapping :dn_attribute => "ou",
554
+ :prefix => "",
555
+ :scope => :one,
556
+ :classes => ["organizationalUnit"]
557
+
558
+ name = "sample"
559
+ ou = ou_class.new(name)
560
+ assert(ou.save)
561
+ assert_equal(name, ou.ou)
562
+
563
+ assert_equal([ou.dn],
564
+ ou_class.search(:value => name).collect {|dn, attrs| dn})
565
+ ou_class.required_classes += ["organization"]
566
+ assert_equal([],
567
+ ou_class.search(:value => name).collect {|dn, attrs| dn})
568
+ end
569
+
570
+ def test_search_with_attributes_without_object_class
571
+ make_temporary_user do |user, password|
572
+ entries = @user_class.search(:filter => "#{user.dn_attribute}=#{user.id}",
573
+ :attributes => ["uidNumber"])
574
+ assert_equal([[user.dn, {"uidNumber" => [user.uid_number.to_s]}]],
575
+ entries)
576
+ end
577
+ end
578
+
579
+ def test_new_without_argument
580
+ user = @user_class.new
581
+ assert_equal(@user_class_classes, user.classes)
582
+ assert(user.respond_to?(:cn))
583
+ end
584
+
585
+ def test_new_with_invalid_argument
586
+ @user_class.new
587
+ assert_raises(ArgumentError) do
588
+ @user_class.new(100)
589
+ end
590
+ end
591
+
592
+ def test_loose_dn
593
+ make_temporary_user do |user,|
594
+ assert(user.class.exists?(user.dn.to_s))
595
+ assert(user.class.exists?(user.dn.to_s.gsub(/\b,/, " , ")))
596
+ assert(user.class.exists?(user.dn.to_s.gsub(/\b=/, " = ")))
597
+ end
598
+ end
599
+
600
+ def test_new_without_class
601
+ no_class_class = Class.new(ActiveLdap::Base)
602
+ no_class_class.ldap_mapping :dn_attribute => "dc", :prefix => "",
603
+ :classes => []
604
+ assert_raises(ActiveLdap::UnknownAttribute) do
605
+ no_class_class.new("xxx")
606
+ end
607
+ end
608
+
609
+ def test_save_for_dNSDomain
610
+ domain_class = Class.new(ActiveLdap::Base)
611
+ domain_class.ldap_mapping :dn_attribute => "dc", :prefix => "",
612
+ :classes => ['top', 'dcObject', 'dNSDomain']
613
+ name = "ftp"
614
+ a_record = "192.168.1.1"
615
+
616
+ domain = domain_class.new('ftp')
617
+ domain.a_record = a_record
618
+ assert(domain.save)
619
+ assert_equal(a_record, domain.a_record)
620
+ assert_equal(a_record, domain_class.find(name).a_record)
621
+ ensure
622
+ domain_class.delete(name) if domain_class.exists?(name)
623
+ end
624
+
625
+ def test_dn_by_index_getter
626
+ make_temporary_user do |user,|
627
+ assert_equal(user.dn, user["dn"])
628
+ end
629
+ end
630
+
631
+ def test_create_multiple
632
+ ensure_delete_user("temp-user1") do |uid1,|
633
+ ensure_delete_user("temp-user2") do |uid2,|
634
+ attributes = {
635
+ :uid => uid2,
636
+ :sn => uid2,
637
+ :cn => uid2,
638
+ :uid_number => "1000",
639
+ :gid_number => "1000",
640
+ :home_directory => "/home/#{uid2}",
641
+ }
642
+
643
+ user1, user2 = @user_class.create([{:uid => uid1}, attributes])
644
+ assert(!user1.errors.empty?)
645
+ assert(!@user_class.exists?(uid1))
646
+
647
+ assert_equal([], user2.errors.to_a)
648
+ assert(@user_class.exists?(uid2))
649
+ attributes.each do |key, value|
650
+ value = value.to_i if [:uid_number, :gid_number].include?(key)
651
+ assert_equal(value, user2[key])
652
+ end
653
+ end
654
+ end
655
+ end
656
+
657
+ def test_create
658
+ ensure_delete_user("temp-user") do |uid,|
659
+ user = @user_class.create(:uid => uid)
660
+ assert(!user.errors.empty?)
661
+ assert(!@user_class.exists?(uid))
662
+
663
+ attributes = {
664
+ :uid => uid,
665
+ :sn => uid,
666
+ :cn => uid,
667
+ :uid_number => "1000",
668
+ :gid_number => "1000",
669
+ :home_directory => "/home/#{uid}",
670
+ }
671
+ user = @user_class.create(attributes)
672
+ assert_equal([], user.errors.to_a)
673
+ assert(@user_class.exists?(uid))
674
+ attributes.each do |key, value|
675
+ value = value.to_i if [:uid_number, :gid_number].include?(key)
676
+ assert_equal(value, user[key])
677
+ end
678
+ end
679
+ end
680
+
681
+ def test_reload_of_not_exists_entry
682
+ make_temporary_user do |user,|
683
+ assert_nothing_raised do
684
+ user.reload
685
+ end
686
+
687
+ user.destroy
688
+
689
+ assert_raises(ActiveLdap::EntryNotFound) do
690
+ user.reload
691
+ end
692
+ end
693
+ end
694
+
695
+ def test_reload_and_new_entry
696
+ make_temporary_user do |user1,|
697
+ user2 = @user_class.new(user1.uid)
698
+ assert_equal(user1.attributes["uid"], user2.attributes["uid"])
699
+ assert_not_equal(user1.attributes["objectClass"],
700
+ @user_class.required_classes)
701
+ assert_equal(@user_class.required_classes,
702
+ user2.attributes["objectClass"])
703
+ assert_not_equal(user1.attributes["objectClass"],
704
+ user2.attributes["objectClass"])
705
+ assert(user2.exists?)
706
+ assert(user2.new_entry?)
707
+
708
+ user2.reload
709
+ assert(user2.exists?)
710
+ assert(!user2.new_entry?)
711
+ assert_equal(user1.attributes, user2.attributes)
712
+ end
713
+ end
714
+
715
+ def test_exists_for_instance
716
+ make_temporary_user do |user,|
717
+ assert(user.exists?)
718
+ assert(!user.new_entry?)
719
+
720
+ new_user = @user_class.new(user.uid)
721
+ assert(new_user.exists?)
722
+ assert(new_user.new_entry?)
723
+
724
+ user.destroy
725
+ assert(!user.exists?)
726
+ assert(user.new_entry?)
727
+
728
+ assert(!new_user.exists?)
729
+ assert(new_user.new_entry?)
730
+ end
731
+ end
732
+
733
+ def test_exists_without_required_object_class
734
+ make_temporary_user do |user,|
735
+ @user_class.required_classes -= ["posixAccount"]
736
+ user.remove_class("posixAccount")
737
+ assert(user.save)
738
+
739
+ assert(@user_class.exists?(user.dn))
740
+ @user_class.required_classes += ["posixAccount"]
741
+ assert(!@user_class.exists?(user.dn))
742
+ assert_raises(ActiveLdap::EntryNotFound) do
743
+ @user_class.find(user.dn)
744
+ end
745
+ end
746
+ end
747
+
748
+ def test_find_dns_without_required_object_class
749
+ make_temporary_user do |user1,|
750
+ make_temporary_user do |user2,|
751
+ make_temporary_user do |user3,|
752
+ @user_class.required_classes -= ["posixAccount"]
753
+ user1.remove_class("posixAccount")
754
+ assert(user1.save)
755
+
756
+ @user_class.required_classes += ["posixAccount"]
757
+ assert_raises(ActiveLdap::EntryNotFound) do
758
+ @user_class.find(user1.dn, user2.dn, user3.dn)
759
+ end
760
+ assert_equal([user2.dn, user3.dn],
761
+ @user_class.find(user2.dn, user3.dn).collect {|u| u.dn})
762
+ end
763
+ end
764
+ end
765
+ end
766
+
767
+ def test_reload
768
+ make_temporary_user do |user1,|
769
+ user2 = @user_class.find(user1.uid)
770
+ assert_equal(user1.attributes, user2.attributes)
771
+
772
+ user1.cn = "new #{user1.cn}"
773
+ assert_not_equal(user1.attributes, user2.attributes)
774
+ assert_equal(user1.attributes.reject {|k, v| k == "cn"},
775
+ user2.attributes.reject {|k, v| k == "cn"})
776
+
777
+ user2.reload
778
+ assert_not_equal(user1.attributes, user2.attributes)
779
+ assert_equal(user1.attributes.reject {|k, v| k == "cn"},
780
+ user2.attributes.reject {|k, v| k == "cn"})
781
+
782
+ assert(user1.save)
783
+ assert_not_equal(user1.attributes, user2.attributes)
784
+ assert_equal(user1.attributes.reject {|k, v| k == "cn"},
785
+ user2.attributes.reject {|k, v| k == "cn"})
786
+
787
+ user2.reload
788
+ assert_equal(user1.cn, user2.cn)
789
+ assert_equal(user1.attributes.reject {|k, v| k == "cn"},
790
+ user2.attributes.reject {|k, v| k == "cn"})
791
+ end
792
+ end
793
+
794
+ def test_inherit_base
795
+ sub_user_class = Class.new(@user_class)
796
+ sub_user_class.ldap_mapping :prefix => "ou=Sub"
797
+ assert_equal("ou=Sub,#{@user_class.base}", sub_user_class.base)
798
+ sub_user_class.send(:include, Module.new)
799
+ assert_equal("ou=Sub,#{@user_class.base}", sub_user_class.base)
800
+
801
+ sub_sub_user_class = Class.new(sub_user_class)
802
+ sub_sub_user_class.ldap_mapping :prefix => "ou=SubSub"
803
+ assert_equal("ou=SubSub,#{sub_user_class.base}", sub_sub_user_class.base)
804
+ sub_sub_user_class.send(:include, Module.new)
805
+ assert_equal("ou=SubSub,#{sub_user_class.base}", sub_sub_user_class.base)
806
+ end
807
+
808
+ def test_compare
809
+ make_temporary_user do |user1,|
810
+ make_temporary_user do |user2,|
811
+ make_temporary_user do |user3,|
812
+ make_temporary_user do |user4,|
813
+ actual = ([user1, user2, user3] & [user1, user4])
814
+ assert_equal([user1].collect {|user| user.id},
815
+ actual.collect {|user| user.id})
816
+ end
817
+ end
818
+ end
819
+ end
820
+ end
821
+
822
+ def test_ldap_mapping_symbol_dn_attribute
823
+ ou_class = Class.new(ActiveLdap::Base)
824
+ ou_class.ldap_mapping(:dn_attribute => :ou,
825
+ :prefix => "",
826
+ :classes => ["top", "organizationalUnit"])
827
+ assert_equal(["ou=Groups,#{current_configuration['base']}",
828
+ "ou=Users,#{current_configuration['base']}"],
829
+ ou_class.find(:all).collect(&:dn).sort)
830
+ end
831
+
832
+ def test_ldap_mapping_validation
833
+ ou_class = Class.new(ActiveLdap::Base)
834
+ assert_raises(ArgumentError) do
835
+ ou_class.ldap_mapping :dnattr => "ou"
836
+ end
837
+
838
+ assert_nothing_raised do
839
+ ou_class.ldap_mapping :dn_attribute => "ou",
840
+ :prefix => "",
841
+ :classes => ["top", "organizationalUnit"]
842
+ end
843
+ end
844
+
845
+ class TestToXML < self
846
+ def test_root
847
+ ou = ou_class.new("Sample")
848
+ assert_equal(<<-EOX, ou.to_xml(:root => "ou"))
849
+ <ou>
850
+ <dn>#{ou.dn}</dn>
851
+ <objectClasses type="array">
852
+ <objectClass>organizationalUnit</objectClass>
853
+ <objectClass>top</objectClass>
854
+ </objectClasses>
855
+ <ous type="array">
856
+ <ou>Sample</ou>
857
+ </ous>
858
+ </ou>
859
+ EOX
860
+ end
861
+
862
+ def test_default
863
+ ou = ou_class.new("Sample")
864
+ assert_equal(<<-EOX, ou.to_xml)
865
+ <anonymous>
866
+ <dn>#{ou.dn}</dn>
867
+ <objectClasses type="array">
868
+ <objectClass>organizationalUnit</objectClass>
869
+ <objectClass>top</objectClass>
870
+ </objectClasses>
871
+ <ous type="array">
872
+ <ou>Sample</ou>
873
+ </ous>
874
+ </anonymous>
875
+ EOX
876
+ end
877
+
878
+ def test_complex
879
+ make_temporary_user do |user, password|
880
+ xml = normalize_attributes_order(user.to_xml(:root => "user"))
881
+ assert_equal(<<-EOX, xml)
882
+ <user>
883
+ <dn>#{user.dn}</dn>
884
+ <cns type="array">
885
+ <cn>#{user.cn}</cn>
886
+ </cns>
887
+ <gidNumber>#{user.gid_number}</gidNumber>
888
+ <homeDirectory>#{user.home_directory}</homeDirectory>
889
+ <jpegPhotos type="array">
890
+ <jpegPhoto base64="true">#{base64(jpeg_photo)}</jpegPhoto>
891
+ </jpegPhotos>
892
+ <objectClasses type="array">
893
+ <objectClass>inetOrgPerson</objectClass>
894
+ <objectClass>organizationalPerson</objectClass>
895
+ <objectClass>person</objectClass>
896
+ <objectClass>posixAccount</objectClass>
897
+ <objectClass>shadowAccount</objectClass>
898
+ </objectClasses>
899
+ <sns type="array">
900
+ <sn>#{user.sn}</sn>
901
+ </sns>
902
+ <uids type="array">
903
+ <uid>#{user.uid}</uid>
904
+ </uids>
905
+ <uidNumber>#{user.uid_number}</uidNumber>
906
+ <userCertificates type="array">
907
+ <userCertificate base64="true" binary="true">#{base64(certificate)}</userCertificate>
908
+ </userCertificates>
909
+ <userPasswords type="array">
910
+ <userPassword>#{user.user_password}</userPassword>
911
+ </userPasswords>
912
+ </user>
913
+ EOX
914
+ end
915
+ end
916
+
917
+ def test_except
918
+ ou = ou_class.new("Sample")
919
+ except = [:objectClass]
920
+ assert_equal(<<-EOX, ou.to_xml(:root => "sample", :except => except))
921
+ <sample>
922
+ <dn>#{ou.dn}</dn>
923
+ <ous type="array">
924
+ <ou>Sample</ou>
925
+ </ous>
926
+ </sample>
927
+ EOX
928
+ end
929
+
930
+ def test_except_dn
931
+ ou = ou_class.new("Sample")
932
+ except = [:dn, :object_class]
933
+ assert_equal(<<-EOX, ou.to_xml(:root => "sample", :except => except))
934
+ <sample>
935
+ <ous type="array">
936
+ <ou>Sample</ou>
937
+ </ous>
938
+ </sample>
939
+ EOX
940
+ end
941
+
942
+ def test_only
943
+ ou = ou_class.new("Sample")
944
+ only = [:objectClass]
945
+ assert_equal(<<-EOX, ou.to_xml(:root => "sample", :only => only))
946
+ <sample>
947
+ <objectClasses type="array">
948
+ <objectClass>organizationalUnit</objectClass>
949
+ <objectClass>top</objectClass>
950
+ </objectClasses>
951
+ </sample>
952
+ EOX
953
+ end
954
+
955
+ def test_only_dn
956
+ ou = ou_class.new("Sample")
957
+ only = [:dn, :object_class]
958
+ assert_equal(<<-EOX, ou.to_xml(:root => "sample", :only => only))
959
+ <sample>
960
+ <dn>#{ou.dn}</dn>
961
+ <objectClasses type="array">
962
+ <objectClass>organizationalUnit</objectClass>
963
+ <objectClass>top</objectClass>
964
+ </objectClasses>
965
+ </sample>
966
+ EOX
967
+ end
968
+
969
+ def test_escape
970
+ make_temporary_user do |user, password|
971
+ sn = user.sn
972
+ user.sn = "<#{sn}>"
973
+ except = [:jpeg_photo, :user_certificate]
974
+ assert_equal(<<-EOX, user.to_xml(:root => "user", :except => except))
975
+ <user>
976
+ <dn>#{user.dn}</dn>
977
+ <cns type="array">
978
+ <cn>#{user.cn}</cn>
979
+ </cns>
980
+ <gidNumber>#{user.gid_number}</gidNumber>
981
+ <homeDirectory>#{user.home_directory}</homeDirectory>
982
+ <objectClasses type="array">
983
+ <objectClass>inetOrgPerson</objectClass>
984
+ <objectClass>organizationalPerson</objectClass>
985
+ <objectClass>person</objectClass>
986
+ <objectClass>posixAccount</objectClass>
987
+ <objectClass>shadowAccount</objectClass>
988
+ </objectClasses>
989
+ <sns type="array">
990
+ <sn>&lt;#{sn}&gt;</sn>
991
+ </sns>
992
+ <uids type="array">
993
+ <uid>#{user.uid}</uid>
994
+ </uids>
995
+ <uidNumber>#{user.uid_number}</uidNumber>
996
+ <userPasswords type="array">
997
+ <userPassword>#{user.user_password}</userPassword>
998
+ </userPasswords>
999
+ </user>
1000
+ EOX
1001
+ end
1002
+ end
1003
+
1004
+ def test_type_ldif
1005
+ make_temporary_user do |user, password|
1006
+ sn = user.sn
1007
+ user.sn = "<#{sn}>"
1008
+ except = [:jpeg_photo, :user_certificate]
1009
+ options = {:root => "user", :except => except, :type => :ldif}
1010
+ assert_equal(<<-EOX, user.to_xml(options))
1011
+ <user>
1012
+ <dn>#{user.dn}</dn>
1013
+ <cn>#{user.cn}</cn>
1014
+ <gidNumber>#{user.gid_number}</gidNumber>
1015
+ <homeDirectory>#{user.home_directory}</homeDirectory>
1016
+ <objectClass>inetOrgPerson</objectClass>
1017
+ <objectClass>organizationalPerson</objectClass>
1018
+ <objectClass>person</objectClass>
1019
+ <objectClass>posixAccount</objectClass>
1020
+ <objectClass>shadowAccount</objectClass>
1021
+ <sn>&lt;#{sn}&gt;</sn>
1022
+ <uid>#{user.uid}</uid>
1023
+ <uidNumber>#{user.uid_number}</uidNumber>
1024
+ <userPassword>#{user.user_password}</userPassword>
1025
+ </user>
1026
+ EOX
1027
+ end
1028
+ end
1029
+
1030
+ def test_nil
1031
+ ou = ou_class.new("Sample")
1032
+ ou.description = [nil]
1033
+ assert_equal(<<-EOX, ou.to_xml(:root => "sample"))
1034
+ <sample>
1035
+ <dn>#{ou.dn}</dn>
1036
+ <objectClasses type="array">
1037
+ <objectClass>organizationalUnit</objectClass>
1038
+ <objectClass>top</objectClass>
1039
+ </objectClasses>
1040
+ <ous type="array">
1041
+ <ou>Sample</ou>
1042
+ </ous>
1043
+ </sample>
1044
+ EOX
1045
+ end
1046
+
1047
+ def test_single_value
1048
+ make_temporary_user do |user, password|
1049
+ only = [:dn, :uidNumber]
1050
+ assert_equal(<<-EOX, user.to_xml(:root => "user", :only => only))
1051
+ <user>
1052
+ <dn>#{user.dn}</dn>
1053
+ <uidNumber>#{user.uid_number}</uidNumber>
1054
+ </user>
1055
+ EOX
1056
+ end
1057
+ end
1058
+
1059
+ def test_single_value_nil
1060
+ make_temporary_user do |user, password|
1061
+ only = [:dn, :uidNumber]
1062
+ user.uid_number = nil
1063
+ assert_equal(<<-EOX, user.to_xml(:root => "user", :only => only))
1064
+ <user>
1065
+ <dn>#{user.dn}</dn>
1066
+ </user>
1067
+ EOX
1068
+ end
1069
+ end
1070
+ end
1071
+
1072
+ def test_save
1073
+ make_temporary_user do |user, password|
1074
+ user.sn = nil
1075
+ assert(!user.save)
1076
+ assert_raises(ActiveLdap::EntryInvalid) do
1077
+ user.save!
1078
+ end
1079
+
1080
+ user.sn = "Surname"
1081
+ assert(user.save)
1082
+ user.sn = "Surname2"
1083
+ assert_nothing_raised {user.save!}
1084
+ end
1085
+ end
1086
+
1087
+ def test_have_attribute?
1088
+ make_temporary_user do |user, password|
1089
+ assert_true(user.have_attribute?(:cn))
1090
+ assert_true(user.have_attribute?(:commonName))
1091
+ assert_true(user.have_attribute?(:common_name))
1092
+ assert_true(user.have_attribute?(:commonname))
1093
+ assert_true(user.have_attribute?(:COMMONNAME))
1094
+
1095
+ assert_false(user.have_attribute?(:unknown_attribute))
1096
+ end
1097
+ end
1098
+
1099
+ def test_attribute_present?
1100
+ make_temporary_user do |user, password|
1101
+ assert(user.attribute_present?(:sn))
1102
+ user.sn = nil
1103
+ assert(!user.attribute_present?(:sn))
1104
+ user.sn = "Surname"
1105
+ assert(user.attribute_present?(:sn))
1106
+ user.sn = [nil]
1107
+ assert(!user.attribute_present?(:sn))
1108
+ end
1109
+ end
1110
+
1111
+ def test_attribute_present_with_unknown_attribute
1112
+ make_temporary_user do |user, password|
1113
+ assert(!user.attribute_present?(:unknown_attribute))
1114
+ end
1115
+ end
1116
+
1117
+ def test_update_all
1118
+ make_temporary_user do |user, password|
1119
+ make_temporary_user do |user2, password2|
1120
+ user2_cn = user2.cn
1121
+ new_cn = "New #{user.cn}"
1122
+ @user_class.update_all({:cn => new_cn}, user.uid)
1123
+ assert_equal(new_cn, @user_class.find(user.uid).cn)
1124
+ assert_equal(user2_cn, @user_class.find(user2.uid).cn)
1125
+
1126
+ new_sn = "New SN"
1127
+ @user_class.update_all({:sn => [new_sn]})
1128
+ assert_equal(new_sn, @user_class.find(user.uid).sn)
1129
+ assert_equal(new_sn, @user_class.find(user2.uid).sn)
1130
+
1131
+ new_sn2 = "New SN2"
1132
+ @user_class.update_all({:sn => [new_sn2]}, user2.uid)
1133
+ assert_equal(new_sn, @user_class.find(user.uid).sn)
1134
+ assert_equal(new_sn2, @user_class.find(user2.uid).sn)
1135
+ end
1136
+ end
1137
+ end
1138
+
1139
+ def test_update
1140
+ make_temporary_user do |user, password|
1141
+ new_cn = "New #{user.cn}"
1142
+ new_user = @user_class.update(user.dn, {:cn => new_cn})
1143
+ assert_equal(new_cn, new_user.cn)
1144
+
1145
+ make_temporary_user do |user2, password2|
1146
+ new_sns = ["New SN1", "New SN2"]
1147
+ new_cn2 = "New #{user2.cn}"
1148
+ new_user, new_user2 = @user_class.update([user.dn, user2.dn],
1149
+ [{:sn => new_sns[0]},
1150
+ {:sn => new_sns[1],
1151
+ :cn => new_cn2}])
1152
+ assert_equal(new_sns, [new_user.sn, new_user2.sn])
1153
+ assert_equal(new_cn2, new_user2.cn)
1154
+ end
1155
+ end
1156
+ end
1157
+
1158
+ def test_to_key
1159
+ uid = "bob"
1160
+ new_user = @user_class.new
1161
+ assert_equal(nil, new_user.to_key)
1162
+
1163
+ new_user.uid = uid
1164
+ assert_equal([new_user.dn], new_user.to_key)
1165
+ end
1166
+
1167
+ private
1168
+ def detect_modify(object)
1169
+ modify_called = false
1170
+ singleton_class = class << object; self; end
1171
+ singleton_class.send(:define_method, :modify_entry) do |*args|
1172
+ dn, attributes, options = args
1173
+ options ||= {}
1174
+ modify_detector = Object.new
1175
+ modify_detector.instance_variable_set("@called", false)
1176
+ def modify_detector.modify(dn, entries, options)
1177
+ @called = true
1178
+ end
1179
+ options[:connection] = modify_detector
1180
+ result = super(dn, attributes, options)
1181
+ modify_called = modify_detector.instance_variable_get("@called")
1182
+ result
1183
+ end
1184
+ yield
1185
+ modify_called
1186
+ end
1187
+
1188
+ def assert_to_ldif(entry)
1189
+ records = ActiveLdap::LDIF.parse(entry.to_ldif).records
1190
+ parsed_entries = records.collect do |record|
1191
+ entry.class.send(:instantiate, [record.dn, record.attributes])
1192
+ end
1193
+ assert_equal([entry], parsed_entries)
1194
+ end
1195
+
1196
+ def base64(string)
1197
+ [string].pack("m").gsub(/\n/u, "")
1198
+ end
1199
+
1200
+ def normalize_attributes_order(xml)
1201
+ xml.gsub(/<(\S+) (.+?)(\/?)>/) do |matched|
1202
+ name = $1
1203
+ attributes = $2
1204
+ close_mark = $3
1205
+ attributes = attributes.scan(/(\S+)="(.+?)"/)
1206
+ normalized_attributes = attributes.sort_by do |key, value|
1207
+ key
1208
+ end.collect do |key, value|
1209
+ "#{key}=\"#{value}\""
1210
+ end.join(' ')
1211
+ "<#{name} #{normalized_attributes}#{close_mark}>"
1212
+ end
1213
+ end
1214
+ end