xn_devise_ldap_authenticatable 0.8.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/CHANGELOG.md +7 -0
  4. data/Gemfile +8 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.md +143 -0
  7. data/Rakefile +16 -0
  8. data/lib/devise_ldap_authenticatable.rb +49 -0
  9. data/lib/devise_ldap_authenticatable/exception.rb +6 -0
  10. data/lib/devise_ldap_authenticatable/ldap/adapter.rb +87 -0
  11. data/lib/devise_ldap_authenticatable/ldap/connection.rb +243 -0
  12. data/lib/devise_ldap_authenticatable/logger.rb +11 -0
  13. data/lib/devise_ldap_authenticatable/model.rb +120 -0
  14. data/lib/devise_ldap_authenticatable/strategy.rb +39 -0
  15. data/lib/devise_ldap_authenticatable/version.rb +3 -0
  16. data/lib/generators/devise_ldap_authenticatable/install_generator.rb +63 -0
  17. data/lib/generators/devise_ldap_authenticatable/templates/ldap.yml +55 -0
  18. data/spec/ldap/.gitignore +2 -0
  19. data/spec/ldap/base.ldif +73 -0
  20. data/spec/ldap/clear.ldif +26 -0
  21. data/spec/ldap/local.schema +6 -0
  22. data/spec/ldap/openldap-data/.gitignore +2 -0
  23. data/spec/ldap/openldap-data/run/.gitignore +2 -0
  24. data/spec/ldap/openldap-data/run/.gitkeep +0 -0
  25. data/spec/ldap/run-server +31 -0
  26. data/spec/ldap/server.pem +38 -0
  27. data/spec/ldap/slapd-test.conf.erb +107 -0
  28. data/spec/rails_app/Rakefile +7 -0
  29. data/spec/rails_app/app/controllers/application_controller.rb +7 -0
  30. data/spec/rails_app/app/controllers/posts_controller.rb +15 -0
  31. data/spec/rails_app/app/helpers/application_helper.rb +2 -0
  32. data/spec/rails_app/app/helpers/posts_helper.rb +2 -0
  33. data/spec/rails_app/app/models/post.rb +2 -0
  34. data/spec/rails_app/app/models/user.rb +7 -0
  35. data/spec/rails_app/app/views/layouts/application.html.erb +26 -0
  36. data/spec/rails_app/app/views/posts/index.html.erb +2 -0
  37. data/spec/rails_app/config.ru +4 -0
  38. data/spec/rails_app/config/application.rb +46 -0
  39. data/spec/rails_app/config/boot.rb +13 -0
  40. data/spec/rails_app/config/cucumber.yml +8 -0
  41. data/spec/rails_app/config/database.yml +25 -0
  42. data/spec/rails_app/config/environment.rb +5 -0
  43. data/spec/rails_app/config/environments/development.rb +21 -0
  44. data/spec/rails_app/config/environments/production.rb +46 -0
  45. data/spec/rails_app/config/environments/test.rb +34 -0
  46. data/spec/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  47. data/spec/rails_app/config/initializers/devise.rb +247 -0
  48. data/spec/rails_app/config/initializers/inflections.rb +10 -0
  49. data/spec/rails_app/config/initializers/mime_types.rb +5 -0
  50. data/spec/rails_app/config/initializers/secret_token.rb +7 -0
  51. data/spec/rails_app/config/initializers/session_store.rb +8 -0
  52. data/spec/rails_app/config/ldap.yml +22 -0
  53. data/spec/rails_app/config/ldap_with_boolean_ssl.yml +22 -0
  54. data/spec/rails_app/config/ldap_with_erb.yml +23 -0
  55. data/spec/rails_app/config/ldap_with_uid.yml +18 -0
  56. data/spec/rails_app/config/locales/devise.en.yml +59 -0
  57. data/spec/rails_app/config/locales/en.yml +5 -0
  58. data/spec/rails_app/config/routes.rb +64 -0
  59. data/spec/rails_app/config/ssl_ldap.yml +21 -0
  60. data/spec/rails_app/config/ssl_ldap_with_erb.yml +23 -0
  61. data/spec/rails_app/config/ssl_ldap_with_uid.yml +18 -0
  62. data/spec/rails_app/db/migrate/20100708120448_devise_create_users.rb +40 -0
  63. data/spec/rails_app/db/schema.rb +35 -0
  64. data/spec/rails_app/features/manage_logins.feature +35 -0
  65. data/spec/rails_app/features/step_definitions/login_steps.rb +21 -0
  66. data/spec/rails_app/features/step_definitions/web_steps.rb +219 -0
  67. data/spec/rails_app/features/support/env.rb +58 -0
  68. data/spec/rails_app/features/support/paths.rb +38 -0
  69. data/spec/rails_app/lib/tasks/.gitkeep +0 -0
  70. data/spec/rails_app/lib/tasks/cucumber.rake +53 -0
  71. data/spec/rails_app/public/404.html +26 -0
  72. data/spec/rails_app/public/422.html +26 -0
  73. data/spec/rails_app/public/500.html +26 -0
  74. data/spec/rails_app/public/images/rails.png +0 -0
  75. data/spec/rails_app/public/javascripts/application.js +2 -0
  76. data/spec/rails_app/public/javascripts/controls.js +965 -0
  77. data/spec/rails_app/public/javascripts/dragdrop.js +974 -0
  78. data/spec/rails_app/public/javascripts/effects.js +1123 -0
  79. data/spec/rails_app/public/javascripts/prototype.js +4874 -0
  80. data/spec/rails_app/public/javascripts/rails.js +118 -0
  81. data/spec/rails_app/public/stylesheets/.gitkeep +0 -0
  82. data/spec/rails_app/script/cucumber +10 -0
  83. data/spec/rails_app/script/rails +6 -0
  84. data/spec/spec_helper.rb +55 -0
  85. data/spec/support/factories.rb +16 -0
  86. data/spec/unit/connection_spec.rb +14 -0
  87. data/spec/unit/user_spec.rb +331 -0
  88. data/xn_devise_ldap_authenticatable.gemspec +36 -0
  89. metadata +363 -0
@@ -0,0 +1,118 @@
1
+ document.observe("dom:loaded", function() {
2
+ function handleRemote(element) {
3
+ var method, url, params;
4
+
5
+ if (element.tagName.toLowerCase() === 'form') {
6
+ method = element.readAttribute('method') || 'post';
7
+ url = element.readAttribute('action');
8
+ params = element.serialize(true);
9
+ } else {
10
+ method = element.readAttribute('data-method') || 'get';
11
+ url = element.readAttribute('href');
12
+ params = {};
13
+ }
14
+
15
+ var event = element.fire("ajax:before");
16
+ if (event.stopped) return false;
17
+
18
+ new Ajax.Request(url, {
19
+ method: method,
20
+ parameters: params,
21
+ asynchronous: true,
22
+ evalScripts: true,
23
+
24
+ onLoading: function(request) { element.fire("ajax:loading", {request: request}); },
25
+ onLoaded: function(request) { element.fire("ajax:loaded", {request: request}); },
26
+ onInteractive: function(request) { element.fire("ajax:interactive", {request: request}); },
27
+ onComplete: function(request) { element.fire("ajax:complete", {request: request}); },
28
+ onSuccess: function(request) { element.fire("ajax:success", {request: request}); },
29
+ onFailure: function(request) { element.fire("ajax:failure", {request: request}); }
30
+ });
31
+
32
+ element.fire("ajax:after");
33
+ }
34
+
35
+ function handleMethod(element) {
36
+ var method, url, token_name, token;
37
+
38
+ method = element.readAttribute('data-method');
39
+ url = element.readAttribute('href');
40
+ csrf_param = $$('meta[name=csrf-param]').first();
41
+ csrf_token = $$('meta[name=csrf-token]').first();
42
+
43
+ var form = new Element('form', { method: "POST", action: url, style: "display: none;" });
44
+ element.parentNode.appendChild(form);
45
+
46
+ if (method != 'post') {
47
+ var field = new Element('input', { type: 'hidden', name: '_method', value: method });
48
+ form.appendChild(field);
49
+ }
50
+
51
+ if (csrf_param) {
52
+ var param = csrf_param.readAttribute('content');
53
+ var token = csrf_token.readAttribute('content');
54
+ var field = new Element('input', { type: 'hidden', name: param, value: token });
55
+ form.appendChild(field);
56
+ }
57
+
58
+ form.submit();
59
+ }
60
+
61
+ $(document.body).observe("click", function(event) {
62
+ var message = event.findElement().readAttribute('data-confirm');
63
+ if (message && !confirm(message)) {
64
+ event.stop();
65
+ return false;
66
+ }
67
+
68
+ var element = event.findElement("a[data-remote]");
69
+ if (element) {
70
+ handleRemote(element);
71
+ event.stop();
72
+ return true;
73
+ }
74
+
75
+ var element = event.findElement("a[data-method]");
76
+ if (element) {
77
+ handleMethod(element);
78
+ event.stop();
79
+ return true;
80
+ }
81
+ });
82
+
83
+ // TODO: I don't think submit bubbles in IE
84
+ $(document.body).observe("submit", function(event) {
85
+ var element = event.findElement(),
86
+ message = element.readAttribute('data-confirm');
87
+ if (message && !confirm(message)) {
88
+ event.stop();
89
+ return false;
90
+ }
91
+
92
+ var inputs = element.select("input[type=submit][data-disable-with]");
93
+ inputs.each(function(input) {
94
+ input.disabled = true;
95
+ input.writeAttribute('data-original-value', input.value);
96
+ input.value = input.readAttribute('data-disable-with');
97
+ });
98
+
99
+ var element = event.findElement("form[data-remote]");
100
+ if (element) {
101
+ handleRemote(element);
102
+ event.stop();
103
+ }
104
+ });
105
+
106
+ $(document.body).observe("ajax:after", function(event) {
107
+ var element = event.findElement();
108
+
109
+ if (element.tagName.toLowerCase() === 'form') {
110
+ var inputs = element.select("input[type=submit][disabled=true][data-disable-with]");
111
+ inputs.each(function(input) {
112
+ input.value = input.readAttribute('data-original-value');
113
+ input.writeAttribute('data-original-value', null);
114
+ input.disabled = false;
115
+ });
116
+ }
117
+ });
118
+ });
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ vendored_cucumber_bin = Dir["#{File.dirname(__FILE__)}/../vendor/{gems,plugins}/cucumber*/bin/cucumber"].first
4
+ if vendored_cucumber_bin
5
+ load File.expand_path(vendored_cucumber_bin)
6
+ else
7
+ require 'rubygems' unless ENV['NO_RUBYGEMS']
8
+ require 'cucumber'
9
+ load Cucumber::BINARY
10
+ end
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
@@ -0,0 +1,55 @@
1
+ ENV["RAILS_ENV"] = "test"
2
+
3
+ require File.expand_path("rails_app/config/environment.rb", File.dirname(__FILE__))
4
+ require 'rspec/rails'
5
+ require 'rspec/autorun'
6
+ require 'factory_girl' # not sure why this is not already required
7
+
8
+ # Rails 4.1 and RSpec are a bit on different pages on who should run migrations
9
+ # on the test db and when.
10
+ #
11
+ # https://github.com/rspec/rspec-rails/issues/936
12
+ if defined?(ActiveRecord::Migration) && ActiveRecord::Migration.respond_to?(:maintain_test_schema!)
13
+ ActiveRecord::Migration.maintain_test_schema!
14
+ end
15
+
16
+ Dir[File.expand_path("support/**/*.rb", File.dirname(__FILE__))].each {|f| require f}
17
+
18
+ RSpec.configure do |config|
19
+ config.mock_with :rspec
20
+ config.use_transactional_fixtures = true
21
+ config.infer_base_class_for_anonymous_controllers = false
22
+ end
23
+
24
+ def ldap_root
25
+ File.expand_path('ldap', File.dirname(__FILE__))
26
+ end
27
+
28
+ def ldap_connect_string
29
+ if ENV["LDAP_SSL"]
30
+ "-x -H ldaps://localhost:3389 -D 'cn=admin,dc=test,dc=com' -w secret"
31
+ else
32
+ "-x -h localhost -p 3389 -D 'cn=admin,dc=test,dc=com' -w secret"
33
+ end
34
+ end
35
+
36
+ def reset_ldap_server!
37
+ if ENV["LDAP_SSL"]
38
+ `ldapmodify #{ldap_connect_string} -f #{File.join(ldap_root, 'clear.ldif')}`
39
+ `ldapadd #{ldap_connect_string} -f #{File.join(ldap_root, 'base.ldif')}`
40
+ else
41
+ `ldapmodify #{ldap_connect_string} -f #{File.join(ldap_root, 'clear.ldif')}`
42
+ `ldapadd #{ldap_connect_string} -f #{File.join(ldap_root, 'base.ldif')}`
43
+ end
44
+ end
45
+
46
+ def default_devise_settings!
47
+ ::Devise.ldap_logger = true
48
+ ::Devise.ldap_create_user = false
49
+ ::Devise.ldap_update_password = true
50
+ ::Devise.ldap_config = "#{Rails.root}/config/#{"ssl_" if ENV["LDAP_SSL"]}ldap.yml"
51
+ ::Devise.ldap_check_group_membership = false
52
+ ::Devise.ldap_check_attributes = false
53
+ ::Devise.ldap_auth_username_builder = Proc.new() {|attribute, login, ldap| "#{attribute}=#{login},#{ldap.base}" }
54
+ ::Devise.authentication_keys = [:email]
55
+ end
@@ -0,0 +1,16 @@
1
+ FactoryGirl.define do
2
+ factory :user do
3
+ email "example.user@test.com"
4
+ password "secret"
5
+ end
6
+
7
+ factory :admin, :class => User do
8
+ email "example.admin@test.com"
9
+ password "admin_secret"
10
+ end
11
+
12
+ factory :other, :class => User do
13
+ email "other.user@test.com"
14
+ password "other_secret"
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ require File.expand_path('../spec_helper', File.dirname(__FILE__))
2
+
3
+ describe 'Connection' do
4
+ it 'accepts a proc for ldap_config' do
5
+ ::Devise.ldap_config = Proc.new() {{
6
+ 'host' => 'localhost',
7
+ 'port' => 3389,
8
+ 'base' => 'ou=testbase,dc=test,dc=com',
9
+ 'attribute' => 'cn',
10
+ }}
11
+ connection = Devise::LDAP::Connection.new()
12
+ expect(connection.ldap.base).to eq('ou=testbase,dc=test,dc=com')
13
+ end
14
+ end
@@ -0,0 +1,331 @@
1
+ require File.expand_path('../spec_helper', File.dirname(__FILE__))
2
+
3
+ describe 'Users' do
4
+
5
+ def should_be_validated(user, password, message = "Password is invalid")
6
+ assert(user.valid_ldap_authentication?(password), message)
7
+ end
8
+
9
+ def should_not_be_validated(user, password, message = "Password is not properly set")
10
+ assert(!user.valid_ldap_authentication?(password), message)
11
+ end
12
+
13
+ describe "With default settings" do
14
+ before do
15
+ default_devise_settings!
16
+ reset_ldap_server!
17
+ end
18
+
19
+ describe "look up and ldap user" do
20
+ it "should return true for a user that does exist in LDAP" do
21
+ assert_equal true, ::Devise::LDAP::Adapter.valid_login?('example.user@test.com')
22
+ end
23
+
24
+ it "should return false for a user that doesn't exist in LDAP" do
25
+ assert_equal false, ::Devise::LDAP::Adapter.valid_login?('barneystinson')
26
+ end
27
+ end
28
+
29
+ describe "create a basic user" do
30
+ before do
31
+ @user = Factory.create(:user)
32
+ end
33
+
34
+ it "should check for password validation" do
35
+ assert_equal(@user.email, "example.user@test.com")
36
+ should_be_validated @user, "secret"
37
+ should_not_be_validated @user, "wrong_secret"
38
+ should_not_be_validated @user, "Secret"
39
+ end
40
+ end
41
+
42
+ describe "change a LDAP password" do
43
+ before do
44
+ @user = Factory.create(:user)
45
+ end
46
+
47
+ it "should change password" do
48
+ should_be_validated @user, "secret"
49
+ @user.password = "changed"
50
+ @user.change_password!("secret")
51
+ should_be_validated @user, "changed", "password was not changed properly on the LDAP sevrer"
52
+ end
53
+
54
+ it "should not allow to change password if setting is false" do
55
+ should_be_validated @user, "secret"
56
+ ::Devise.ldap_update_password = false
57
+ @user.reset_password!("wrong_secret", "wrong_secret")
58
+ should_not_be_validated @user, "wrong_secret"
59
+ should_be_validated @user, "secret"
60
+ end
61
+ end
62
+
63
+ describe "create new local user if user is in LDAP" do
64
+
65
+ before do
66
+ assert(User.all.blank?, "There shouldn't be any users in the database")
67
+ end
68
+
69
+ it "should not create user in the database" do
70
+ @user = User.find_for_ldap_authentication(:email => "example.user@test.com", :password => "secret")
71
+ assert(User.all.blank?)
72
+ assert(@user.new_record?)
73
+ end
74
+
75
+ describe "creating users is enabled" do
76
+ before do
77
+ ::Devise.ldap_create_user = true
78
+ end
79
+
80
+ it "should create a user in the database" do
81
+ @user = User.find_for_ldap_authentication(:email => "example.user@test.com", :password => "secret")
82
+ assert_equal(User.all.size, 1)
83
+ User.all.collect(&:email).should include("example.user@test.com")
84
+ assert(@user.persisted?)
85
+ end
86
+
87
+ it "should not create a user in the database if the password is wrong_secret" do
88
+ @user = User.find_for_ldap_authentication(:email => "example.user", :password => "wrong_secret")
89
+ assert(User.all.blank?, "There's users in the database")
90
+ end
91
+
92
+ it "should not create a user if the user is not in LDAP" do
93
+ @user = User.find_for_ldap_authentication(:email => "wrong_secret.user@test.com", :password => "wrong_secret")
94
+ assert(User.all.blank?, "There's users in the database")
95
+ end
96
+
97
+ it "should create a user in the database if case insensitivity does not matter" do
98
+ ::Devise.case_insensitive_keys = []
99
+ @user = Factory.create(:user)
100
+
101
+ expect do
102
+ User.find_for_ldap_authentication(:email => "EXAMPLE.user@test.com", :password => "secret")
103
+ end.to change { User.count }.by(1)
104
+ end
105
+
106
+ it "should not create a user in the database if case insensitivity matters" do
107
+ ::Devise.case_insensitive_keys = [:email]
108
+ @user = Factory.create(:user)
109
+
110
+ expect do
111
+ User.find_for_ldap_authentication(:email => "EXAMPLE.user@test.com", :password => "secret")
112
+ end.to_not change { User.count }
113
+ end
114
+
115
+ it "should create a user with downcased email in the database if case insensitivity matters" do
116
+ ::Devise.case_insensitive_keys = [:email]
117
+
118
+ @user = User.find_for_ldap_authentication(:email => "EXAMPLE.user@test.com", :password => "secret")
119
+ User.all.collect(&:email).should include("example.user@test.com")
120
+ end
121
+ end
122
+
123
+ end
124
+
125
+ describe "use groups for authorization" do
126
+ before do
127
+ @admin = Factory.create(:admin)
128
+ @user = Factory.create(:user)
129
+ ::Devise.authentication_keys = [:email]
130
+ ::Devise.ldap_check_group_membership = true
131
+ end
132
+
133
+ it "should admin should be allowed in" do
134
+ should_be_validated @admin, "admin_secret"
135
+ end
136
+
137
+ it "should admin should have the proper groups set" do
138
+ @admin.ldap_groups.should include('cn=admins,ou=groups,dc=test,dc=com')
139
+ end
140
+
141
+ it "should user should not be allowed in" do
142
+ should_not_be_validated @user, "secret"
143
+ end
144
+ end
145
+
146
+ describe "check group membership" do
147
+ before do
148
+ @admin = Factory.create(:admin)
149
+ @user = Factory.create(:user)
150
+ end
151
+
152
+ it "should return true for admin being in the admins group" do
153
+ assert_equal true, @admin.in_ldap_group?('cn=admins,ou=groups,dc=test,dc=com')
154
+ end
155
+
156
+ it "should return false for admin being in the admins group using the 'foobar' group attribute" do
157
+ assert_equal false, @admin.in_ldap_group?('cn=admins,ou=groups,dc=test,dc=com', 'foobar')
158
+ end
159
+
160
+ it "should return true for user being in the users group" do
161
+ assert_equal true, @user.in_ldap_group?('cn=users,ou=groups,dc=test,dc=com')
162
+ end
163
+
164
+ it "should return false for user being in the admins group" do
165
+ assert_equal false, @user.in_ldap_group?('cn=admins,ou=groups,dc=test,dc=com')
166
+ end
167
+
168
+ it "should return false for a user being in a nonexistent group" do
169
+ assert_equal false, @user.in_ldap_group?('cn=thisgroupdoesnotexist,ou=groups,dc=test,dc=com')
170
+ end
171
+ end
172
+
173
+ describe "check group membership w/out admin bind" do
174
+ before do
175
+ @user = Factory.create(:user)
176
+ ::Devise.ldap_check_group_membership_without_admin = true
177
+ end
178
+
179
+ after do
180
+ ::Devise.ldap_check_group_membership_without_admin = false
181
+ end
182
+
183
+ it "should return true for user being in the users group" do
184
+ assert_equal true, @user.in_ldap_group?('cn=users,ou=groups,dc=test,dc=com')
185
+ end
186
+
187
+ it "should return false for user being in the admins group" do
188
+ assert_equal false, @user.in_ldap_group?('cn=admins,ou=groups,dc=test,dc=com')
189
+ end
190
+
191
+ it "should return false for a user being in a nonexistent group" do
192
+ assert_equal false, @user.in_ldap_group?('cn=thisgroupdoesnotexist,ou=groups,dc=test,dc=com')
193
+ end
194
+
195
+ # TODO: add a test that confirms the user's own binding is used rather
196
+ # than the admin binding by creating an LDAP user who can't do group
197
+ # lookups perhaps?
198
+
199
+ # TODO: add a test to demonstrate this function won't work on a user
200
+ # after the initial login request if the password isn't available. This
201
+ # might have to be more of a full stack test.
202
+ end
203
+
204
+ describe "use role attribute for authorization" do
205
+ before do
206
+ @admin = Factory.create(:admin)
207
+ @user = Factory.create(:user)
208
+ ::Devise.ldap_check_attributes = true
209
+ end
210
+
211
+ it "should admin should be allowed in" do
212
+ should_be_validated @admin, "admin_secret"
213
+ end
214
+
215
+ it "should user should not be allowed in" do
216
+ should_not_be_validated @user, "secret"
217
+ end
218
+ end
219
+
220
+ describe "use admin setting to bind" do
221
+ before do
222
+ @admin = Factory.create(:admin)
223
+ @user = Factory.create(:user)
224
+ ::Devise.ldap_use_admin_to_bind = true
225
+ end
226
+
227
+ it "should description" do
228
+ should_be_validated @admin, "admin_secret"
229
+ end
230
+ end
231
+
232
+ end
233
+
234
+ describe "use uid for login" do
235
+ before do
236
+ default_devise_settings!
237
+ reset_ldap_server!
238
+ ::Devise.ldap_config = "#{Rails.root}/config/#{"ssl_" if ENV["LDAP_SSL"]}ldap_with_uid.yml"
239
+ ::Devise.authentication_keys = [:uid]
240
+ end
241
+
242
+ describe "description" do
243
+ before do
244
+ @admin = Factory.create(:admin)
245
+ @user = Factory.create(:user, :uid => "example_user")
246
+ end
247
+
248
+ it "should be able to authenticate using uid" do
249
+ should_be_validated @user, "secret"
250
+ should_not_be_validated @admin, "admin_secret"
251
+ end
252
+ end
253
+
254
+ describe "create user" do
255
+ before do
256
+ ::Devise.ldap_create_user = true
257
+ end
258
+
259
+ it "should create a user in the database" do
260
+ @user = User.find_for_ldap_authentication(:uid => "example_user", :password => "secret")
261
+ assert_equal(User.all.size, 1)
262
+ User.all.collect(&:uid).should include("example_user")
263
+ end
264
+
265
+ it "should call ldap_before_save hooks" do
266
+ User.class_eval do
267
+ def ldap_before_save
268
+ @foobar = 'foobar'
269
+ end
270
+ end
271
+ user = User.find_for_ldap_authentication(:uid => "example_user", :password => "secret")
272
+ assert_equal 'foobar', user.instance_variable_get(:"@foobar")
273
+ User.class_eval do
274
+ undef ldap_before_save
275
+ end
276
+ end
277
+
278
+ it "should not call ldap_before_save hook if not defined" do
279
+ should_be_validated Factory.create(:user, :uid => "example_user"), "secret"
280
+ end
281
+ end
282
+ end
283
+
284
+ describe "using ERB in the config file" do
285
+ before do
286
+ default_devise_settings!
287
+ reset_ldap_server!
288
+ ::Devise.ldap_config = "#{Rails.root}/config/#{"ssl_" if ENV["LDAP_SSL"]}ldap_with_erb.yml"
289
+ end
290
+
291
+ describe "authenticate" do
292
+ before do
293
+ @admin = Factory.create(:admin)
294
+ @user = Factory.create(:user)
295
+ end
296
+
297
+ it "should be able to authenticate" do
298
+ should_be_validated @user, "secret"
299
+ should_be_validated @admin, "admin_secret"
300
+ end
301
+ end
302
+ end
303
+
304
+ describe "using variants in the config file" do
305
+ before do
306
+ default_devise_settings!
307
+ reset_ldap_server!
308
+ ::Devise.ldap_config = Rails.root.join 'config', 'ldap_with_boolean_ssl.yml'
309
+ end
310
+
311
+ it "should not fail if config file has ssl: true" do
312
+ Devise::LDAP::Connection.new
313
+ end
314
+ end
315
+
316
+ describe "use username builder" do
317
+ before do
318
+ default_devise_settings!
319
+ reset_ldap_server!
320
+ ::Devise.ldap_auth_username_builder = Proc.new() do |attribute, login, ldap|
321
+ "#{attribute}=#{login},ou=others,dc=test,dc=com"
322
+ end
323
+ @other = Factory.create(:other)
324
+ end
325
+
326
+ it "should be able to authenticate" do
327
+ should_be_validated @other, "other_secret"
328
+ end
329
+ end
330
+
331
+ end