Topic: Rspec redirect issues - (on Ruby 1.9.1 and Rails 2.3.5)

I'm developing in Rails 2.3.5 on Ruby 1.9.1 and using Cucumber 0.4.4, Rspec  1.2.9 and Webrat 0.6.0 to steer the way. I'm also using restful_authentication and am having issues getting the preconfigured features to work. There were some scenarios that were failing that have been easily fixed, but I'm now getting to a point where I'm starting to think something must be broken in Rspec for these latest versions of Rails and Ruby.

The first issue is that there are instances where redirects will work just fine in the browser but show up as not working in the tests. Other times, the steps defined will pass the redirects without issues.

Controller Code:

class SessionsController < ApplicationController
  # render new.rhtml
  def new
  end

  def create
    logout_keeping_session!
    user = User.authenticate(params[:login], params[:password])
    if user
      self.current_user = user
      new_cookie_flag = (params[:remember_me] == "1")
      handle_remember_cookie! new_cookie_flag
      redirect_back_or_default('/')
      flash[:notice] = "Logged in successfully"
    else
      note_failed_signin
      @login       = params[:login]
      @remember_me = params[:remember_me]
      render :action => 'new'
    end
  end

  def destroy
    logout_killing_session!
    flash[:notice] = "You have been logged out."
    redirect_back_or_default('/')
  end

protected
  # Track failed login attempts
  def note_failed_signin
    flash[:error] = "Couldn't log you in as '#{params[:login]}'"
    logger.warn "Failed login for '#{params[:login]}' from #{request.remote_ip} at #{Time.now.utc}"
  end
end

Cucumber Scenarios:

  # This one fails at the should be redirected
  Scenario: Anonymous user can log in
    Given an anonymous user
     And  an activated user named 'reggie'
    When  she creates a singular sessions with login: 'reggie', password: 'monkey', remember me: ''
    Then  she should be redirected to the home page
    ...

  # This one does get through the redirect
  Scenario: Anonymous (logged out) user can log out.
    Given an anonymous user
    When  she goes to /logout
    Then  she should be redirected to the home page
    ...

Steps:

Given "an anonymous user" do
  log_out!
Given "$an $user_type user named '$login'" do |_, user_type, login|
  create_user! user_type, named_user(login)
end
When /^(\w+) creates an? ([\w ]+) with ([\w: \',]+)$/ do |actor, resource, attributes|
  attributes = attributes.to_hash_from_story
  if resource =~ %r{singular ([\w/]+)}
    resource = $1.downcase.singularize
    post "/#{resource}", attributes
  else
    post "/#{resource.downcase.pluralize}", { resource.downcase.singularize => attributes }
  end
end
Then "$actor should be redirected to $path" do |_, path|
  response.should redirect_to(grok_path(path))
end

def log_out
  get '/sessions/destroy'
end
def log_out!
  log_out
  response.should redirect_to('/')
  follow_redirect!
end
def create_user!(user_type, user_params)
  user_params['password_confirmation'] ||= user_params['password'] ||= user_params['password']
  create_user user_params
  response.should redirect_to('/')
  follow_redirect!
end
def create_user(user_params={})
  @user_params       ||= user_params
  post "/users", :user => user_params
  @user = User.find_by_login(user_params['login'])
end

Can anyone make sense of why this should be failing in some cases and not others? It seems totally bizarre - even within the first scenario mentioned (which failed at should redirect) it goes through a should redirect call in the 'Given an anonymous user' step.

The other bit is even stranger. Somehow, flash[:notice] seems to be broken in rspec (possibly just cucumber+..). When I put a flash[:notice] in my code that shows up fine on my browser, the step

Then I should see "You are now logged in"

fails. The weirdest part is that if I test for a flash[:error] message, it works fine! This one is SO bizarre I have a hard time believing that I didn't muddle something up somewhere, but I've have checked and checked again and rechecked everything that would seems obvious to check (to see if there are any assignments of flash[:notice] in the cucumber code, making sure things are spelled correctly and so on). I'm happy to put code up relating to that, but I'll leave it at this for now.

I should mention that I have been getting these strange deprecation messages every time I run rspec or cucumber and I decided to check into these as a possible source but as of yet have found nothing related to them. I started a separate post on this as I can't say for sure that it is related - http://railsforum.com/viewtopic.php?pid=114577#p114577.

Have other people been having experiences like these? Any ideas on what might be malfunctioning - things to check?

Chris

Re: Rspec redirect issues - (on Ruby 1.9.1 and Rails 2.3.5)

Not sure if this is actually the source of the problem or you just copied the wrong stuff into this post, but ...

re: flash[:notice], I see "Logged in successfully" in the controller code and "You are now logged in" in the Cucumber step.

HTH,
David

Re: Rspec redirect issues - (on Ruby 1.9.1 and Rails 2.3.5)

Thanks for the reply. Unfortunately that is not the issue.

Re: Rspec redirect issues - (on Ruby 1.9.1 and Rails 2.3.5)

I went ahead and installed the ZenTest gem so that I could use mulitruby to start managing my ruby versions (see http://blog.robseaman.com/2008/12/20/sw … ultiruby). I can now say that this happens when using ruby 1.8.7 as well as ruby 1.9.1 so the ruby version is not the issue. I have several apps that have this bug - it ONLY shows up in the cucumber tests. The flash[:notice] messages render just fine in the browser. Still haven't figured out why this is happening, but want keep things up to date on this topic in case any one else comes across this issue.

Chris

Re: Rspec redirect issues - (on Ruby 1.9.1 and Rails 2.3.5)

I can verify this behavior with Rails 2.3.4, Ruby 1.8.7, webrat 0.5.3, and cucumber 0.4.4.  The flash shows up in my browser, but it's not getting rendered in the page within the cucumber test.  The only plugins I'm using are haml and restful-authentication.

Re: Rspec redirect issues - (on Ruby 1.9.1 and Rails 2.3.5)

Hey there. I'm starting to think that restful_authentication might be at fault here. I thought for a sec that it might be haml, so I wrote a quick test with haml_index and erb_index actions within a simple welcome controller. There were no other plugins or gems or anything (other than webrat and cucumber) - just one controller with two actions, each of which assigned a flash[:notice]. To my surprise both actions tested out just fine! Didn't look at things for a while, but just now I started another project that wasn't set up with restful_auth but rather with Ryan Bates' nifty_authentication. To my surprise once again, the errors didn't crop up.

Now, before I can say that my money is on restful_auth being at fault there is something I still have to test. There is another application I wrote with super simple authentication along the lines of one of Ryan's early screencasts. I seem to remember having the flash errors with this one too, but I'll have to go back and take a look to be sure. I'll post results once I have them.

Thanks for sharing your verifications.

Chris

Re: Rspec redirect issues - (on Ruby 1.9.1 and Rails 2.3.5)

I can confirm that it is indeed restful-authentication that causes the problem. This has been discussed as a suspected bug in Webrat over here https://webrat.lighthouseapp.com/projec … -rails-234

As a workaround one can change the code in rest_auth_features_helper.rb as detailed here http://gist.github.com/258869

Cheers,
Martin

Re: Rspec redirect issues - (on Ruby 1.9.1 and Rails 2.3.5)

Great! Glad that has been cleared up : )

Chris

Re: Rspec redirect issues - (on Ruby 1.9.1 and Rails 2.3.5)

That patch from the gist fixes only the flash issues. Were you guys able to fix the redirect errors in the features? I am still getting those.

Re: Rspec redirect issues - (on Ruby 1.9.1 and Rails 2.3.5)

No, I haven't looked at it in a while.

Re: Rspec redirect issues - (on Ruby 1.9.1 and Rails 2.3.5)

I'm also getting redirect issues with the latest webrat (0.7.0). I am also thinking  the redirects are a webrat issue - haven't fully grokked the response in this post:

http://stackoverflow.com/questions/6799 … d-cucumber

also I am thinking it may be a combination webrat and the way in which the restful_auth step-defs are written.  anyone with more info

and Metasoarus, No as in, "you weren't able to fix it" or "you aren't getting them anymore?"

Re: Rspec redirect issues - (on Ruby 1.9.1 and Rails 2.3.5)

"No" as in "I haven't been working on any of my cuked restful_authentication projects lately and have been strapped for time to take another peak." I'll see if I can make some time for that though.

Chris

Re: Rspec redirect issues - (on Ruby 1.9.1 and Rails 2.3.5)

okay, that stack overflow response helped...now I have that flash message problem...will post again once i've resolved more.

Re: Rspec redirect issues - (on Ruby 1.9.1 and Rails 2.3.5)

Hi,everyone
I have the *flas[:notice]* problem. I tried to hack the rest_auth_features_helper.rb, yet I still got a empty flash object.
So i add the following code to the rest_auth_features_helper.rb:

puts "Session[#{session.object_id}] : #{@_flash.to_s}"

finding that session was reset after every redirect.

  #
  # Account Creation
  #
  Scenario: Anonymous user can create an account                                 # features\accounts.feature:24
Session[52452552] : noticeYou have been logged out.
Session[52435044] :
    Given an anonymous user                                                      # features/step_definitions/user_steps.rb:8
    And no user with login: 'Oona' exists                                        # features/step_definitions/ra_resource_steps.rb:40
Session[52367928] : noticeThanks for signing up!
    When she registers an account as the preloaded 'Oona'                        # features/step_definitions/user_steps.rb:42
    Then she should be redirected to the home page                               # features/step_definitions/ra_response_steps.rb:19
Session[53901624] :
    When she follows that redirect!                                              # features/step_definitions/ra_navigation_steps.rb:46
    Then she should see a notice message 'Thanks for signing up!'                # features/step_definitions/ra_response_steps.rb:112
      </Thanks for signing up!/> expected but was
      <"">.
      <false> is not true. (Spec::Expectations::ExpectationNotMetError)
      ./features/step_definitions/ra_response_steps.rb:113:in `/^she should +see an? (\w+) message '([\w !]+)'$/'
      features\accounts.feature:30:in `Then she should see a notice message 'Thanks for signing up!''
    And a user with login: 'oona' should exist                                   # features/step_definitions/ra_resource_steps.rb:53
    And the user should have login: 'oona', and email: 'unactivated@example.com' # features/step_definitions/ra_resource_steps.rb:65
    And oona should be logged in                                                 # features/step_definitions/user_steps.rb:68

somebody plz help...

Last edited by Zheng.Han (2010-08-13 10:30:35)

Re: Rspec redirect issues - (on Ruby 1.9.1 and Rails 2.3.5)

One option (possibly a little more involved) is to use authlogic instead. Supposedly they have some helpers that let you easily migrate restful_auth over to authlogic, but I haven't tried it myself, so I can't really testify. Authlogic has been way easier for me to use personally. I use it for all my user models now, and plan on converting the old restful_auth apps over as I go back to them and refactor/build upon. Let me know how it goes for you.

Re: Rspec redirect issues - (on Ruby 1.9.1 and Rails 2.3.5)

metasoarous wrote:

One option (possibly a little more involved) is to use authlogic instead. Supposedly they have some helpers that let you easily migrate restful_auth over to authlogic, but I haven't tried it myself, so I can't really testify. Authlogic has been way easier for me to use personally. I use it for all my user models now, and plan on converting the old restful_auth apps over as I go back to them and refactor/build upon. Let me know how it goes for you.

I'll be shifting to authlogic as I just started prototyping my app.
thank you