0
votes

Hi Ruby on rails users,

I'm testing a controller that requires JWT(json web token) to use features but getting Rspec getting ActionController::UrlGenerationError: No route match error message.

Here's what I'm working on.

Controller:

class V1::VisitsController < ApplicationController
    before_action :authorize_request

    def index
        # Do something and return a list of visits
        #render json: visits, status: :ok
    end
end

Test


require 'rails_helper'
require 'json'

RSpec.configure do |c|
  c.include RequestSpecHelper
end

RSpec.describe V1::VisitsController, type: :controller do
  describe "GET index" do
    it "cannot retrieve a list of visits from the same group without a valid token" do
            post :index,
                params: {
                    :group_id => me.group_id
                }
            expect(response).to have_http_status(:unauthorized)
        end

        it "can retrieve a list of visits from the same group with a valid token" do
            # Getting error here
            jwt = confirm_and_login_user(me)
            post index, headers: { "token" => "#{jwt}" }
            expect(response).to have_http_status(:ok)
        end

and I'm using the following custom helper... stored in spec/support/request_spec_helper.rb

module RequestSpecHelper
    def confirm_and_login_user(user)
      post :auth_login_url, params: {email: user.email, password: user.password}
      result = JSON.parse(response.body)
      return result['token']
    end
end

with routes look like the following:


Prefix Verb   URI Pattern                                                                              Controller#Action
                    users GET    /users(.:format)                                                                         users#index
                          POST   /users(.:format)                                                                         users#create
                     user GET    /users/:_username(.:format)                                                              users#show
                          PATCH  /users/:_username(.:format)                                                              users#update
                          PUT    /users/:_username(.:format)                                                              users#update
                          DELETE /users/:_username(.:format)                                                              users#destroy
               auth_login POST   /auth/login(.:format)                                                                    authentication#login
             v1_locations GET    /v1/locations(.:format)                                                                  v1/locations#index
                          POST   /v1/locations(.:format)                                                                  v1/locations#create
              v1_location GET    /v1/locations/:_location(.:format)                                                       v1/locations#show
                          PATCH  /v1/locations/:_location(.:format)                                                       v1/locations#update
                          PUT    /v1/locations/:_location(.:format)                                                       v1/locations#update
                          DELETE /v1/locations/:_location(.:format)                                                       v1/locations#destroy
                v1_visits GET    /v1/visits(.:format)                                                                     v1/visits#index
                          POST   /v1/visits(.:format)                                                                     v1/visits#create
                 v1_visit GET    /v1/visits/:_visit(.:format)                                                             v1/visits#show
                          PATCH  /v1/visits/:_visit(.:format)                                                             v1/visits#update
                          PUT    /v1/visits/:_visit(.:format)                                                             v1/visits#update
                          DELETE /v1/visits/:_visit(.:format)                                                             v1/visits#destroy

and this is the error msg I'm getting

..F..................

Failures:

  1) V1::VisitsController GET index can retrieve a list of visits from the same group with a valid token
     Failure/Error: post :auth_login_url, params: {email: user.email, password: user.password}

     ActionController::UrlGenerationError:
       No route matches {:action=>"auth_login_url", :controller=>"v1/visits", :email=>"me@gmailcom", :password=>"hashed-password"}
     # ./spec/support/request_spec_helper.rb:3:in `confirm_and_login_user'
     # ./spec/controllers/v1/visits_controller_spec.rb:66:in `block (3 levels) in <main>'

Finished in 0.28883 seconds (files took 0.80089 seconds to load)

I can see the problem. post auth_login_url in the helper is trying to look into V1::VisitsController which doesn't have such a route. I believe letting this helper know that it's authentication#login that needs to be executed will fix the issue but I can't find how.

How can I make it work?

Thanks,

1

1 Answers

0
votes

If I am not wrong, you have to actually invoke the auth_login_url method instead of passing it as a symbol. Instead of:

post :auth_login_url, params: {email: user.email, password: user.password}

just use

post auth_login_url, params: {email: user.email, password: user.password}

Note : has been removed.