yretuta
yretuta

Reputation: 8111

authentication problems in rails functional tests

I am testing this controller:

class SessionsController < ApplicationController
  def new
  end

  def create
    @current_user = User.find_by_login_and_password(
      params[:login], params[:password]
    )

    if @current_user
      session[:user_id] = @current_user.id
      if session[:return_to]
        redirect_to session[:return_to]
        session[:return_to] = nil
      else
        redirect_to stories_path
      end
    else
      render :action => 'new'
    end
  end

  def destroy
    session[:user_id] = @current_user = nil
  end

end

with this fixture (users.yml):

patrick:
  login: patrick
  password: sekrit
  name: Patrick Lenz
  email: [email protected]

john:
  login: john
  password: gh752px
  name: John Doe
  email: [email protected]

and this functional test:

require 'test_helper'

class SessionsControllerTest < ActionController::TestCase
  def test_should_show_login_form
    get :new
    assert_response :success
    assert_template 'new'
    assert_select 'form p', 4
  end

  def test_should_perform_user_login
    post :create, :login =>'patrick', :password => 'sekrit'
    assert_redirected_to stories_path
    assert_equal users(:patrick).id, session[:user_id]
    assert_equal users(:patrick), assigns(:current_user)
  end
end

However, I get this failure:

test_should_perform_user_login(SessionsControllerTest) [/home/ygamretuta/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-2.3.9/lib/action_controller/test_case.rb:119]:
Expected response to be a <:redirect>, but was <200>.
Expected block to return true value

the login logic is working but my tests fail, my route to the stories_path:

story_votes GET    /stories/:story_id/votes(.:format)          {:controller=>"votes", :action=>"index"}
                POST   /stories/:story_id/votes(.:format)          {:controller=>"votes", :action=>"create"}
 new_story_vote GET    /stories/:story_id/votes/new(.:format)      {:controller=>"votes", :action=>"new"}
edit_story_vote GET    /stories/:story_id/votes/:id/edit(.:format) {:controller=>"votes", :action=>"edit"}
     story_vote GET    /stories/:story_id/votes/:id(.:format)      {:controller=>"votes", :action=>"show"}
                PUT    /stories/:story_id/votes/:id(.:format)      {:controller=>"votes", :action=>"update"}
                DELETE /stories/:story_id/votes/:id(.:format)      {:controller=>"votes", :action=>"destroy"}
        stories GET    /stories(.:format)                          {:controller=>"stories", :action=>"index"}
                POST   /stories(.:format)                          {:controller=>"stories", :action=>"create"}
      new_story GET    /stories/new(.:format)                      {:controller=>"stories", :action=>"new"}
     edit_story GET    /stories/:id/edit(.:format)                 {:controller=>"stories", :action=>"edit"}
          story GET    /stories/:id(.:format)                      {:controller=>"stories", :action=>"show"}
                PUT    /stories/:id(.:format)                      {:controller=>"stories", :action=>"update"}

What could I be missing?

EDIT: This is the structure of my users table (it's using a plain text password only for the book examples)

CREATE TABLE "users" (
 "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 
 "login" varchar(255), 
 "password" varchar(255), 
 "name" varchar(255), 
 "email" varchar(255), 
 "created_at" datetime, 
 "updated_at" datetime
)

Upvotes: 1

Views: 1192

Answers (1)

Augusto
Augusto

Reputation: 30062

Ygam, I'm 99% sure that the problem is that the password doesn't match. Are you using salted / encrypted passwords? if that's the case, check that your fixture has plain text passwords.

I'm quite confident that this is the case, because you're doing a post and getting a 200, and the only path in create that returns a 200, is when the user / password pair validation fails.

If you're using salted passwords, you might need to do something like this in your fixture

<% SALT = "NaCl" unless defined?(SALT) %>
one:
  name: dave
  hashed_password: <%= User.encrypt_password('secret', SALT) %>
  salt: <%= SALT %>

two:
  name: MyString
  hashed_password: MyString
  salt: MyString

Upvotes: 3

Related Questions