2010-03-26 06:27:19 -04:00
|
|
|
require 'test_helper'
|
2009-09-17 10:06:46 -04:00
|
|
|
|
|
|
|
class ConfirmableTest < ActiveSupport::TestCase
|
|
|
|
|
2009-09-17 19:03:38 -04:00
|
|
|
def setup
|
2009-09-17 20:29:13 -04:00
|
|
|
setup_mailer
|
2009-09-17 19:03:38 -04:00
|
|
|
end
|
|
|
|
|
2009-10-18 07:14:52 -04:00
|
|
|
test 'should generate confirmation token after creating a record' do
|
|
|
|
assert_nil new_user.confirmation_token
|
|
|
|
assert_not_nil create_user.confirmation_token
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'should never generate the same confirmation token for different users' do
|
|
|
|
confirmation_tokens = []
|
2009-11-24 20:19:12 -05:00
|
|
|
3.times do
|
2009-10-18 07:14:52 -04:00
|
|
|
token = create_user.confirmation_token
|
|
|
|
assert !confirmation_tokens.include?(token)
|
|
|
|
confirmation_tokens << token
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-11-24 20:19:12 -05:00
|
|
|
test 'should confirm a user by updating confirmed at' do
|
2009-09-17 10:06:46 -04:00
|
|
|
user = create_user
|
|
|
|
assert_nil user.confirmed_at
|
|
|
|
assert user.confirm!
|
|
|
|
assert_not_nil user.confirmed_at
|
|
|
|
end
|
|
|
|
|
2009-10-18 07:14:52 -04:00
|
|
|
test 'should clear confirmation token while confirming a user' do
|
2009-10-15 16:36:44 -04:00
|
|
|
user = create_user
|
2009-10-18 07:14:52 -04:00
|
|
|
assert_present user.confirmation_token
|
2009-10-15 16:36:44 -04:00
|
|
|
user.confirm!
|
2009-10-18 07:14:52 -04:00
|
|
|
assert_nil user.confirmation_token
|
2009-10-15 16:36:44 -04:00
|
|
|
end
|
|
|
|
|
2009-09-17 10:06:46 -04:00
|
|
|
test 'should verify whether a user is confirmed or not' do
|
|
|
|
assert_not new_user.confirmed?
|
|
|
|
user = create_user
|
|
|
|
assert_not user.confirmed?
|
|
|
|
user.confirm!
|
|
|
|
assert user.confirmed?
|
|
|
|
end
|
|
|
|
|
2009-11-24 20:19:12 -05:00
|
|
|
test 'should not confirm a user already confirmed' do
|
2009-09-17 10:06:46 -04:00
|
|
|
user = create_user
|
|
|
|
assert user.confirm!
|
2010-02-16 11:00:36 -05:00
|
|
|
assert_blank user.errors[:email]
|
2009-11-24 20:19:12 -05:00
|
|
|
|
2009-09-17 10:06:46 -04:00
|
|
|
assert_not user.confirm!
|
2010-02-16 11:00:36 -05:00
|
|
|
assert_equal "was already confirmed", user.errors[:email].join
|
2009-09-17 10:06:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'should find and confirm an user automatically' do
|
|
|
|
user = create_user
|
2010-03-10 10:13:54 -05:00
|
|
|
confirmed_user = User.confirm_by_token(user.confirmation_token)
|
2009-09-17 10:06:46 -04:00
|
|
|
assert_equal confirmed_user, user
|
|
|
|
assert user.reload.confirmed?
|
|
|
|
end
|
|
|
|
|
2009-11-24 20:19:12 -05:00
|
|
|
test 'should return a new record with errors when a invalid token is given' do
|
2010-03-10 10:13:54 -05:00
|
|
|
confirmed_user = User.confirm_by_token('invalid_confirmation_token')
|
2010-03-28 16:26:07 -04:00
|
|
|
assert_not confirmed_user.persisted?
|
2010-02-16 11:00:36 -05:00
|
|
|
assert_equal "is invalid", confirmed_user.errors[:confirmation_token].join
|
2009-09-18 09:20:45 -04:00
|
|
|
end
|
|
|
|
|
2009-11-24 20:19:12 -05:00
|
|
|
test 'should return a new record with errors when a blank token is given' do
|
2010-03-10 10:13:54 -05:00
|
|
|
confirmed_user = User.confirm_by_token('')
|
2010-03-28 16:26:07 -04:00
|
|
|
assert_not confirmed_user.persisted?
|
2010-02-16 11:00:36 -05:00
|
|
|
assert_equal "can't be blank", confirmed_user.errors[:confirmation_token].join
|
2009-09-17 10:06:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'should generate errors for a user email if user is already confirmed' do
|
|
|
|
user = create_user
|
2009-12-21 15:10:23 -05:00
|
|
|
user.confirmed_at = Time.now
|
|
|
|
user.save
|
2010-03-10 10:13:54 -05:00
|
|
|
confirmed_user = User.confirm_by_token(user.confirmation_token)
|
2009-09-17 10:06:46 -04:00
|
|
|
assert confirmed_user.confirmed?
|
2010-02-16 11:00:36 -05:00
|
|
|
assert_equal "was already confirmed", confirmed_user.errors[:email].join
|
2009-09-17 10:06:46 -04:00
|
|
|
end
|
2009-09-17 18:54:19 -04:00
|
|
|
|
2009-09-17 20:29:13 -04:00
|
|
|
test 'should send confirmation instructions by email' do
|
2009-09-18 09:20:45 -04:00
|
|
|
assert_email_sent do
|
2009-09-17 20:29:13 -04:00
|
|
|
create_user
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'should not send confirmation when trying to save an invalid user' do
|
2009-09-18 09:20:45 -04:00
|
|
|
assert_email_not_sent do
|
2009-09-17 20:29:13 -04:00
|
|
|
user = new_user
|
|
|
|
user.stubs(:valid?).returns(false)
|
|
|
|
user.save
|
|
|
|
end
|
|
|
|
end
|
2009-10-07 23:53:04 -04:00
|
|
|
|
2009-12-14 18:30:28 -05:00
|
|
|
test 'should not generate a new token neither send e-mail if skip_confirmation! is invoked' do
|
|
|
|
user = new_user
|
|
|
|
user.skip_confirmation!
|
|
|
|
|
|
|
|
assert_email_not_sent do
|
|
|
|
user.save!
|
|
|
|
assert_nil user.confirmation_token
|
|
|
|
assert_not_nil user.confirmed_at
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-07 23:53:04 -04:00
|
|
|
test 'should find a user to send confirmation instructions' do
|
|
|
|
user = create_user
|
|
|
|
confirmation_user = User.send_confirmation_instructions(:email => user.email)
|
|
|
|
assert_equal confirmation_user, user
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'should return a new user if no email was found' do
|
|
|
|
confirmation_user = User.send_confirmation_instructions(:email => "invalid@email.com")
|
2010-03-28 16:26:07 -04:00
|
|
|
assert_not confirmation_user.persisted?
|
2009-10-07 23:53:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'should add error to new user email if no email was found' do
|
|
|
|
confirmation_user = User.send_confirmation_instructions(:email => "invalid@email.com")
|
|
|
|
assert confirmation_user.errors[:email]
|
2010-02-16 11:00:36 -05:00
|
|
|
assert_equal "not found", confirmation_user.errors[:email].join
|
2009-10-07 23:53:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'should send email instructions for the user confirm it\'s email' do
|
|
|
|
user = create_user
|
|
|
|
assert_email_sent do
|
|
|
|
User.send_confirmation_instructions(:email => user.email)
|
|
|
|
end
|
|
|
|
end
|
2010-05-06 08:48:26 -04:00
|
|
|
|
|
|
|
test 'should always have confirmation token when email is sent' do
|
|
|
|
user = new_user
|
|
|
|
user.instance_eval { def confirmation_required?; false end }
|
|
|
|
user.save
|
|
|
|
user.send_confirmation_instructions
|
|
|
|
assert_not_nil user.confirmation_token
|
|
|
|
end
|
2009-10-08 18:50:46 -04:00
|
|
|
|
2009-10-30 05:49:18 -04:00
|
|
|
test 'should not resend email instructions if the user change his email' do
|
2009-10-08 18:50:46 -04:00
|
|
|
user = create_user
|
|
|
|
user.email = 'new_test@example.com'
|
|
|
|
assert_email_not_sent do
|
|
|
|
user.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-30 05:49:18 -04:00
|
|
|
test 'should not reset confirmation status or token when updating email' do
|
2009-10-08 18:50:46 -04:00
|
|
|
user = create_user
|
|
|
|
user.confirm!
|
|
|
|
user.email = 'new_test@example.com'
|
|
|
|
user.save!
|
2009-10-15 15:54:04 -04:00
|
|
|
|
2009-10-30 05:49:18 -04:00
|
|
|
user.reload
|
|
|
|
assert user.confirmed?
|
|
|
|
assert_nil user.confirmation_token
|
2009-10-15 16:05:46 -04:00
|
|
|
end
|
|
|
|
|
2009-10-15 15:54:04 -04:00
|
|
|
test 'should not be able to send instructions if the user is already confirmed' do
|
|
|
|
user = create_user
|
|
|
|
user.confirm!
|
2010-03-10 10:13:54 -05:00
|
|
|
assert_not user.resend_confirmation_token
|
2009-10-15 15:54:04 -04:00
|
|
|
assert user.confirmed?
|
2010-02-16 11:00:36 -05:00
|
|
|
assert_equal 'was already confirmed', user.errors[:email].join
|
2009-10-15 15:54:04 -04:00
|
|
|
end
|
2009-10-20 19:32:30 -04:00
|
|
|
|
|
|
|
test 'confirm time should fallback to devise confirm in default configuration' do
|
2009-11-24 20:19:12 -05:00
|
|
|
swap Devise, :confirm_within => 1.day do
|
2009-10-20 19:32:30 -04:00
|
|
|
user = new_user
|
2009-10-22 07:09:34 -04:00
|
|
|
user.confirmation_sent_at = 2.days.ago
|
2009-10-20 19:32:30 -04:00
|
|
|
assert_not user.active?
|
2009-11-24 20:19:12 -05:00
|
|
|
|
2009-11-03 06:35:11 -05:00
|
|
|
Devise.confirm_within = 3.days
|
2009-10-20 19:32:30 -04:00
|
|
|
assert user.active?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'should be active when confirmation sent at is not overpast' do
|
2009-11-24 20:19:12 -05:00
|
|
|
swap Devise, :confirm_within => 5.days do
|
|
|
|
Devise.confirm_within = 5.days
|
|
|
|
user = create_user
|
|
|
|
|
|
|
|
user.confirmation_sent_at = 4.days.ago
|
|
|
|
assert user.active?
|
|
|
|
|
|
|
|
user.confirmation_sent_at = 5.days.ago
|
|
|
|
assert_not user.active?
|
|
|
|
end
|
2009-10-20 19:32:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'should be active when already confirmed' do
|
|
|
|
user = create_user
|
|
|
|
assert_not user.confirmed?
|
|
|
|
assert_not user.active?
|
2009-11-24 20:19:12 -05:00
|
|
|
|
2009-10-20 19:32:30 -04:00
|
|
|
user.confirm!
|
|
|
|
assert user.confirmed?
|
|
|
|
assert user.active?
|
|
|
|
end
|
|
|
|
|
2009-11-24 20:19:12 -05:00
|
|
|
test 'should not be active when confirm in is zero' do
|
2009-11-03 06:35:11 -05:00
|
|
|
Devise.confirm_within = 0.days
|
2009-10-20 19:32:30 -04:00
|
|
|
user = create_user
|
|
|
|
user.confirmation_sent_at = Date.today
|
|
|
|
assert_not user.active?
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'should not be active without confirmation' do
|
|
|
|
user = create_user
|
2009-12-21 15:10:23 -05:00
|
|
|
user.confirmation_sent_at = nil
|
|
|
|
user.save
|
2009-10-20 19:32:30 -04:00
|
|
|
assert_not user.reload.active?
|
|
|
|
end
|
2010-05-05 17:31:11 -04:00
|
|
|
|
|
|
|
test 'should be active without confirmation when confirmation is not required' do
|
|
|
|
user = create_user
|
|
|
|
user.instance_eval { def confirmation_required?; false end }
|
|
|
|
user.confirmation_sent_at = nil
|
|
|
|
user.save
|
|
|
|
assert user.reload.active?
|
|
|
|
end
|
2009-09-17 10:06:46 -04:00
|
|
|
end
|