To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / .svn / pristine / dc / dce158397cf543f178989488879d39aee998d33c.svn-base @ 1297:0a574315af3e

History | View | Annotate | Download (34.5 KB)

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