comparison test/unit/user_test.rb @ 524:1248a47e81b3 feature_36

Merge from branch "luisf"
author luisf <luis.figueira@eecs.qmul.ac.uk>
date Mon, 25 Jul 2011 14:39:38 +0100
parents cbce1fd3b1b7
children cbb26bc654de
comparison
equal deleted inserted replaced
519:3be6bc3c2a17 524:1248a47e81b3
1 # redMine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006 Jean-Philippe Lang 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 # 3 #
4 # This program is free software; you can redistribute it and/or 4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License 5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2 6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version. 7 # of the License, or (at your option) any later version.
13 # 13 #
14 # You should have received a copy of the GNU General Public License 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 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. 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 require File.dirname(__FILE__) + '/../test_helper' 18 require File.expand_path('../../test_helper', __FILE__)
19 19
20 class UserTest < ActiveSupport::TestCase 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 22
23 def setup 23 def setup
107 assert @admin.save, @admin.errors.full_messages.join("; ") 107 assert @admin.save, @admin.errors.full_messages.join("; ")
108 @admin.reload 108 @admin.reload
109 assert_equal "john", @admin.login 109 assert_equal "john", @admin.login
110 end 110 end
111 111
112 def test_destroy 112 def test_destroy_should_delete_members_and_roles
113 User.find(2).destroy 113 members = Member.find_all_by_user_id(2)
114 ms = members.size
115 rs = members.collect(&:roles).flatten.size
116
117 assert_difference 'Member.count', - ms do
118 assert_difference 'MemberRole.count', - rs do
119 User.find(2).destroy
120 end
121 end
122
114 assert_nil User.find_by_id(2) 123 assert_nil User.find_by_id(2)
115 assert Member.find_all_by_user_id(2).empty? 124 assert Member.find_all_by_user_id(2).empty?
116 end 125 end
117 126
118 def test_validate 127 def test_destroy_should_update_attachments
128 attachment = Attachment.create!(:container => Project.find(1),
129 :file => uploaded_test_file("testfile.txt", "text/plain"),
130 :author_id => 2)
131
132 User.find(2).destroy
133 assert_nil User.find_by_id(2)
134 assert_equal User.anonymous, attachment.reload.author
135 end
136
137 def test_destroy_should_update_comments
138 comment = Comment.create!(
139 :commented => News.create!(:project_id => 1, :author_id => 1, :title => 'foo', :description => 'foo'),
140 :author => User.find(2),
141 :comments => 'foo'
142 )
143
144 User.find(2).destroy
145 assert_nil User.find_by_id(2)
146 assert_equal User.anonymous, comment.reload.author
147 end
148
149 def test_destroy_should_update_issues
150 issue = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'foo')
151
152 User.find(2).destroy
153 assert_nil User.find_by_id(2)
154 assert_equal User.anonymous, issue.reload.author
155 end
156
157 def test_destroy_should_unassign_issues
158 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo', :assigned_to_id => 2)
159
160 User.find(2).destroy
161 assert_nil User.find_by_id(2)
162 assert_nil issue.reload.assigned_to
163 end
164
165 def test_destroy_should_update_journals
166 issue = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'foo')
167 issue.init_journal(User.find(2), "update")
168 issue.save!
169
170 User.find(2).destroy
171 assert_nil User.find_by_id(2)
172 assert_equal User.anonymous, issue.journals.first.reload.user
173 end
174
175 def test_destroy_should_update_journal_details_old_value
176 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo', :assigned_to_id => 2)
177 issue.init_journal(User.find(1), "update")
178 issue.assigned_to_id = nil
179 assert_difference 'JournalDetail.count' do
180 issue.save!
181 end
182 journal_detail = JournalDetail.first(:order => 'id DESC')
183 assert_equal '2', journal_detail.old_value
184
185 User.find(2).destroy
186 assert_nil User.find_by_id(2)
187 assert_equal User.anonymous.id.to_s, journal_detail.reload.old_value
188 end
189
190 def test_destroy_should_update_journal_details_value
191 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo')
192 issue.init_journal(User.find(1), "update")
193 issue.assigned_to_id = 2
194 assert_difference 'JournalDetail.count' do
195 issue.save!
196 end
197 journal_detail = JournalDetail.first(:order => 'id DESC')
198 assert_equal '2', journal_detail.value
199
200 User.find(2).destroy
201 assert_nil User.find_by_id(2)
202 assert_equal User.anonymous.id.to_s, journal_detail.reload.value
203 end
204
205 def test_destroy_should_update_messages
206 board = Board.create!(:project_id => 1, :name => 'Board', :description => 'Board')
207 message = Message.create!(:board_id => board.id, :author_id => 2, :subject => 'foo', :content => 'foo')
208
209 User.find(2).destroy
210 assert_nil User.find_by_id(2)
211 assert_equal User.anonymous, message.reload.author
212 end
213
214 def test_destroy_should_update_news
215 news = News.create!(:project_id => 1, :author_id => 2, :title => 'foo', :description => 'foo')
216
217 User.find(2).destroy
218 assert_nil User.find_by_id(2)
219 assert_equal User.anonymous, news.reload.author
220 end
221
222 def test_destroy_should_delete_private_queries
223 query = Query.new(:name => 'foo', :is_public => false)
224 query.project_id = 1
225 query.user_id = 2
226 query.save!
227
228 User.find(2).destroy
229 assert_nil User.find_by_id(2)
230 assert_nil Query.find_by_id(query.id)
231 end
232
233 def test_destroy_should_update_public_queries
234 query = Query.new(:name => 'foo', :is_public => true)
235 query.project_id = 1
236 query.user_id = 2
237 query.save!
238
239 User.find(2).destroy
240 assert_nil User.find_by_id(2)
241 assert_equal User.anonymous, query.reload.user
242 end
243
244 def test_destroy_should_update_time_entries
245 entry = TimeEntry.new(:hours => '2', :spent_on => Date.today, :activity => TimeEntryActivity.create!(:name => 'foo'))
246 entry.project_id = 1
247 entry.user_id = 2
248 entry.save!
249
250 User.find(2).destroy
251 assert_nil User.find_by_id(2)
252 assert_equal User.anonymous, entry.reload.user
253 end
254
255 def test_destroy_should_delete_tokens
256 token = Token.create!(:user_id => 2, :value => 'foo')
257
258 User.find(2).destroy
259 assert_nil User.find_by_id(2)
260 assert_nil Token.find_by_id(token.id)
261 end
262
263 def test_destroy_should_delete_watchers
264 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo')
265 watcher = Watcher.create!(:user_id => 2, :watchable => issue)
266
267 User.find(2).destroy
268 assert_nil User.find_by_id(2)
269 assert_nil Watcher.find_by_id(watcher.id)
270 end
271
272 def test_destroy_should_update_wiki_contents
273 wiki_content = WikiContent.create!(
274 :text => 'foo',
275 :author_id => 2,
276 :page => WikiPage.create!(:title => 'Foo', :wiki => Wiki.create!(:project_id => 1, :start_page => 'Start'))
277 )
278 wiki_content.text = 'bar'
279 assert_difference 'WikiContent::Version.count' do
280 wiki_content.save!
281 end
282
283 User.find(2).destroy
284 assert_nil User.find_by_id(2)
285 assert_equal User.anonymous, wiki_content.reload.author
286 wiki_content.versions.each do |version|
287 assert_equal User.anonymous, version.reload.author
288 end
289 end
290
291 def test_destroy_should_nullify_issue_categories
292 category = IssueCategory.create!(:project_id => 1, :assigned_to_id => 2, :name => 'foo')
293
294 User.find(2).destroy
295 assert_nil User.find_by_id(2)
296 assert_nil category.reload.assigned_to_id
297 end
298
299 def test_destroy_should_nullify_changesets
300 changeset = Changeset.create!(
301 :repository => Repository::Subversion.create!(
302 :project_id => 1,
303 :url => 'file:///var/svn'
304 ),
305 :revision => '12',
306 :committed_on => Time.now,
307 :committer => 'jsmith'
308 )
309 assert_equal 2, changeset.user_id
310
311 User.find(2).destroy
312 assert_nil User.find_by_id(2)
313 assert_nil changeset.reload.user_id
314 end
315
316 def test_anonymous_user_should_not_be_destroyable
317 assert_no_difference 'User.count' do
318 assert_equal false, User.anonymous.destroy
319 end
320 end
321
322 def test_validate_login_presence
119 @admin.login = "" 323 @admin.login = ""
120 assert !@admin.save 324 assert !@admin.save
121 assert_equal 1, @admin.errors.count 325 assert_equal 1, @admin.errors.count
326 end
327
328 def test_validate_mail_notification_inclusion
329 u = User.new
330 u.mail_notification = 'foo'
331 u.save
332 assert_not_nil u.errors.on(:mail_notification)
122 end 333 end
123 334
124 context "User#try_to_login" do 335 context "User#try_to_login" do
125 should "fall-back to case-insensitive if user login is not found as-typed." do 336 should "fall-back to case-insensitive if user login is not found as-typed." do
126 user = User.try_to_login("AdMin", "admin") 337 user = User.try_to_login("AdMin", "admin")
148 assert user.save 359 assert user.save
149 360
150 user = User.try_to_login("admin", "hello") 361 user = User.try_to_login("admin", "hello")
151 assert_kind_of User, user 362 assert_kind_of User, user
152 assert_equal "admin", user.login 363 assert_equal "admin", user.login
153 assert_equal User.hash_password("hello"), user.hashed_password
154 end 364 end
155 365
156 def test_name_format 366 def test_name_format
157 assert_equal 'Smith, John', @jsmith.name(:lastname_coma_firstname) 367 assert_equal 'Smith, John', @jsmith.name(:lastname_coma_firstname)
158 Setting.user_format = :firstname_lastname 368 Setting.user_format = :firstname_lastname
168 @jsmith.status = User::STATUS_LOCKED 378 @jsmith.status = User::STATUS_LOCKED
169 assert @jsmith.save 379 assert @jsmith.save
170 380
171 user = User.try_to_login("jsmith", "jsmith") 381 user = User.try_to_login("jsmith", "jsmith")
172 assert_equal nil, user 382 assert_equal nil, user
383 end
384
385 context ".try_to_login" do
386 context "with good credentials" do
387 should "return the user" do
388 user = User.try_to_login("admin", "admin")
389 assert_kind_of User, user
390 assert_equal "admin", user.login
391 end
392 end
393
394 context "with wrong credentials" do
395 should "return nil" do
396 assert_nil User.try_to_login("admin", "foo")
397 end
398 end
173 end 399 end
174 400
175 if ldap_configured? 401 if ldap_configured?
176 context "#try_to_login using LDAP" do 402 context "#try_to_login using LDAP" do
177 context "with failed connection to the LDAP server" do 403 context "with failed connection to the LDAP server" do
294 520
295 # user with no role 521 # user with no role
296 assert_nil @dlopper.roles_for_project(Project.find(2)).detect {|role| role.member?} 522 assert_nil @dlopper.roles_for_project(Project.find(2)).detect {|role| role.member?}
297 end 523 end
298 524
525 def test_projects_by_role_for_user_with_role
526 user = User.find(2)
527 assert_kind_of Hash, user.projects_by_role
528 assert_equal 2, user.projects_by_role.size
529 assert_equal [1,5], user.projects_by_role[Role.find(1)].collect(&:id).sort
530 assert_equal [2], user.projects_by_role[Role.find(2)].collect(&:id).sort
531 end
532
533 def test_projects_by_role_for_user_with_no_role
534 user = User.generate!
535 assert_equal({}, user.projects_by_role)
536 end
537
538 def test_projects_by_role_for_anonymous
539 assert_equal({}, User.anonymous.projects_by_role)
540 end
541
542 def test_valid_notification_options
543 # without memberships
544 assert_equal 5, User.find(7).valid_notification_options.size
545 # with memberships
546 assert_equal 6, User.find(2).valid_notification_options.size
547 end
548
549 def test_valid_notification_options_class_method
550 assert_equal 5, User.valid_notification_options.size
551 assert_equal 5, User.valid_notification_options(User.find(7)).size
552 assert_equal 6, User.valid_notification_options(User.find(2)).size
553 end
554
299 def test_mail_notification_all 555 def test_mail_notification_all
300 @jsmith.mail_notification = 'all' 556 @jsmith.mail_notification = 'all'
301 @jsmith.notified_project_ids = [] 557 @jsmith.notified_project_ids = []
302 @jsmith.save 558 @jsmith.save
303 @jsmith.reload 559 @jsmith.reload
435 @assignee = User.generate_with_protected! 691 @assignee = User.generate_with_protected!
436 @issue = Issue.generate_for_project!(@project, :assigned_to => @assignee, :author => @author) 692 @issue = Issue.generate_for_project!(@project, :assigned_to => @assignee, :author => @author)
437 end 693 end
438 694
439 should "be true for a user with :all" do 695 should "be true for a user with :all" do
440 @author.update_attribute(:mail_notification, :all) 696 @author.update_attribute(:mail_notification, 'all')
441 assert @author.notify_about?(@issue) 697 assert @author.notify_about?(@issue)
442 end 698 end
443 699
444 should "be false for a user with :none" do 700 should "be false for a user with :none" do
445 @author.update_attribute(:mail_notification, :none) 701 @author.update_attribute(:mail_notification, 'none')
446 assert ! @author.notify_about?(@issue) 702 assert ! @author.notify_about?(@issue)
447 end 703 end
448 704
449 should "be false for a user with :only_my_events and isn't an author, creator, or assignee" do 705 should "be false for a user with :only_my_events and isn't an author, creator, or assignee" do
450 @user = User.generate_with_protected!(:mail_notification => :only_my_events) 706 @user = User.generate_with_protected!(:mail_notification => 'only_my_events')
707 Member.create!(:user => @user, :project => @project, :role_ids => [1])
451 assert ! @user.notify_about?(@issue) 708 assert ! @user.notify_about?(@issue)
452 end 709 end
453 710
454 should "be true for a user with :only_my_events and is the author" do 711 should "be true for a user with :only_my_events and is the author" do
455 @author.update_attribute(:mail_notification, :only_my_events) 712 @author.update_attribute(:mail_notification, 'only_my_events')
456 assert @author.notify_about?(@issue) 713 assert @author.notify_about?(@issue)
457 end 714 end
458 715
459 should "be true for a user with :only_my_events and is the assignee" do 716 should "be true for a user with :only_my_events and is the assignee" do
460 @assignee.update_attribute(:mail_notification, :only_my_events) 717 @assignee.update_attribute(:mail_notification, 'only_my_events')
461 assert @assignee.notify_about?(@issue) 718 assert @assignee.notify_about?(@issue)
462 end 719 end
463 720
464 should "be true for a user with :only_assigned and is the assignee" do 721 should "be true for a user with :only_assigned and is the assignee" do
465 @assignee.update_attribute(:mail_notification, :only_assigned) 722 @assignee.update_attribute(:mail_notification, 'only_assigned')
466 assert @assignee.notify_about?(@issue) 723 assert @assignee.notify_about?(@issue)
467 end 724 end
468 725
469 should "be false for a user with :only_assigned and is not the assignee" do 726 should "be false for a user with :only_assigned and is not the assignee" do
470 @author.update_attribute(:mail_notification, :only_assigned) 727 @author.update_attribute(:mail_notification, 'only_assigned')
471 assert ! @author.notify_about?(@issue) 728 assert ! @author.notify_about?(@issue)
472 end 729 end
473 730
474 should "be true for a user with :only_owner and is the author" do 731 should "be true for a user with :only_owner and is the author" do
475 @author.update_attribute(:mail_notification, :only_owner) 732 @author.update_attribute(:mail_notification, 'only_owner')
476 assert @author.notify_about?(@issue) 733 assert @author.notify_about?(@issue)
477 end 734 end
478 735
479 should "be false for a user with :only_owner and is not the author" do 736 should "be false for a user with :only_owner and is not the author" do
480 @assignee.update_attribute(:mail_notification, :only_owner) 737 @assignee.update_attribute(:mail_notification, 'only_owner')
481 assert ! @assignee.notify_about?(@issue) 738 assert ! @assignee.notify_about?(@issue)
482 end 739 end
483 740
484 should "be false if the mail_notification is anything else" do 741 should "be true for a user with :selected and is the author" do
485 @assignee.update_attribute(:mail_notification, :somthing_else) 742 @author.update_attribute(:mail_notification, 'selected')
486 assert ! @assignee.notify_about?(@issue) 743 assert @author.notify_about?(@issue)
487 end 744 end
488 745
746 should "be true for a user with :selected and is the assignee" do
747 @assignee.update_attribute(:mail_notification, 'selected')
748 assert @assignee.notify_about?(@issue)
749 end
750
751 should "be false for a user with :selected and is not the author or assignee" do
752 @user = User.generate_with_protected!(:mail_notification => 'selected')
753 Member.create!(:user => @user, :project => @project, :role_ids => [1])
754 assert ! @user.notify_about?(@issue)
755 end
489 end 756 end
490 757
491 context "other events" do 758 context "other events" do
492 should 'be added and tested' 759 should 'be added and tested'
493 end 760 end
761 end
762
763 def test_salt_unsalted_passwords
764 # Restore a user with an unsalted password
765 user = User.find(1)
766 user.salt = nil
767 user.hashed_password = User.hash_password("unsalted")
768 user.save!
769
770 User.salt_unsalted_passwords!
771
772 user.reload
773 # Salt added
774 assert !user.salt.blank?
775 # Password still valid
776 assert user.check_password?("unsalted")
777 assert_equal user, User.try_to_login(user.login, "unsalted")
494 end 778 end
495 779
496 if Object.const_defined?(:OpenID) 780 if Object.const_defined?(:OpenID)
497 781
498 def test_setting_identity_url 782 def test_setting_identity_url