1
- require 'test_helper '
1
+ require 'spec_helper '
2
2
3
- class UserTest < ActiveSupport :: TestCase
3
+ describe 'Users' do
4
4
5
5
def should_be_validated ( user , password , message = "Password is invalid" )
6
6
assert ( user . valid_ldap_authentication? ( password ) , message )
@@ -10,47 +10,47 @@ def should_not_be_validated(user, password, message = "Password is not properly
10
10
assert ( !user . valid_ldap_authentication? ( password ) , message )
11
11
end
12
12
13
- context "With default settings" do
14
- setup do
13
+ describe "With default settings" do
14
+ before do
15
15
default_devise_settings!
16
16
reset_ldap_server!
17
17
end
18
18
19
- context "look up and ldap user" do
20
- should " return true for a user that does exist in LDAP" do
19
+ describe "look up and ldap user" do
20
+ it "should return true for a user that does exist in LDAP" do
21
21
assert_equal true , ::
Devise ::
LdapAdapter . valid_login? ( '[email protected] ' )
22
22
end
23
23
24
- should " return false for a user that doesn't exist in LDAP" do
24
+ it "should return false for a user that doesn't exist in LDAP" do
25
25
assert_equal false , ::Devise ::LdapAdapter . valid_login? ( 'barneystinson' )
26
26
end
27
27
end
28
28
29
- context "create a basic user" do
30
- setup do
31
- @user = Factory ( :user )
29
+ describe "create a basic user" do
30
+ before do
31
+ @user = Factory . create ( :user )
32
32
end
33
33
34
- should " check for password validation" do
34
+ it "should check for password validation" do
35
35
assert_equal ( @user . email , "[email protected] " )
36
36
should_be_validated @user , "secret"
37
37
should_not_be_validated @user , "wrong_secret"
38
38
should_not_be_validated @user , "Secret"
39
39
end
40
40
end
41
41
42
- context "change a LDAP password" do
43
- setup do
44
- @user = Factory ( :user )
42
+ describe "change a LDAP password" do
43
+ before do
44
+ @user = Factory . create ( :user )
45
45
end
46
46
47
- should " change password" do
47
+ it "should change password" do
48
48
should_be_validated @user , "secret"
49
49
@user . reset_password! ( "changed" , "changed" )
50
50
should_be_validated @user , "changed" , "password was not changed properly on the LDAP sevrer"
51
51
end
52
52
53
- should " not allow to change password if setting is false" do
53
+ it "should not allow to change password if setting is false" do
54
54
should_be_validated @user , "secret"
55
55
::Devise . ldap_update_password = false
56
56
@user . reset_password! ( "wrong_secret" , "wrong_secret" )
@@ -59,57 +59,57 @@ def should_not_be_validated(user, password, message = "Password is not properly
59
59
end
60
60
end
61
61
62
- context "create new local user if user is in LDAP" do
62
+ describe "create new local user if user is in LDAP" do
63
63
64
- setup do
64
+ before do
65
65
assert ( User . all . blank? , "There shouldn't be any users in the database" )
66
66
end
67
67
68
- should " don't create user in the database" do
68
+ it "should don't create user in the database" do
69
69
@user = User . authenticate_with_ldap ( :email => "[email protected] " , :password => "secret" )
70
70
assert ( User . all . blank? )
71
71
end
72
72
73
- context "creating users is enabled" do
74
- setup do
73
+ describe "creating users is enabled" do
74
+ before do
75
75
::Devise . ldap_create_user = true
76
76
end
77
77
78
- should " create a user in the database" do
78
+ it "should create a user in the database" do
79
79
@user = User . authenticate_with_ldap ( :email => "[email protected] " , :password => "secret" )
80
80
assert_equal ( User . all . size , 1 )
81
81
assert_contains ( User . all . collect ( &
:email ) , "[email protected] " , "user not in database" )
82
82
end
83
83
84
- should " not create a user in the database if the password is wrong_secret" do
84
+ it "should not create a user in the database if the password is wrong_secret" do
85
85
@user = User . authenticate_with_ldap ( :email => "example.user" , :password => "wrong_secret" )
86
86
assert ( User . all . blank? , "There's users in the database" )
87
87
end
88
88
89
- should " create a user if the user is not in LDAP" do
89
+ it "should create a user if the user is not in LDAP" do
90
90
@user = User . authenticate_with_ldap ( :email => "[email protected] " , :password => "wrong_secret" )
91
91
assert ( User . all . blank? , "There's users in the database" )
92
92
end
93
93
94
- should " create a user in the database if case insensitivity does not matter" do
94
+ it "should create a user in the database if case insensitivity does not matter" do
95
95
::Devise . case_insensitive_keys = false
96
- @user = Factory ( :user )
96
+ @user = Factory . create ( :user )
97
97
98
98
assert_difference "User.count" , +1 do
99
99
User . authenticate_with_ldap ( :email => "[email protected] " , :password => "secret" )
100
100
end
101
101
end
102
102
103
- should " not create a user in the database if case insensitivity matters" do
103
+ it "should not create a user in the database if case insensitivity matters" do
104
104
::Devise . case_insensitive_keys = [ :email ]
105
- @user = Factory ( :user )
105
+ @user = Factory . create ( :user )
106
106
107
107
assert_no_difference "User.count" do
108
108
User . authenticate_with_ldap ( :email => "[email protected] " , :password => "secret" )
109
109
end
110
110
end
111
111
112
- should " create a user with downcased email in the database if case insensitivity matters" do
112
+ it "should create a user with downcased email in the database if case insensitivity matters" do
113
113
::Devise . case_insensitive_keys = [ :email ]
114
114
115
115
@user = User . authenticate_with_ldap ( :email => "[email protected] " , :password => "secret" )
@@ -119,94 +119,94 @@ def should_not_be_validated(user, password, message = "Password is not properly
119
119
120
120
end
121
121
122
- context "use groups for authorization" do
123
- setup do
124
- @admin = Factory ( :admin )
125
- @user = Factory ( :user )
122
+ describe "use groups for authorization" do
123
+ before do
124
+ @admin = Factory . create ( :admin )
125
+ @user = Factory . create ( :user )
126
126
::Devise . authentication_keys = [ :email ]
127
127
::Devise . ldap_check_group_membership = true
128
128
end
129
129
130
- should " admin should be allowed in" do
130
+ it "should admin should be allowed in" do
131
131
should_be_validated @admin , "admin_secret"
132
132
end
133
133
134
- should " admin should have the proper groups set" do
134
+ it "should admin should have the proper groups set" do
135
135
assert_contains ( @admin . ldap_groups , /cn=admins/ , "groups attribute not being set properly" )
136
136
end
137
137
138
- should " user should not be allowed in" do
138
+ it "should user should not be allowed in" do
139
139
should_not_be_validated @user , "secret"
140
140
end
141
141
142
- should " not be validated if group with different attribute is removed" do
142
+ it "should not be validated if group with different attribute is removed" do
143
143
`ldapmodify #{ ldap_connect_string } -f ../ldap/delete_authorization_role.ldif`
144
144
should_not_be_validated @admin , "admin_secret"
145
145
end
146
146
end
147
147
148
- context "use role attribute for authorization" do
149
- setup do
150
- @admin = Factory ( :admin )
151
- @user = Factory ( :user )
148
+ describe "use role attribute for authorization" do
149
+ before do
150
+ @admin = Factory . create ( :admin )
151
+ @user = Factory . create ( :user )
152
152
::Devise . ldap_check_attributes = true
153
153
end
154
154
155
- should " admin should be allowed in" do
155
+ it "should admin should be allowed in" do
156
156
should_be_validated @admin , "admin_secret"
157
157
end
158
158
159
- should " user should not be allowed in" do
159
+ it "should user should not be allowed in" do
160
160
should_not_be_validated @user , "secret"
161
161
end
162
162
end
163
163
164
- context "use admin setting to bind" do
165
- setup do
166
- @admin = Factory ( :admin )
167
- @user = Factory ( :user )
164
+ describe "use admin setting to bind" do
165
+ before do
166
+ @admin = Factory . create ( :admin )
167
+ @user = Factory . create ( :user )
168
168
::Devise . ldap_use_admin_to_bind = true
169
169
end
170
170
171
- should " description" do
171
+ it "should description" do
172
172
should_be_validated @admin , "admin_secret"
173
173
end
174
174
end
175
175
176
176
end
177
177
178
- context "use uid for login" do
179
- setup do
178
+ describe "use uid for login" do
179
+ before do
180
180
default_devise_settings!
181
181
reset_ldap_server!
182
182
::Devise . ldap_config = "#{ Rails . root } /config/#{ "ssl_" if ENV [ "LDAP_SSL" ] } ldap_with_uid.yml"
183
183
::Devise . authentication_keys = [ :uid ]
184
184
end
185
185
186
- context "description" do
187
- setup do
188
- @admin = Factory ( :admin )
189
- @user = Factory ( :user , :uid => "example_user" )
186
+ describe "description" do
187
+ before do
188
+ @admin = Factory . create ( :admin )
189
+ @user = Factory . create ( :user , :uid => "example_user" )
190
190
end
191
191
192
- should " be able to authenticate using uid" do
192
+ it "should be able to authenticate using uid" do
193
193
should_be_validated @user , "secret"
194
194
should_not_be_validated @admin , "admin_secret"
195
195
end
196
196
end
197
197
198
- context "create user" do
199
- setup do
198
+ describe "create user" do
199
+ before do
200
200
::Devise . ldap_create_user = true
201
201
end
202
202
203
- should " create a user in the database" do
203
+ it "should create a user in the database" do
204
204
@user = User . authenticate_with_ldap ( :uid => "example_user" , :password => "secret" )
205
205
assert_equal ( User . all . size , 1 )
206
206
assert_contains ( User . all . collect ( &:uid ) , "example_user" , "user not in database" )
207
207
end
208
208
209
- should " call ldap_before_save hooks" do
209
+ it "should call ldap_before_save hooks" do
210
210
User . class_eval do
211
211
def ldap_before_save
212
212
@foobar = 'foobar'
@@ -219,59 +219,59 @@ def ldap_before_save
219
219
end
220
220
end
221
221
222
- should " not call ldap_before_save hook if not defined" do
222
+ it "should not call ldap_before_save hook if not defined" do
223
223
assert_nothing_raised do
224
- should_be_validated Factory ( :user , :uid => "example_user" ) , "secret"
224
+ should_be_validated Factory . create ( :user , :uid => "example_user" ) , "secret"
225
225
end
226
226
end
227
227
end
228
228
end
229
229
230
- context "using ERB in the config file" do
231
- setup do
230
+ describe "using ERB in the config file" do
231
+ before do
232
232
default_devise_settings!
233
233
reset_ldap_server!
234
234
::Devise . ldap_config = "#{ Rails . root } /config/#{ "ssl_" if ENV [ "LDAP_SSL" ] } ldap_with_erb.yml"
235
235
end
236
236
237
- context "authenticate" do
238
- setup do
239
- @admin = Factory ( :admin )
240
- @user = Factory ( :user )
237
+ describe "authenticate" do
238
+ before do
239
+ @admin = Factory . create ( :admin )
240
+ @user = Factory . create ( :user )
241
241
end
242
242
243
- should " be able to authenticate" do
243
+ it "should be able to authenticate" do
244
244
should_be_validated @user , "secret"
245
245
should_be_validated @admin , "admin_secret"
246
246
end
247
247
end
248
248
end
249
249
250
- context "using variants in the config file" do
251
- setup do
250
+ describe "using variants in the config file" do
251
+ before do
252
252
default_devise_settings!
253
253
reset_ldap_server!
254
254
::Devise . ldap_config = Rails . root . join 'config' , 'ldap_with_boolean_ssl.yml'
255
255
end
256
256
257
- should " not fail if config file has ssl: true" do
257
+ it "should not fail if config file has ssl: true" do
258
258
assert_nothing_raised do
259
259
Devise ::LdapAdapter ::LdapConnect . new
260
260
end
261
261
end
262
262
end
263
263
264
- context "use username builder" do
265
- setup do
264
+ describe "use username builder" do
265
+ before do
266
266
default_devise_settings!
267
267
reset_ldap_server!
268
268
::Devise . ldap_auth_username_builder = Proc . new ( ) do |attribute , login , ldap |
269
269
"#{ attribute } =#{ login } ,ou=others,dc=test,dc=com"
270
270
end
271
- @other = Factory ( :other )
271
+ @other = Factory . create ( :other )
272
272
end
273
273
274
- should " be able to authenticate" do
274
+ it "should be able to authenticate" do
275
275
should_be_validated @other , "other_secret"
276
276
end
277
277
end
0 commit comments