nsarno/knock

Couldn't get test example to work - Rspec, rails 5.0

benkoshy opened this issue · 0 comments

Hi all,

Wondering if you have any advice: There was a similar post before on getting the tests working. But I couldn't get it working:

  • I'm using Rspec on
  • Rails ~> 5.0 :

It responds with a forbidden access error: expected the response to have a success status code (2xx) but it was 401. Any advice much appreciated. Here it is below.

require 'rails_helper'
include ActionController::RespondWith

describe "A request spec going to an authorised resource - seeing if authentication is occurring", type: :request do
  context "context: the client exists and can be found, " do
      before(:each) do
        @current_user = FactoryBot.create :user        
        @client = FactoryBot.create :client         
      end
  
      it 'responds successfully' do
         token =   Knock::AuthToken.new(payload: { sub: @current_user.id }).token    
         get api_find_client_by_name_path(@client.name), headers: {'Accept' => 'application/json', "Authorization" => "Bearer: #{token}"}         
         expect(response).to have_http_status(:success)         
      end      
  end
end

It's failing here:

127.0.0.1 - - [07/Feb/2018:16:41:15 AEDT] "GET /api/clients/1.json HTTP/1.1" 401 0
- -> /api/clients/1.json
Started GET "/api/clients/1.json" for 127.0.0.1 at 2018-02-07 16:41:26 +1100
Processing by Api::V1::ClientsController#show as JSON
  Parameters: {"id"=>"1"}
Filter chain halted as :authenticate_user rendered or redirected
Completed 401 Unauthorized in 1ms (ActiveRecord: 0.0ms)

My question is that when authenticate user is called, how will devise know to check for a token in the header file?

anyways, any advice much appreciated.

rgds

Ben