1
votes

I am using ruby 2.3.1 with rails 5.0.7. I have the following errors when running RSpec:

Failures:

1) AuthenticationController POST /auth/login when request is valid returns an authentication token Failure/Error: before { post 'auth/login', params: valid_credentials , headers: headers }

 ActionController::UrlGenerationError:
   No route matches {:action=>"auth/login", :controller=>"authentication", :email=>"[email protected]", :password=>"foobar"}
 # ./spec/controllers/authentication_controller_spec.rb:32:in `block (4 levels) in <top (required)>'
 # ./spec/rails_helper.rb:85:in `block (3 levels) in <top (required)>'
 # ./spec/rails_helper.rb:84:in `block (2 levels) in <top (required)>'

2) AuthenticationController POST /auth/login When request is invalid returns a failure message Failure/Error: before { post '/auth/login', params: invalid_credentials, headers: headers }

 ActionController::UrlGenerationError:
   No route matches {:action=>"/auth/login", :controller=>"authentication", :email=>"[email protected]", :password=>"30NoVoGu7h"}
 # ./spec/controllers/authentication_controller_spec.rb:41:in `block (4 levels) in <top (required)>'
 # ./spec/rails_helper.rb:85:in `block (3 levels) in <top (required)>'
 # ./spec/rails_helper.rb:84:in `block (2 levels) in <top (required)>'

Here is my routes.rb:

Rails.application.routes.draw do # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

resources :todos do

 resources :items

end

post 'auth/login', to: 'authentication#authenticate'

post 'signup', to: 'users#create'

end

Here are the generated routes:

Prefix Verb URI Pattern Controller#Action

    todo_items GET    /todos/:todo_id/items(.:format)     items#index

       POST   /todos/:todo_id/items(.:format)     items#create


    todo_item GET    /todos/:todo_id/items/:id(.:format) items#show

       PATCH  /todos/:todo_id/items/:id(.:format) items#update

       PUT    /todos/:todo_id/items/:id(.:format) items#update

       DELETE /todos/:todo_id/items/:id(.:format) items#destroy

 todos GET    /todos(.:format)                    todos#index

       POST   /todos(.:format)                    todos#create

  todo GET    /todos/:id(.:format)                todos#show

       PATCH  /todos/:id(.:format)                todos#update

       PUT    /todos/:id(.:format)                todos#update

       DELETE /todos/:id(.:format)                todos#destroy

  auth_login POST   /auth/login(.:format)        authentication#authenticate

signup POST   /signup(.:format)                   users#create

Here is the authentication_controller.spec:

    require 'rails_helper'

    RSpec.describe AuthenticationController, type: :controller do
      # Authentication test suite
      describe 'POST /auth/login' do
        # create test user
        let!(:user) { create(:user) }

        # set headers for authorization
        let(:headers) { valid_headers.except('Authorization')  }

        # set test valid and invalid credentials
        let(:valid_credentials) do 
           { 
              email: user.email, 
              password: user.password 
           } 
        end # let(:valid_credentials) do 

        let(:invalid_credentials) do 
          {
             email: Faker::Internet.email,
             password: Faker::Internet.password
          }
        end # let(:invalid_credentials) do 

        # set request.headers to our custom headers
        # before { allow(request).to receive(:headers).and_return(headers) }

        # returns auth token when request is valid
        context 'when request is valid' do
          before { post 'auth/login', params:  valid_credentials , headers: headers }

          it 'returns an authentication token' do
            expect(json['auth_token']).not_to be_nil
          end #  it 'returns an authentication token' do
        end #     context 'when request is valid' do

        # returns failure message when request is invalid
        context 'When request is invalid' do
          before { post '/auth/login', params: invalid_credentials, headers: headers }

          it 'returns a failure message' do
            expect(json['message']).to match(/Invalid credentials/)
          end # it 'returns a failure message' do
        end # context 'When request is invalid' do


      end # describe 'POST /auth/login' do

    end # RSpec.describe AuthenticationController, type: :controller do

Here is authentication_controller:

    class AuthenticationController < ApplicationController

      skip_before_action :authorize_request, only: :authenticate
      # return auth token once user is authenticated
      def authenticate
        auth_token = AuthenticateUser.new(auth_params[:email], auth_params[:password]).call
        json_response(auth_token: auth_token)
      end # def authenticate

      private

      def auth_params
        params.permit(:email, :password)
      end

    end

Any assistance will be appreciated. Thanks in advance.

1

1 Answers

4
votes

You're hitting the wrong action. You're already in your Authentication controller so other than the params you just need the action:

post 'authenticate', params:  valid_credentials , headers: headers

The place you are posting must match the end point of the controller, whatever you've defined the method as, in this case 'authenticate'