require 'spec_helper'

describe 'Reset password' do

  describe "Password reset instructions" do
    before do
      user = create(:user, email: 'foo@wadus.com')
      user.activate!

      visit root_path
      click_link 'password_reset_link'
      current_path.should eq new_password_reset_path
    end

    it 'should confirm sending instructions' do
      fill_in 'email', with: 'foo@wadus.com'
      click_button 'update_password_button'
      current_path.should eq root_path
      page.should have_content 'Instructions have been sent to your email address.'
    end

    it 'should show error message if user not found' do
      fill_in 'email', with: 'feee@wadus.com'
      click_button 'update_password_button'
      page.should have_content 'User not found'
    end
  end

  describe "Password change" do
    before do
      user = create(:user_with_forgotten_password, email: 'foo@wadus.com')
      user.activate!
      visit edit_password_reset_path(user.reset_password_token)
    end

    it 'should not change password if no password entered' do
      fill_in 'Password', with: 'f'
      fill_in 'Confirm', with: 'f'
      click_button 'update_password_button'
      page.should have_content 'must be at least 3 characters long'
    end

    it 'should not change password if confirmation does not match' do
      fill_in 'Password', with: 'foopass'
      fill_in 'Confirm', with: 'foopasse'
      click_button 'update_password_button'
      page.should have_content 'should match confirmation'
    end

    it 'should confirm password change' do
      fill_in 'Password', with: 'foopass'
      fill_in 'Confirm', with: 'foopass'
      click_button 'update_password_button'
      current_path.should eq root_path
      page.should have_content 'Password was successfully updated.'
      fill_in 'session_email', :with => 'foo@wadus.com'
      fill_in 'session_password', :with => 'foopass'
      click_button 'new_session_submit_button'
      current_path.should eq home_path
    end
  end

end
