##// END OF EJS Templates
Check that admin LDAP user is untouched after authentication (#5263)....
Jean-Philippe Lang -
r3520:9306f3ea6c4f
parent child
Show More
@@ -1,300 +1,312
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 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 127 @auth_source = AuthSourceLdap.find(1)
128 128 end
129 129
130 130 context "with a successful authentication" do
131 should "create a new user account" do
131 should "create a new user account if it doesn't exist" do
132 132 assert_difference('User.count') do
133 User.try_to_login('edavis', '123456')
133 user = User.try_to_login('edavis', '123456')
134 assert !user.admin?
135 end
136 end
137
138 should "retrieve existing user" do
139 user = User.try_to_login('edavis', '123456')
140 user.admin = true
141 user.save!
142
143 assert_no_difference('User.count') do
144 user = User.try_to_login('edavis', '123456')
145 assert user.admin?
134 146 end
135 147 end
136 148 end
137 149 end
138 150 end
139 151
140 152 else
141 153 puts "Skipping LDAP tests."
142 154 end
143 155
144 156 def test_create_anonymous
145 157 AnonymousUser.delete_all
146 158 anon = User.anonymous
147 159 assert !anon.new_record?
148 160 assert_kind_of AnonymousUser, anon
149 161 end
150 162
151 163 should_have_one :rss_token
152 164
153 165 def test_rss_key
154 166 assert_nil @jsmith.rss_token
155 167 key = @jsmith.rss_key
156 168 assert_equal 40, key.length
157 169
158 170 @jsmith.reload
159 171 assert_equal key, @jsmith.rss_key
160 172 end
161 173
162 174
163 175 should_have_one :api_token
164 176
165 177 context "User#api_key" do
166 178 should "generate a new one if the user doesn't have one" do
167 179 user = User.generate_with_protected!(:api_token => nil)
168 180 assert_nil user.api_token
169 181
170 182 key = user.api_key
171 183 assert_equal 40, key.length
172 184 user.reload
173 185 assert_equal key, user.api_key
174 186 end
175 187
176 188 should "return the existing api token value" do
177 189 user = User.generate_with_protected!
178 190 token = Token.generate!(:action => 'api')
179 191 user.api_token = token
180 192 assert user.save
181 193
182 194 assert_equal token.value, user.api_key
183 195 end
184 196 end
185 197
186 198 context "User#find_by_api_key" do
187 199 should "return nil if no matching key is found" do
188 200 assert_nil User.find_by_api_key('zzzzzzzzz')
189 201 end
190 202
191 203 should "return nil if the key is found for an inactive user" do
192 204 user = User.generate_with_protected!(:status => User::STATUS_LOCKED)
193 205 token = Token.generate!(:action => 'api')
194 206 user.api_token = token
195 207 user.save
196 208
197 209 assert_nil User.find_by_api_key(token.value)
198 210 end
199 211
200 212 should "return the user if the key is found for an active user" do
201 213 user = User.generate_with_protected!(:status => User::STATUS_ACTIVE)
202 214 token = Token.generate!(:action => 'api')
203 215 user.api_token = token
204 216 user.save
205 217
206 218 assert_equal user, User.find_by_api_key(token.value)
207 219 end
208 220 end
209 221
210 222 def test_roles_for_project
211 223 # user with a role
212 224 roles = @jsmith.roles_for_project(Project.find(1))
213 225 assert_kind_of Role, roles.first
214 226 assert_equal "Manager", roles.first.name
215 227
216 228 # user with no role
217 229 assert_nil @dlopper.roles_for_project(Project.find(2)).detect {|role| role.member?}
218 230 end
219 231
220 232 def test_mail_notification_all
221 233 @jsmith.mail_notification = true
222 234 @jsmith.notified_project_ids = []
223 235 @jsmith.save
224 236 @jsmith.reload
225 237 assert @jsmith.projects.first.recipients.include?(@jsmith.mail)
226 238 end
227 239
228 240 def test_mail_notification_selected
229 241 @jsmith.mail_notification = false
230 242 @jsmith.notified_project_ids = [1]
231 243 @jsmith.save
232 244 @jsmith.reload
233 245 assert Project.find(1).recipients.include?(@jsmith.mail)
234 246 end
235 247
236 248 def test_mail_notification_none
237 249 @jsmith.mail_notification = false
238 250 @jsmith.notified_project_ids = []
239 251 @jsmith.save
240 252 @jsmith.reload
241 253 assert !@jsmith.projects.first.recipients.include?(@jsmith.mail)
242 254 end
243 255
244 256 def test_comments_sorting_preference
245 257 assert !@jsmith.wants_comments_in_reverse_order?
246 258 @jsmith.pref.comments_sorting = 'asc'
247 259 assert !@jsmith.wants_comments_in_reverse_order?
248 260 @jsmith.pref.comments_sorting = 'desc'
249 261 assert @jsmith.wants_comments_in_reverse_order?
250 262 end
251 263
252 264 def test_find_by_mail_should_be_case_insensitive
253 265 u = User.find_by_mail('JSmith@somenet.foo')
254 266 assert_not_nil u
255 267 assert_equal 'jsmith@somenet.foo', u.mail
256 268 end
257 269
258 270 def test_random_password
259 271 u = User.new
260 272 u.random_password
261 273 assert !u.password.blank?
262 274 assert !u.password_confirmation.blank?
263 275 end
264 276
265 277 if Object.const_defined?(:OpenID)
266 278
267 279 def test_setting_identity_url
268 280 normalized_open_id_url = 'http://example.com/'
269 281 u = User.new( :identity_url => 'http://example.com/' )
270 282 assert_equal normalized_open_id_url, u.identity_url
271 283 end
272 284
273 285 def test_setting_identity_url_without_trailing_slash
274 286 normalized_open_id_url = 'http://example.com/'
275 287 u = User.new( :identity_url => 'http://example.com' )
276 288 assert_equal normalized_open_id_url, u.identity_url
277 289 end
278 290
279 291 def test_setting_identity_url_without_protocol
280 292 normalized_open_id_url = 'http://example.com/'
281 293 u = User.new( :identity_url => 'example.com' )
282 294 assert_equal normalized_open_id_url, u.identity_url
283 295 end
284 296
285 297 def test_setting_blank_identity_url
286 298 u = User.new( :identity_url => 'example.com' )
287 299 u.identity_url = ''
288 300 assert u.identity_url.blank?
289 301 end
290 302
291 303 def test_setting_invalid_identity_url
292 304 u = User.new( :identity_url => 'this is not an openid url' )
293 305 assert u.identity_url.blank?
294 306 end
295 307
296 308 else
297 309 puts "Skipping openid tests."
298 310 end
299 311
300 312 end
General Comments 0
You need to be logged in to leave comments. Login now