Skip to content

Rails 3.1 Devise Token authorization controller spec fails on named route with constraints #2774

Closed
sjtipton opened this Issue Aug 31, 2011 · 6 comments

5 participants

@sjtipton

Major players in this implementation

  • Rails 3.1.0
  • Devise 1.4.3
  • Rspec 2.6.4
  • sunspot_rails 1.2.1

This is also posted to StackOverflow with no views other than ours!

http://stackoverflow.com/questions/7247659/rails-3-1-devise-token-authorization-controller-spec-fails-on-named-route-with-co

The Problem

I am contributing on a project for a web service using Rails 3.1.0 stable, Rspec 2.6.4, sunspot_rails 1.2.1 and Devise 1.4.3 for token authentication, passed as a query parameter. While the implementation works for the route and controller in question, there are two specs that still fail.

They started failing when rails was updated from 3.1.rc4 to 3.1.rc5. We have since stepped up to 3.1.0

After very lengthy and fruitless googling I was hoping there was someone out there who could help shed some light on the problem.

Using the following route scope:

scope :controller => :job_postings, :path => "/api/v1" do

  get "/job_postings" => :index,
                :defaults => { :format => "json" }

  get "/job_postings/near/:lat/:lng(/:precision)" => :near, :as => "near",
                :defaults => { :format => "json", :page => 1, :precision => '5' },
                :constraints => { :lat => /\-*\d+\.\d+/,
                                              :lng => /\-*\d+\.\d+/,
                                              :precision => /[0-9]+/ }
  ...
end

With these controller specs:

require 'spec_helper'
require 'support/solr_spec_helper'
include SolrSpecHelper

describe JobPostingsController do

  render_views

  before(:each) do
    @job_posting       = Factory.create(:job_posting)
    @job_posting_count = JobPosting.where(:state => 'VA', :city => 'Virginia Beach').count
    assert_not_equal @job_posting_count, 0, "No job postings found in the database"

    @user = Factory(:user)
    assert(@user.confirm!, "Failed confirmation!")

    solr_setup
    JobPosting.reindex
    assert_not_equal JobPosting.indexed, 0, "No job postings were found in the Solr index"
  end

  after(:each) do
    @user.destroy
    JobPosting.remove_all_from_index!
    JobPosting.delete_all
  end

  describe "GET index" do

    context "when valid" do

      it "should be sucessful when a request with no parameters is passed" do
        get :index, :format => :json, :auth_token => @user.authentication_token
        response.should be_success
      end
    ...
    end
  ...
  end

  describe "GET 'near'" do

    context "when valid" do

      it "A request the parameters :lat => '36.8527778', :lng => '-75.9783333' should be successful" do
        get :near, :lat => '36.8527778', :lng => '-75.9783333', :auth_token => @user.authentication_token
        response.should be_success
      end

      it "A request the parameters :lat => '36.8527778', :lng => '-75.9783333', :precision => '3' should be successful" do
        get :near, :lat => '36.8527778', :lng => '-75.9783333', :precision => '3', :auth_token => @user.authentication_token
        response.should be_success
      end
    end
  ...
  end
  ...
  end
end

I am getting the following 2 failures:

Failures:

  1) JobPostingsController GET 'near' when valid A request the parameters :lat => '36.8527778', :lng => '-75.9783333' should be successful
     Failure/Error: get :near, :lat => '36.8527778', :lng => '-75.9783333', :auth_token => @user.authentication_token
     ActionController::RoutingError:
       No route matches {:lat=>"36.8527778", :lng=>"-75.9783333", :auth_token=>"RFhpxYNYeZnjqwJLjqpz", :controller=>"job_postings", :action=>"near"}
     # ./spec/controllers/job_postings_controller_spec.rb:75:in `block (4 levels) in <top (required)>'

  2) JobPostingsController GET 'near' when valid A request the parameters :lat => '36.8527778', :lng => '-75.9783333', :precision => '3' should be successful
     Failure/Error: get :near, :lat => '36.8527778', :lng => '-75.9783333', :precision => '3', :auth_token => @user.authentication_token
     ActionController::RoutingError:
       No route matches {:lat=>"36.8527778", :lng=>"-75.9783333", :precision=>"3", :auth_token=>"X6pLAjczKzH4YH6YbY1q", :controller=>"job_postings", :action=>"near"}
     # ./spec/controllers/job_postings_controller_spec.rb:80:in `block (4 levels) in <top (required)>'

Please note that the other actions (e.g. :index) pass when sending the auth_token query param.

Also note that the routing specs for the :near action pass as well:

...
it "routes the named route 'near'" do

  { :get => near_path(lat: '36.832027',
                      lng: '-76.283998') }.should route_to(controller: "job_postings",
                                                           action: "near",
                                                           format: 'json',
                                                           page: 1,
                                                           precision: '5',
                                                           lat: '36.832027',
                                                           lng: '-76.283998')

end

it "should route to job_postings#near" do
  get('/api/v1/job_postings/near/36.832027/-76.283998/5').should route_to(controller: "job_postings",
                                                                          action: "near",
                                                                          format: 'json',
                                                                          page: 1,
                                                                          precision: '5',
                                                                          lat: '36.832027',
                                                                          lng: '-76.283998')
end
...

Here are the result of running rake routes:

job_postings GET /api/v1/job_postings(.:format) {:format=>"json", :controller=>"job_postings", :action=>"index"}
near GET /api/v1/job_postings/near/:lat/:lng(/:precision)(.:format) {:lat=>/\-*\d+\.\d+/, :lng=>/\-*\d+\.\d+/, :precision=>"5", :format=>"json", :controller=>"job_postings", :action=>"near"}

What could potentially be causing these 2 specs to fail?

If any more information or details are required please let me know.

Thanks in advance for any suggestions/help you can provide.

@arunagw
Ruby on Rails member
arunagw commented Dec 17, 2011

@sjtipton Can you please see if issue still exists with the latest version??

@sjtipton

Absolutely! Latest version of which particular gem?

@arunagw
Ruby on Rails member
arunagw commented Dec 17, 2011

3.1.3 of Rails

@phudgins

We have updated to 3.1.3 of Rails with the same failures.

@mattfawcett

I think this is the same issue as #2781

@sikachu
Ruby on Rails member
sikachu commented Apr 27, 2012

#5219 was merged as a fixed for #2781, which seems like a duplicate fo this one. I'm closing this.

@sikachu sikachu closed this Apr 27, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.