views:

572

answers:

2

I am trying to implement some tests to validate the behavior for Authlogic password resets as explained in http://www.binarylogic.com/2008/11/16/tutorial-reset-passwords-with-authlogic/

I am using Authlogic, Shoulda, Webrat and Factory Girl and here's my test:

require 'test_helper'

class PasswordResetTest < ActionController::IntegrationTest


  setup :activate_authlogic

  context "A registered user" do
    setup do
      @reggie = Factory(:reggie)

    end

    should "not allow logged in users to change password" do
      visit signin_path
      fill_in 'Email', :with => @reggie.email
      fill_in 'Password', :with => @reggie.password
      click_button 'Sign In'
      assert_equal controller.session['user_credentials'], @reggie.persistence_token
      visit change_password_path
      assert_equal account_path, path
      assert_match /must be logged out/, flash[:notice]
      visit signout_path
      assert_equal controller.session['user_credentials'], nil
      visit change_password_path
      assert_equal change_password_path, path
    end

    should "allow logged out users to change password" do
      visit signout_path
      assert_equal controller.session['user_credentials'], nil
      visit change_password_path
      assert_template :new
      fill_in 'email', :with => @reggie.email
      click_button 'Reset my password'
      assert_match /Please check your email/, flash[:notice]
      assert !ActionMailer::Base.deliveries.empty?
      sent = ActionMailer::Base.deliveries.first
      assert_equal [@reggie.email], sent.to
      assert_match /Password Reset Instructions/, sent.subject
      assert_not_nil @reggie.perishable_token
      #TODO
      p "Perishable Token #{@reggie.perishable_token}"
      assert_match assigns[:edit_password_reset_url], sent.body
    end
  end
end

In the last 2 lines of the test, I am trying to make sure the link sent out has the right perishable_token and it always comes up different between the printed Perishable Token and the token in the link sent out.

How should I test this behavior?

Thanks, Siva

A: 

Careful. Authlogic is magic. Certain operations cause the User object to mutate and when it does, the perishable_token well, perishes (gets regenerated).

I wonder if your visit signout_path is really logging you out. Typically, if your UserSession is RESTful you'd have to issue an HTTP DELETE to the resource to actually delete the session. Just visiting the path (with a GET) won't delete the session unless you have an explicit route for it (mapping e.g. '/logout' to :controller => 'user_sessions', :action => 'destroy')

Bill Burcham
A: 

Change the line on notifier.rb to this:

body :edit_password_resets_url => edit_password_resets_url(user.perishable_token)