Background sending password reset instructions.
This commit is contained in:
parent
0e08c59827
commit
96f2aaa57a
5 changed files with 56 additions and 14 deletions
10
app/models/jobs/reset_password.rb
Normal file
10
app/models/jobs/reset_password.rb
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
module Jobs
|
||||
class ResetPassword < Base
|
||||
@queue = :mail
|
||||
|
||||
def self.perform(user_id)
|
||||
user = User.find(user_id)
|
||||
::Devise.mailer.reset_password_instructions(user).deliver
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -105,6 +105,11 @@ class User < ActiveRecord::Base
|
|||
user
|
||||
end
|
||||
|
||||
def send_reset_password_instructions
|
||||
generate_reset_password_token! if should_generate_token?
|
||||
Resque.enqueue(Jobs::ResetPassword, self.id)
|
||||
end
|
||||
|
||||
|
||||
def update_user_preferences(pref_hash)
|
||||
if self.disable_mail
|
||||
|
|
|
|||
|
|
@ -18,9 +18,8 @@ describe Devise::PasswordsController do
|
|||
response.should be_success
|
||||
end
|
||||
it "doesn't send email" do
|
||||
expect {
|
||||
post :create, "user" => {"email" => "foo@example.com"}
|
||||
}.to change(Devise.mailer.deliveries, :length).by(0)
|
||||
Resque.should_not_receive(:enqueue)
|
||||
post :create, "user" => {"email" => "foo@example.com"}
|
||||
end
|
||||
end
|
||||
context "when there is a user with that email" do
|
||||
|
|
@ -28,17 +27,9 @@ describe Devise::PasswordsController do
|
|||
post :create, "user" => {"email" => alice.email}
|
||||
response.should redirect_to(new_user_session_path)
|
||||
end
|
||||
it "sends email" do
|
||||
expect {
|
||||
post :create, "user" => {"email" => alice.email}
|
||||
}.to change(Devise.mailer.deliveries, :length).by(1)
|
||||
end
|
||||
it "sends email with a non-blank body" do
|
||||
it "sends email (enqueued to Resque)" do
|
||||
Resque.should_receive(:enqueue).with(Jobs::ResetPassword, alice.id)
|
||||
post :create, "user" => {"email" => alice.email}
|
||||
email = Devise.mailer.deliveries.last
|
||||
email_body = email.body.to_s
|
||||
email_body = email.html_part.body.raw_source if email_body.blank? && email.html_part.present?
|
||||
email_body.should_not be_blank
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
14
spec/models/jobs/reset_password_spec.rb
Normal file
14
spec/models/jobs/reset_password_spec.rb
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Jobs::ResetPassword do
|
||||
describe "#perform" do
|
||||
it "given a user id it sends the reset password instructions for that user" do
|
||||
user = Factory :user
|
||||
expect {
|
||||
mail = Jobs::ResetPassword.perform(user.id)
|
||||
mail.to.should == [user.email]
|
||||
mail.body.should include("change your password")
|
||||
}.to change(Devise.mailer.deliveries, :length).by(1)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -983,4 +983,26 @@ describe User do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#send_reset_password_instructions" do
|
||||
it "generates a reset password token if it's supposed to" do
|
||||
user = User.new
|
||||
user.stub!(:should_generate_token?).and_return(true)
|
||||
user.should_receive(:generate_reset_password_token)
|
||||
user.send_reset_password_instructions
|
||||
end
|
||||
|
||||
it "does not generate a reset password token if it's not supposed to" do
|
||||
user = User.new
|
||||
user.stub!(:should_generate_token?).and_return(false)
|
||||
user.should_not_receive(:generate_reset_password_token)
|
||||
user.send_reset_password_instructions
|
||||
end
|
||||
|
||||
it "queues up a job to send the reset password instructions" do
|
||||
user = Factory :user
|
||||
Resque.should_receive(:enqueue).with(Jobs::ResetPassword, user.id)
|
||||
user.send_reset_password_instructions
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue