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-11-05 08:03:52 -04:00
|
|
|
assert_equal "was already confirmed, please try signing in", user.errors[:email].join
|
2009-09-17 10:06:46 -04:00
|
|
|
end
|
|
|
|
|
2011-02-06 10:34:31 -05:00
|
|
|
test 'should find and confirm a user automatically' do
|
2009-09-17 10:06:46 -04:00
|
|
|
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-11-05 08:03:52 -04:00
|
|
|
assert_equal "was already confirmed, please try signing in", 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
|
2011-08-12 12:27:57 -04:00
|
|
|
assert_email_sent "mynewuser@example.com" do
|
|
|
|
create_user :email => "mynewuser@example.com"
|
2009-09-17 20:29:13 -04:00
|
|
|
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
|
2011-04-17 21:14:56 -04:00
|
|
|
confirmation_user = User.send_confirmation_instructions(:email => "invalid@example.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
|
2011-04-17 21:14:56 -04:00
|
|
|
confirmation_user = User.send_confirmation_instructions(:email => "invalid@example.com")
|
2009-10-07 23:53:04 -04:00
|
|
|
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
|
|
|
|
|
2011-07-29 17:17:31 -04:00
|
|
|
test 'should send email instructions for the user confirm its email' do
|
2009-10-07 23:53:04 -04:00
|
|
|
user = create_user
|
2011-08-12 12:27:57 -04:00
|
|
|
assert_email_sent user.email do
|
2009-10-07 23:53:04 -04:00
|
|
|
User.send_confirmation_instructions(:email => user.email)
|
|
|
|
end
|
|
|
|
end
|
2011-02-06 16:23:36 -05:00
|
|
|
|
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
|
2010-07-14 12:03:34 -04:00
|
|
|
assert_not_nil user.reload.confirmation_token
|
2010-05-06 08:48:26 -04:00
|
|
|
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-11-05 08:03:52 -04:00
|
|
|
assert_equal 'was already confirmed, please try signing in', 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
|
2011-12-11 14:18:02 -05:00
|
|
|
swap Devise, :allow_unconfirmed_access_for => 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
|
2011-03-25 10:39:08 -04:00
|
|
|
assert_not user.active_for_authentication?
|
2009-11-24 20:19:12 -05:00
|
|
|
|
2011-12-11 14:18:02 -05:00
|
|
|
Devise.allow_unconfirmed_access_for = 3.days
|
2011-03-25 10:39:08 -04:00
|
|
|
assert user.active_for_authentication?
|
2009-10-20 19:32:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'should be active when confirmation sent at is not overpast' do
|
2011-12-11 14:18:02 -05:00
|
|
|
swap Devise, :allow_unconfirmed_access_for => 5.days do
|
|
|
|
Devise.allow_unconfirmed_access_for = 5.days
|
2009-11-24 20:19:12 -05:00
|
|
|
user = create_user
|
|
|
|
|
|
|
|
user.confirmation_sent_at = 4.days.ago
|
2011-03-25 10:39:08 -04:00
|
|
|
assert user.active_for_authentication?
|
2009-11-24 20:19:12 -05:00
|
|
|
|
|
|
|
user.confirmation_sent_at = 5.days.ago
|
2011-03-25 10:39:08 -04:00
|
|
|
assert_not user.active_for_authentication?
|
2009-11-24 20:19:12 -05:00
|
|
|
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?
|
2011-03-25 10:39:08 -04:00
|
|
|
assert_not user.active_for_authentication?
|
2009-11-24 20:19:12 -05:00
|
|
|
|
2009-10-20 19:32:30 -04:00
|
|
|
user.confirm!
|
|
|
|
assert user.confirmed?
|
2011-03-25 10:39:08 -04:00
|
|
|
assert user.active_for_authentication?
|
2009-10-20 19:32:30 -04:00
|
|
|
end
|
|
|
|
|
2009-11-24 20:19:12 -05:00
|
|
|
test 'should not be active when confirm in is zero' do
|
2011-12-11 14:18:02 -05:00
|
|
|
Devise.allow_unconfirmed_access_for = 0.days
|
2009-10-20 19:32:30 -04:00
|
|
|
user = create_user
|
|
|
|
user.confirmation_sent_at = Date.today
|
2011-03-25 10:39:08 -04:00
|
|
|
assert_not user.active_for_authentication?
|
2009-10-20 19:32:30 -04:00
|
|
|
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
|
2011-03-25 10:39:08 -04:00
|
|
|
assert_not user.reload.active_for_authentication?
|
2009-10-20 19:32:30 -04:00
|
|
|
end
|
2011-02-06 16:23:36 -05:00
|
|
|
|
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
|
2011-03-25 10:39:08 -04:00
|
|
|
assert user.reload.active_for_authentication?
|
2010-05-05 17:31:11 -04:00
|
|
|
end
|
2011-02-06 16:23:36 -05:00
|
|
|
|
2011-07-29 17:17:31 -04:00
|
|
|
test 'should find a user to send email instructions for the user confirm its email by authentication_keys' do
|
2011-01-27 11:20:37 -05:00
|
|
|
swap Devise, :authentication_keys => [:username, :email] do
|
|
|
|
user = create_user
|
|
|
|
confirm_user = User.send_confirmation_instructions(:email => user.email, :username => user.username)
|
|
|
|
assert_equal confirm_user, user
|
|
|
|
end
|
|
|
|
end
|
2011-02-06 16:23:36 -05:00
|
|
|
|
2011-01-27 11:20:37 -05:00
|
|
|
test 'should require all confirmation_keys' do
|
2011-02-06 16:23:36 -05:00
|
|
|
swap Devise, :confirmation_keys => [:username, :email] do
|
|
|
|
user = create_user
|
|
|
|
confirm_user = User.send_confirmation_instructions(:email => user.email)
|
|
|
|
assert_not confirm_user.persisted?
|
|
|
|
assert_equal "can't be blank", confirm_user.errors[:username].join
|
|
|
|
end
|
2011-01-27 11:20:37 -05:00
|
|
|
end
|
2009-09-17 10:06:46 -04:00
|
|
|
end
|
2011-04-03 15:26:14 -04:00
|
|
|
|
2011-12-11 14:07:30 -05:00
|
|
|
class ReconfirmableTest < ActiveSupport::TestCase
|
2011-12-20 14:54:49 -05:00
|
|
|
test 'should not worry about validations on confirm even with reconfirmable' do
|
|
|
|
admin = create_admin
|
|
|
|
admin.reset_password_token = "a"
|
|
|
|
assert admin.confirm!
|
|
|
|
end
|
|
|
|
|
2011-04-03 15:26:14 -04:00
|
|
|
test 'should generate confirmation token after changing email' do
|
2011-12-11 14:07:30 -05:00
|
|
|
admin = create_admin
|
|
|
|
assert admin.confirm!
|
|
|
|
assert_nil admin.confirmation_token
|
|
|
|
assert admin.update_attributes(:email => 'new_test@example.com')
|
|
|
|
assert_not_nil admin.confirmation_token
|
2011-04-03 15:26:14 -04:00
|
|
|
end
|
|
|
|
|
2011-12-31 06:22:58 -05:00
|
|
|
test 'should regenerate confirmation token after changing email' do
|
|
|
|
admin = create_admin
|
|
|
|
assert admin.confirm!
|
|
|
|
assert admin.update_attributes(:email => 'old_test@example.com')
|
|
|
|
token = admin.confirmation_token
|
|
|
|
assert admin.update_attributes(:email => 'new_test@example.com')
|
|
|
|
assert_not_equal token, admin.confirmation_token
|
|
|
|
end
|
|
|
|
|
2011-04-03 15:26:14 -04:00
|
|
|
test 'should send confirmation instructions by email after changing email' do
|
2011-12-11 14:07:30 -05:00
|
|
|
admin = create_admin
|
|
|
|
assert admin.confirm!
|
2011-08-12 12:27:57 -04:00
|
|
|
assert_email_sent "new_test@example.com" do
|
2011-12-11 14:07:30 -05:00
|
|
|
assert admin.update_attributes(:email => 'new_test@example.com')
|
2011-04-03 15:26:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'should not send confirmation by email after changing password' do
|
2011-12-11 14:07:30 -05:00
|
|
|
admin = create_admin
|
|
|
|
assert admin.confirm!
|
2011-04-03 15:26:14 -04:00
|
|
|
assert_email_not_sent do
|
2011-12-11 14:07:30 -05:00
|
|
|
assert admin.update_attributes(:password => 'newpass', :password_confirmation => 'newpass')
|
2011-04-03 15:26:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'should stay confirmed when email is changed' do
|
2011-12-11 14:07:30 -05:00
|
|
|
admin = create_admin
|
|
|
|
assert admin.confirm!
|
|
|
|
assert admin.update_attributes(:email => 'new_test@example.com')
|
|
|
|
assert admin.confirmed?
|
2011-04-03 15:26:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'should update email only when it is confirmed' do
|
2011-12-11 14:07:30 -05:00
|
|
|
admin = create_admin
|
|
|
|
assert admin.confirm!
|
|
|
|
assert admin.update_attributes(:email => 'new_test@example.com')
|
|
|
|
assert_not_equal 'new_test@example.com', admin.email
|
|
|
|
assert admin.confirm!
|
|
|
|
assert_equal 'new_test@example.com', admin.email
|
2011-04-03 15:26:14 -04:00
|
|
|
end
|
|
|
|
|
2011-12-11 14:07:30 -05:00
|
|
|
test 'should not allow admin to get past confirmation email by resubmitting their new address' do
|
|
|
|
admin = create_admin
|
|
|
|
assert admin.confirm!
|
|
|
|
assert admin.update_attributes(:email => 'new_test@example.com')
|
|
|
|
assert_not_equal 'new_test@example.com', admin.email
|
|
|
|
assert admin.update_attributes(:email => 'new_test@example.com')
|
|
|
|
assert_not_equal 'new_test@example.com', admin.email
|
2011-08-12 16:51:04 -04:00
|
|
|
end
|
|
|
|
|
2011-12-11 14:07:30 -05:00
|
|
|
test 'should find a admin by send confirmation instructions with unconfirmed_email' do
|
|
|
|
admin = create_admin
|
|
|
|
assert admin.confirm!
|
|
|
|
assert admin.update_attributes(:email => 'new_test@example.com')
|
|
|
|
confirmation_admin = Admin.send_confirmation_instructions(:email => admin.unconfirmed_email)
|
|
|
|
assert_equal confirmation_admin, admin
|
2011-04-03 15:26:14 -04:00
|
|
|
end
|
|
|
|
|
2011-12-11 14:07:30 -05:00
|
|
|
test 'should return a new admin if no email or unconfirmed_email was found' do
|
|
|
|
confirmation_admin = Admin.send_confirmation_instructions(:email => "invalid@email.com")
|
|
|
|
assert_not confirmation_admin.persisted?
|
2011-04-03 15:26:14 -04:00
|
|
|
end
|
|
|
|
|
2011-12-11 14:07:30 -05:00
|
|
|
test 'should add error to new admin email if no email or unconfirmed_email was found' do
|
|
|
|
confirmation_admin = Admin.send_confirmation_instructions(:email => "invalid@email.com")
|
|
|
|
assert confirmation_admin.errors[:email]
|
|
|
|
assert_equal "not found", confirmation_admin.errors[:email].join
|
2011-04-03 15:26:14 -04:00
|
|
|
end
|
2011-04-17 17:37:47 -04:00
|
|
|
|
2011-12-11 14:07:30 -05:00
|
|
|
test 'should find admin with email in unconfirmed_emails' do
|
|
|
|
admin = create_admin
|
|
|
|
admin.unconfirmed_email = "new_test@email.com"
|
|
|
|
assert admin.save
|
|
|
|
admin = Admin.find_by_unconfirmed_email_with_errors(:email => "new_test@email.com")
|
|
|
|
assert admin.persisted?
|
2011-04-17 17:37:47 -04:00
|
|
|
end
|
2011-04-03 15:26:14 -04:00
|
|
|
end
|