Permalink
Browse files

add password reset workflow service object

  • Loading branch information...
1 parent e18d987 commit 823316968ddfb6db1a6596ad26bc1604e1f50d10 @phlipper committed May 6, 2013
@@ -0,0 +1,10 @@
+module Thincloud::Authentication
+ # Public: Execute the workflow steps to reset a password for an Identity
+ class PasswordResetWorkflow
+ def self.call(email)
+ return unless identity = Identity.find_by_email(email)
+ identity.generate_password_token!
+ PasswordsMailer.password_reset(identity.id).deliver
+ end
+ end
+end
@@ -0,0 +1,41 @@
+require "minitest_helper"
+
+module Thincloud::Authentication
+ describe PasswordResetWorkflow do
+ subject { PasswordResetWorkflow }
+
+ it { subject.must_respond_to :call }
+
+ describe "without a valid email" do
+ before do
+ Identity.expects(:find_by_email).with("foo@bar.com")
+ Identity.any_instance.expects(:generate_password_token!).never
+ PasswordsMailer.expects(:password_reset).never
+ end
+
+ it { subject.call("foo@bar.com") }
+ end
+
+ describe "with a valid email" do
+ let(:identity) do
+ attrs = { email: "foo@bar.com", password_reset_token: "abc123" }
+ Identity.new(attrs).tap { |i| i.id = 999 }
+ end
+
+ before do
+ Identity.stubs(:find).with(999).returns(identity)
+ Identity.stubs(:find_by_email).with("foo@bar.com").returns(identity)
+ identity.expects(:generate_password_token!)
+ end
+
+ it "sends an email with the reset token" do
+ subject.call("foo@bar.com")
+ email = ActionMailer::Base.deliveries.first
+ email.to.must_include "foo@bar.com"
+ email.subject.must_equal "Password Reset"
+ email.body.encoded.must_match "/passwords/abc123/edit"
+ end
+ end
+
+ end
+end

0 comments on commit 8233169

Please sign in to comment.