##// END OF EJS Templates
add missing fixtures at unit user test in running on empty database...
Toshi MARUYAMA -
r7325:cf8564987e58
parent child
Show More
@@ -1,843 +1,851
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 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 fixtures :users, :members, :projects, :roles, :member_roles, :auth_sources
21 fixtures :users, :members, :projects, :roles, :member_roles, :auth_sources,
22 :trackers, :issue_statuses,
23 :projects_trackers,
24 :watchers,
25 :issue_categories, :enumerations, :issues,
26 :journals, :journal_details,
27 :groups_users,
28 :enabled_modules,
29 :workflows
22 30
23 31 def setup
24 32 @admin = User.find(1)
25 33 @jsmith = User.find(2)
26 34 @dlopper = User.find(3)
27 35 end
28 36
29 37 test 'object_daddy creation' do
30 38 User.generate_with_protected!(:firstname => 'Testing connection')
31 39 User.generate_with_protected!(:firstname => 'Testing connection')
32 40 assert_equal 2, User.count(:all, :conditions => {:firstname => 'Testing connection'})
33 41 end
34 42
35 43 def test_truth
36 44 assert_kind_of User, @jsmith
37 45 end
38 46
39 47 def test_mail_should_be_stripped
40 48 u = User.new
41 49 u.mail = " foo@bar.com "
42 50 assert_equal "foo@bar.com", u.mail
43 51 end
44 52
45 53 def test_mail_validation
46 54 u = User.new
47 55 u.mail = ''
48 56 assert !u.valid?
49 57 assert_equal I18n.translate('activerecord.errors.messages.blank'), u.errors.on(:mail)
50 58 end
51 59
52 60 def test_create
53 61 user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
54 62
55 63 user.login = "jsmith"
56 64 user.password, user.password_confirmation = "password", "password"
57 65 # login uniqueness
58 66 assert !user.save
59 67 assert_equal 1, user.errors.count
60 68
61 69 user.login = "newuser"
62 70 user.password, user.password_confirmation = "passwd", "password"
63 71 # password confirmation
64 72 assert !user.save
65 73 assert_equal 1, user.errors.count
66 74
67 75 user.password, user.password_confirmation = "password", "password"
68 76 assert user.save
69 77 end
70 78
71 79 context "User#before_create" do
72 80 should "set the mail_notification to the default Setting" do
73 81 @user1 = User.generate_with_protected!
74 82 assert_equal 'only_my_events', @user1.mail_notification
75 83
76 84 with_settings :default_notification_option => 'all' do
77 85 @user2 = User.generate_with_protected!
78 86 assert_equal 'all', @user2.mail_notification
79 87 end
80 88 end
81 89 end
82 90
83 91 context "User.login" do
84 92 should "be case-insensitive." do
85 93 u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
86 94 u.login = 'newuser'
87 95 u.password, u.password_confirmation = "password", "password"
88 96 assert u.save
89 97
90 98 u = User.new(:firstname => "Similar", :lastname => "User", :mail => "similaruser@somenet.foo")
91 99 u.login = 'NewUser'
92 100 u.password, u.password_confirmation = "password", "password"
93 101 assert !u.save
94 102 assert_equal I18n.translate('activerecord.errors.messages.taken'), u.errors.on(:login)
95 103 end
96 104 end
97 105
98 106 def test_mail_uniqueness_should_not_be_case_sensitive
99 107 u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
100 108 u.login = 'newuser1'
101 109 u.password, u.password_confirmation = "password", "password"
102 110 assert u.save
103 111
104 112 u = User.new(:firstname => "new", :lastname => "user", :mail => "newUser@Somenet.foo")
105 113 u.login = 'newuser2'
106 114 u.password, u.password_confirmation = "password", "password"
107 115 assert !u.save
108 116 assert_equal I18n.translate('activerecord.errors.messages.taken'), u.errors.on(:mail)
109 117 end
110 118
111 119 def test_update
112 120 assert_equal "admin", @admin.login
113 121 @admin.login = "john"
114 122 assert @admin.save, @admin.errors.full_messages.join("; ")
115 123 @admin.reload
116 124 assert_equal "john", @admin.login
117 125 end
118 126
119 127 def test_destroy_should_delete_members_and_roles
120 128 members = Member.find_all_by_user_id(2)
121 129 ms = members.size
122 130 rs = members.collect(&:roles).flatten.size
123 131
124 132 assert_difference 'Member.count', - ms do
125 133 assert_difference 'MemberRole.count', - rs do
126 134 User.find(2).destroy
127 135 end
128 136 end
129 137
130 138 assert_nil User.find_by_id(2)
131 139 assert Member.find_all_by_user_id(2).empty?
132 140 end
133 141
134 142 def test_destroy_should_update_attachments
135 143 attachment = Attachment.create!(:container => Project.find(1),
136 144 :file => uploaded_test_file("testfile.txt", "text/plain"),
137 145 :author_id => 2)
138 146
139 147 User.find(2).destroy
140 148 assert_nil User.find_by_id(2)
141 149 assert_equal User.anonymous, attachment.reload.author
142 150 end
143 151
144 152 def test_destroy_should_update_comments
145 153 comment = Comment.create!(
146 154 :commented => News.create!(:project_id => 1, :author_id => 1, :title => 'foo', :description => 'foo'),
147 155 :author => User.find(2),
148 156 :comments => 'foo'
149 157 )
150 158
151 159 User.find(2).destroy
152 160 assert_nil User.find_by_id(2)
153 161 assert_equal User.anonymous, comment.reload.author
154 162 end
155 163
156 164 def test_destroy_should_update_issues
157 165 issue = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'foo')
158 166
159 167 User.find(2).destroy
160 168 assert_nil User.find_by_id(2)
161 169 assert_equal User.anonymous, issue.reload.author
162 170 end
163 171
164 172 def test_destroy_should_unassign_issues
165 173 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo', :assigned_to_id => 2)
166 174
167 175 User.find(2).destroy
168 176 assert_nil User.find_by_id(2)
169 177 assert_nil issue.reload.assigned_to
170 178 end
171 179
172 180 def test_destroy_should_update_journals
173 181 issue = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'foo')
174 182 issue.init_journal(User.find(2), "update")
175 183 issue.save!
176 184
177 185 User.find(2).destroy
178 186 assert_nil User.find_by_id(2)
179 187 assert_equal User.anonymous, issue.journals.first.reload.user
180 188 end
181 189
182 190 def test_destroy_should_update_journal_details_old_value
183 191 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo', :assigned_to_id => 2)
184 192 issue.init_journal(User.find(1), "update")
185 193 issue.assigned_to_id = nil
186 194 assert_difference 'JournalDetail.count' do
187 195 issue.save!
188 196 end
189 197 journal_detail = JournalDetail.first(:order => 'id DESC')
190 198 assert_equal '2', journal_detail.old_value
191 199
192 200 User.find(2).destroy
193 201 assert_nil User.find_by_id(2)
194 202 assert_equal User.anonymous.id.to_s, journal_detail.reload.old_value
195 203 end
196 204
197 205 def test_destroy_should_update_journal_details_value
198 206 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo')
199 207 issue.init_journal(User.find(1), "update")
200 208 issue.assigned_to_id = 2
201 209 assert_difference 'JournalDetail.count' do
202 210 issue.save!
203 211 end
204 212 journal_detail = JournalDetail.first(:order => 'id DESC')
205 213 assert_equal '2', journal_detail.value
206 214
207 215 User.find(2).destroy
208 216 assert_nil User.find_by_id(2)
209 217 assert_equal User.anonymous.id.to_s, journal_detail.reload.value
210 218 end
211 219
212 220 def test_destroy_should_update_messages
213 221 board = Board.create!(:project_id => 1, :name => 'Board', :description => 'Board')
214 222 message = Message.create!(:board_id => board.id, :author_id => 2, :subject => 'foo', :content => 'foo')
215 223
216 224 User.find(2).destroy
217 225 assert_nil User.find_by_id(2)
218 226 assert_equal User.anonymous, message.reload.author
219 227 end
220 228
221 229 def test_destroy_should_update_news
222 230 news = News.create!(:project_id => 1, :author_id => 2, :title => 'foo', :description => 'foo')
223 231
224 232 User.find(2).destroy
225 233 assert_nil User.find_by_id(2)
226 234 assert_equal User.anonymous, news.reload.author
227 235 end
228 236
229 237 def test_destroy_should_delete_private_queries
230 238 query = Query.new(:name => 'foo', :is_public => false)
231 239 query.project_id = 1
232 240 query.user_id = 2
233 241 query.save!
234 242
235 243 User.find(2).destroy
236 244 assert_nil User.find_by_id(2)
237 245 assert_nil Query.find_by_id(query.id)
238 246 end
239 247
240 248 def test_destroy_should_update_public_queries
241 249 query = Query.new(:name => 'foo', :is_public => true)
242 250 query.project_id = 1
243 251 query.user_id = 2
244 252 query.save!
245 253
246 254 User.find(2).destroy
247 255 assert_nil User.find_by_id(2)
248 256 assert_equal User.anonymous, query.reload.user
249 257 end
250 258
251 259 def test_destroy_should_update_time_entries
252 260 entry = TimeEntry.new(:hours => '2', :spent_on => Date.today, :activity => TimeEntryActivity.create!(:name => 'foo'))
253 261 entry.project_id = 1
254 262 entry.user_id = 2
255 263 entry.save!
256 264
257 265 User.find(2).destroy
258 266 assert_nil User.find_by_id(2)
259 267 assert_equal User.anonymous, entry.reload.user
260 268 end
261 269
262 270 def test_destroy_should_delete_tokens
263 271 token = Token.create!(:user_id => 2, :value => 'foo')
264 272
265 273 User.find(2).destroy
266 274 assert_nil User.find_by_id(2)
267 275 assert_nil Token.find_by_id(token.id)
268 276 end
269 277
270 278 def test_destroy_should_delete_watchers
271 279 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo')
272 280 watcher = Watcher.create!(:user_id => 2, :watchable => issue)
273 281
274 282 User.find(2).destroy
275 283 assert_nil User.find_by_id(2)
276 284 assert_nil Watcher.find_by_id(watcher.id)
277 285 end
278 286
279 287 def test_destroy_should_update_wiki_contents
280 288 wiki_content = WikiContent.create!(
281 289 :text => 'foo',
282 290 :author_id => 2,
283 291 :page => WikiPage.create!(:title => 'Foo', :wiki => Wiki.create!(:project_id => 1, :start_page => 'Start'))
284 292 )
285 293 wiki_content.text = 'bar'
286 294 assert_difference 'WikiContent::Version.count' do
287 295 wiki_content.save!
288 296 end
289 297
290 298 User.find(2).destroy
291 299 assert_nil User.find_by_id(2)
292 300 assert_equal User.anonymous, wiki_content.reload.author
293 301 wiki_content.versions.each do |version|
294 302 assert_equal User.anonymous, version.reload.author
295 303 end
296 304 end
297 305
298 306 def test_destroy_should_nullify_issue_categories
299 307 category = IssueCategory.create!(:project_id => 1, :assigned_to_id => 2, :name => 'foo')
300 308
301 309 User.find(2).destroy
302 310 assert_nil User.find_by_id(2)
303 311 assert_nil category.reload.assigned_to_id
304 312 end
305 313
306 314 def test_destroy_should_nullify_changesets
307 315 changeset = Changeset.create!(
308 316 :repository => Repository::Subversion.create!(
309 317 :project_id => 1,
310 318 :url => 'file:///var/svn'
311 319 ),
312 320 :revision => '12',
313 321 :committed_on => Time.now,
314 322 :committer => 'jsmith'
315 323 )
316 324 assert_equal 2, changeset.user_id
317 325
318 326 User.find(2).destroy
319 327 assert_nil User.find_by_id(2)
320 328 assert_nil changeset.reload.user_id
321 329 end
322 330
323 331 def test_anonymous_user_should_not_be_destroyable
324 332 assert_no_difference 'User.count' do
325 333 assert_equal false, User.anonymous.destroy
326 334 end
327 335 end
328 336
329 337 def test_validate_login_presence
330 338 @admin.login = ""
331 339 assert !@admin.save
332 340 assert_equal 1, @admin.errors.count
333 341 end
334 342
335 343 def test_validate_mail_notification_inclusion
336 344 u = User.new
337 345 u.mail_notification = 'foo'
338 346 u.save
339 347 assert_not_nil u.errors.on(:mail_notification)
340 348 end
341 349
342 350 context "User#try_to_login" do
343 351 should "fall-back to case-insensitive if user login is not found as-typed." do
344 352 user = User.try_to_login("AdMin", "admin")
345 353 assert_kind_of User, user
346 354 assert_equal "admin", user.login
347 355 end
348 356
349 357 should "select the exact matching user first" do
350 358 case_sensitive_user = User.generate_with_protected!(:login => 'changed', :password => 'admin', :password_confirmation => 'admin')
351 359 # bypass validations to make it appear like existing data
352 360 case_sensitive_user.update_attribute(:login, 'ADMIN')
353 361
354 362 user = User.try_to_login("ADMIN", "admin")
355 363 assert_kind_of User, user
356 364 assert_equal "ADMIN", user.login
357 365
358 366 end
359 367 end
360 368
361 369 def test_password
362 370 user = User.try_to_login("admin", "admin")
363 371 assert_kind_of User, user
364 372 assert_equal "admin", user.login
365 373 user.password = "hello"
366 374 assert user.save
367 375
368 376 user = User.try_to_login("admin", "hello")
369 377 assert_kind_of User, user
370 378 assert_equal "admin", user.login
371 379 end
372 380
373 381 def test_validate_password_length
374 382 with_settings :password_min_length => '100' do
375 383 user = User.new(:firstname => "new100", :lastname => "user100", :mail => "newuser100@somenet.foo")
376 384 user.login = "newuser100"
377 385 user.password, user.password_confirmation = "password100", "password100"
378 386 assert !user.save
379 387 assert_equal 1, user.errors.count
380 388 end
381 389 end
382 390
383 391 def test_name_format
384 392 assert_equal 'Smith, John', @jsmith.name(:lastname_coma_firstname)
385 393 Setting.user_format = :firstname_lastname
386 394 assert_equal 'John Smith', @jsmith.reload.name
387 395 Setting.user_format = :username
388 396 assert_equal 'jsmith', @jsmith.reload.name
389 397 end
390 398
391 399 def test_lock
392 400 user = User.try_to_login("jsmith", "jsmith")
393 401 assert_equal @jsmith, user
394 402
395 403 @jsmith.status = User::STATUS_LOCKED
396 404 assert @jsmith.save
397 405
398 406 user = User.try_to_login("jsmith", "jsmith")
399 407 assert_equal nil, user
400 408 end
401 409
402 410 context ".try_to_login" do
403 411 context "with good credentials" do
404 412 should "return the user" do
405 413 user = User.try_to_login("admin", "admin")
406 414 assert_kind_of User, user
407 415 assert_equal "admin", user.login
408 416 end
409 417 end
410 418
411 419 context "with wrong credentials" do
412 420 should "return nil" do
413 421 assert_nil User.try_to_login("admin", "foo")
414 422 end
415 423 end
416 424 end
417 425
418 426 if ldap_configured?
419 427 context "#try_to_login using LDAP" do
420 428 context "with failed connection to the LDAP server" do
421 429 should "return nil" do
422 430 @auth_source = AuthSourceLdap.find(1)
423 431 AuthSource.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::LdapError, 'Cannot connect')
424 432
425 433 assert_equal nil, User.try_to_login('edavis', 'wrong')
426 434 end
427 435 end
428 436
429 437 context "with an unsuccessful authentication" do
430 438 should "return nil" do
431 439 assert_equal nil, User.try_to_login('edavis', 'wrong')
432 440 end
433 441 end
434 442
435 443 context "on the fly registration" do
436 444 setup do
437 445 @auth_source = AuthSourceLdap.find(1)
438 446 end
439 447
440 448 context "with a successful authentication" do
441 449 should "create a new user account if it doesn't exist" do
442 450 assert_difference('User.count') do
443 451 user = User.try_to_login('edavis', '123456')
444 452 assert !user.admin?
445 453 end
446 454 end
447 455
448 456 should "retrieve existing user" do
449 457 user = User.try_to_login('edavis', '123456')
450 458 user.admin = true
451 459 user.save!
452 460
453 461 assert_no_difference('User.count') do
454 462 user = User.try_to_login('edavis', '123456')
455 463 assert user.admin?
456 464 end
457 465 end
458 466 end
459 467 end
460 468 end
461 469
462 470 else
463 471 puts "Skipping LDAP tests."
464 472 end
465 473
466 474 def test_create_anonymous
467 475 AnonymousUser.delete_all
468 476 anon = User.anonymous
469 477 assert !anon.new_record?
470 478 assert_kind_of AnonymousUser, anon
471 479 end
472 480
473 481 def test_ensure_single_anonymous_user
474 482 AnonymousUser.delete_all
475 483 anon1 = User.anonymous
476 484 assert !anon1.new_record?
477 485 assert_kind_of AnonymousUser, anon1
478 486 anon2 = AnonymousUser.create(
479 487 :lastname => 'Anonymous', :firstname => '',
480 488 :mail => '', :login => '', :status => 0)
481 489 assert_equal 1, anon2.errors.count
482 490 end
483 491
484 492 should_have_one :rss_token
485 493
486 494 def test_rss_key
487 495 assert_nil @jsmith.rss_token
488 496 key = @jsmith.rss_key
489 497 assert_equal 40, key.length
490 498
491 499 @jsmith.reload
492 500 assert_equal key, @jsmith.rss_key
493 501 end
494 502
495 503
496 504 should_have_one :api_token
497 505
498 506 context "User#api_key" do
499 507 should "generate a new one if the user doesn't have one" do
500 508 user = User.generate_with_protected!(:api_token => nil)
501 509 assert_nil user.api_token
502 510
503 511 key = user.api_key
504 512 assert_equal 40, key.length
505 513 user.reload
506 514 assert_equal key, user.api_key
507 515 end
508 516
509 517 should "return the existing api token value" do
510 518 user = User.generate_with_protected!
511 519 token = Token.generate!(:action => 'api')
512 520 user.api_token = token
513 521 assert user.save
514 522
515 523 assert_equal token.value, user.api_key
516 524 end
517 525 end
518 526
519 527 context "User#find_by_api_key" do
520 528 should "return nil if no matching key is found" do
521 529 assert_nil User.find_by_api_key('zzzzzzzzz')
522 530 end
523 531
524 532 should "return nil if the key is found for an inactive user" do
525 533 user = User.generate_with_protected!(:status => User::STATUS_LOCKED)
526 534 token = Token.generate!(:action => 'api')
527 535 user.api_token = token
528 536 user.save
529 537
530 538 assert_nil User.find_by_api_key(token.value)
531 539 end
532 540
533 541 should "return the user if the key is found for an active user" do
534 542 user = User.generate_with_protected!(:status => User::STATUS_ACTIVE)
535 543 token = Token.generate!(:action => 'api')
536 544 user.api_token = token
537 545 user.save
538 546
539 547 assert_equal user, User.find_by_api_key(token.value)
540 548 end
541 549 end
542 550
543 551 def test_roles_for_project
544 552 # user with a role
545 553 roles = @jsmith.roles_for_project(Project.find(1))
546 554 assert_kind_of Role, roles.first
547 555 assert_equal "Manager", roles.first.name
548 556
549 557 # user with no role
550 558 assert_nil @dlopper.roles_for_project(Project.find(2)).detect {|role| role.member?}
551 559 end
552 560
553 561 def test_projects_by_role_for_user_with_role
554 562 user = User.find(2)
555 563 assert_kind_of Hash, user.projects_by_role
556 564 assert_equal 2, user.projects_by_role.size
557 565 assert_equal [1,5], user.projects_by_role[Role.find(1)].collect(&:id).sort
558 566 assert_equal [2], user.projects_by_role[Role.find(2)].collect(&:id).sort
559 567 end
560 568
561 569 def test_projects_by_role_for_user_with_no_role
562 570 user = User.generate!
563 571 assert_equal({}, user.projects_by_role)
564 572 end
565 573
566 574 def test_projects_by_role_for_anonymous
567 575 assert_equal({}, User.anonymous.projects_by_role)
568 576 end
569 577
570 578 def test_valid_notification_options
571 579 # without memberships
572 580 assert_equal 5, User.find(7).valid_notification_options.size
573 581 # with memberships
574 582 assert_equal 6, User.find(2).valid_notification_options.size
575 583 end
576 584
577 585 def test_valid_notification_options_class_method
578 586 assert_equal 5, User.valid_notification_options.size
579 587 assert_equal 5, User.valid_notification_options(User.find(7)).size
580 588 assert_equal 6, User.valid_notification_options(User.find(2)).size
581 589 end
582 590
583 591 def test_mail_notification_all
584 592 @jsmith.mail_notification = 'all'
585 593 @jsmith.notified_project_ids = []
586 594 @jsmith.save
587 595 @jsmith.reload
588 596 assert @jsmith.projects.first.recipients.include?(@jsmith.mail)
589 597 end
590 598
591 599 def test_mail_notification_selected
592 600 @jsmith.mail_notification = 'selected'
593 601 @jsmith.notified_project_ids = [1]
594 602 @jsmith.save
595 603 @jsmith.reload
596 604 assert Project.find(1).recipients.include?(@jsmith.mail)
597 605 end
598 606
599 607 def test_mail_notification_only_my_events
600 608 @jsmith.mail_notification = 'only_my_events'
601 609 @jsmith.notified_project_ids = []
602 610 @jsmith.save
603 611 @jsmith.reload
604 612 assert !@jsmith.projects.first.recipients.include?(@jsmith.mail)
605 613 end
606 614
607 615 def test_comments_sorting_preference
608 616 assert !@jsmith.wants_comments_in_reverse_order?
609 617 @jsmith.pref.comments_sorting = 'asc'
610 618 assert !@jsmith.wants_comments_in_reverse_order?
611 619 @jsmith.pref.comments_sorting = 'desc'
612 620 assert @jsmith.wants_comments_in_reverse_order?
613 621 end
614 622
615 623 def test_find_by_mail_should_be_case_insensitive
616 624 u = User.find_by_mail('JSmith@somenet.foo')
617 625 assert_not_nil u
618 626 assert_equal 'jsmith@somenet.foo', u.mail
619 627 end
620 628
621 629 def test_random_password
622 630 u = User.new
623 631 u.random_password
624 632 assert !u.password.blank?
625 633 assert !u.password_confirmation.blank?
626 634 end
627 635
628 636 context "#change_password_allowed?" do
629 637 should "be allowed if no auth source is set" do
630 638 user = User.generate_with_protected!
631 639 assert user.change_password_allowed?
632 640 end
633 641
634 642 should "delegate to the auth source" do
635 643 user = User.generate_with_protected!
636 644
637 645 allowed_auth_source = AuthSource.generate!
638 646 def allowed_auth_source.allow_password_changes?; true; end
639 647
640 648 denied_auth_source = AuthSource.generate!
641 649 def denied_auth_source.allow_password_changes?; false; end
642 650
643 651 assert user.change_password_allowed?
644 652
645 653 user.auth_source = allowed_auth_source
646 654 assert user.change_password_allowed?, "User not allowed to change password, though auth source does"
647 655
648 656 user.auth_source = denied_auth_source
649 657 assert !user.change_password_allowed?, "User allowed to change password, though auth source does not"
650 658 end
651 659
652 660 end
653 661
654 662 context "#allowed_to?" do
655 663 context "with a unique project" do
656 664 should "return false if project is archived" do
657 665 project = Project.find(1)
658 666 Project.any_instance.stubs(:status).returns(Project::STATUS_ARCHIVED)
659 667 assert ! @admin.allowed_to?(:view_issues, Project.find(1))
660 668 end
661 669
662 670 should "return false if related module is disabled" do
663 671 project = Project.find(1)
664 672 project.enabled_module_names = ["issue_tracking"]
665 673 assert @admin.allowed_to?(:add_issues, project)
666 674 assert ! @admin.allowed_to?(:view_wiki_pages, project)
667 675 end
668 676
669 677 should "authorize nearly everything for admin users" do
670 678 project = Project.find(1)
671 679 assert ! @admin.member_of?(project)
672 680 %w(edit_issues delete_issues manage_news manage_documents manage_wiki).each do |p|
673 681 assert @admin.allowed_to?(p.to_sym, project)
674 682 end
675 683 end
676 684
677 685 should "authorize normal users depending on their roles" do
678 686 project = Project.find(1)
679 687 assert @jsmith.allowed_to?(:delete_messages, project) #Manager
680 688 assert ! @dlopper.allowed_to?(:delete_messages, project) #Developper
681 689 end
682 690 end
683 691
684 692 context "with multiple projects" do
685 693 should "return false if array is empty" do
686 694 assert ! @admin.allowed_to?(:view_project, [])
687 695 end
688 696
689 697 should "return true only if user has permission on all these projects" do
690 698 assert @admin.allowed_to?(:view_project, Project.all)
691 699 assert ! @dlopper.allowed_to?(:view_project, Project.all) #cannot see Project(2)
692 700 assert @jsmith.allowed_to?(:edit_issues, @jsmith.projects) #Manager or Developer everywhere
693 701 assert ! @jsmith.allowed_to?(:delete_issue_watchers, @jsmith.projects) #Dev cannot delete_issue_watchers
694 702 end
695 703
696 704 should "behave correctly with arrays of 1 project" do
697 705 assert ! User.anonymous.allowed_to?(:delete_issues, [Project.first])
698 706 end
699 707 end
700 708
701 709 context "with options[:global]" do
702 710 should "authorize if user has at least one role that has this permission" do
703 711 @dlopper2 = User.find(5) #only Developper on a project, not Manager anywhere
704 712 @anonymous = User.find(6)
705 713 assert @jsmith.allowed_to?(:delete_issue_watchers, nil, :global => true)
706 714 assert ! @dlopper2.allowed_to?(:delete_issue_watchers, nil, :global => true)
707 715 assert @dlopper2.allowed_to?(:add_issues, nil, :global => true)
708 716 assert ! @anonymous.allowed_to?(:add_issues, nil, :global => true)
709 717 assert @anonymous.allowed_to?(:view_issues, nil, :global => true)
710 718 end
711 719 end
712 720 end
713 721
714 722 context "User#notify_about?" do
715 723 context "Issues" do
716 724 setup do
717 725 @project = Project.find(1)
718 726 @author = User.generate_with_protected!
719 727 @assignee = User.generate_with_protected!
720 728 @issue = Issue.generate_for_project!(@project, :assigned_to => @assignee, :author => @author)
721 729 end
722 730
723 731 should "be true for a user with :all" do
724 732 @author.update_attribute(:mail_notification, 'all')
725 733 assert @author.notify_about?(@issue)
726 734 end
727 735
728 736 should "be false for a user with :none" do
729 737 @author.update_attribute(:mail_notification, 'none')
730 738 assert ! @author.notify_about?(@issue)
731 739 end
732 740
733 741 should "be false for a user with :only_my_events and isn't an author, creator, or assignee" do
734 742 @user = User.generate_with_protected!(:mail_notification => 'only_my_events')
735 743 Member.create!(:user => @user, :project => @project, :role_ids => [1])
736 744 assert ! @user.notify_about?(@issue)
737 745 end
738 746
739 747 should "be true for a user with :only_my_events and is the author" do
740 748 @author.update_attribute(:mail_notification, 'only_my_events')
741 749 assert @author.notify_about?(@issue)
742 750 end
743 751
744 752 should "be true for a user with :only_my_events and is the assignee" do
745 753 @assignee.update_attribute(:mail_notification, 'only_my_events')
746 754 assert @assignee.notify_about?(@issue)
747 755 end
748 756
749 757 should "be true for a user with :only_assigned and is the assignee" do
750 758 @assignee.update_attribute(:mail_notification, 'only_assigned')
751 759 assert @assignee.notify_about?(@issue)
752 760 end
753 761
754 762 should "be false for a user with :only_assigned and is not the assignee" do
755 763 @author.update_attribute(:mail_notification, 'only_assigned')
756 764 assert ! @author.notify_about?(@issue)
757 765 end
758 766
759 767 should "be true for a user with :only_owner and is the author" do
760 768 @author.update_attribute(:mail_notification, 'only_owner')
761 769 assert @author.notify_about?(@issue)
762 770 end
763 771
764 772 should "be false for a user with :only_owner and is not the author" do
765 773 @assignee.update_attribute(:mail_notification, 'only_owner')
766 774 assert ! @assignee.notify_about?(@issue)
767 775 end
768 776
769 777 should "be true for a user with :selected and is the author" do
770 778 @author.update_attribute(:mail_notification, 'selected')
771 779 assert @author.notify_about?(@issue)
772 780 end
773 781
774 782 should "be true for a user with :selected and is the assignee" do
775 783 @assignee.update_attribute(:mail_notification, 'selected')
776 784 assert @assignee.notify_about?(@issue)
777 785 end
778 786
779 787 should "be false for a user with :selected and is not the author or assignee" do
780 788 @user = User.generate_with_protected!(:mail_notification => 'selected')
781 789 Member.create!(:user => @user, :project => @project, :role_ids => [1])
782 790 assert ! @user.notify_about?(@issue)
783 791 end
784 792 end
785 793
786 794 context "other events" do
787 795 should 'be added and tested'
788 796 end
789 797 end
790 798
791 799 def test_salt_unsalted_passwords
792 800 # Restore a user with an unsalted password
793 801 user = User.find(1)
794 802 user.salt = nil
795 803 user.hashed_password = User.hash_password("unsalted")
796 804 user.save!
797 805
798 806 User.salt_unsalted_passwords!
799 807
800 808 user.reload
801 809 # Salt added
802 810 assert !user.salt.blank?
803 811 # Password still valid
804 812 assert user.check_password?("unsalted")
805 813 assert_equal user, User.try_to_login(user.login, "unsalted")
806 814 end
807 815
808 816 if Object.const_defined?(:OpenID)
809 817
810 818 def test_setting_identity_url
811 819 normalized_open_id_url = 'http://example.com/'
812 820 u = User.new( :identity_url => 'http://example.com/' )
813 821 assert_equal normalized_open_id_url, u.identity_url
814 822 end
815 823
816 824 def test_setting_identity_url_without_trailing_slash
817 825 normalized_open_id_url = 'http://example.com/'
818 826 u = User.new( :identity_url => 'http://example.com' )
819 827 assert_equal normalized_open_id_url, u.identity_url
820 828 end
821 829
822 830 def test_setting_identity_url_without_protocol
823 831 normalized_open_id_url = 'http://example.com/'
824 832 u = User.new( :identity_url => 'example.com' )
825 833 assert_equal normalized_open_id_url, u.identity_url
826 834 end
827 835
828 836 def test_setting_blank_identity_url
829 837 u = User.new( :identity_url => 'example.com' )
830 838 u.identity_url = ''
831 839 assert u.identity_url.blank?
832 840 end
833 841
834 842 def test_setting_invalid_identity_url
835 843 u = User.new( :identity_url => 'this is not an openid url' )
836 844 assert u.identity_url.blank?
837 845 end
838 846
839 847 else
840 848 puts "Skipping openid tests."
841 849 end
842 850
843 851 end
General Comments 0
You need to be logged in to leave comments. Login now