Remove the code that was dynamically adding columns to active record
This commit is contained in:
parent
9a6ac7ab69
commit
d952dea32b
|
@ -202,61 +202,54 @@ class ConfirmationTest < ActionController::IntegrationTest
|
|||
end
|
||||
end
|
||||
|
||||
class ConfirmationOnChangeTest < ConfirmationTest
|
||||
|
||||
def create_second_user(options={})
|
||||
@user = nil
|
||||
create_user(options)
|
||||
class ConfirmationOnChangeTest < ActionController::IntegrationTest
|
||||
def create_second_admin(options={})
|
||||
@admin = nil
|
||||
create_admin(options)
|
||||
end
|
||||
|
||||
def setup
|
||||
add_unconfirmed_email_column
|
||||
Devise.reconfirmable = true
|
||||
def visit_admin_confirmation_with_token(confirmation_token)
|
||||
visit admin_confirmation_path(:confirmation_token => confirmation_token)
|
||||
end
|
||||
|
||||
def teardown
|
||||
remove_unconfirmed_email_column
|
||||
Devise.reconfirmable = false
|
||||
end
|
||||
test 'admin should be able to request a new confirmation after email changed' do
|
||||
admin = create_admin
|
||||
admin.update_attributes(:email => 'new_test@example.com')
|
||||
|
||||
test 'user should be able to request a new confirmation after email changed' do
|
||||
user = create_user(:confirm => true)
|
||||
user.update_attributes(:email => 'new_test@example.com')
|
||||
|
||||
visit new_user_session_path
|
||||
visit new_admin_session_path
|
||||
click_link "Didn't receive confirmation instructions?"
|
||||
|
||||
fill_in 'email', :with => user.unconfirmed_email
|
||||
fill_in 'email', :with => admin.unconfirmed_email
|
||||
assert_difference "ActionMailer::Base.deliveries.size" do
|
||||
click_button 'Resend confirmation instructions'
|
||||
end
|
||||
|
||||
assert_current_url '/users/sign_in'
|
||||
assert_current_url '/admin_area/sign_in'
|
||||
assert_contain 'You will receive an email with instructions about how to confirm your account in a few minutes'
|
||||
end
|
||||
|
||||
test 'user with valid confirmation token should be able to confirm email after email changed' do
|
||||
user = create_user(:confirm => true)
|
||||
user.update_attributes(:email => 'new_test@example.com')
|
||||
assert 'new_test@example.com', user.unconfirmed_email
|
||||
visit_user_confirmation_with_token(user.confirmation_token)
|
||||
test 'admin with valid confirmation token should be able to confirm email after email changed' do
|
||||
admin = create_admin
|
||||
admin.update_attributes(:email => 'new_test@example.com')
|
||||
assert_equal 'new_test@example.com', admin.unconfirmed_email
|
||||
visit_admin_confirmation_with_token(admin.confirmation_token)
|
||||
|
||||
assert_contain 'Your account was successfully confirmed.'
|
||||
assert_current_url '/'
|
||||
assert user.reload.confirmed?
|
||||
assert_not user.reload.pending_reconfirmation?
|
||||
assert_current_url '/admin_area/home'
|
||||
assert admin.reload.confirmed?
|
||||
assert_not admin.reload.pending_reconfirmation?
|
||||
end
|
||||
|
||||
test 'user email should be unique also within unconfirmed_email' do
|
||||
user = create_user(:confirm => true)
|
||||
user.update_attributes(:email => 'new_test@example.com')
|
||||
assert 'new_test@example.com', user.unconfirmed_email
|
||||
test 'admin email should be unique also within unconfirmed_email' do
|
||||
admin = create_admin
|
||||
admin.update_attributes(:email => 'new_admin_test@example.com')
|
||||
assert_equal 'new_admin_test@example.com', admin.unconfirmed_email
|
||||
|
||||
create_second_user(:email => "new_test@example.com")
|
||||
create_second_admin(:email => "new_admin_test@example.com")
|
||||
|
||||
visit_user_confirmation_with_token(user.confirmation_token)
|
||||
visit_admin_confirmation_with_token(admin.confirmation_token)
|
||||
assert_have_selector '#error_explanation'
|
||||
assert_contain /Email.*already.*taken/
|
||||
assert user.reload.pending_reconfirmation?
|
||||
assert admin.reload.pending_reconfirmation?
|
||||
end
|
||||
end
|
||||
|
|
|
@ -293,43 +293,32 @@ class RegistrationTest < ActionController::IntegrationTest
|
|||
end
|
||||
|
||||
class ReconfirmableRegistrationTest < ActionController::IntegrationTest
|
||||
def setup
|
||||
add_unconfirmed_email_column
|
||||
Devise.reconfirmable = true
|
||||
end
|
||||
test 'a signed in admin should see a more appropriate flash message when editing his account if reconfirmable is enabled' do
|
||||
sign_in_as_admin
|
||||
get edit_admin_registration_path
|
||||
|
||||
def teardown
|
||||
remove_unconfirmed_email_column
|
||||
Devise.reconfirmable = false
|
||||
end
|
||||
|
||||
test 'a signed in user should see a more appropriate flash message when editing his account if reconfirmable is enabled' do
|
||||
sign_in_as_user
|
||||
get edit_user_registration_path
|
||||
|
||||
fill_in 'email', :with => 'user.new@example.com'
|
||||
fill_in 'email', :with => 'admin.new@example.com'
|
||||
fill_in 'current password', :with => '123456'
|
||||
click_button 'Update'
|
||||
|
||||
assert_current_url '/'
|
||||
assert_current_url '/admin_area/home'
|
||||
assert_contain 'but we need to verify your new email address'
|
||||
|
||||
assert_equal "user.new@example.com", User.first.unconfirmed_email
|
||||
assert_equal "admin.new@example.com", Admin.first.unconfirmed_email
|
||||
end
|
||||
|
||||
test 'a signed in user should not see a reconfirmation message if they did not change their password' do
|
||||
sign_in_as_user
|
||||
get edit_user_registration_path
|
||||
test 'a signed in admin should not see a reconfirmation message if they did not change their password' do
|
||||
sign_in_as_admin
|
||||
get edit_admin_registration_path
|
||||
|
||||
fill_in 'password', :with => 'pas123'
|
||||
fill_in 'password confirmation', :with => 'pas123'
|
||||
fill_in 'current password', :with => '123456'
|
||||
click_button 'Update'
|
||||
|
||||
assert_current_url '/'
|
||||
assert_current_url '/admin_area/home'
|
||||
assert_contain 'You updated your account successfully.'
|
||||
|
||||
assert User.first.valid_password?('pas123')
|
||||
assert Admin.first.valid_password?('pas123')
|
||||
end
|
||||
end
|
||||
|
||||
end
|
|
@ -237,98 +237,80 @@ class ConfirmableTest < ActiveSupport::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
class ReconfirmableTest < ConfirmableTest
|
||||
def setup
|
||||
add_unconfirmed_email_column
|
||||
Devise.reconfirmable = true
|
||||
end
|
||||
|
||||
def teardown
|
||||
remove_unconfirmed_email_column
|
||||
Devise.reconfirmable = false
|
||||
end
|
||||
|
||||
def test_should_not_resend_email_instructions_if_the_user_change_his_email
|
||||
#behaves differently
|
||||
end
|
||||
|
||||
def test_should_not_reset_confirmation_status_or_token_when_updating_email
|
||||
#behaves differently
|
||||
end
|
||||
|
||||
class ReconfirmableTest < ActiveSupport::TestCase
|
||||
test 'should generate confirmation token after changing email' do
|
||||
user = create_user
|
||||
assert user.confirm!
|
||||
assert_nil user.confirmation_token
|
||||
assert user.update_attributes(:email => 'new_test@example.com')
|
||||
assert_not_nil user.confirmation_token
|
||||
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
|
||||
end
|
||||
|
||||
test 'should send confirmation instructions by email after changing email' do
|
||||
user = create_user
|
||||
assert user.confirm!
|
||||
admin = create_admin
|
||||
assert admin.confirm!
|
||||
assert_email_sent "new_test@example.com" do
|
||||
assert user.update_attributes(:email => 'new_test@example.com')
|
||||
assert admin.update_attributes(:email => 'new_test@example.com')
|
||||
end
|
||||
end
|
||||
|
||||
test 'should not send confirmation by email after changing password' do
|
||||
user = create_user
|
||||
assert user.confirm!
|
||||
admin = create_admin
|
||||
assert admin.confirm!
|
||||
assert_email_not_sent do
|
||||
assert user.update_attributes(:password => 'newpass', :password_confirmation => 'newpass')
|
||||
assert admin.update_attributes(:password => 'newpass', :password_confirmation => 'newpass')
|
||||
end
|
||||
end
|
||||
|
||||
test 'should stay confirmed when email is changed' do
|
||||
user = create_user
|
||||
assert user.confirm!
|
||||
assert user.update_attributes(:email => 'new_test@example.com')
|
||||
assert user.confirmed?
|
||||
admin = create_admin
|
||||
assert admin.confirm!
|
||||
assert admin.update_attributes(:email => 'new_test@example.com')
|
||||
assert admin.confirmed?
|
||||
end
|
||||
|
||||
test 'should update email only when it is confirmed' do
|
||||
user = create_user
|
||||
assert user.confirm!
|
||||
assert user.update_attributes(:email => 'new_test@example.com')
|
||||
assert_not_equal 'new_test@example.com', user.email
|
||||
assert user.confirm!
|
||||
assert_equal 'new_test@example.com', user.email
|
||||
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
|
||||
end
|
||||
|
||||
test 'should not allow user to get past confirmation email by resubmitting their new address' do
|
||||
user = create_user
|
||||
assert user.confirm!
|
||||
assert user.update_attributes(:email => 'new_test@example.com')
|
||||
assert_not_equal 'new_test@example.com', user.email
|
||||
assert user.update_attributes(:email => 'new_test@example.com')
|
||||
assert_not_equal 'new_test@example.com', user.email
|
||||
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
|
||||
end
|
||||
|
||||
test 'should find a user by send confirmation instructions with unconfirmed_email' do
|
||||
user = create_user
|
||||
assert user.confirm!
|
||||
assert user.update_attributes(:email => 'new_test@example.com')
|
||||
confirmation_user = User.send_confirmation_instructions(:email => user.unconfirmed_email)
|
||||
assert_equal confirmation_user, user
|
||||
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
|
||||
end
|
||||
|
||||
test 'should return a new user if no email or unconfirmed_email was found' do
|
||||
confirmation_user = User.send_confirmation_instructions(:email => "invalid@email.com")
|
||||
assert_not confirmation_user.persisted?
|
||||
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?
|
||||
end
|
||||
|
||||
test 'should add error to new user email if no email or unconfirmed_email was found' do
|
||||
confirmation_user = User.send_confirmation_instructions(:email => "invalid@email.com")
|
||||
assert confirmation_user.errors[:email]
|
||||
assert_equal "not found", confirmation_user.errors[:email].join
|
||||
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
|
||||
end
|
||||
|
||||
test 'should find user with email in unconfirmed_emails' do
|
||||
user = create_user
|
||||
user.unconfirmed_email = "new_test@email.com"
|
||||
assert user.save
|
||||
user = User.find_by_unconfirmed_email_with_errors(:email => "new_test@email.com")
|
||||
assert user.persisted?
|
||||
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?
|
||||
end
|
||||
end
|
||||
|
|
|
@ -16,6 +16,12 @@ class Admin
|
|||
## Rememberable
|
||||
field :remember_created_at, :type => Time
|
||||
|
||||
## Confirmable
|
||||
field :confirmation_token, :type => String
|
||||
field :confirmed_at, :type => Time
|
||||
field :confirmation_sent_at, :type => Time
|
||||
field :unconfirmed_email, :type => String # Only if using reconfirmable
|
||||
|
||||
## Encryptable
|
||||
field :password_salt, :type => String
|
||||
|
||||
|
|
|
@ -4,7 +4,10 @@ module SharedAdmin
|
|||
included do
|
||||
devise :database_authenticatable, :encryptable, :registerable,
|
||||
:timeoutable, :recoverable, :lockable, :confirmable,
|
||||
:unlock_strategy => :time, :lock_strategy => :none, :confirm_within => 2.weeks
|
||||
:unlock_strategy => :time, :lock_strategy => :none,
|
||||
:confirm_within => 2.weeks, :reconfirmable => true
|
||||
|
||||
validates_uniqueness_of :email, :allow_blank => true, :if => :email_changed?
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -84,26 +84,4 @@ class ActiveSupport::TestCase
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
# TODO: Get rid of this in favor of a real model with unconfirmed
|
||||
def add_unconfirmed_email_column
|
||||
if DEVISE_ORM == :active_record
|
||||
ActiveRecord::Base.connection.add_column(:users, :unconfirmed_email, :string)
|
||||
User.reset_column_information
|
||||
elsif DEVISE_ORM == :mongoid
|
||||
User.field(:unconfirmed_email, :type => String)
|
||||
end
|
||||
end
|
||||
|
||||
# TODO: Get rid of this in favor of a real model with unconfirmed
|
||||
def remove_unconfirmed_email_column
|
||||
if DEVISE_ORM == :active_record
|
||||
ActiveRecord::Base.connection.remove_column(:users, :unconfirmed_email)
|
||||
User.reset_column_information
|
||||
elsif DEVISE_ORM == :mongoid
|
||||
User.fields.delete(:unconfirmed_email)
|
||||
User.send(:undefine_attribute_methods)
|
||||
User.send(:define_attribute_methods, User.fields.keys)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -23,7 +23,8 @@ class ActionDispatch::IntegrationTest
|
|||
def create_admin(options={})
|
||||
@admin ||= begin
|
||||
admin = Admin.create!(
|
||||
:email => 'admin@test.com', :password => '123456', :password_confirmation => '123456'
|
||||
:email => options[:email] || 'admin@test.com',
|
||||
:password => '123456', :password_confirmation => '123456'
|
||||
)
|
||||
admin.confirm! unless options[:confirm] == false
|
||||
admin
|
||||
|
|
Loading…
Reference in New Issue