comparison .svn/pristine/e2/e2b323ec405901a9b13bdff14ce49dc5afc50cd7.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

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