Skip to content

Commit

Permalink
Merge pull request heartcombo#1418 from locomotivecms/simple_scoped_m…
Browse files Browse the repository at this point in the history
…ailer

Re-define the devise mailer inside a model
  • Loading branch information
josevalim committed Nov 6, 2011
2 parents 1bace6d + bbd117b commit e9c263c
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 8 deletions.
4 changes: 4 additions & 0 deletions lib/devise/models/authenticatable.rb
Expand Up @@ -75,6 +75,10 @@ def inactive_message
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)

Expand Down
2 changes: 1 addition & 1 deletion lib/devise/models/confirmable.rb
Expand Up @@ -47,7 +47,7 @@ def confirmed?
# 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.
Expand Down
2 changes: 1 addition & 1 deletion lib/devise/models/lockable.rb
Expand Up @@ -49,7 +49,7 @@ def access_locked?

# 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.
Expand Down
6 changes: 3 additions & 3 deletions lib/devise/models/recoverable.rb
Expand Up @@ -40,7 +40,7 @@ def reset_password!(new_password, new_password_confirmation)
# 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.
Expand All @@ -64,7 +64,7 @@ def send_reset_password_instructions
# 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

Expand Down Expand Up @@ -121,7 +121,7 @@ def reset_password_by_token(attributes={})
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
Expand Down
15 changes: 14 additions & 1 deletion test/integration/confirmable_test.rb
Expand Up @@ -6,7 +6,7 @@ def visit_user_confirmation_with_token(confirmation_token)
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

Expand All @@ -15,10 +15,23 @@ def visit_user_confirmation_with_token(confirmation_token)

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
Expand Down
15 changes: 14 additions & 1 deletion test/integration/lockable_test.rb
Expand Up @@ -6,7 +6,7 @@ def visit_user_unlock_with_token(unlock_token)
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

Expand All @@ -15,10 +15,23 @@ def visit_user_unlock_with_token(unlock_token)

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
Expand Down
10 changes: 10 additions & 0 deletions test/integration/recoverable_test.rb
Expand Up @@ -38,6 +38,16 @@ def reset_password(options={}, &block)
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')
Expand Down
19 changes: 18 additions & 1 deletion test/integration/registerable_test.rb
Expand Up @@ -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'
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions test/mailers/confirmation_instructions_test.rb
Expand Up @@ -8,6 +8,11 @@ def setup
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
Expand Down
5 changes: 5 additions & 0 deletions test/mailers/reset_password_instructions_test.rb
Expand Up @@ -8,6 +8,11 @@ def setup
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
Expand Down
5 changes: 5 additions & 0 deletions test/mailers/unlock_instructions_test.rb
Expand Up @@ -8,6 +8,11 @@ def setup
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
Expand Down
1 change: 1 addition & 0 deletions test/rails_app/lib/shared_admin.rb
Expand Up @@ -6,4 +6,5 @@ module SharedAdmin
:timeoutable, :recoverable, :rememberable, :lockable,
:unlock_strategy => :time
end

end

0 comments on commit e9c263c

Please sign in to comment.