title: Controller test fails for rails user authentication
body: <p>--preface: ignore if you want.<br>
  I'm new to rails, and working on a project that will require user authentication.
  I found <a href="http://www.aidanf.net/rails_user_authentication_tutorial" rel="nofollow noreferrer">this tutorial</a> and have been trying to go through it and understand what's happening. Of course, it's not exactly what I need as-is, so I've been modifying as I go along. The tutorial is also out of date in some areas, so of course I've had to update my code. So part of my problem is that I'm not sure if the bug is in my modifications, or some function that's been deprecated, or what.</p>
  
  <p>--the question<br>
  This is the (simplest) test that fails. (" expected to not be nil" on the first assert statement.)</p>
  
  <pre><code>def test_authentication
    #check we can log in
    post :login, :user =&gt; { :username =&gt; "bob", :password =&gt; "test" }
    assert_not_nil session[:user_id]
    assert_equal users(:bob).id, session[:user_id]
    assert_response :redirect
    assert_redirected_to :action =&gt; 'welcome'
  end
  </code></pre>
  
  <p>It calls the user_controller action login:</p>
  
  <pre><code>def login
    if request.post?
      if session[:user_id] = User.authenticate(params[:user][:username], params[:user][:password])
      flash[:message] = "Login succeeded!"
      redirect_to_stored
      else
        flash[:warning] = "Login failed."
      end
    end
  end
  </code></pre>
  
  <p>which calls the User method authenticate. I know that authenticate works properly, however, because I have a single test that does pass:</p>
  
  <pre><code>def test_registration
    #check that we can register and are logged in automatically
    post :register, :user =&gt; { :username =&gt; "newuser", :password =&gt; "pass", :password_confirmation =&gt; "pass", :email =&gt; "newuser@web.com" }
    assert_response :redirect
    assert_not_nil session[:user_id]
    assert_redirected_to :action =&gt; 'welcome'
  end
  </code></pre>
  
  <p>which calls the user_controller action register</p>
  
  <pre><code>def register
    @user = User.new(params[:user])
    if request.post?
      if @user.save
        session[:user_id] = User.authenticate(@user.username, @user.password)
        flash[:message] = "Registration succeeded"
        redirect_to :action =&gt; 'welcome'
      end
    else
      flash[:warning] = "Registration failed"
    end
  end
  </code></pre>
  
  <p>which successfully calls authenticate.</p>
  
  <p>the users fixture has one relevant record:</p>
  
  <pre><code>bob:
    username: bob
    email: bob@mcbob.com 
    hashed_password: 77a0d943cdbace52716a9ef9fae12e45e2788d39 # test
    salt: 1000
  </code></pre>
  
  <p>I've tested the hashed password and salt - "test" is the correct password.</p>
  
  <p>So by my analysis, the bug has got to be in one of 3 places:<br>
  how I'm sending my post request,<br>
  how I'm accessing the parameters in the login action,<br>
  or some aspect of the fixture loading.</p>
  
  <p>(originally I was using the tutorial's code to load the fixture explicitly (self.use_instantiated_fixtures = true; fixtures :users), but I read that all fixtures are automatically loaded before testing, so I took it out. That didn't change a thing.)</p>
  
  <p>Of course, since I can't seem to find the problem in those areas, it could just as well be anywhere else.</p>