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,133 @@
1
+ require 'al-test-utils'
2
+
3
+ class TestLoad < Test::Unit::TestCase
4
+ include AlTestUtils
5
+
6
+ priority :must
7
+ def test_load_modify_record
8
+ ldif = ActiveLdap::LDIF.new
9
+ make_temporary_user do |user, password|
10
+ user.display_name = "Display Name"
11
+ assert(user.save)
12
+
13
+ user = @user_class.find(user.dn)
14
+ assert_equal("Display Name", user.display_name)
15
+
16
+ record = ActiveLdap::LDIF::ModifyRecord.new(user.dn)
17
+ ldif << record
18
+
19
+ original_home_directory = user.home_directory
20
+ new_home_directory = "#{original_home_directory}-new"
21
+ record.add_operation(:delete, "homeDirectory", [], {})
22
+ record.add_operation(:add, "homeDirectory", [],
23
+ {"homeDirectory" => [new_home_directory]})
24
+
25
+ original_descriptions = user.description(true)
26
+ new_description = "new description"
27
+ record.add_operation(:add, "description", [],
28
+ {"description" => [new_description]})
29
+
30
+ record.add_operation(:delete, "DisplayName", [], {})
31
+
32
+ new_sn = ["New SN1", "New SN2"]
33
+ record.add_operation(:replace, "sn", [], {"sn" => new_sn})
34
+
35
+ ActiveLdap::Base.load(ldif.to_s)
36
+
37
+ user = @user_class.find(user.dn)
38
+ assert_equal(new_home_directory, user.home_directory)
39
+ assert_equal(original_descriptions + [new_description],
40
+ user.description(true))
41
+ assert_nil(user.display_name)
42
+ assert_equal(new_sn, user.sn)
43
+ end
44
+ end
45
+
46
+ def test_load_move_dn_record
47
+ assert_load_move_dn_record(ActiveLdap::LDIF::ModifyDNRecord)
48
+ assert_load_move_dn_record(ActiveLdap::LDIF::ModifyRDNRecord)
49
+ end
50
+
51
+ def test_load_copy_dn_record
52
+ assert_load_copy_dn_record(ActiveLdap::LDIF::ModifyDNRecord)
53
+ assert_load_copy_dn_record(ActiveLdap::LDIF::ModifyRDNRecord)
54
+ end
55
+
56
+ def test_load_delete_record
57
+ ldif = ActiveLdap::LDIF.new
58
+ make_temporary_user do |user, password|
59
+ record = ActiveLdap::LDIF::DeleteRecord.new(user.dn)
60
+ ldif << record
61
+ assert_true(@user_class.exists?(user.dn))
62
+ ActiveLdap::Base.load(ldif.to_s)
63
+ assert_false(@user_class.exists?(user.dn))
64
+ end
65
+ end
66
+
67
+ def test_load_add_record
68
+ ldif = ActiveLdap::LDIF.new
69
+ make_temporary_user do |user, password|
70
+ new_description = "new description"
71
+ attributes = {
72
+ "description" => [new_description]
73
+ }
74
+ original_descriptions = user.description(true)
75
+ record = ActiveLdap::LDIF::AddRecord.new(user.dn, [], attributes)
76
+ ldif << record
77
+ ActiveLdap::Base.load(ldif.to_s)
78
+ user.reload
79
+ assert_equal(original_descriptions + [new_description],
80
+ user.description(true))
81
+ end
82
+ end
83
+
84
+ def test_load_content_records
85
+ ldif = ActiveLdap::LDIF.new
86
+ 2.times do
87
+ make_temporary_user do |user, password|
88
+ ldif << ActiveLdap::LDIF.parse(user.to_ldif).records[0]
89
+ end
90
+ end
91
+
92
+ original_n_users = @user_class.count
93
+ ActiveLdap::Base.load(ldif.to_s)
94
+ assert_equal(2, @user_class.count - original_n_users)
95
+ end
96
+
97
+ priority :normal
98
+
99
+ private
100
+ def assert_load_copy_dn_record(record_class)
101
+ ldif = ActiveLdap::LDIF.new
102
+ make_temporary_user do |user, password|
103
+ new_rdn = "uid=XXX"
104
+ ensure_delete_user(new_rdn) do
105
+ record = record_class.new(user.dn, [], new_rdn, false)
106
+ ldif << record
107
+ assert_true(@user_class.exists?(user.dn))
108
+ assert_false(@user_class.exists?(new_rdn))
109
+ ActiveLdap::Base.load(ldif.to_s)
110
+ assert_true(@user_class.exists?(user.dn))
111
+ assert_true(@user_class.exists?(new_rdn))
112
+ assert_equal(user.cn, @user_class.find(new_rdn).cn)
113
+ end
114
+ end
115
+ end
116
+
117
+ def assert_load_move_dn_record(record_class)
118
+ ldif = ActiveLdap::LDIF.new
119
+ make_temporary_user do |user, password|
120
+ new_rdn = "uid=XXX"
121
+ ensure_delete_user(new_rdn) do
122
+ record = record_class.new(user.dn, [], new_rdn, true)
123
+ ldif << record
124
+ assert_true(@user_class.exists?(user.dn))
125
+ assert_false(@user_class.exists?(new_rdn))
126
+ ActiveLdap::Base.load(ldif.to_s)
127
+ assert_false(@user_class.exists?(user.dn))
128
+ assert_true(@user_class.exists?(new_rdn))
129
+ assert_equal(user.cn, @user_class.find(new_rdn).cn)
130
+ end
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,75 @@
1
+ require 'al-test-utils'
2
+
3
+ class TestLPasswd < Test::Unit::TestCase
4
+ include AlTestUtils
5
+
6
+ def setup
7
+ super
8
+ @command = File.join(@examples_dir, "lpasswd")
9
+ make_ou("People")
10
+ @user_class.prefix = "ou=People"
11
+ end
12
+
13
+ priority :must
14
+
15
+ priority :normal
16
+ def test_non_exist_user
17
+ ensure_delete_user("test-user") do |uid,|
18
+ assert(!@user_class.exists?(uid))
19
+ assert_equal([false, "User #{uid} doesn't exist.\n"], run_command(uid))
20
+ assert(!@user_class.exists?(uid))
21
+ end
22
+ end
23
+
24
+ def test_change_password
25
+ make_temporary_user do |user, password|
26
+ new_password = "new#{password}"
27
+ assert_lpasswd_successfully(user.uid, password, new_password)
28
+ end
29
+ end
30
+
31
+ def test_password_mismatch
32
+ make_temporary_user do |user, password|
33
+ message = "[#{user.dn}] Password: \n" * 2
34
+ message = "#{message}Password mismatch!\n" * 3
35
+ assert_lpasswd_failed(user.id, password, message) do |input, output|
36
+ 3.times do
37
+ output.puts("new#{password}1")
38
+ output.puts("new#{password}2")
39
+ output.flush
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ private
46
+ def assert_lpasswd_successfully(name, current, new, *args, &block)
47
+ _wrap_assertion do
48
+ assert(@user_class.exists?(name))
49
+ assert_send([ActiveLdap::UserPassword, :valid?,
50
+ current, @user_class.find(name).user_password])
51
+ args.concat([name])
52
+ block ||= Proc.new do |input, output|
53
+ output.puts(new)
54
+ output.puts(new)
55
+ output.flush
56
+ end
57
+ assert_equal([true, "[#{@user_class.find(name).dn}] Password: \n" * 2],
58
+ run_command(*args, &block))
59
+ assert_send([ActiveLdap::UserPassword, :valid?,
60
+ new, @user_class.find(name).user_password])
61
+ end
62
+ end
63
+
64
+ def assert_lpasswd_failed(name, current, message, *args, &block)
65
+ _wrap_assertion do
66
+ assert(@user_class.exists?(name))
67
+ assert_send([ActiveLdap::UserPassword, :valid?,
68
+ current, @user_class.find(name).user_password])
69
+ args.concat([name])
70
+ assert_equal([false, message], run_command(*args, &block))
71
+ assert_send([ActiveLdap::UserPassword, :valid?,
72
+ current, @user_class.find(name).user_password])
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,74 @@
1
+ require 'al-test-utils'
2
+
3
+ class TestObjectClass < Test::Unit::TestCase
4
+ include AlTestUtils
5
+
6
+ priority :must
7
+ def test_pass_nil_to_set_classes
8
+ make_temporary_group do |group|
9
+ assert_raises(ActiveLdap::RequiredObjectClassMissed) do
10
+ group.classes = nil
11
+ end
12
+ end
13
+ end
14
+
15
+ priority :normal
16
+ def test_pass_nil_to_replace_class
17
+ make_temporary_group do |group|
18
+ assert_raises(ActiveLdap::RequiredObjectClassMissed) do
19
+ group.replace_class(nil)
20
+ end
21
+ end
22
+ end
23
+
24
+ def test_case_insensitive_match
25
+ assert_nothing_raised do
26
+ @group_class.send(:instantiate,
27
+ [
28
+ "cn=test-group,#{@group_class.base}",
29
+ {
30
+ :cn => "test-group",
31
+ :objectClass => ["TOP", "posixgroup"],
32
+ }
33
+ ])
34
+ end
35
+ end
36
+
37
+ def test_ensure_recommended_classes
38
+ make_temporary_group do |group|
39
+ added_class = "labeledURIObject"
40
+
41
+ assert_equal([], group.class.recommended_classes)
42
+ group.class.recommended_classes += [added_class]
43
+ assert_equal([added_class], group.class.recommended_classes)
44
+
45
+ assert_equal([added_class],
46
+ group.class.recommended_classes - group.classes)
47
+ group.ensure_recommended_classes
48
+ assert_equal([],
49
+ group.class.recommended_classes - group.classes)
50
+ end
51
+ end
52
+
53
+ def test_unknown_object_class
54
+ make_temporary_group do |group|
55
+ assert_raises(ActiveLdap::ObjectClassError) do
56
+ group.add_class("unknownObjectClass")
57
+ end
58
+ end
59
+ end
60
+
61
+ def test_remove_required_class
62
+ make_temporary_group do |group|
63
+ assert_raises(ActiveLdap::RequiredObjectClassMissed) do
64
+ group.remove_class("posixGroup")
65
+ end
66
+ end
67
+ end
68
+
69
+ def test_invalid_object_class_value
70
+ make_temporary_group do |group|
71
+ assert_raises(TypeError) {group.add_class(:posixAccount)}
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,131 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'al-test-utils'
4
+
5
+ class TestPersistence < Test::Unit::TestCase
6
+ include AlTestUtils
7
+
8
+ class TestDestroy < self
9
+ class TestClass < self
10
+ def test_by_dn
11
+ make_temporary_user do |user,|
12
+ assert(@user_class.exists?(user.uid))
13
+ @user_class.destroy(user.dn)
14
+ assert(!@user_class.exists?(user.uid))
15
+ end
16
+ end
17
+
18
+ def test_by_dn_value
19
+ make_temporary_user do |user,|
20
+ assert(@user_class.exists?(user.dn))
21
+ @user_class.destroy(user.uid)
22
+ assert(!@user_class.exists?(user.dn))
23
+ end
24
+ end
25
+
26
+ def test_by_dn_attribute
27
+ make_temporary_user do |user,|
28
+ assert(@user_class.exists?(user.dn))
29
+ @user_class.destroy("uid=#{user.uid}")
30
+ assert(!@user_class.exists?(user.dn))
31
+ end
32
+ end
33
+
34
+ def test_multiple
35
+ make_temporary_user do |user1,|
36
+ make_temporary_user do |user2,|
37
+ make_temporary_user do |user3,|
38
+ assert(@user_class.exists?(user1.uid))
39
+ assert(@user_class.exists?(user2.uid))
40
+ assert(@user_class.exists?(user3.uid))
41
+ @user_class.destroy([user1.dn, user2.uid, "uid=#{user3.uid}"])
42
+ assert(!@user_class.exists?(user1.uid))
43
+ assert(!@user_class.exists?(user2.uid))
44
+ assert(!@user_class.exists?(user3.uid))
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ class TestInstance < self
52
+ def test_existence
53
+ make_temporary_user do |user,|
54
+ assert(@user_class.exists?(user.uid))
55
+ user.destroy
56
+ assert(!@user_class.exists?(user.uid))
57
+ end
58
+ end
59
+
60
+ def test_frozen
61
+ make_temporary_user do |user,|
62
+ assert_false(user.frozen?)
63
+ user.destroy
64
+ assert_true(user.frozen?)
65
+ end
66
+ end
67
+ end
68
+ end
69
+
70
+ class TestDelete < self
71
+ class TestClass < self
72
+ def test_by_dn
73
+ make_temporary_user do |user,|
74
+ assert(@user_class.exists?(user.uid))
75
+ @user_class.delete(user.dn)
76
+ assert(!@user_class.exists?(user.uid))
77
+ end
78
+ end
79
+
80
+ def test_by_dn_value
81
+ make_temporary_user do |user,|
82
+ assert(@user_class.exists?(user.dn))
83
+ @user_class.delete(user.uid)
84
+ assert(!@user_class.exists?(user.dn))
85
+ end
86
+ end
87
+
88
+ def test_by_dn_attribute
89
+ make_temporary_user do |user,|
90
+ assert(@user_class.exists?(user.dn))
91
+ @user_class.delete("uid=#{user.uid}")
92
+ assert(!@user_class.exists?(user.dn))
93
+ end
94
+ end
95
+
96
+ def test_multiple
97
+ make_temporary_user do |user1,|
98
+ make_temporary_user do |user2,|
99
+ make_temporary_user do |user3,|
100
+ assert(@user_class.exists?(user1.uid))
101
+ assert(@user_class.exists?(user2.uid))
102
+ assert(@user_class.exists?(user3.uid))
103
+ @user_class.delete([user1.dn, user2.uid, "uid=#{user3.uid}"])
104
+ assert(!@user_class.exists?(user1.uid))
105
+ assert(!@user_class.exists?(user2.uid))
106
+ assert(!@user_class.exists?(user3.uid))
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
112
+
113
+ class TestInstance < self
114
+ def test_existence
115
+ make_temporary_user do |user,|
116
+ assert(@user_class.exists?(user.uid))
117
+ user.delete
118
+ assert(!@user_class.exists?(user.uid))
119
+ end
120
+ end
121
+
122
+ def test_frozen
123
+ make_temporary_user do |user,|
124
+ assert_false(user.frozen?)
125
+ user.delete
126
+ assert_true(user.frozen?)
127
+ end
128
+ end
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,175 @@
1
+ require 'al-test-utils'
2
+
3
+ class TestReflection < Test::Unit::TestCase
4
+ include AlTestUtils
5
+
6
+ priority :must
7
+
8
+ priority :normal
9
+ def test_base_class
10
+ assert_equal(ActiveLdap::Base, ActiveLdap::Base.base_class)
11
+ assert_equal(@user_class, @user_class.base_class)
12
+ sub_user_class = Class.new(@user_class)
13
+ assert_equal(@user_class, sub_user_class.base_class)
14
+ end
15
+
16
+ def test_respond_to?
17
+ make_temporary_user do |user, password|
18
+ attributes = (user.must + user.may).collect(&:name) - ["objectClass"]
19
+ _wrap_assertion do
20
+ attributes.each do |name|
21
+ assert_respond_to(user, name)
22
+ end
23
+ assert_not_respond_to(user, "objectClass")
24
+ end
25
+
26
+ user.replace_class(user.class.required_classes)
27
+ new_attributes = collect_attributes(user.class.required_classes)
28
+ new_attributes -= ["objectClass"]
29
+
30
+ _wrap_assertion do
31
+ assert_not_equal([], new_attributes)
32
+ new_attributes.each do |name|
33
+ assert_respond_to(user, name)
34
+ end
35
+
36
+ remained_attributes = (attributes - new_attributes)
37
+ assert_not_equal([], remained_attributes)
38
+ remained_attributes.each do |name|
39
+ assert_not_respond_to(user, name)
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ def test_methods
46
+ make_temporary_user do |user, password|
47
+ assert_equal(user.methods.uniq.size, user.methods.size)
48
+ assert_equal(user.methods(false).uniq.size, user.methods(false).size)
49
+ end
50
+
51
+ make_temporary_user do |user, password|
52
+ attributes = user.must.collect(&:name) + user.may.collect(&:name)
53
+ attributes = (attributes - ["objectClass"]).map(&:to_sym)
54
+ assert_equal([], attributes - user.methods)
55
+
56
+ assert_methods_with_only_required_classes(user, attributes)
57
+ end
58
+
59
+ make_temporary_user do |user, password|
60
+ user.remove_class("inetOrgPerson")
61
+ attributes = user.must.collect(&:name) + user.may.collect(&:name)
62
+ attributes = (attributes - ["objectClass"]).map(&:to_sym)
63
+ assert_equal([], attributes - user.methods)
64
+
65
+ assert_methods_with_only_required_classes(user, attributes)
66
+ end
67
+
68
+ make_temporary_user do |user, password|
69
+ attributes = user.must.collect(&:name) + user.may.collect(&:name)
70
+ attributes = attributes.map(&:downcase).map(&:to_sym)
71
+ assert_not_equal([], attributes - user.methods)
72
+ assert_not_equal([], attributes - user.methods(false))
73
+
74
+ normalize_attributes_list = Proc.new do |*attributes_list|
75
+ attributes_list.collect do |attrs|
76
+ attrs.collect {|x| x.downcase}
77
+ end
78
+ end
79
+ assert_methods_with_only_required_classes(user, attributes,
80
+ &normalize_attributes_list)
81
+ end
82
+
83
+ make_temporary_user do |user, password|
84
+ attributes = user.must.collect(&:name) + user.may.collect(&:name)
85
+ attributes -= ["objectClass"]
86
+ attributes = attributes.collect(&:underscore).map(&:to_sym)
87
+ assert_equal([], attributes - user.methods)
88
+
89
+ normalize_attributes_list = Proc.new do |*attributes_list|
90
+ attributes_list.collect do |attrs|
91
+ attrs.collect(&:underscore)
92
+ end
93
+ end
94
+ assert_methods_with_only_required_classes(user, attributes,
95
+ &normalize_attributes_list)
96
+ end
97
+
98
+ make_temporary_user do |user, password|
99
+ user.remove_class("inetOrgPerson")
100
+ attributes = user.must.collect(&:name) + user.may.collect(&:name)
101
+ attributes -= ["objectClass"]
102
+ attributes = attributes.collect(&:underscore).map(&:to_sym)
103
+ assert_equal([], attributes - user.methods)
104
+
105
+ normalize_attributes_list = Proc.new do |*attributes_list|
106
+ attributes_list.collect do |attrs|
107
+ attrs.collect(&:underscore)
108
+ end
109
+ end
110
+ assert_methods_with_only_required_classes(user, attributes,
111
+ &normalize_attributes_list)
112
+ end
113
+ end
114
+
115
+ def test_attribute_names
116
+ make_temporary_user do |user, password|
117
+ attributes = collect_attributes(user.classes)
118
+ assert_equal([], attributes.uniq - user.attribute_names)
119
+ assert_equal([], user.attribute_names - attributes.uniq)
120
+ end
121
+ end
122
+
123
+ private
124
+ def assert_methods_with_only_required_classes(object, attributes)
125
+ old_classes = (object.classes - object.class.required_classes).uniq
126
+ old_attributes = collect_attributes(old_classes, false).uniq.sort
127
+ required_attributes = collect_attributes(object.class.required_classes,
128
+ false).uniq.sort
129
+ if block_given?
130
+ old_attributes, required_attributes =
131
+ yield(old_attributes, required_attributes)
132
+ end
133
+
134
+ [old_attributes, required_attributes].map{|a| a.map!(&:to_sym)}
135
+
136
+ object.replace_class(object.class.required_classes)
137
+
138
+ assert_equal([],
139
+ old_attributes -
140
+ (attributes - object.methods - required_attributes) -
141
+ required_attributes)
142
+ end
143
+
144
+ def assert_respond_to(object, name)
145
+ assert_true(object.respond_to?(name), name)
146
+ assert_true(object.respond_to?("#{name}="), "#{name}=")
147
+ assert_true(object.respond_to?("#{name}?"), "#{name}?")
148
+ assert_true(object.respond_to?("#{name}_before_type_cast"),
149
+ "#{name}_before_type_cast")
150
+ end
151
+
152
+ def assert_not_respond_to(object, name)
153
+ assert_false(object.respond_to?(name), name)
154
+ assert_false(object.respond_to?("#{name}="), "#{name}=")
155
+ assert_false(object.respond_to?("#{name}?"), "#{name}?")
156
+ assert_false(object.respond_to?("#{name}_before_type_cast"),
157
+ "#{name}_before_type_cast")
158
+ end
159
+
160
+ def collect_attributes(object_classes, with_aliases=true)
161
+ attributes = []
162
+ object_classes.each do |object_class|
163
+ object_klass = ActiveLdap::Base.schema.object_class(object_class)
164
+ if with_aliases
165
+ (object_klass.must + object_klass.may).each do |attribute|
166
+ attributes << attribute.name
167
+ attributes.concat(attribute.aliases)
168
+ end
169
+ else
170
+ attributes.concat((object_klass.must + object_klass.may).collect(&:name))
171
+ end
172
+ end
173
+ attributes
174
+ end
175
+ end