简体   繁体   English

如何使用rspec在设计中测试json登录

[英]How to test json login in devise with rspec

I am trying to build a rails login process with devise that will allow the user to signin/signout through a mobile application. 我正在尝试使用设计构建rails登录过程,允许用户通过移动应用程序登录/注销。

I created a SessionsController like that : 我创建了一个像这样的SessionsController:

class SessionsController < Devise::SessionsController

  def create  
    resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
    set_flash_message(:notice, :signed_in) if is_navigational_format?
    sign_in(resource_name, resource)

    respond_to do |format|  
      format.html { super }  
      format.json {  
        render :status => 200, :json => { :error => "Success", :user =>  resource}.to_json  
      }  
    end  
  end

  def destroy  
    super  
  end
end

My routes : 我的路线:

devise_for :users, :controllers => {:sessions => "sessions"}
resources :users

Then I have the following spec to test the process : 然后我有以下规范来测试这个过程:

require 'spec_helper'

describe SessionsController do

  describe "POST 'signin'" do

    before (:each) do
      @user = Factory(:user)
    end

    it "should login with json request" do
      @expected = @user.to_json

      post :create, :user => {:email => 'user@test.com', :password => 'please'}, :content_type => 'application/json', :format => :json
      response.body.should == @expected
    end

  end

end

And I get the following error : 我收到以下错误:

Failure/Error: post :create, :user => {:email => 'user@test.com', :password => 'please'}, :content_type => 'application/json', :format => :json
     AbstractController::ActionNotFound:
       Could not find devise mapping for path "/users/sign_in.json?content_type=application%2Fjson&user%5Bemail%5D=user%40test.com&user%5Bpassword%5D=please".
       Maybe you forgot to wrap your route inside the scope block?

[EDIT] It seems like the functionality is ok but only the test is broken ; [编辑]似乎功能还可以,但只有测试被打破; because if I run this small script : 因为如果我运行这个小脚本:

require 'rest_client'
require "active_support/core_ext"

RestClient.post "http://localhost:3000/users/sign_in", {:user => {:email => 'user@test.com', :password => 'please'}}.to_json, :content_type => :json, :accept => :json

I get the following result : 我得到以下结果:

"{\"error\":\"Success\",\"user\":{\"email\":\"user@test.com\",\"name\":\"First User\"}}"

which is the expected one. 这是预期的。

I do not know if it is the best solution but here is how I successfully test my json login process through webservices on devise : 我不知道它是否是最好的解决方案,但这是我如何通过设备上的webservices成功测试我的json登录过程:

require 'spec_helper'
require 'rest_client'
require 'active_support/core_ext'

describe "sign up / sign out / sign in edit / cancel account" do

before (:each) do
  @user = {:name => "tester", :email =>"tester@test.biz"}
end

describe "POST 'sign up'" do

  it "should sign up with json request" do
    response_json = RestClient.post "http://localhost:3000/sign_up", {:user=>{:name=>"tester", :email => "tester@test.biz", :password => "FILTERED", :password_confirmation => "FILTERED"}}.to_json, :content_type => :json, :accept => :json
    response = ActiveSupport::JSON.decode(response_json)
    response["response"].should == "ok"
    response["user"].should == @user.as_json
    @@token = response["token"]
    @@token.should_not == nil
  end

end


describe "DELETE 'sign out'" do

  it "should logout with json request" do
    response_json = RestClient.delete "http://localhost:3000/users/sign_out?auth_token=#{@@token}", :accept => :json
    response = ActiveSupport::JSON.decode(response_json)
    response["response"].should == "ok"
  end

end


describe "POST 'sign in'" do

  it "should login with json request" do
    response_json = RestClient.post "http://localhost:3000/users/sign_in", {:user => {:email => "tester@test.biz", :password => "FILTERED"}}.to_json, :content_type => :json, :accept => :json
    response = ActiveSupport::JSON.decode(response_json)
    response["response"].should == "ok"
    response["user"].should == @user.as_json
    @@token = response["token"]
    @@token.should_not == nil
  end

end


describe "PUT 'edit'" do

  it "should edit user name with json request" do
    response_json = RestClient.put "http://localhost:3000/users?auth_token=#{@@token}", {:user => {:name => "tester2", :email => "tester@test.biz", :current_password => "FILTERED"}}.to_json, :content_type => :json, :accept => :json
    response = ActiveSupport::JSON.decode(response_json)
    response["response"].should == "ok"
    response["user"]["email"].should == "tester@test.biz"
    response["user"]["name"].should == "tester2"
    @@token = response["token"]
    @@token.should_not == nil
  end

end


describe "DELETE 'account'" do

  it "should logout with json request" do
    response_json = RestClient.delete "http://localhost:3000/users?auth_token=#{@@token}", :accept => :json
    response = ActiveSupport::JSON.decode(response_json)
    response["response"].should == "ok"
  end

end
end

With that I can create a new user, log out, log in again, edit the account and delete the account. 有了它,我可以创建一个新用户,注销,再次登录,编辑帐户和删除帐户。 Everything through 5 webservices in json. 一切都通过json中的5个webservices。 This order of execution allows the tests to be played every time, but I think there is missing a real rollback process to be executed at the end of the campaign. 这个执行顺序允许每次都进行测试,但我认为在广告系列结束时缺少一个真正的回滚过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM