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