jeudi 28 janvier 2016

RSpec with Knock returning unauthorized

I'm trying to use the Knock gem in a rails api. When testing the response is always unauthorized with response.body equaling f.

deliveries_controller_spec.rb

require 'rails_helper'

module V1
  RSpec.describe DeliveriesController, type: :controller do
    context 'index' do
      before do
        @deliveries = [DateTime.now, (DateTime.now - 1), DateTime.now, (DateTime.now + 1), DateTime.now].each_with_object([]) do |time, stash|
          stash << create(:delivery, eta: time)
        end

        @deliveries.each_with_index do |delivery, index|
          delivery.locations << create(:location, name: "Location #{index}")
        end
        authenticate
      end
      it 'give all deliveries that have an eta for the current day' do
        get :index
        expected_count = 3
        full_count = @deliveries.size
        puts response.body
        expect(json_data.size).to_not be(full_count)
        expect(json_data.size).to be(expected_count)
      end
      context 'by location' do
        it 'give all deliveries for a location for that day' do
          location = @deliveries.last.locations.first
          get :index, location_id: location.id
          expected_count = 1
          full_count = @deliveries.size
          puts response.body
          expect(json_data.size).to_not be(full_count)
          expect(json_data.size).to be(expected_count)
        end
      end
    end
    context 'CRUD' do
      it 'show' do
        delivery = create(:delivery)
        get :show, id: delivery
        puts response.body
        expect(response).to be_successful
        expect(json_data).to include(:id, :type, :attributes)
      end
      it 'create' do
        post :create, json_api_format { attributes_for(:delivery) }
        puts response.body
        expect(response).to be_successful
        expect(json_data).to include(:id, :type, :attributes)
      end

      it 'update' do
        delivery = create(:delivery)
        old_cargo = delivery.cargo
        expected_cargo = 'Updated Cargo'
        delivery.cargo = expected_cargo
        patch :update, json_api_format(delivery.id) { delivery.attributes }
        delivery.reload
        puts response.body
        expect(delivery.cargo).to_not eq(old_cargo)
        expect(delivery.cargo).to eq(expected_cargo)
      end
    end
  end
end

deliveries_controller.rb

module V1
  class DeliveriesController < ApplicationController
    before_action :authenticate
    def index
      if location
        render json: location.deliveries.today, status: :ok
      else
        render json: Delivery.today, include: '*', status: :ok
      end
    end

    def show
      delivery = Delivery.find delivery_id
      render json: delivery, include: '*', status: :ok
    end

    def create
      delivery = Delivery.create delivery_params
      render json: delivery, include: '*', status: :created
    end

    def update
      delivery = Delivery.find delivery_id
      delivery.update delivery_params
      render json: delivery, status: :ok
    end

    private

    def location
      @location ||= Location.find_by(id: location_params[:location_id])
    end

    def location_params
      params.permit(:location_id)
    end

    def delivery_id
      params[:id]
    end

    def delivery_params
      ActiveModelSerializers::Deserialization
        .jsonapi_parse(params, only: [:cargo, :cargo_quantity, :notes, :arrive_at, :escort,
                                      :priority, :processing_time, :eta, :icon_url, :escort_badge_number])
    end
  end
end

spec support

 def authenticate
   token = Knock::AuthToken.new(payload: { sub: create(:driver).id }).token
   request.env['HTTP_AUTHORIZATION'] = "Bearer #{token}"
 end

Aucun commentaire:

Enregistrer un commentaire