1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00

Merge pull request #1418 from locomotivecms/simple_scoped_mailer

Re-define the devise mailer inside a model
This commit is contained in:
José Valim 2011-11-06 01:38:08 -08:00
commit e9c263c326
12 changed files with 81 additions and 8 deletions

View file

@ -75,6 +75,10 @@ module Devise
def authenticatable_salt
end
def devise_mailer
Devise.mailer
end
module ClassMethods
Devise::Models.config(self, :authentication_keys, :request_keys, :strip_whitespace_keys, :case_insensitive_keys, :http_authenticatable, :params_authenticatable)

View file

@ -47,7 +47,7 @@ module Devise
# Send confirmation instructions by email
def send_confirmation_instructions
generate_confirmation_token! if self.confirmation_token.nil?
::Devise.mailer.confirmation_instructions(self).deliver
self.devise_mailer.confirmation_instructions(self).deliver
end
# Resend confirmation token. This method does not need to generate a new token.

View file

@ -49,7 +49,7 @@ module Devise
# Send unlock instructions by email
def send_unlock_instructions
::Devise.mailer.unlock_instructions(self).deliver
self.devise_mailer.unlock_instructions(self).deliver
end
# Resend the unlock instructions if the user is locked.

View file

@ -40,7 +40,7 @@ module Devise
# Resets reset password token and send reset password instructions by email
def send_reset_password_instructions
generate_reset_password_token! if should_generate_token?
::Devise.mailer.reset_password_instructions(self).deliver
self.devise_mailer.reset_password_instructions(self).deliver
end
# Checks if the reset password token sent is within the limit time.
@ -64,7 +64,7 @@ module Devise
# reset_password_period_valid? # will always return false
#
def reset_password_period_valid?
return true unless respond_to?(:reset_password_sent_at)
return true unless respond_to?(:reset_password_sent_at)
reset_password_sent_at && reset_password_sent_at.utc >= self.class.reset_password_within.ago
end
@ -121,7 +121,7 @@ module Devise
recoverable = find_or_initialize_with_error_by(:reset_password_token, attributes[:reset_password_token])
if recoverable.persisted?
if recoverable.reset_password_period_valid?
recoverable.reset_password!(attributes[:password], attributes[:password_confirmation])
recoverable.reset_password!(attributes[:password], attributes[:password_confirmation])
else
recoverable.errors.add(:reset_password_token, :expired)
end

View file

@ -6,7 +6,7 @@ class ConfirmationTest < ActionController::IntegrationTest
visit user_confirmation_path(:confirmation_token => confirmation_token)
end
test 'user should be able to request a new confirmation' do
def resend_confirmation
user = create_user(:confirm => false)
ActionMailer::Base.deliveries.clear
@ -15,10 +15,23 @@ 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
assert_current_url '/users/sign_in'
assert_contain 'You will receive an email with instructions about how to confirm your account in a few minutes'
assert_equal 1, ActionMailer::Base.deliveries.size
assert_equal ['please-change-me@config-initializers-devise.com'], ActionMailer::Base.deliveries.first.from
end
test 'user should receive a confirmation from a custom mailer' do
User.any_instance.stubs(:devise_mailer).returns(Users::Mailer)
resend_confirmation
assert_equal ['custom@example.com'], ActionMailer::Base.deliveries.first.from
end
test 'user with invalid confirmation token should not be able to confirm an account' do

View file

@ -6,7 +6,7 @@ class LockTest < ActionController::IntegrationTest
visit user_unlock_path(:unlock_token => unlock_token)
end
test 'user should be able to request a new unlock token' do
def send_unlock_request
user = create_user(:locked => true)
ActionMailer::Base.deliveries.clear
@ -15,10 +15,23 @@ class LockTest < ActionController::IntegrationTest
fill_in 'email', :with => user.email
click_button 'Resend unlock instructions'
end
test 'user should be able to request a new unlock token' do
send_unlock_request
assert_template 'sessions/new'
assert_contain 'You will receive an email with instructions about how to unlock your account in a few minutes'
assert_equal 1, ActionMailer::Base.deliveries.size
assert_equal ['please-change-me@config-initializers-devise.com'], ActionMailer::Base.deliveries.first.from
end
test 'user should receive the instructions from a custom mailer' do
User.any_instance.stubs(:devise_mailer).returns(Users::Mailer)
send_unlock_request
assert_equal ['custom@example.com'], ActionMailer::Base.deliveries.first.from
end
test 'unlocked user should not be able to request a unlock token' do

View file

@ -38,6 +38,16 @@ class PasswordTest < ActionController::IntegrationTest
assert_contain 'You will receive an email with instructions about how to reset your password in a few minutes.'
end
test 'reset password with email should send an email from a custom mailer' do
create_user(:email => 'Foo@Bar.com')
User.any_instance.stubs(:devise_mailer).returns(Users::Mailer)
request_forgot_password do
fill_in 'email', :with => 'foo@bar.com'
end
assert_equal ['custom@example.com'], ActionMailer::Base.deliveries.last.from
end
test 'reset password with email of different case should fail when email is NOT the list of case insensitive keys' do
swap Devise, :case_insensitive_keys => [] do
create_user(:email => 'Foo@Bar.com')

View file

@ -36,13 +36,19 @@ class RegistrationTest < ActionController::IntegrationTest
assert_current_url "/?custom=1"
end
test 'a guest user should be able to sign up successfully and be blocked by confirmation' do
def user_sign_up
ActionMailer::Base.deliveries.clear
get new_user_registration_path
fill_in 'email', :with => 'new_user@test.com'
fill_in 'password', :with => 'new_user123'
fill_in 'password confirmation', :with => 'new_user123'
click_button 'Sign up'
end
test 'a guest user should be able to sign up successfully and be blocked by confirmation' do
user_sign_up
assert_contain 'You have signed up successfully. However, we could not sign you in because your account is unconfirmed.'
assert_not_contain 'You have to confirm your account before continuing'
@ -55,6 +61,17 @@ class RegistrationTest < ActionController::IntegrationTest
assert_not user.confirmed?
end
test 'a guest user should receive the confirmation instructions from the default mailer' do
user_sign_up
assert_equal ['please-change-me@config-initializers-devise.com'], ActionMailer::Base.deliveries.first.from
end
test 'a guest user should receive the confirmation instructions from a custom mailer' do
User.any_instance.stubs(:devise_mailer).returns(Users::Mailer)
user_sign_up
assert_equal ['custom@example.com'], ActionMailer::Base.deliveries.first.from
end
test 'a guest user should be blocked by confirmation and redirected to a custom path' do
Devise::RegistrationsController.any_instance.stubs(:after_inactive_sign_up_path_for).returns("/?custom=1")
get new_user_registration_path

View file

@ -8,6 +8,11 @@ class ConfirmationInstructionsTest < ActionMailer::TestCase
Devise.mailer_sender = 'test@example.com'
end
def teardown
Devise.mailer = 'Devise::Mailer'
Devise.mailer_sender = 'please-change-me@config-initializers-devise.com'
end
def user
@user ||= create_user
end

View file

@ -8,6 +8,11 @@ class ResetPasswordInstructionsTest < ActionMailer::TestCase
Devise.mailer_sender = 'test@example.com'
end
def teardown
Devise.mailer = 'Devise::Mailer'
Devise.mailer_sender = 'please-change-me@config-initializers-devise.com'
end
def user
@user ||= begin
user = create_user

View file

@ -8,6 +8,11 @@ class UnlockInstructionsTest < ActionMailer::TestCase
Devise.mailer_sender = 'test@example.com'
end
def teardown
Devise.mailer = 'Devise::Mailer'
Devise.mailer_sender = 'please-change-me@config-initializers-devise.com'
end
def user
@user ||= begin
user = create_user

View file

@ -6,4 +6,5 @@ module SharedAdmin
:timeoutable, :recoverable, :rememberable, :lockable,
:unlock_strategy => :time
end
end