设备401使用自定义会话控制器返回正确的登录

时间:2011-10-12 13:19:24

标签: ruby-on-rails ruby json devise

我遇到了与设计和创建自定义会话控制器有关的一些问题。

我正在创建一个应用程序,我希望json是唯一的登录形式。所以这就是我到目前为止所做的。

会话控制器

class SessionsController < Devise::SessionsController

  # Create operation should login the user and respond with json status
  def create
    resource = warden.authenticate!(:scope => resource_name, :recall => "sessions#failure")
    sign_in(resource_name, resource)

    respond_to do | format |
      format.json { render :json => { :success => true, :user => resource }, :status => 200 }
    end
  end

  def failure
    return render :status => 401, :json => {:success => false, :errors => ["Login failed."]}
  end  

end

会话路线:

  devise_for :users, :controllers => {:sessions => "sessions"}, :skip => [:sessions] do
    get '/login' => 'sessions#new', :as => :new_user_session
    post '/login' => 'sessions#create', :as => :user_session
    get '/logout' => 'sessions#destroy', :as => :destroy_user_session
  end

测试我正在尝试运行:

require 'spec_helper'

describe SessionsController do
  include Devise::TestHelpers

  describe "POST 'create'" do
      describe "invalid signin" do

        before(:each) do
          @attr = { :email => "master_yoda@jedi.com", :password => "invalid" }
          request.env["devise.mapping"] = Devise.mappings[:user]
        end

        it "should return an error" do
          post :create, :user => @attr,  :format => :json

          puts @response.body
        end

      end
  end
end

现在,当我运行此测试时。我得到一个http响应而不是json:

#<ActionDispatch::Response:0x00000004cb8bb8>

当我通过命令行执行卷曲操作来输入数据时

这是命令:

curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d ' {"user":{"email":"aaa@blah.com","password":"aaaaaaaaa"}}'  http://192.168.202.128:3000/login

第一次执行发送401然后后续执行发送200

失败:

* About to connect() to 192.168.202.128 port 3000 (#0)
*   Trying 192.168.202.128... connected
* Connected to 192.168.202.128 (192.168.202.128) port 3000 (#0)
> POST /login HTTP/1.1
> User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5
> Host: 192.168.202.128:3000
> Accept: application/json
> Content-type: application/json
> Content-Length: 57
> 
< HTTP/1.1 401 Unauthorized 
< Content-Type: application/json; charset=utf-8
< X-Ua-Compatible: IE=Edge
< Cache-Control: no-cache
< X-Runtime: 2.026670
< Content-Length: 44
< Server: WEBrick/1.3.1 (Ruby/1.9.2/2011-07-09)
< Date: Mon, 10 Oct 2011 14:03:36 GMT
< Connection: Keep-Alive
< Set-Cookie: _styylt_session=BAh7BkkiD3Nlc3Npb25faWQGOgZFRkkiJWIxZDZiMmZkNTdlYTFkMzQ1MTRkZGZmNDA1NjljYzU5BjsAVA%3D%3D--fd27eebb575f61cf805b0f3ada4d2780fdc7f929; path=/; HttpOnly
< 
* Connection #0 to host 192.168.202.128 left intact
* Closing connection #0
{"success":false,"errors":["Login failed."]}

成功:

* About to connect() to 192.168.202.128 port 3000 (#0)
*   Trying 192.168.202.128... connected
* Connected to 192.168.202.128 (192.168.202.128) port 3000 (#0)
> POST /login HTTP/1.1
> User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5
> Host: 192.168.202.128:3000
> Accept: application/json
> Content-type: application/json
> Content-Length: 57
> 
< HTTP/1.1 200 OK 
< Content-Type: application/json; charset=utf-8
< X-Ua-Compatible: IE=Edge
< Etag: "c4da2521fa2f6c5721e7cf8d3c7626ce"
< Cache-Control: max-age=0, private, must-revalidate
< X-Runtime: 0.377351
< Content-Length: 71
< Server: WEBrick/1.3.1 (Ruby/1.9.2/2011-07-09)
< Date: Mon, 10 Oct 2011 14:03:45 GMT
< Connection: Keep-Alive
< Set-Cookie: _styylt_session=BAh7B0kiGXdhcmRlbi51c2VyLnVzZXIua2V5BjoGRVRbCEkiCVVzZXIGOwBGWwZpCUkiIiQyYSQxMCRnUEJzRDZXSHNTYkk0RkZxSlNjV0cuBjsAVEkiD3Nlc3Npb25faWQGOwBGSSIlY2M2ZGEzMWY0ZDM2ZDc2NjRmNjdhN2I1MjQ4NmFlMTkGOwBU--8fec7bbcf648276f24e3c19a3ce0562060eeabb1; path=/; HttpOnly
< 
* Connection #0 to host 192.168.202.128 left intact
* Closing connection #0
{"success":true,"user":{"email":"aaa@blah.com","username":"userblah2"}}

我不明白我做错了什么或者在这里发生了什么。我查看了大多数帖子,但找不到具体的答案。

希望有人能够提供帮助:)

谢谢

2 个答案:

答案 0 :(得分:2)

这是Devise测试帮助程序中的错误。它已在2074中修复。

答案 1 :(得分:0)

我遇到了完全相同的问题。我无法理解为什么response.body返回“ActionDispatch :: Response ...”而不是实际的响应主体。这似乎与warden.authenticate有问题!仅在测试中排队。正如你所描述的那样,使用curl也适用于我。最后,我只是将我的规范更改为以下内容:

context "with invalid credentials" do
  it "re-renders login page" do
    post :create, person: { email: @person.email, password: 'badpassword' }
    response.should be_ok
    response.should render_template("devise/sessions/new")
  end

  it "returns an error with json" do
    post :create, person: { email: @person.email, password: 'badpassword' }, format: 'json'
    response.status.should == 403
  end
end

虽然您无法完全测试json响应,但至少您仍在测试响应错误状态代码。