This commit is contained in:
Michael Sofaer & Sarah Mei 2011-11-14 14:16:20 -08:00
commit 8294472420
6 changed files with 57 additions and 15 deletions

View 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

View file

@ -108,6 +108,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

View file

@ -945,7 +945,7 @@ en:
show_getting_started: 'Re-enable Getting Started'
getting_started: 'New User Prefrences'
following: "Following Settings"
auto_follow_back: "Automatically follow back if a someone follows you"
auto_follow_back: "Automatically follow back if someone follows you"
auto_follow_aspect: "Aspect for automatically followed users:"
privacy_settings:

View file

@ -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,18 +27,10 @@ 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
end
end

View 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

View file

@ -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