title: How to create session in Rails 4 using Authlogic and HTTP post request (test)
body: <p>I am somewhat new to Rails 4 and am working on a project that requires authentication. I have recently implemented Authlogic to handle it.</p>
  
  <p>The problem is a specific test for the session controller where I try to log in using HTTP post, which fails redirecting me to the login path. I am interested in this because it will probably be used later to log an Android application using the users in my Rails app.</p>
  
  <p>This is the test:</p>
  
  <pre><code>sessions_controller_test.rb
     test "should post create" do
       this_user = users(:one)
       post :create, username: this_user.username, password: 'secret'
       assert_redirected_to admin_url
       assert_equal this_user.id, current_user.id #session[:user_id]
     end
  </code></pre>
  
  <p>This is the code being tested:</p>
  
  <pre><code>sessions_controller.rb       
     def create
      @user_session = Session.new(params[:user_session])
      if @user_session.save
       flash[:notice] = "Login successful."
       redirect_to admin_url
      else
       redirect_to login_url, alert: "Invalid user/password combination"
      end
     end
  </code></pre>
  
  <p>And the test result:</p>
  
  <pre><code>  2) Failure:
  SessionsControllerTest#test_should_post_create [/home/mario/railsprj/depot/test/controllers/sessions_controller_test.rb:13]:
  Expected response to be a redirect to &lt;http://test.host/admin&gt; but was a redirect to &lt;http://test.host/login&gt;.
  Expected "http://test.host/admin" to be === "http://test.host/login".
  </code></pre>
  
  <p>Thanks in advance.</p>