How to test json login during development using rspec

I am trying to create a rails login process using an application that will allow the user to subscribe / unsubscribe through a mobile application.

I created a SessionController like this:

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 specification 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] The functionality seems to be in order, but only the test is broken; because if I ran this little 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 expected.

+7
source share
1 answer

I don't know if this is the best solution, but here is how I successfully tested my json login process through webservices during development:

 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 this, I can create a new user, log out, log back in, edit the account and delete the account. All through 5 web services in json. This order of execution allows you to run tests every time, but I think that at the end of the campaign there is no real rollback process.

+7
source

All Articles