Add options to expire confirmation tokens

With this patch, functionality is added to expire the confirmation
tokens that are being sent by email.
For example, if a token is valid for 3 days only, it cannot be used for
confirmation on the 4th day.
This commit is contained in:
Nils Landt
2012-07-09 14:43:12 +02:00
parent 8463c6dce4
commit 87f2fa9767
9 changed files with 120 additions and 11 deletions

View File

@@ -16,7 +16,7 @@ class ConfirmationTest < ActionController::IntegrationTest
fill_in 'email', :with => user.email
click_button 'Resend confirmation instructions'
end
test 'user should be able to request a new confirmation' do
resend_confirmation
@@ -50,6 +50,33 @@ class ConfirmationTest < ActionController::IntegrationTest
assert user.reload.confirmed?
end
test 'user with valid confirmation token should not be able to confirm an account after the token has expired' do
swap Devise, :expire_confirmation_token_after => 3.days do
# TODO: once again, confirmation_sent_at is not being set to the correct date
user = create_user(:confirm => false, :confirmation_sent_at => 4.days.ago)
#user.confirmation_sent_at = 4.days.ago
assert_not user.confirmed?
visit_user_confirmation_with_token(user.confirmation_token)
assert_contain 'Your account was successfully confirmed.'
assert_current_url '/'
assert user.reload.confirmed?
end
end
test 'user with valid confirmation token should be able to confirm an account before the token has expires' do
swap Devise, :expire_confirmation_token_after => 3.days do
# TODO: once again, confirmation_sent_at is not being set to the correct date
user = create_user(:confirm => false, :confirmation_sent_at => 2.days.ago)
assert_not user.confirmed?
visit_user_confirmation_with_token(user.confirmation_token)
assert_have_selector '#error_explanation'
assert_contain /needs to be confirmed within/
assert_not user.reload.confirmed?
end
end
test 'user should be redirected to a custom path after confirmation' do
Devise::ConfirmationsController.any_instance.stubs(:after_confirmation_path_for).returns("/?custom=1")
@@ -239,7 +266,7 @@ class ConfirmationOnChangeTest < ActionController::IntegrationTest
assert admin.reload.confirmed?
assert_not admin.reload.pending_reconfirmation?
end
test 'admin with previously valid confirmation token should not be able to confirm email after email changed again' do
admin = create_admin
admin.update_attributes(:email => 'first_test@example.com')
@@ -247,11 +274,11 @@ class ConfirmationOnChangeTest < ActionController::IntegrationTest
confirmation_token = admin.confirmation_token
admin.update_attributes(:email => 'second_test@example.com')
assert_equal 'second_test@example.com', admin.unconfirmed_email
visit_admin_confirmation_with_token(confirmation_token)
assert_have_selector '#error_explanation'
assert_contain /Confirmation token(.*)invalid/
visit_admin_confirmation_with_token(admin.confirmation_token)
assert_contain 'Your account was successfully confirmed.'
assert_current_url '/admin_area/home'

View File

@@ -235,6 +235,38 @@ class ConfirmableTest < ActiveSupport::TestCase
assert_equal "can't be blank", confirm_user.errors[:username].join
end
end
def confirm_user_by_token_with_confirmation_sent_at confirmation_sent_at
user = create_user
user.confirmation_sent_at = confirmation_sent_at
confirmed_user = User.confirm_by_token(user.confirmation_token)
assert_equal confirmed_user, user
user.reload.confirmed?
end
test 'should accept confirmation email token even after 5 years when no expiration is set' do
assert confirm_user_by_token_with_confirmation_sent_at(5.years.ago)
end
test 'should accept confirmation email token after 2 days when expiration is set to 3 days' do
swap Devise, :expire_confirmation_token_after => 3.days do
assert confirm_user_by_token_with_confirmation_sent_at(2.days.ago)
end
end
test 'should not accept confirmation email token after 4 days when expiration is set to 3 days' do
swap Devise, :expire_confirmation_token_after => 3.days do
#assert_not confirm_user_by_token_with_confirmation_sent_at(4.days.ago)
# TODO: confirmation_sent_at is Time.now during confirm_by_token
# TODO: when everything works, use the test line above
user = create_user
user.confirmation_sent_at = 4.days.ago
assert_not user.confirmed?
confirmed_user = User.confirm_by_token(user.confirmation_token)
assert_equal confirmed_user, user
assert_not user.reload.confirmed?
end
end
end
class ReconfirmableTest < ActiveSupport::TestCase

View File

@@ -12,7 +12,8 @@ class ActionDispatch::IntegrationTest
:email => options[:email] || 'user@test.com',
:password => options[:password] || '12345678',
:password_confirmation => options[:password] || '12345678',
:created_at => Time.now.utc
:created_at => Time.now.utc,
:confirmation_sent_at => options[:confirmation_sent_at]
)
user.confirm! unless options[:confirm] == false
user.lock_access! if options[:locked] == true