2011-10-04 30 views
7

Tôi đang cố gắng xây dựng quy trình đăng nhập đường ray với ý tưởng cho phép người dùng đăng nhập/đăng xuất thông qua ứng dụng dành cho thiết bị di động.Làm thế nào để kiểm tra đăng nhập json trong lập với rspec

Tôi tạo ra một SessionsController như thế:

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 

tuyến đường của tôi:

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

Sau đó, tôi có spec sau để kiểm tra quá trình:

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 => '[email protected]', :password => 'please'}, :content_type => 'application/json', :format => :json 
     response.body.should == @expected 
    end 

    end 

end 

Và tôi nhận được lỗi sau:

Failure/Error: post :create, :user => {:email => '[email protected]', :password => 'please'}, :content_type => 'application/json', :format => :json 
    AbstractController::ActionNotFound: 
     Could not find devise mapping for path "https://stackoverflow.com/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] Dường như chức năng là ok nhưng chỉ thử nghiệm bị hỏng; bởi vì nếu tôi chạy script nhỏ này:

require 'rest_client' 
require "active_support/core_ext" 

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

tôi nhận được kết quả sau:

"{\"error\":\"Success\",\"user\":{\"email\":\"[email protected]\",\"name\":\"First User\"}}" 

đó là một trong những dự kiến.

Trả lời

7

Tôi không biết nếu nó là giải pháp tốt nhất nhưng đây là cách tôi thử nghiệm thành công quá trình đăng nhập json của tôi thông qua webservices trên devise:

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 =>"[email protected]"} 
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 => "[email protected]", :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 => "[email protected]", :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 => "[email protected]", :current_password => "FILTERED"}}.to_json, :content_type => :json, :accept => :json 
    response = ActiveSupport::JSON.decode(response_json) 
    response["response"].should == "ok" 
    response["user"]["email"].should == "[email protected]" 
    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 

Với rằng tôi có thể tạo một người dùng mới, đăng xuất, đăng nhập một lần nữa, chỉnh sửa tài khoản và xóa tài khoản. Tất cả mọi thứ thông qua 5 webservices trong json. Thứ tự thực hiện này cho phép các thử nghiệm được phát mỗi lần, nhưng tôi nghĩ rằng có một quá trình rollback thực được thực hiện vào cuối chiến dịch.

+0

có vẻ như localhost: 3000 cần được bật cho thử nghiệm? – Karan

+0

Có: Tôi sử dụng bảo vệ để xem qua các sửa đổi và chạy thử nghiệm trong quá trình phát triển, vì vậy localhost: 3000 luôn được bật. – obo

Các vấn đề liên quan