##// END OF EJS Templates
Rails4: replace deprecated find_all_by_* at UserTest...
Toshi MARUYAMA -
r12296:907663f83c3b
parent child
Show More
@@ -1,1149 +1,1149
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 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.expand_path('../../test_helper', __FILE__)
19 19
20 20 class UserTest < ActiveSupport::TestCase
21 21 fixtures :users, :members, :projects, :roles, :member_roles, :auth_sources,
22 22 :trackers, :issue_statuses,
23 23 :projects_trackers,
24 24 :watchers,
25 25 :issue_categories, :enumerations, :issues,
26 26 :journals, :journal_details,
27 27 :groups_users,
28 28 :enabled_modules
29 29
30 30 def setup
31 31 @admin = User.find(1)
32 32 @jsmith = User.find(2)
33 33 @dlopper = User.find(3)
34 34 end
35 35
36 36 def test_sorted_scope_should_sort_user_by_display_name
37 37 assert_equal User.all.map(&:name).map(&:downcase).sort,
38 38 User.sorted.all.map(&:name).map(&:downcase)
39 39 end
40 40
41 41 def test_generate
42 42 User.generate!(:firstname => 'Testing connection')
43 43 User.generate!(:firstname => 'Testing connection')
44 44 assert_equal 2, User.where(:firstname => 'Testing connection').count
45 45 end
46 46
47 47 def test_truth
48 48 assert_kind_of User, @jsmith
49 49 end
50 50
51 51 def test_mail_should_be_stripped
52 52 u = User.new
53 53 u.mail = " foo@bar.com "
54 54 assert_equal "foo@bar.com", u.mail
55 55 end
56 56
57 57 def test_mail_validation
58 58 u = User.new
59 59 u.mail = ''
60 60 assert !u.valid?
61 61 assert_include I18n.translate('activerecord.errors.messages.blank'), u.errors[:mail]
62 62 end
63 63
64 64 def test_login_length_validation
65 65 user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
66 66 user.login = "x" * (User::LOGIN_LENGTH_LIMIT+1)
67 67 assert !user.valid?
68 68
69 69 user.login = "x" * (User::LOGIN_LENGTH_LIMIT)
70 70 assert user.valid?
71 71 assert user.save
72 72 end
73 73
74 74 def test_generate_password_should_respect_minimum_password_length
75 75 with_settings :password_min_length => 15 do
76 76 user = User.generate!(:generate_password => true)
77 77 assert user.password.length >= 15
78 78 end
79 79 end
80 80
81 81 def test_generate_password_should_not_generate_password_with_less_than_10_characters
82 82 with_settings :password_min_length => 4 do
83 83 user = User.generate!(:generate_password => true)
84 84 assert user.password.length >= 10
85 85 end
86 86 end
87 87
88 88 def test_generate_password_on_create_should_set_password
89 89 user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
90 90 user.login = "newuser"
91 91 user.generate_password = true
92 92 assert user.save
93 93
94 94 password = user.password
95 95 assert user.check_password?(password)
96 96 end
97 97
98 98 def test_generate_password_on_update_should_update_password
99 99 user = User.find(2)
100 100 hash = user.hashed_password
101 101 user.generate_password = true
102 102 assert user.save
103 103
104 104 password = user.password
105 105 assert user.check_password?(password)
106 106 assert_not_equal hash, user.reload.hashed_password
107 107 end
108 108
109 109 def test_create
110 110 user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
111 111
112 112 user.login = "jsmith"
113 113 user.password, user.password_confirmation = "password", "password"
114 114 # login uniqueness
115 115 assert !user.save
116 116 assert_equal 1, user.errors.count
117 117
118 118 user.login = "newuser"
119 119 user.password, user.password_confirmation = "password", "pass"
120 120 # password confirmation
121 121 assert !user.save
122 122 assert_equal 1, user.errors.count
123 123
124 124 user.password, user.password_confirmation = "password", "password"
125 125 assert user.save
126 126 end
127 127
128 128 def test_user_before_create_should_set_the_mail_notification_to_the_default_setting
129 129 @user1 = User.generate!
130 130 assert_equal 'only_my_events', @user1.mail_notification
131 131 with_settings :default_notification_option => 'all' do
132 132 @user2 = User.generate!
133 133 assert_equal 'all', @user2.mail_notification
134 134 end
135 135 end
136 136
137 137 def test_user_login_should_be_case_insensitive
138 138 u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
139 139 u.login = 'newuser'
140 140 u.password, u.password_confirmation = "password", "password"
141 141 assert u.save
142 142 u = User.new(:firstname => "Similar", :lastname => "User",
143 143 :mail => "similaruser@somenet.foo")
144 144 u.login = 'NewUser'
145 145 u.password, u.password_confirmation = "password", "password"
146 146 assert !u.save
147 147 assert_include I18n.translate('activerecord.errors.messages.taken'), u.errors[:login]
148 148 end
149 149
150 150 def test_mail_uniqueness_should_not_be_case_sensitive
151 151 u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
152 152 u.login = 'newuser1'
153 153 u.password, u.password_confirmation = "password", "password"
154 154 assert u.save
155 155
156 156 u = User.new(:firstname => "new", :lastname => "user", :mail => "newUser@Somenet.foo")
157 157 u.login = 'newuser2'
158 158 u.password, u.password_confirmation = "password", "password"
159 159 assert !u.save
160 160 assert_include I18n.translate('activerecord.errors.messages.taken'), u.errors[:mail]
161 161 end
162 162
163 163 def test_update
164 164 assert_equal "admin", @admin.login
165 165 @admin.login = "john"
166 166 assert @admin.save, @admin.errors.full_messages.join("; ")
167 167 @admin.reload
168 168 assert_equal "john", @admin.login
169 169 end
170 170
171 171 def test_update_should_not_fail_for_legacy_user_with_different_case_logins
172 172 u1 = User.new(:firstname => "new", :lastname => "user", :mail => "newuser1@somenet.foo")
173 173 u1.login = 'newuser1'
174 174 assert u1.save
175 175
176 176 u2 = User.new(:firstname => "new", :lastname => "user", :mail => "newuser2@somenet.foo")
177 177 u2.login = 'newuser1'
178 178 assert u2.save(:validate => false)
179 179
180 180 user = User.find(u2.id)
181 181 user.firstname = "firstname"
182 182 assert user.save, "Save failed"
183 183 end
184 184
185 185 def test_destroy_should_delete_members_and_roles
186 members = Member.find_all_by_user_id(2)
187 ms = members.size
186 members = Member.where(:user_id => 2)
187 ms = members.count
188 188 rs = members.collect(&:roles).flatten.size
189
189 assert ms > 0
190 assert rs > 0
190 191 assert_difference 'Member.count', - ms do
191 192 assert_difference 'MemberRole.count', - rs do
192 193 User.find(2).destroy
193 194 end
194 195 end
195
196 196 assert_nil User.find_by_id(2)
197 assert Member.find_all_by_user_id(2).empty?
197 assert_equal 0, Member.where(:user_id => 2).count
198 198 end
199 199
200 200 def test_destroy_should_update_attachments
201 201 attachment = Attachment.create!(:container => Project.find(1),
202 202 :file => uploaded_test_file("testfile.txt", "text/plain"),
203 203 :author_id => 2)
204 204
205 205 User.find(2).destroy
206 206 assert_nil User.find_by_id(2)
207 207 assert_equal User.anonymous, attachment.reload.author
208 208 end
209 209
210 210 def test_destroy_should_update_comments
211 211 comment = Comment.create!(
212 212 :commented => News.create!(:project_id => 1,
213 213 :author_id => 1, :title => 'foo', :description => 'foo'),
214 214 :author => User.find(2),
215 215 :comments => 'foo'
216 216 )
217 217
218 218 User.find(2).destroy
219 219 assert_nil User.find_by_id(2)
220 220 assert_equal User.anonymous, comment.reload.author
221 221 end
222 222
223 223 def test_destroy_should_update_issues
224 224 issue = Issue.create!(:project_id => 1, :author_id => 2,
225 225 :tracker_id => 1, :subject => 'foo')
226 226
227 227 User.find(2).destroy
228 228 assert_nil User.find_by_id(2)
229 229 assert_equal User.anonymous, issue.reload.author
230 230 end
231 231
232 232 def test_destroy_should_unassign_issues
233 233 issue = Issue.create!(:project_id => 1, :author_id => 1,
234 234 :tracker_id => 1, :subject => 'foo', :assigned_to_id => 2)
235 235
236 236 User.find(2).destroy
237 237 assert_nil User.find_by_id(2)
238 238 assert_nil issue.reload.assigned_to
239 239 end
240 240
241 241 def test_destroy_should_update_journals
242 242 issue = Issue.create!(:project_id => 1, :author_id => 2,
243 243 :tracker_id => 1, :subject => 'foo')
244 244 issue.init_journal(User.find(2), "update")
245 245 issue.save!
246 246
247 247 User.find(2).destroy
248 248 assert_nil User.find_by_id(2)
249 249 assert_equal User.anonymous, issue.journals.first.reload.user
250 250 end
251 251
252 252 def test_destroy_should_update_journal_details_old_value
253 253 issue = Issue.create!(:project_id => 1, :author_id => 1,
254 254 :tracker_id => 1, :subject => 'foo', :assigned_to_id => 2)
255 255 issue.init_journal(User.find(1), "update")
256 256 issue.assigned_to_id = nil
257 257 assert_difference 'JournalDetail.count' do
258 258 issue.save!
259 259 end
260 260 journal_detail = JournalDetail.order('id DESC').first
261 261 assert_equal '2', journal_detail.old_value
262 262
263 263 User.find(2).destroy
264 264 assert_nil User.find_by_id(2)
265 265 assert_equal User.anonymous.id.to_s, journal_detail.reload.old_value
266 266 end
267 267
268 268 def test_destroy_should_update_journal_details_value
269 269 issue = Issue.create!(:project_id => 1, :author_id => 1,
270 270 :tracker_id => 1, :subject => 'foo')
271 271 issue.init_journal(User.find(1), "update")
272 272 issue.assigned_to_id = 2
273 273 assert_difference 'JournalDetail.count' do
274 274 issue.save!
275 275 end
276 276 journal_detail = JournalDetail.order('id DESC').first
277 277 assert_equal '2', journal_detail.value
278 278
279 279 User.find(2).destroy
280 280 assert_nil User.find_by_id(2)
281 281 assert_equal User.anonymous.id.to_s, journal_detail.reload.value
282 282 end
283 283
284 284 def test_destroy_should_update_messages
285 285 board = Board.create!(:project_id => 1, :name => 'Board', :description => 'Board')
286 286 message = Message.create!(:board_id => board.id, :author_id => 2,
287 287 :subject => 'foo', :content => 'foo')
288 288 User.find(2).destroy
289 289 assert_nil User.find_by_id(2)
290 290 assert_equal User.anonymous, message.reload.author
291 291 end
292 292
293 293 def test_destroy_should_update_news
294 294 news = News.create!(:project_id => 1, :author_id => 2,
295 295 :title => 'foo', :description => 'foo')
296 296 User.find(2).destroy
297 297 assert_nil User.find_by_id(2)
298 298 assert_equal User.anonymous, news.reload.author
299 299 end
300 300
301 301 def test_destroy_should_delete_private_queries
302 302 query = Query.new(:name => 'foo', :visibility => Query::VISIBILITY_PRIVATE)
303 303 query.project_id = 1
304 304 query.user_id = 2
305 305 query.save!
306 306
307 307 User.find(2).destroy
308 308 assert_nil User.find_by_id(2)
309 309 assert_nil Query.find_by_id(query.id)
310 310 end
311 311
312 312 def test_destroy_should_update_public_queries
313 313 query = Query.new(:name => 'foo', :visibility => Query::VISIBILITY_PUBLIC)
314 314 query.project_id = 1
315 315 query.user_id = 2
316 316 query.save!
317 317
318 318 User.find(2).destroy
319 319 assert_nil User.find_by_id(2)
320 320 assert_equal User.anonymous, query.reload.user
321 321 end
322 322
323 323 def test_destroy_should_update_time_entries
324 324 entry = TimeEntry.new(:hours => '2', :spent_on => Date.today,
325 325 :activity => TimeEntryActivity.create!(:name => 'foo'))
326 326 entry.project_id = 1
327 327 entry.user_id = 2
328 328 entry.save!
329 329
330 330 User.find(2).destroy
331 331 assert_nil User.find_by_id(2)
332 332 assert_equal User.anonymous, entry.reload.user
333 333 end
334 334
335 335 def test_destroy_should_delete_tokens
336 336 token = Token.create!(:user_id => 2, :value => 'foo')
337 337
338 338 User.find(2).destroy
339 339 assert_nil User.find_by_id(2)
340 340 assert_nil Token.find_by_id(token.id)
341 341 end
342 342
343 343 def test_destroy_should_delete_watchers
344 344 issue = Issue.create!(:project_id => 1, :author_id => 1,
345 345 :tracker_id => 1, :subject => 'foo')
346 346 watcher = Watcher.create!(:user_id => 2, :watchable => issue)
347 347
348 348 User.find(2).destroy
349 349 assert_nil User.find_by_id(2)
350 350 assert_nil Watcher.find_by_id(watcher.id)
351 351 end
352 352
353 353 def test_destroy_should_update_wiki_contents
354 354 wiki_content = WikiContent.create!(
355 355 :text => 'foo',
356 356 :author_id => 2,
357 357 :page => WikiPage.create!(:title => 'Foo',
358 358 :wiki => Wiki.create!(:project_id => 3,
359 359 :start_page => 'Start'))
360 360 )
361 361 wiki_content.text = 'bar'
362 362 assert_difference 'WikiContent::Version.count' do
363 363 wiki_content.save!
364 364 end
365 365
366 366 User.find(2).destroy
367 367 assert_nil User.find_by_id(2)
368 368 assert_equal User.anonymous, wiki_content.reload.author
369 369 wiki_content.versions.each do |version|
370 370 assert_equal User.anonymous, version.reload.author
371 371 end
372 372 end
373 373
374 374 def test_destroy_should_nullify_issue_categories
375 375 category = IssueCategory.create!(:project_id => 1, :assigned_to_id => 2, :name => 'foo')
376 376
377 377 User.find(2).destroy
378 378 assert_nil User.find_by_id(2)
379 379 assert_nil category.reload.assigned_to_id
380 380 end
381 381
382 382 def test_destroy_should_nullify_changesets
383 383 changeset = Changeset.create!(
384 384 :repository => Repository::Subversion.create!(
385 385 :project_id => 1,
386 386 :url => 'file:///tmp',
387 387 :identifier => 'tmp'
388 388 ),
389 389 :revision => '12',
390 390 :committed_on => Time.now,
391 391 :committer => 'jsmith'
392 392 )
393 393 assert_equal 2, changeset.user_id
394 394
395 395 User.find(2).destroy
396 396 assert_nil User.find_by_id(2)
397 397 assert_nil changeset.reload.user_id
398 398 end
399 399
400 400 def test_anonymous_user_should_not_be_destroyable
401 401 assert_no_difference 'User.count' do
402 402 assert_equal false, User.anonymous.destroy
403 403 end
404 404 end
405 405
406 406 def test_validate_login_presence
407 407 @admin.login = ""
408 408 assert !@admin.save
409 409 assert_equal 1, @admin.errors.count
410 410 end
411 411
412 412 def test_validate_mail_notification_inclusion
413 413 u = User.new
414 414 u.mail_notification = 'foo'
415 415 u.save
416 416 assert_not_equal [], u.errors[:mail_notification]
417 417 end
418 418
419 419 def test_password
420 420 user = User.try_to_login("admin", "admin")
421 421 assert_kind_of User, user
422 422 assert_equal "admin", user.login
423 423 user.password = "hello123"
424 424 assert user.save
425 425
426 426 user = User.try_to_login("admin", "hello123")
427 427 assert_kind_of User, user
428 428 assert_equal "admin", user.login
429 429 end
430 430
431 431 def test_validate_password_length
432 432 with_settings :password_min_length => '100' do
433 433 user = User.new(:firstname => "new100",
434 434 :lastname => "user100", :mail => "newuser100@somenet.foo")
435 435 user.login = "newuser100"
436 436 user.password, user.password_confirmation = "password100", "password100"
437 437 assert !user.save
438 438 assert_equal 1, user.errors.count
439 439 end
440 440 end
441 441
442 442 def test_name_format
443 443 assert_equal 'John S.', @jsmith.name(:firstname_lastinitial)
444 444 assert_equal 'Smith, John', @jsmith.name(:lastname_coma_firstname)
445 445 with_settings :user_format => :firstname_lastname do
446 446 assert_equal 'John Smith', @jsmith.reload.name
447 447 end
448 448 with_settings :user_format => :username do
449 449 assert_equal 'jsmith', @jsmith.reload.name
450 450 end
451 451 with_settings :user_format => :lastname do
452 452 assert_equal 'Smith', @jsmith.reload.name
453 453 end
454 454 end
455 455
456 456 def test_today_should_return_the_day_according_to_user_time_zone
457 457 preference = User.find(1).pref
458 458 date = Date.new(2012, 05, 15)
459 459 time = Time.gm(2012, 05, 15, 23, 30).utc # 2012-05-15 23:30 UTC
460 460 Date.stubs(:today).returns(date)
461 461 Time.stubs(:now).returns(time)
462 462
463 463 preference.update_attribute :time_zone, 'Baku' # UTC+4
464 464 assert_equal '2012-05-16', User.find(1).today.to_s
465 465
466 466 preference.update_attribute :time_zone, 'La Paz' # UTC-4
467 467 assert_equal '2012-05-15', User.find(1).today.to_s
468 468
469 469 preference.update_attribute :time_zone, ''
470 470 assert_equal '2012-05-15', User.find(1).today.to_s
471 471 end
472 472
473 473 def test_time_to_date_should_return_the_date_according_to_user_time_zone
474 474 preference = User.find(1).pref
475 475 time = Time.gm(2012, 05, 15, 23, 30).utc # 2012-05-15 23:30 UTC
476 476
477 477 preference.update_attribute :time_zone, 'Baku' # UTC+4
478 478 assert_equal '2012-05-16', User.find(1).time_to_date(time).to_s
479 479
480 480 preference.update_attribute :time_zone, 'La Paz' # UTC-4
481 481 assert_equal '2012-05-15', User.find(1).time_to_date(time).to_s
482 482
483 483 preference.update_attribute :time_zone, ''
484 484 assert_equal '2012-05-15', User.find(1).time_to_date(time).to_s
485 485 end
486 486
487 487 def test_fields_for_order_statement_should_return_fields_according_user_format_setting
488 488 with_settings :user_format => 'lastname_coma_firstname' do
489 489 assert_equal ['users.lastname', 'users.firstname', 'users.id'],
490 490 User.fields_for_order_statement
491 491 end
492 492 end
493 493
494 494 def test_fields_for_order_statement_width_table_name_should_prepend_table_name
495 495 with_settings :user_format => 'lastname_firstname' do
496 496 assert_equal ['authors.lastname', 'authors.firstname', 'authors.id'],
497 497 User.fields_for_order_statement('authors')
498 498 end
499 499 end
500 500
501 501 def test_fields_for_order_statement_with_blank_format_should_return_default
502 502 with_settings :user_format => '' do
503 503 assert_equal ['users.firstname', 'users.lastname', 'users.id'],
504 504 User.fields_for_order_statement
505 505 end
506 506 end
507 507
508 508 def test_fields_for_order_statement_with_invalid_format_should_return_default
509 509 with_settings :user_format => 'foo' do
510 510 assert_equal ['users.firstname', 'users.lastname', 'users.id'],
511 511 User.fields_for_order_statement
512 512 end
513 513 end
514 514
515 515 test ".try_to_login with good credentials should return the user" do
516 516 user = User.try_to_login("admin", "admin")
517 517 assert_kind_of User, user
518 518 assert_equal "admin", user.login
519 519 end
520 520
521 521 test ".try_to_login with wrong credentials should return nil" do
522 522 assert_nil User.try_to_login("admin", "foo")
523 523 end
524 524
525 525 def test_try_to_login_with_locked_user_should_return_nil
526 526 @jsmith.status = User::STATUS_LOCKED
527 527 @jsmith.save!
528 528
529 529 user = User.try_to_login("jsmith", "jsmith")
530 530 assert_equal nil, user
531 531 end
532 532
533 533 def test_try_to_login_with_locked_user_and_not_active_only_should_return_user
534 534 @jsmith.status = User::STATUS_LOCKED
535 535 @jsmith.save!
536 536
537 537 user = User.try_to_login("jsmith", "jsmith", false)
538 538 assert_equal @jsmith, user
539 539 end
540 540
541 541 test ".try_to_login should fall-back to case-insensitive if user login is not found as-typed" do
542 542 user = User.try_to_login("AdMin", "admin")
543 543 assert_kind_of User, user
544 544 assert_equal "admin", user.login
545 545 end
546 546
547 547 test ".try_to_login should select the exact matching user first" do
548 548 case_sensitive_user = User.generate! do |user|
549 549 user.password = "admin123"
550 550 end
551 551 # bypass validations to make it appear like existing data
552 552 case_sensitive_user.update_attribute(:login, 'ADMIN')
553 553
554 554 user = User.try_to_login("ADMIN", "admin123")
555 555 assert_kind_of User, user
556 556 assert_equal "ADMIN", user.login
557 557 end
558 558
559 559 if ldap_configured?
560 560 context "#try_to_login using LDAP" do
561 561 context "with failed connection to the LDAP server" do
562 562 should "return nil" do
563 563 @auth_source = AuthSourceLdap.find(1)
564 564 AuthSource.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::LdapError, 'Cannot connect')
565 565
566 566 assert_equal nil, User.try_to_login('edavis', 'wrong')
567 567 end
568 568 end
569 569
570 570 context "with an unsuccessful authentication" do
571 571 should "return nil" do
572 572 assert_equal nil, User.try_to_login('edavis', 'wrong')
573 573 end
574 574 end
575 575
576 576 context "binding with user's account" do
577 577 setup do
578 578 @auth_source = AuthSourceLdap.find(1)
579 579 @auth_source.account = "uid=$login,ou=Person,dc=redmine,dc=org"
580 580 @auth_source.account_password = ''
581 581 @auth_source.save!
582 582
583 583 @ldap_user = User.new(:mail => 'example1@redmine.org', :firstname => 'LDAP', :lastname => 'user', :auth_source_id => 1)
584 584 @ldap_user.login = 'example1'
585 585 @ldap_user.save!
586 586 end
587 587
588 588 context "with a successful authentication" do
589 589 should "return the user" do
590 590 assert_equal @ldap_user, User.try_to_login('example1', '123456')
591 591 end
592 592 end
593 593
594 594 context "with an unsuccessful authentication" do
595 595 should "return nil" do
596 596 assert_nil User.try_to_login('example1', '11111')
597 597 end
598 598 end
599 599 end
600 600
601 601 context "on the fly registration" do
602 602 setup do
603 603 @auth_source = AuthSourceLdap.find(1)
604 604 @auth_source.update_attribute :onthefly_register, true
605 605 end
606 606
607 607 context "with a successful authentication" do
608 608 should "create a new user account if it doesn't exist" do
609 609 assert_difference('User.count') do
610 610 user = User.try_to_login('edavis', '123456')
611 611 assert !user.admin?
612 612 end
613 613 end
614 614
615 615 should "retrieve existing user" do
616 616 user = User.try_to_login('edavis', '123456')
617 617 user.admin = true
618 618 user.save!
619 619
620 620 assert_no_difference('User.count') do
621 621 user = User.try_to_login('edavis', '123456')
622 622 assert user.admin?
623 623 end
624 624 end
625 625 end
626 626
627 627 context "binding with user's account" do
628 628 setup do
629 629 @auth_source = AuthSourceLdap.find(1)
630 630 @auth_source.account = "uid=$login,ou=Person,dc=redmine,dc=org"
631 631 @auth_source.account_password = ''
632 632 @auth_source.save!
633 633 end
634 634
635 635 context "with a successful authentication" do
636 636 should "create a new user account if it doesn't exist" do
637 637 assert_difference('User.count') do
638 638 user = User.try_to_login('example1', '123456')
639 639 assert_kind_of User, user
640 640 end
641 641 end
642 642 end
643 643
644 644 context "with an unsuccessful authentication" do
645 645 should "return nil" do
646 646 assert_nil User.try_to_login('example1', '11111')
647 647 end
648 648 end
649 649 end
650 650 end
651 651 end
652 652
653 653 else
654 654 puts "Skipping LDAP tests."
655 655 end
656 656
657 657 def test_create_anonymous
658 658 AnonymousUser.delete_all
659 659 anon = User.anonymous
660 660 assert !anon.new_record?
661 661 assert_kind_of AnonymousUser, anon
662 662 end
663 663
664 664 def test_ensure_single_anonymous_user
665 665 AnonymousUser.delete_all
666 666 anon1 = User.anonymous
667 667 assert !anon1.new_record?
668 668 assert_kind_of AnonymousUser, anon1
669 669 anon2 = AnonymousUser.create(
670 670 :lastname => 'Anonymous', :firstname => '',
671 671 :mail => '', :login => '', :status => 0)
672 672 assert_equal 1, anon2.errors.count
673 673 end
674 674
675 675 def test_rss_key
676 676 assert_nil @jsmith.rss_token
677 677 key = @jsmith.rss_key
678 678 assert_equal 40, key.length
679 679
680 680 @jsmith.reload
681 681 assert_equal key, @jsmith.rss_key
682 682 end
683 683
684 684 def test_rss_key_should_not_be_generated_twice
685 685 assert_difference 'Token.count', 1 do
686 686 key1 = @jsmith.rss_key
687 687 key2 = @jsmith.rss_key
688 688 assert_equal key1, key2
689 689 end
690 690 end
691 691
692 692 def test_api_key_should_not_be_generated_twice
693 693 assert_difference 'Token.count', 1 do
694 694 key1 = @jsmith.api_key
695 695 key2 = @jsmith.api_key
696 696 assert_equal key1, key2
697 697 end
698 698 end
699 699
700 700 test "#api_key should generate a new one if the user doesn't have one" do
701 701 user = User.generate!(:api_token => nil)
702 702 assert_nil user.api_token
703 703
704 704 key = user.api_key
705 705 assert_equal 40, key.length
706 706 user.reload
707 707 assert_equal key, user.api_key
708 708 end
709 709
710 710 test "#api_key should return the existing api token value" do
711 711 user = User.generate!
712 712 token = Token.create!(:action => 'api')
713 713 user.api_token = token
714 714 assert user.save
715 715
716 716 assert_equal token.value, user.api_key
717 717 end
718 718
719 719 test "#find_by_api_key should return nil if no matching key is found" do
720 720 assert_nil User.find_by_api_key('zzzzzzzzz')
721 721 end
722 722
723 723 test "#find_by_api_key should return nil if the key is found for an inactive user" do
724 724 user = User.generate!
725 725 user.status = User::STATUS_LOCKED
726 726 token = Token.create!(:action => 'api')
727 727 user.api_token = token
728 728 user.save
729 729
730 730 assert_nil User.find_by_api_key(token.value)
731 731 end
732 732
733 733 test "#find_by_api_key should return the user if the key is found for an active user" do
734 734 user = User.generate!
735 735 token = Token.create!(:action => 'api')
736 736 user.api_token = token
737 737 user.save
738 738
739 739 assert_equal user, User.find_by_api_key(token.value)
740 740 end
741 741
742 742 def test_default_admin_account_changed_should_return_false_if_account_was_not_changed
743 743 user = User.find_by_login("admin")
744 744 user.password = "admin"
745 745 assert user.save(:validate => false)
746 746
747 747 assert_equal false, User.default_admin_account_changed?
748 748 end
749 749
750 750 def test_default_admin_account_changed_should_return_true_if_password_was_changed
751 751 user = User.find_by_login("admin")
752 752 user.password = "newpassword"
753 753 user.save!
754 754
755 755 assert_equal true, User.default_admin_account_changed?
756 756 end
757 757
758 758 def test_default_admin_account_changed_should_return_true_if_account_is_disabled
759 759 user = User.find_by_login("admin")
760 760 user.password = "admin"
761 761 user.status = User::STATUS_LOCKED
762 762 assert user.save(:validate => false)
763 763
764 764 assert_equal true, User.default_admin_account_changed?
765 765 end
766 766
767 767 def test_default_admin_account_changed_should_return_true_if_account_does_not_exist
768 768 user = User.find_by_login("admin")
769 769 user.destroy
770 770
771 771 assert_equal true, User.default_admin_account_changed?
772 772 end
773 773
774 774 def test_membership_with_project_should_return_membership
775 775 project = Project.find(1)
776 776
777 777 membership = @jsmith.membership(project)
778 778 assert_kind_of Member, membership
779 779 assert_equal @jsmith, membership.user
780 780 assert_equal project, membership.project
781 781 end
782 782
783 783 def test_membership_with_project_id_should_return_membership
784 784 project = Project.find(1)
785 785
786 786 membership = @jsmith.membership(1)
787 787 assert_kind_of Member, membership
788 788 assert_equal @jsmith, membership.user
789 789 assert_equal project, membership.project
790 790 end
791 791
792 792 def test_membership_for_non_member_should_return_nil
793 793 project = Project.find(1)
794 794
795 795 user = User.generate!
796 796 membership = user.membership(1)
797 797 assert_nil membership
798 798 end
799 799
800 800 def test_roles_for_project
801 801 # user with a role
802 802 roles = @jsmith.roles_for_project(Project.find(1))
803 803 assert_kind_of Role, roles.first
804 804 assert_equal "Manager", roles.first.name
805 805
806 806 # user with no role
807 807 assert_nil @dlopper.roles_for_project(Project.find(2)).detect {|role| role.member?}
808 808 end
809 809
810 810 def test_projects_by_role_for_user_with_role
811 811 user = User.find(2)
812 812 assert_kind_of Hash, user.projects_by_role
813 813 assert_equal 2, user.projects_by_role.size
814 814 assert_equal [1,5], user.projects_by_role[Role.find(1)].collect(&:id).sort
815 815 assert_equal [2], user.projects_by_role[Role.find(2)].collect(&:id).sort
816 816 end
817 817
818 818 def test_accessing_projects_by_role_with_no_projects_should_return_an_empty_array
819 819 user = User.find(2)
820 820 assert_equal [], user.projects_by_role[Role.find(3)]
821 821 # should not update the hash
822 822 assert_nil user.projects_by_role.values.detect(&:blank?)
823 823 end
824 824
825 825 def test_projects_by_role_for_user_with_no_role
826 826 user = User.generate!
827 827 assert_equal({}, user.projects_by_role)
828 828 end
829 829
830 830 def test_projects_by_role_for_anonymous
831 831 assert_equal({}, User.anonymous.projects_by_role)
832 832 end
833 833
834 834 def test_valid_notification_options
835 835 # without memberships
836 836 assert_equal 5, User.find(7).valid_notification_options.size
837 837 # with memberships
838 838 assert_equal 6, User.find(2).valid_notification_options.size
839 839 end
840 840
841 841 def test_valid_notification_options_class_method
842 842 assert_equal 5, User.valid_notification_options.size
843 843 assert_equal 5, User.valid_notification_options(User.find(7)).size
844 844 assert_equal 6, User.valid_notification_options(User.find(2)).size
845 845 end
846 846
847 847 def test_mail_notification_all
848 848 @jsmith.mail_notification = 'all'
849 849 @jsmith.notified_project_ids = []
850 850 @jsmith.save
851 851 @jsmith.reload
852 852 assert @jsmith.projects.first.recipients.include?(@jsmith.mail)
853 853 end
854 854
855 855 def test_mail_notification_selected
856 856 @jsmith.mail_notification = 'selected'
857 857 @jsmith.notified_project_ids = [1]
858 858 @jsmith.save
859 859 @jsmith.reload
860 860 assert Project.find(1).recipients.include?(@jsmith.mail)
861 861 end
862 862
863 863 def test_mail_notification_only_my_events
864 864 @jsmith.mail_notification = 'only_my_events'
865 865 @jsmith.notified_project_ids = []
866 866 @jsmith.save
867 867 @jsmith.reload
868 868 assert !@jsmith.projects.first.recipients.include?(@jsmith.mail)
869 869 end
870 870
871 871 def test_comments_sorting_preference
872 872 assert !@jsmith.wants_comments_in_reverse_order?
873 873 @jsmith.pref.comments_sorting = 'asc'
874 874 assert !@jsmith.wants_comments_in_reverse_order?
875 875 @jsmith.pref.comments_sorting = 'desc'
876 876 assert @jsmith.wants_comments_in_reverse_order?
877 877 end
878 878
879 879 def test_find_by_mail_should_be_case_insensitive
880 880 u = User.find_by_mail('JSmith@somenet.foo')
881 881 assert_not_nil u
882 882 assert_equal 'jsmith@somenet.foo', u.mail
883 883 end
884 884
885 885 def test_random_password
886 886 u = User.new
887 887 u.random_password
888 888 assert !u.password.blank?
889 889 assert !u.password_confirmation.blank?
890 890 end
891 891
892 892 test "#change_password_allowed? should be allowed if no auth source is set" do
893 893 user = User.generate!
894 894 assert user.change_password_allowed?
895 895 end
896 896
897 897 test "#change_password_allowed? should delegate to the auth source" do
898 898 user = User.generate!
899 899
900 900 allowed_auth_source = AuthSource.generate!
901 901 def allowed_auth_source.allow_password_changes?; true; end
902 902
903 903 denied_auth_source = AuthSource.generate!
904 904 def denied_auth_source.allow_password_changes?; false; end
905 905
906 906 assert user.change_password_allowed?
907 907
908 908 user.auth_source = allowed_auth_source
909 909 assert user.change_password_allowed?, "User not allowed to change password, though auth source does"
910 910
911 911 user.auth_source = denied_auth_source
912 912 assert !user.change_password_allowed?, "User allowed to change password, though auth source does not"
913 913 end
914 914
915 915 def test_own_account_deletable_should_be_true_with_unsubscrive_enabled
916 916 with_settings :unsubscribe => '1' do
917 917 assert_equal true, User.find(2).own_account_deletable?
918 918 end
919 919 end
920 920
921 921 def test_own_account_deletable_should_be_false_with_unsubscrive_disabled
922 922 with_settings :unsubscribe => '0' do
923 923 assert_equal false, User.find(2).own_account_deletable?
924 924 end
925 925 end
926 926
927 927 def test_own_account_deletable_should_be_false_for_a_single_admin
928 928 User.delete_all(["admin = ? AND id <> ?", true, 1])
929 929
930 930 with_settings :unsubscribe => '1' do
931 931 assert_equal false, User.find(1).own_account_deletable?
932 932 end
933 933 end
934 934
935 935 def test_own_account_deletable_should_be_true_for_an_admin_if_other_admin_exists
936 936 User.generate! do |user|
937 937 user.admin = true
938 938 end
939 939
940 940 with_settings :unsubscribe => '1' do
941 941 assert_equal true, User.find(1).own_account_deletable?
942 942 end
943 943 end
944 944
945 945 context "#allowed_to?" do
946 946 context "with a unique project" do
947 947 should "return false if project is archived" do
948 948 project = Project.find(1)
949 949 Project.any_instance.stubs(:status).returns(Project::STATUS_ARCHIVED)
950 950 assert_equal false, @admin.allowed_to?(:view_issues, Project.find(1))
951 951 end
952 952
953 953 should "return false for write action if project is closed" do
954 954 project = Project.find(1)
955 955 Project.any_instance.stubs(:status).returns(Project::STATUS_CLOSED)
956 956 assert_equal false, @admin.allowed_to?(:edit_project, Project.find(1))
957 957 end
958 958
959 959 should "return true for read action if project is closed" do
960 960 project = Project.find(1)
961 961 Project.any_instance.stubs(:status).returns(Project::STATUS_CLOSED)
962 962 assert_equal true, @admin.allowed_to?(:view_project, Project.find(1))
963 963 end
964 964
965 965 should "return false if related module is disabled" do
966 966 project = Project.find(1)
967 967 project.enabled_module_names = ["issue_tracking"]
968 968 assert_equal true, @admin.allowed_to?(:add_issues, project)
969 969 assert_equal false, @admin.allowed_to?(:view_wiki_pages, project)
970 970 end
971 971
972 972 should "authorize nearly everything for admin users" do
973 973 project = Project.find(1)
974 974 assert ! @admin.member_of?(project)
975 975 %w(edit_issues delete_issues manage_news add_documents manage_wiki).each do |p|
976 976 assert_equal true, @admin.allowed_to?(p.to_sym, project)
977 977 end
978 978 end
979 979
980 980 should "authorize normal users depending on their roles" do
981 981 project = Project.find(1)
982 982 assert_equal true, @jsmith.allowed_to?(:delete_messages, project) #Manager
983 983 assert_equal false, @dlopper.allowed_to?(:delete_messages, project) #Developper
984 984 end
985 985 end
986 986
987 987 context "with multiple projects" do
988 988 should "return false if array is empty" do
989 989 assert_equal false, @admin.allowed_to?(:view_project, [])
990 990 end
991 991
992 992 should "return true only if user has permission on all these projects" do
993 993 assert_equal true, @admin.allowed_to?(:view_project, Project.all)
994 994 assert_equal false, @dlopper.allowed_to?(:view_project, Project.all) #cannot see Project(2)
995 995 assert_equal true, @jsmith.allowed_to?(:edit_issues, @jsmith.projects) #Manager or Developer everywhere
996 996 assert_equal false, @jsmith.allowed_to?(:delete_issue_watchers, @jsmith.projects) #Dev cannot delete_issue_watchers
997 997 end
998 998
999 999 should "behave correctly with arrays of 1 project" do
1000 1000 assert_equal false, User.anonymous.allowed_to?(:delete_issues, [Project.first])
1001 1001 end
1002 1002 end
1003 1003
1004 1004 context "with options[:global]" do
1005 1005 should "authorize if user has at least one role that has this permission" do
1006 1006 @dlopper2 = User.find(5) #only Developper on a project, not Manager anywhere
1007 1007 @anonymous = User.find(6)
1008 1008 assert_equal true, @jsmith.allowed_to?(:delete_issue_watchers, nil, :global => true)
1009 1009 assert_equal false, @dlopper2.allowed_to?(:delete_issue_watchers, nil, :global => true)
1010 1010 assert_equal true, @dlopper2.allowed_to?(:add_issues, nil, :global => true)
1011 1011 assert_equal false, @anonymous.allowed_to?(:add_issues, nil, :global => true)
1012 1012 assert_equal true, @anonymous.allowed_to?(:view_issues, nil, :global => true)
1013 1013 end
1014 1014 end
1015 1015 end
1016 1016
1017 1017 context "User#notify_about?" do
1018 1018 context "Issues" do
1019 1019 setup do
1020 1020 @project = Project.find(1)
1021 1021 @author = User.generate!
1022 1022 @assignee = User.generate!
1023 1023 @issue = Issue.generate!(:project => @project, :assigned_to => @assignee, :author => @author)
1024 1024 end
1025 1025
1026 1026 should "be true for a user with :all" do
1027 1027 @author.update_attribute(:mail_notification, 'all')
1028 1028 assert @author.notify_about?(@issue)
1029 1029 end
1030 1030
1031 1031 should "be false for a user with :none" do
1032 1032 @author.update_attribute(:mail_notification, 'none')
1033 1033 assert ! @author.notify_about?(@issue)
1034 1034 end
1035 1035
1036 1036 should "be false for a user with :only_my_events and isn't an author, creator, or assignee" do
1037 1037 @user = User.generate!(:mail_notification => 'only_my_events')
1038 1038 Member.create!(:user => @user, :project => @project, :role_ids => [1])
1039 1039 assert ! @user.notify_about?(@issue)
1040 1040 end
1041 1041
1042 1042 should "be true for a user with :only_my_events and is the author" do
1043 1043 @author.update_attribute(:mail_notification, 'only_my_events')
1044 1044 assert @author.notify_about?(@issue)
1045 1045 end
1046 1046
1047 1047 should "be true for a user with :only_my_events and is the assignee" do
1048 1048 @assignee.update_attribute(:mail_notification, 'only_my_events')
1049 1049 assert @assignee.notify_about?(@issue)
1050 1050 end
1051 1051
1052 1052 should "be true for a user with :only_assigned and is the assignee" do
1053 1053 @assignee.update_attribute(:mail_notification, 'only_assigned')
1054 1054 assert @assignee.notify_about?(@issue)
1055 1055 end
1056 1056
1057 1057 should "be false for a user with :only_assigned and is not the assignee" do
1058 1058 @author.update_attribute(:mail_notification, 'only_assigned')
1059 1059 assert ! @author.notify_about?(@issue)
1060 1060 end
1061 1061
1062 1062 should "be true for a user with :only_owner and is the author" do
1063 1063 @author.update_attribute(:mail_notification, 'only_owner')
1064 1064 assert @author.notify_about?(@issue)
1065 1065 end
1066 1066
1067 1067 should "be false for a user with :only_owner and is not the author" do
1068 1068 @assignee.update_attribute(:mail_notification, 'only_owner')
1069 1069 assert ! @assignee.notify_about?(@issue)
1070 1070 end
1071 1071
1072 1072 should "be true for a user with :selected and is the author" do
1073 1073 @author.update_attribute(:mail_notification, 'selected')
1074 1074 assert @author.notify_about?(@issue)
1075 1075 end
1076 1076
1077 1077 should "be true for a user with :selected and is the assignee" do
1078 1078 @assignee.update_attribute(:mail_notification, 'selected')
1079 1079 assert @assignee.notify_about?(@issue)
1080 1080 end
1081 1081
1082 1082 should "be false for a user with :selected and is not the author or assignee" do
1083 1083 @user = User.generate!(:mail_notification => 'selected')
1084 1084 Member.create!(:user => @user, :project => @project, :role_ids => [1])
1085 1085 assert ! @user.notify_about?(@issue)
1086 1086 end
1087 1087 end
1088 1088 end
1089 1089
1090 1090 def test_notify_about_news
1091 1091 user = User.generate!
1092 1092 news = News.new
1093 1093
1094 1094 User::MAIL_NOTIFICATION_OPTIONS.map(&:first).each do |option|
1095 1095 user.mail_notification = option
1096 1096 assert_equal (option != 'none'), user.notify_about?(news)
1097 1097 end
1098 1098 end
1099 1099
1100 1100 def test_salt_unsalted_passwords
1101 1101 # Restore a user with an unsalted password
1102 1102 user = User.find(1)
1103 1103 user.salt = nil
1104 1104 user.hashed_password = User.hash_password("unsalted")
1105 1105 user.save!
1106 1106
1107 1107 User.salt_unsalted_passwords!
1108 1108
1109 1109 user.reload
1110 1110 # Salt added
1111 1111 assert !user.salt.blank?
1112 1112 # Password still valid
1113 1113 assert user.check_password?("unsalted")
1114 1114 assert_equal user, User.try_to_login(user.login, "unsalted")
1115 1115 end
1116 1116
1117 1117 if Object.const_defined?(:OpenID)
1118 1118 def test_setting_identity_url
1119 1119 normalized_open_id_url = 'http://example.com/'
1120 1120 u = User.new( :identity_url => 'http://example.com/' )
1121 1121 assert_equal normalized_open_id_url, u.identity_url
1122 1122 end
1123 1123
1124 1124 def test_setting_identity_url_without_trailing_slash
1125 1125 normalized_open_id_url = 'http://example.com/'
1126 1126 u = User.new( :identity_url => 'http://example.com' )
1127 1127 assert_equal normalized_open_id_url, u.identity_url
1128 1128 end
1129 1129
1130 1130 def test_setting_identity_url_without_protocol
1131 1131 normalized_open_id_url = 'http://example.com/'
1132 1132 u = User.new( :identity_url => 'example.com' )
1133 1133 assert_equal normalized_open_id_url, u.identity_url
1134 1134 end
1135 1135
1136 1136 def test_setting_blank_identity_url
1137 1137 u = User.new( :identity_url => 'example.com' )
1138 1138 u.identity_url = ''
1139 1139 assert u.identity_url.blank?
1140 1140 end
1141 1141
1142 1142 def test_setting_invalid_identity_url
1143 1143 u = User.new( :identity_url => 'this is not an openid url' )
1144 1144 assert u.identity_url.blank?
1145 1145 end
1146 1146 else
1147 1147 puts "Skipping openid tests."
1148 1148 end
1149 1149 end
General Comments 0
You need to be logged in to leave comments. Login now