cirandas.net

ref: master

test/unit/user_test.rb


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
# encoding: UTF-8
require_relative "../test_helper"

class UserTest < ActiveSupport::TestCase
  # Be sure to include AuthenticatedTestHelper in test/test_helper.rb instead.
  # Then, you can remove it from this and the functional test.
  include AuthenticatedTestHelper
  fixtures :users, :environments

  def test_should_create_user
    assert_difference 'User.count' do
      user = new_user
      refute user.new_record?, "#{user.errors.full_messages.to_sentence}"
    end
  end

  def test_should_require_login
    assert_no_difference 'User.count' do
      u = new_user(:login => nil)
      assert u.errors[:login].present?
    end
  end

  def test_should_require_password
    assert_no_difference 'User.count' do
      u = new_user(:password => nil)
      assert u.errors[:password].present?
    end
  end

  def test_should_require_password_confirmation
    assert_no_difference 'User.count' do
      u = new_user(:password_confirmation => nil)
      assert u.errors[:password_confirmation].present?
    end
  end

  def test_should_require_email
    assert_no_difference 'User.count' do
      u = new_user(:email => nil)
      assert u.errors[:email].present?
    end
  end

  def test_should_reset_password
    users(:johndoe).update(:password => 'new password', :password_confirmation => 'new password')
    assert_equal users(:johndoe), User.authenticate('johndoe', 'new password')
  end

  def test_should_not_rehash_password
    users(:johndoe).update(:login => 'johndoe2')
    assert_equal users(:johndoe), User.authenticate('johndoe2', 'test')
  end

  def test_should_authenticate_user
    assert_equal users(:johndoe), User.authenticate('johndoe', 'test')
    assert_equal users(:johndoe), User.authenticate('johndoe@localhost.localdomain', 'test')
    assert_equal nil, User.authenticate('wrongemail@localhost', 'test')
  end

  def test_should_authenticate_user_of_nondefault_environment
    assert_equal users(:other_ze), User.authenticate('ze', 'test', environments(:anhetegua_net))
  end

  def test_should_set_remember_token
    users(:johndoe).remember_me
    assert_not_nil users(:johndoe).remember_token
    assert_not_nil users(:johndoe).remember_token_expires_at
  end

  def test_should_unset_remember_token
    users(:johndoe).remember_me
    assert_not_nil users(:johndoe).remember_token
    users(:johndoe).forget_me
    assert_nil users(:johndoe).remember_token
  end

  def test_should_create_person
    users_count = User.count
    person_count = Person.count

    user = create_user('new_user', :email => 'new_user@example.com', :password => 'test', :password_confirmation => 'test')

    assert Person.exists?(['user_id = ?', user.id])

    assert_equal users_count + 1, User.count
    assert_equal person_count + 1, Person.count
  end

  def test_should_create_person_with_identifier_different_from_login
    user = User.create!(:login => 'new_user', :email => 'new_user@example.com', :password => 'test', :password_confirmation => 'test', :person_data => {:identifier => "new_test"})

    assert Person.exists?(['user_id = ?', user.id])

    assert user.login != user.person.identifier
  end

  def test_login_validation
    u = User.new
    u.valid?
    assert u.errors[:login.to_s].present?

    u.login = 'with space'
    u.valid?
    assert u.errors[:login.to_s].present?

    u.login = 'áéíóú'
    u.valid?
    assert u.errors[:login.to_s].present?

    u.login = 'rightformat2007'
    u.valid?
    refute  u.errors[:login.to_s].present?

    u.login = 'rightformat'
    u.valid?
    refute  u.errors[:login.to_s].present?

    u.login = 'right_format'
    u.valid?
    refute  u.errors[:login.to_s].present?
  end

  def test_should_change_password
    user = create_user('changetest', :password => 'test', :password_confirmation => 'test', :email => 'changetest@example.com')
    user.activate
    assert_nothing_raised do
      user.change_password!('test', 'newpass', 'newpass')
    end
    refute user.authenticated?('test')
    assert user.authenticated?('newpass')
  end

  def test_should_give_correct_current_password_for_changing_password
    user = create_user('changetest', :password => 'test', :password_confirmation => 'test', :email => 'changetest@example.com')
    user.activate
    assert_raise User::IncorrectPassword do
      user.change_password!('wrong', 'newpass', 'newpass')
    end
    refute user.authenticated?('newpass')
    assert user.authenticated?('test')
  end

  should 'require matching confirmation when changing password by force' do
    user = create_user('changetest', :password => 'test', :password_confirmation => 'test', :email => 'changetest@example.com')
    user.activate

    assert_raise ActiveRecord::RecordInvalid do
      user.force_change_password!('newpass', 'newpasswrong')
    end
    refute user.authenticated?('newpass')
    assert user.authenticated?('test')
  end

  should 'be able to force password change' do
    user = create_user('changetest', :password => 'test', :password_confirmation => 'test', :email => 'changetest@example.com')
    assert_nothing_raised  do
      user.force_change_password!('newpass', 'newpass')
    end

    user.activate
    assert user.authenticated?('newpass')
  end

  def test_should_create_person_when_creating_user
    count = Person.count
    refute Person.find_by(identifier: 'lalala')
    new_user(:login => 'lalala', :email => 'lalala@example.com')
    assert Person.find_by(identifier: 'lalala')
  end

  should 'set the same environment for user and person objects' do
    env = fast_create(Environment)
    user = new_user(:environment_id => env.id)
    assert_equal env, user.environment
    assert_equal env, user.person.environment
  end

  def test_should_destroy_person_when_destroying_user
    user = new_user(:login => 'lalala', :email => 'lalala@example.com')
    assert Person.find_by(identifier: 'lalala')
    user.destroy
    refute Person.find_by(identifier: 'lalala')
  end

  def test_should_encrypt_password_with_salted_sha1
    user = User.new(:login => 'lalala', :email => 'lalala@example.com', :password => 'test', :password_confirmation => 'test')
#TODO UPGRADE Leandro: I comment this code. The user model already create a person model
#    user.build_person(person_data)
    user.stubs(:salt).returns('testsalt')
    user.save!

    # SHA1+salt crypted form for password 'test', and salt 'testsalt',
    # calculated by hand at IRB
    crypted_password = '77606e8e9227f73618eefdfd36f8eb1b8b52ca5f'

    assert_equal crypted_password, user.crypted_password
  end

  def test_should_support_md5_passwords
    # ATTENTION this test explicitly exposes the crypted form of 'test'. This
    # makes 'test' a terrible password. :)
    user = new_user(:login => 'lalala', :email => 'lalala@example.com', :password => 'test', :password_confirmation => 'test', :password_type => 'md5')
    assert_equal '098f6bcd4621d373cade4e832627b4f6', user.crypted_password
  end


  def test_should_support_salted_md5_passwords
    user = new_user(:login => 'lalala', :email => 'lalala@example.com', :password => 'test', :password_confirmation => 'test', :password_type => 'salted_md5', :salt => 'test')
    assert_equal '05a671c66aefea124cc08b76ea6d30bb', user.crypted_password
  end

  def test_should_support_crypt_passwords
    user = new_user(:login => 'lalala', :email => 'lalala@example.com', :password => 'test', :password_confirmation => 'test', :password_type => 'crypt', :salt => 'test')
    assert_equal 'teH0wLIpW0gyQ', user.crypted_password
  end

  def test_should_support_clear_passwords
    assert_equal 'test', new_user(:password => 'test', :password_confirmation => 'test', :password_type => 'clear').crypted_password
  end

  def test_should_only_allow_know_encryption_methods
    assert_raise User::UnsupportedEncryptionType do
      User.create(
        :login => 'lalala',
        :email => 'lalala@example.com',
        :password => 'test',
        :password_confirmation => 'test',
        :password_type => 'AN_ENCRYPTION_METHOD_NOT_LIKELY_TO_EXIST' # <<<<
      )
    end
  end

  def test_should_use_salted_sha1_by_default
    assert_equal :salted_sha1, User.system_encryption_method
  end

  def test_should_be_able_to_set_system_encryption_method
    # save
    saved = User.system_encryption_method

    User.system_encryption_method = :some_method
    assert_equal :some_method, User.system_encryption_method

    # restore
    User.system_encryption_method = saved
  end

  def test_new_instances_should_use_system_encryption_method
    User.expects(:system_encryption_method).returns(:clear)
    assert_equal 'clear', new_user.password_type
  end

  def test_should_reencrypt_password_when_using_different_encryption_method_from_the_system_default
    User.stubs(:system_encryption_method).returns(:salted_sha1)

    # a user was created ...
    user = new_user(:login => 'lalala', :email => 'lalala@example.com', :password => 'test', :password_confirmation => 'test', :password_type => 'salted_sha1')

    # then the sysadmin decided to change the encryption method
    User.expects(:system_encryption_method).returns(:md5).at_least_once

    # when the user logs in, her password must be reencrypted with the new
    # method
    user.activate
    user.authenticated?('test')

    # and the new password must be saved back to the database
    user.reload
    assert_equal '098f6bcd4621d373cade4e832627b4f6', user.crypted_password
  end

  def test_should_not_update_encryption_if_password_incorrect
    # a user was created
    User.stubs(:system_encryption_method).returns(:salted_sha1)
    user = new_user(:login => 'lalala', :email => 'lalala@example.com', :password => 'test', :password_confirmation => 'test', :password_type => 'salted_sha1')
    crypted_password = user.crypted_password

    # then the sysadmin deciced to change the encryption method
    User.expects(:system_encryption_method).returns(:md5).at_least_once

    # but the user provided the wrong password
    user.activate
    user.authenticated?('WRONG_PASSWORD')

    # and then her password is not updated
    assert_equal crypted_password, user.crypted_password
  end

  def test_should_have_enable_email_setting
    u = User.new
    u.enable_email = true
    assert_equal true, u.enable_email
  end

  def test_should_have_enable_email_as_false_by_default
    assert_equal false, User.new.enable_email
  end

  should 'enable email' do
    user = create_user('cooler')
    refute user.enable_email
    assert user.enable_email!
    assert user.enable_email
  end

  should 'has email activation pending' do
    user = create_user('cooler')
    user.update_attribute(:environment_id, Environment.default.id)
    EmailActivation.create!(:requestor => user.person, :target => Environment.default)
    assert user.email_activation_pending?
  end

  should 'not has email activation pending if not have environment' do
    user = create_user('cooler')
    user.expects(:environment).returns(nil).at_least_once
    EmailActivation.create!(:requestor => user.person, :target => Environment.default)
    refute user.email_activation_pending?
  end

  should 'has moderate registration pending' do
    user = create_user('cooler')
    ModerateUserRegistration.create!(:requestor => user.person, :target => Environment.default)
    assert user.moderate_registration_pending?
  end

  should 'not has moderate registration pending if not have a pending task' do
    user = create_user('cooler')
    refute user.moderate_registration_pending?
  end

  should 'be able to use [] operator to find users by login' do
    user = fast_create(User)
    assert_equal user, User[user.login]
  end

  should 'user has presence status to know when online or offline' do
    user = User.new
    assert_respond_to user, :chat_status
  end

  should 'remember last status from user' do
    user = User.new
    assert_respond_to user, :last_chat_status
  end

  should "have data_hash method defined" do
    user = fast_create(User)
    assert user.respond_to?(:data_hash)
  end

  should "data_hash method have at least the following keys" do
    user = create_user('coldplay')
    expected_keys = ['login','is_admin','since_month', 'since_year', 'email_domain','friends_list','amount_of_friends', 'enterprises', ]
    data = user.data_hash
    assert(expected_keys.all? { |k| data.has_key?(k) }, "User#data_hash expected to have at least the following keys: #{expected_keys.inspect} (missing: #{(expected_keys-data.keys).inspect})")
  end

  should "data_hash friends_list method have the following keys" do
    person = create_user('coldplay').person
    friend = create_user('coldplayfriend', :chat_status => 'chat', :chat_status_at => DateTime.now).person
    person.add_friend(friend)
    expected_keys = ['avatar','name','jid','status']
    assert_equal [], expected_keys - person.user.data_hash['friends_list']['coldplayfriend'].keys
    assert_equal [], person.user.data_hash['friends_list']['coldplayfriend'].keys - expected_keys
  end

  should "data_hash method return the user information" do
    person = create_user('x_and_y').person
    Person.any_instance.stubs(:is_admin?).returns(true)
    Person.any_instance.stubs(:created_at).returns(DateTime.parse('16-08-2010'))
    expected_hash = {
      'login' => 'x_and_y', 'is_admin' => true, 'since_month' => 8,
      'chat_enabled' => false, 'since_year' => 2010, 'email_domain' => nil,
      'amount_of_friends' => 0, 'friends_list' => {}, 'enterprises' => [],
    }

    assert_equal expected_hash['login'], person.user.data_hash['login']
    assert_equal expected_hash['is_admin'], person.user.data_hash['is_admin']
    assert_equal expected_hash['since_month'], person.user.data_hash['since_month']
    assert_equal expected_hash['chat_enabled'], person.user.data_hash['chat_enabled']
    assert_equal expected_hash['since_year'], person.user.data_hash['since_year']

    # Avatar stuff
    assert_match '/www.gravatar.com/avatar/a0517761d5125820c28d87860bc7c02e', person.user.data_hash['avatar']
    assert_match 'only_path=false', person.user.data_hash['avatar']
    assert_match 'd=', person.user.data_hash['avatar']
    assert_match 'size=20', person.user.data_hash['avatar']

    assert_equal expected_hash['email_domain'], person.user.data_hash['email_domain']
    assert_equal expected_hash['amount_of_friends'], person.user.data_hash['amount_of_friends']
    assert_equal expected_hash['friends_list'], person.user.data_hash['friends_list']
  end

  should "data_hash return the friends_list information" do
    person = create_user('coldplay').person
    friend = create_user('coldplayfriend', :chat_status => 'chat', :chat_status_at => DateTime.now).person
    person.add_friend(friend)
    Person.any_instance.stubs(:profile_custom_icon).returns('/custom_icon')
    expected_hash = {
      'coldplayfriend' => {
        'avatar' => '/custom_icon', 'name' => 'coldplayfriend', 'jid' => 'coldplayfriend@' + Environment.default.default_hostname + '/coldplayfriend', 'status' => 'chat'
      }
    }
    assert_equal expected_hash, person.user.data_hash['friends_list']
  end

  should "data_hash return the correct number of friends parameter" do
    person = create_user('coldplay').person
    friend = create_user('coldplayfriend', :chat_status => 'chat', :chat_status_at => DateTime.now).person
    person.add_friend(friend)
    another_friend = create_user('coldplayanotherfriend', :chat_status => 'chat', :chat_status_at => DateTime.now).person
    person.add_friend(another_friend)
    assert_equal 2, person.user.data_hash['amount_of_friends']
  end

  should "data_hash collect friend with online status and with presence in last 15 minutes" do
    person = create_user('coldplay').person
    friend = create_user('coldplayfriend', :chat_status => 'chat', :chat_status_at => DateTime.now).person
    person.add_friend(friend)
    assert_equal 1, person.user.data_hash['amount_of_friends']
  end

  should "data_hash collect friend with busy status and with presence in last 15 minutes" do
    person = create_user('coldplay').person
    friend = create_user('coldplayfriend', :chat_status => 'dnd', :chat_status_at => DateTime.now).person
    person.add_friend(friend)
    assert_equal 1, person.user.data_hash['amount_of_friends']
  end

  should "data_hash status friend be described" do
    person = create_user('coldplay').person
    friend = create_user('coldplayfriend', :chat_status => 'chat', :chat_status_at => DateTime.now).person
    person.add_friend(friend)
    assert_equal 'chat', person.user.data_hash['friends_list'][friend.identifier]['status']
  end

  should 'return empty list of enterprises on data_hash for newly created user' do
    assert_equal [], create_user('testuser').data_hash['enterprises']
  end

  should 'return list of enterprises in data_hash' do
    user = create_user('testuser')
    enterprise = fast_create(Enterprise, :name => "My enterprise", :identifier => 'my-enterprise')
    user.person.expects(:enterprises).returns([enterprise])
    assert_includes user.data_hash['enterprises'], {'name' => 'My enterprise', 'identifier' => 'my-enterprise'}
  end

  should 'update chat status every 15 minutes' do
    assert_equal 15, User.expires_chat_status_every
  end

  should "return status of chat on environment in data_hash" do
    person = create_user('coldplay').person
    env = person.environment
    env.enable('xmpp_chat')
    env.save
    assert_equal true, person.user.data_hash['chat_enabled']

    env.disable('xmpp_chat')
    env.save
    person.reload
    assert_equal false, person.user.data_hash['chat_enabled']
  end

  should 'respond name with related person name' do
    user = create_user('testuser')
    user.person.name = 'Test User'
    assert_equal 'Test User', user.name
  end

  should 'respond name with login, if there is no person related and name defined' do
    user = create_user('testuser')
    user.person = nil
    user.name = nil
    assert_equal 'testuser', user.name
  end

  should 'respond name with user name attribute' do
    user = create_user('testuser')
    user.login = 'Login User'
    user.person = nil
    user.name = 'Another User'
    assert_equal 'Another User', user.name
  end

  should 'respond name with related person name although user name attribute is defined' do
    user = create_user('testuser')
    user.person.name = 'Person Name'
    user.name = 'Another User'
    user.login = 'Login User'
    assert_equal 'Person Name', user.name
  end

  should 'have activation code' do
    user = create_user('testuser')
    assert_respond_to user, :activation_code
  end

  should 'have activated at' do
    user = create_user('testuser')
    assert_respond_to user, :activated_at
  end

  should 'make activation code before creation' do
    assert_not_nil new_user.activation_code
  end

  should 'deliver e-mail with activation code after creation' do
    assert_difference 'ActionMailer::Base.deliveries.size', 1 do
      new_user :email => 'pending@activation.com'
      process_delayed_job_queue
    end
    assert_equal 'pending@activation.com', ActionMailer::Base.deliveries.last['to'].to_s
  end

  should 'not try to deliver email to template users' do
    Person.any_instance.stubs(:is_template?).returns(true)
    assert_no_difference 'ActionMailer::Base.deliveries.size' do
      new_user
    end
  end

  should 'authenticate an activated user' do
    user = new_user :login => 'testuser', :password => 'test123', :password_confirmation => 'test123'
    user.activate
    assert_equal user, User.authenticate('testuser', 'test123')
  end

  should 'not authenticate a not activated user' do
    user = new_user :login => 'testuser', :password => 'test123', :password_confirmation => 'test123'
    assert_raises User::UserNotActivated do
      User.authenticate('testuser', 'test123')
    end
  end

  should 'have activation code but no activated at when created' do
    user = new_user
    assert_not_nil user.activation_code
    assert_nil user.activated_at
    refute user.person.visible
  end

  should 'activate an user' do
    user = new_user
    assert user.activate
    assert_nil user.activation_code
    assert_not_nil user.activated_at
    assert user.person.visible
  end

  should 'return if the user is activated' do
    user = new_user
    refute user.activated?
    user.activate
    assert user.activated?
  end

  should 'delay activation check with default time' do
    user = new_user
    job = Delayed::Job.last
    assert_match /UserActivationJob/, job.handler
    assert_equal 72, ((job.run_at - user.created_at)/1.hour).round
  end

  should 'delay activation check with custom time' do
    NOOSFERO_CONF.stubs(:[]).with('hours_until_user_activation_check').returns(240)
    NOOSFERO_CONF.stubs(:[]).with('exclude_profile_identifier_pattern')
    user = new_user
    job = Delayed::Job.last
    assert_match /UserActivationJob/, job.handler
    assert_equal 240, ((job.run_at - user.created_at)/1.hour).round
  end

  should 'not create job to check activation to template users' do
    Person.any_instance.stubs(:is_template?).returns(true)

    user = new_user
    assert_equal 0, Delayed::Job.by_handler("--- !ruby/struct:UserActivationJob\nuser_id: #{user.id}\n").count
  end

  should 'deactivate an user' do
    user = new_user
    user.activate
    assert user.deactivate
    assert_nil user.activated_at
    refute user.person.visible
  end

  should 'return if the user is deactivated' do
    user = new_user
    user.activate
    assert user.activated?
    user.deactivate
    refute user.activated?
  end

  should 'activate right after creation when confirmation is not required' do
    e = Environment.default
    e.enable('skip_new_user_email_confirmation')
    e.save!

    assert new_user.activated?
  end

  should 'cancel activation if user has no person associated' do
    user = new_user
    user.stubs(:person).returns(nil)
    refute user.activate
  end

  should 'be able to skip the password requirement' do
    user = User.new(:login => 'quire', :email => 'quire@example.com')
    user.not_require_password!
    assert user.save!
  end

  should 'not deliver welcome e-mail after user activation if not enabled on environment' do
    env = Environment.default
    env.signup_welcome_text = {
      :subject => 'Welcome to the environment',
      :body => 'Thanks for signing up!'
    }
    env.disable('send_welcome_email_to_new_users')
    env.save

    user = new_user :email => 'pending@activation.com'
    assert_no_difference 'ActionMailer::Base.deliveries.size' do
      user.activate
    end
  end

  should 'deliver welcome e-mail after user activation if enabled on environment' do
    env = Environment.default
    env.signup_welcome_text = {
      :subject => 'Welcome to this environment',
      :body => 'Thanks for signing up!'
    }
    env.enable('send_welcome_email_to_new_users')
    env.save

    user = new_user :email => 'pending@activation.com'
    process_delayed_job_queue
    assert_difference 'ActionMailer::Base.deliveries.size', 1 do
      user.activate
      process_delayed_job_queue
    end

    sent = ActionMailer::Base.deliveries.last
    assert_equal ['pending@activation.com'], sent.to
    assert_equal 'Welcome to this environment', sent.subject
    assert_match /Thanks for signing up!/, sent.body.to_s
  end

  should 'deliver welcome e-mail after user activation if enabled on environment with default subject if not defined' do
    env = Environment.default
    env.signup_welcome_text = {
      :body => 'Thanks for signing up!'
    }
    env.enable('send_welcome_email_to_new_users')
    env.save

    user = new_user :email => 'pending@activation.com'
    process_delayed_job_queue
    assert_difference 'ActionMailer::Base.deliveries.size', 1 do
      user.activate
      process_delayed_job_queue
    end

    sent = ActionMailer::Base.deliveries.last
    assert_equal "Welcome to environment #{env.name}", sent.subject
  end

  should 'deliver welcome e-mail after user activation if enabled on environment and replace user_name' do
    env = Environment.default
    env.signup_welcome_text = {
      :subject => 'Welcome to the environment',
      :body => 'Thanks for signing up, {user_name}!',
    }
    env.enable('send_welcome_email_to_new_users')
    env.save

    user = new_user :name => 'John Doe', :email => 'pending@activation.com'
    process_delayed_job_queue
    assert_difference 'ActionMailer::Base.deliveries.size', 1 do
      user.activate
      process_delayed_job_queue
    end

    sent = ActionMailer::Base.deliveries.last
    assert_match /Thanks for signing up, #{user.name}!/, sent.body.to_s
  end

  should 'not deliver welcome e-mail after user activation if enabled on environment but body not filled in' do
    env = Environment.default
    env.signup_welcome_text = {
      :subject => 'Welcome to the environment',
    }
    env.enable('send_welcome_email_to_new_users')
    env.save

    user = new_user :email => 'pending@activation.com'
    assert_no_difference 'ActionMailer::Base.deliveries.size' do
      user.activate
    end
  end

  should 'not deliver welcome e-mail after user activation if enabled on environment but welcome text not defined' do
    env = Environment.default
    env.enable('send_welcome_email_to_new_users')
    env.save

    user = new_user :email => 'pending@activation.com'
    assert_no_difference 'ActionMailer::Base.deliveries.size' do
      user.activate
    end
  end

  should 'create person with name equal to user name if a user name is defined' do
    user = User.new( :login => 'quire', :email => 'quire@example.com', :password => 'quire', :password_confirmation => 'quire' )
    user.name = "Some name"
    user.save
    assert_equal 'Some name', user.person.name
  end

  should 'create person with name equal to user login if no user name is defined' do
    user = User.new( :login => 'quire', :email => 'quire@example.com', :password => 'quire', :password_confirmation => 'quire' )
    user.save
    assert_equal 'quire', user.person.name
  end

  should 'generate private token' do
    user = User.new
    SecureRandom.stubs(:hex).returns('token')
    user.generate_private_token!

    assert user.private_token, 'token'
  end

  should 'deliver e-mail with activation code when resend was requested and user was not activated' do
    user = new_user :email => 'pending@activation.com'
    activation_code = user.activation_code
    Delayed::Job.destroy_all
    assert_difference 'ActionMailer::Base.deliveries.size', 1 do
      user.resend_activation_code
      process_delayed_job_queue
    end
    assert_not_equal activation_code, user.reload.activation_code
    assert_equal 'pending@activation.com', ActionMailer::Base.deliveries.last['to'].to_s
  end

  should 'not deliver e-mail with activation code when resend was requested and user was activated' do
    user = new_user :email => 'pending@activation.com'
    user.activate
    Delayed::Job.destroy_all
    assert_no_difference 'ActionMailer::Base.deliveries.size' do
      user.resend_activation_code
      process_delayed_job_queue
    end
  end

  protected
    def new_user(options = {})
      user = User.new({ :login => 'quire', :email => 'quire@example.com', :password => 'quire', :password_confirmation => 'quire' }.merge(options))
      user.save
      user
    end
end