Commit a5205fa4

mo khan <mo@mokhan.ca>
2015-02-02 04:44:34
extract page objects for password reset.
1 parent 74fbea9
app/controllers/passwords_controller.rb
@@ -5,7 +5,7 @@ class PasswordsController < ApplicationController
 
   def create
     PasswordReset.send_reset_instructions_to(params[:user][:email])
-    redirect_to new_session_path, notice: t('passwords.send_instructions')
+    redirect_to new_session_path, notice: t("passwords.send_instructions")
   end
 
   def edit
spec/features/forgot_password_spec.rb
@@ -1,37 +1,27 @@
 require "rails_helper"
 
-describe "password retrieval", :js => true do
+describe "password retrieval" do
   context "when a user attempts to retrieve their password" do
+    subject { PasswordResetRequestPage.new }
     let(:user) { create(:user) }
-    let(:error_message) { I18n.t('passwords.send_instructions') }
-
-    before :each do
-      visit login_path
-      click_link "Forgot your password?"
-      within "#new_user" do
-        fill_in "user_email", :with => user.email
-      end
-      click_button "Send me reset password instructions"
-    end
+    let(:error_message) { I18n.t("passwords.send_instructions") }
 
     it "sends them an email with instructions" do
+      subject.visit_page
+      subject.reset_password(user.email)
       expect(page).to have_content(error_message)
     end
   end
 
-  context "when a password is reset" do
+  context "when a reset link is sent" do
     let(:user) { create(:user, reset_password_token: SecureRandom.hex(32)) }
+    subject { PasswordResetPage.new(user.reset_password_token) }
 
-    it "lets them log in with the new password" do
-      visit edit_password_path(user.reset_password_token)
-      fill_in "user_password", with: 'donkey'
-      click_button "Change my password"
-      within('.form-inline') do
-        fill_in('session_username', with: user.email)
-        fill_in('session_password', with: "donkey")
-      end
-      click_button("Sign In")
-      expect(page).to have_content("Log Out")
+    it "lets them reset their password" do
+      subject.visit_page
+      subject.change_password_to("donkey")
+      expect(current_path).to eql(new_session_path)
+      expect(user.reload.authenticate("donkey")).to be_truthy
     end
   end
 end
spec/support/pages/password_reset_page.rb
@@ -0,0 +1,12 @@
+require_relative "../web_page.rb"
+
+class PasswordResetPage < WebPage
+  def initialize(reset_password_token)
+    super(edit_password_path(reset_password_token))
+  end
+
+  def change_password_to(new_password)
+    fill_in "user_password", with: new_password
+    click_button "Change my password"
+  end
+end
spec/support/pages/password_reset_request_page.rb
@@ -0,0 +1,14 @@
+require_relative "../web_page.rb"
+
+class PasswordResetRequestPage < WebPage
+  def initialize
+    super(new_password_path)
+  end
+
+  def reset_password(email)
+    within "#new_user" do
+      fill_in "user_email", with: email
+    end
+    click_button "Send me reset password instructions"
+  end
+end