##// END OF EJS Templates
Moves the LDAP test server to a fixture....
Jean-Philippe Lang -
r3376:899f06612a47
parent child
Show More
@@ -1,2 +1,13
1 --- {}
2
1 ---
2 auth_sources_001:
3 id: 1
4 type: AuthSourceLdap
5 name: 'LDAP test server'
6 host: '127.0.0.1'
7 port: 389
8 base_dn: 'OU=Person,DC=redmine,DC=org'
9 attr_login: uid
10 attr_firstname: givenName
11 attr_lastname: sn
12 attr_mail: mail
13 onthefly_register: true
@@ -1,97 +1,89
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2008 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.dirname(__FILE__) + '/../test_helper'
19 19
20 20 class AuthSourceLdapTest < ActiveSupport::TestCase
21
21 fixtures :auth_sources
22
22 23 def setup
23 24 end
24 25
25 26 def test_create
26 27 a = AuthSourceLdap.new(:name => 'My LDAP', :host => 'ldap.example.net', :port => 389, :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName')
27 28 assert a.save
28 29 end
29 30
30 31 def test_should_strip_ldap_attributes
31 32 a = AuthSourceLdap.new(:name => 'My LDAP', :host => 'ldap.example.net', :port => 389, :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName',
32 33 :attr_firstname => 'givenName ')
33 34 assert a.save
34 35 assert_equal 'givenName', a.reload.attr_firstname
35 36 end
36 37
37 38 if ldap_configured?
38 39 context '#authenticate' do
39 40 setup do
40 @auth = AuthSourceLdap.generate!(:name => 'on the fly',
41 :host => '127.0.0.1',
42 :port => 389,
43 :base_dn => 'OU=Person,DC=redmine,DC=org',
44 :attr_login => 'uid',
45 :attr_firstname => 'givenName',
46 :attr_lastname => 'sn',
47 :attr_mail => 'mail',
48 :onthefly_register => true)
49
41 @auth = AuthSourceLdap.find(1)
50 42 end
51 43
52 44 context 'with a valid LDAP user' do
53 45 should 'return the firstname user attributes' do
54 46 response = @auth.authenticate('example1','123456')
55 47 assert response.is_a?(Array), "An array was not returned"
56 48 assert response.first.present?, "No user data returned"
57 49 assert_equal 'Example', response.first[:firstname]
58 50 end
59 51
60 52 should 'return the lastname user attributes' do
61 53 response = @auth.authenticate('example1','123456')
62 54 assert response.is_a?(Array), "An array was not returned"
63 55 assert response.first.present?, "No user data returned"
64 56 assert_equal 'One', response.first[:lastname]
65 57 end
66 58
67 59 should 'return mail user attributes' do
68 60 response = @auth.authenticate('example1','123456')
69 61 assert response.is_a?(Array), "An array was not returned"
70 62 assert response.first.present?, "No user data returned"
71 63 assert_equal 'example1@redmine.org', response.first[:mail]
72 64 end
73 65 end
74 66
75 67 context 'with an invalid LDAP user' do
76 68 should 'return nil' do
77 69 assert_equal nil, @auth.authenticate('nouser','123456')
78 70 end
79 71 end
80 72
81 73 context 'without a login' do
82 74 should 'return nil' do
83 75 assert_equal nil, @auth.authenticate('','123456')
84 76 end
85 77 end
86 78
87 79 context 'without a password' do
88 80 should 'return nil' do
89 81 assert_equal nil, @auth.authenticate('edavis','')
90 82 end
91 83 end
92 84
93 85 end
94 86 else
95 87 puts '(Test LDAP server not configured)'
96 88 end
97 89 end
@@ -1,309 +1,300
1 1 # redMine - project management software
2 2 # Copyright (C) 2006 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.dirname(__FILE__) + '/../test_helper'
19 19
20 20 class UserTest < ActiveSupport::TestCase
21 fixtures :users, :members, :projects, :roles, :member_roles
21 fixtures :users, :members, :projects, :roles, :member_roles, :auth_sources
22 22
23 23 def setup
24 24 @admin = User.find(1)
25 25 @jsmith = User.find(2)
26 26 @dlopper = User.find(3)
27 27 end
28 28
29 29 test 'object_daddy creation' do
30 30 User.generate_with_protected!(:firstname => 'Testing connection')
31 31 User.generate_with_protected!(:firstname => 'Testing connection')
32 32 assert_equal 2, User.count(:all, :conditions => {:firstname => 'Testing connection'})
33 33 end
34 34
35 35 def test_truth
36 36 assert_kind_of User, @jsmith
37 37 end
38 38
39 39 def test_create
40 40 user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
41 41
42 42 user.login = "jsmith"
43 43 user.password, user.password_confirmation = "password", "password"
44 44 # login uniqueness
45 45 assert !user.save
46 46 assert_equal 1, user.errors.count
47 47
48 48 user.login = "newuser"
49 49 user.password, user.password_confirmation = "passwd", "password"
50 50 # password confirmation
51 51 assert !user.save
52 52 assert_equal 1, user.errors.count
53 53
54 54 user.password, user.password_confirmation = "password", "password"
55 55 assert user.save
56 56 end
57 57
58 58 def test_mail_uniqueness_should_not_be_case_sensitive
59 59 u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
60 60 u.login = 'newuser1'
61 61 u.password, u.password_confirmation = "password", "password"
62 62 assert u.save
63 63
64 64 u = User.new(:firstname => "new", :lastname => "user", :mail => "newUser@Somenet.foo")
65 65 u.login = 'newuser2'
66 66 u.password, u.password_confirmation = "password", "password"
67 67 assert !u.save
68 68 assert_equal I18n.translate('activerecord.errors.messages.taken'), u.errors.on(:mail)
69 69 end
70 70
71 71 def test_update
72 72 assert_equal "admin", @admin.login
73 73 @admin.login = "john"
74 74 assert @admin.save, @admin.errors.full_messages.join("; ")
75 75 @admin.reload
76 76 assert_equal "john", @admin.login
77 77 end
78 78
79 79 def test_destroy
80 80 User.find(2).destroy
81 81 assert_nil User.find_by_id(2)
82 82 assert Member.find_all_by_user_id(2).empty?
83 83 end
84 84
85 85 def test_validate
86 86 @admin.login = ""
87 87 assert !@admin.save
88 88 assert_equal 1, @admin.errors.count
89 89 end
90 90
91 91 def test_password
92 92 user = User.try_to_login("admin", "admin")
93 93 assert_kind_of User, user
94 94 assert_equal "admin", user.login
95 95 user.password = "hello"
96 96 assert user.save
97 97
98 98 user = User.try_to_login("admin", "hello")
99 99 assert_kind_of User, user
100 100 assert_equal "admin", user.login
101 101 assert_equal User.hash_password("hello"), user.hashed_password
102 102 end
103 103
104 104 def test_name_format
105 105 assert_equal 'Smith, John', @jsmith.name(:lastname_coma_firstname)
106 106 Setting.user_format = :firstname_lastname
107 107 assert_equal 'John Smith', @jsmith.reload.name
108 108 Setting.user_format = :username
109 109 assert_equal 'jsmith', @jsmith.reload.name
110 110 end
111 111
112 112 def test_lock
113 113 user = User.try_to_login("jsmith", "jsmith")
114 114 assert_equal @jsmith, user
115 115
116 116 @jsmith.status = User::STATUS_LOCKED
117 117 assert @jsmith.save
118 118
119 119 user = User.try_to_login("jsmith", "jsmith")
120 120 assert_equal nil, user
121 121 end
122 122
123 123 if ldap_configured?
124 124 context "#try_to_login using LDAP" do
125 125 context "on the fly registration" do
126 126 setup do
127 @auth_source = AuthSourceLdap.generate!(:name => 'localhost',
128 :host => '127.0.0.1',
129 :port => 389,
130 :base_dn => 'OU=Person,DC=redmine,DC=org',
131 :attr_login => 'uid',
132 :attr_firstname => 'givenName',
133 :attr_lastname => 'sn',
134 :attr_mail => 'mail',
135 :onthefly_register => true)
136
127 @auth_source = AuthSourceLdap.find(1)
137 128 end
138 129
139 130 context "with a successful authentication" do
140 131 should "create a new user account" do
141 132 assert_difference('User.count') do
142 133 User.try_to_login('edavis', '123456')
143 134 end
144 135 end
145 136 end
146 137 end
147 138 end
148 139
149 140 else
150 141 puts "Skipping LDAP tests."
151 142 end
152 143
153 144 def test_create_anonymous
154 145 AnonymousUser.delete_all
155 146 anon = User.anonymous
156 147 assert !anon.new_record?
157 148 assert_kind_of AnonymousUser, anon
158 149 end
159 150
160 151 should_have_one :rss_token
161 152
162 153 def test_rss_key
163 154 assert_nil @jsmith.rss_token
164 155 key = @jsmith.rss_key
165 156 assert_equal 40, key.length
166 157
167 158 @jsmith.reload
168 159 assert_equal key, @jsmith.rss_key
169 160 end
170 161
171 162
172 163 should_have_one :api_token
173 164
174 165 context "User#api_key" do
175 166 should "generate a new one if the user doesn't have one" do
176 167 user = User.generate_with_protected!(:api_token => nil)
177 168 assert_nil user.api_token
178 169
179 170 key = user.api_key
180 171 assert_equal 40, key.length
181 172 user.reload
182 173 assert_equal key, user.api_key
183 174 end
184 175
185 176 should "return the existing api token value" do
186 177 user = User.generate_with_protected!
187 178 token = Token.generate!(:action => 'api')
188 179 user.api_token = token
189 180 assert user.save
190 181
191 182 assert_equal token.value, user.api_key
192 183 end
193 184 end
194 185
195 186 context "User#find_by_api_key" do
196 187 should "return nil if no matching key is found" do
197 188 assert_nil User.find_by_api_key('zzzzzzzzz')
198 189 end
199 190
200 191 should "return nil if the key is found for an inactive user" do
201 192 user = User.generate_with_protected!(:status => User::STATUS_LOCKED)
202 193 token = Token.generate!(:action => 'api')
203 194 user.api_token = token
204 195 user.save
205 196
206 197 assert_nil User.find_by_api_key(token.value)
207 198 end
208 199
209 200 should "return the user if the key is found for an active user" do
210 201 user = User.generate_with_protected!(:status => User::STATUS_ACTIVE)
211 202 token = Token.generate!(:action => 'api')
212 203 user.api_token = token
213 204 user.save
214 205
215 206 assert_equal user, User.find_by_api_key(token.value)
216 207 end
217 208 end
218 209
219 210 def test_roles_for_project
220 211 # user with a role
221 212 roles = @jsmith.roles_for_project(Project.find(1))
222 213 assert_kind_of Role, roles.first
223 214 assert_equal "Manager", roles.first.name
224 215
225 216 # user with no role
226 217 assert_nil @dlopper.roles_for_project(Project.find(2)).detect {|role| role.member?}
227 218 end
228 219
229 220 def test_mail_notification_all
230 221 @jsmith.mail_notification = true
231 222 @jsmith.notified_project_ids = []
232 223 @jsmith.save
233 224 @jsmith.reload
234 225 assert @jsmith.projects.first.recipients.include?(@jsmith.mail)
235 226 end
236 227
237 228 def test_mail_notification_selected
238 229 @jsmith.mail_notification = false
239 230 @jsmith.notified_project_ids = [1]
240 231 @jsmith.save
241 232 @jsmith.reload
242 233 assert Project.find(1).recipients.include?(@jsmith.mail)
243 234 end
244 235
245 236 def test_mail_notification_none
246 237 @jsmith.mail_notification = false
247 238 @jsmith.notified_project_ids = []
248 239 @jsmith.save
249 240 @jsmith.reload
250 241 assert !@jsmith.projects.first.recipients.include?(@jsmith.mail)
251 242 end
252 243
253 244 def test_comments_sorting_preference
254 245 assert !@jsmith.wants_comments_in_reverse_order?
255 246 @jsmith.pref.comments_sorting = 'asc'
256 247 assert !@jsmith.wants_comments_in_reverse_order?
257 248 @jsmith.pref.comments_sorting = 'desc'
258 249 assert @jsmith.wants_comments_in_reverse_order?
259 250 end
260 251
261 252 def test_find_by_mail_should_be_case_insensitive
262 253 u = User.find_by_mail('JSmith@somenet.foo')
263 254 assert_not_nil u
264 255 assert_equal 'jsmith@somenet.foo', u.mail
265 256 end
266 257
267 258 def test_random_password
268 259 u = User.new
269 260 u.random_password
270 261 assert !u.password.blank?
271 262 assert !u.password_confirmation.blank?
272 263 end
273 264
274 265 if Object.const_defined?(:OpenID)
275 266
276 267 def test_setting_identity_url
277 268 normalized_open_id_url = 'http://example.com/'
278 269 u = User.new( :identity_url => 'http://example.com/' )
279 270 assert_equal normalized_open_id_url, u.identity_url
280 271 end
281 272
282 273 def test_setting_identity_url_without_trailing_slash
283 274 normalized_open_id_url = 'http://example.com/'
284 275 u = User.new( :identity_url => 'http://example.com' )
285 276 assert_equal normalized_open_id_url, u.identity_url
286 277 end
287 278
288 279 def test_setting_identity_url_without_protocol
289 280 normalized_open_id_url = 'http://example.com/'
290 281 u = User.new( :identity_url => 'example.com' )
291 282 assert_equal normalized_open_id_url, u.identity_url
292 283 end
293 284
294 285 def test_setting_blank_identity_url
295 286 u = User.new( :identity_url => 'example.com' )
296 287 u.identity_url = ''
297 288 assert u.identity_url.blank?
298 289 end
299 290
300 291 def test_setting_invalid_identity_url
301 292 u = User.new( :identity_url => 'this is not an openid url' )
302 293 assert u.identity_url.blank?
303 294 end
304 295
305 296 else
306 297 puts "Skipping openid tests."
307 298 end
308 299
309 300 end
General Comments 0
You need to be logged in to leave comments. Login now