mercredi 18 novembre 2015

Rails 3.2.13: API Integration Test with Authorization Failure

I am trying to write integration tests for an API I wrote. The api requires authorization prior to access.

I am using FactoryGirl, with the following factory for my user:

factory :user do
    name "John Doe"
    sequence(:email) { |n| "person-#{n}@testSuite.com" }
    password "password"
    password_confirmation { |u| u.password }
    api_key "1234567890"
end

The authenticate method which is called in my controller's before_filter is:

def authenticate
  authenticate_or_request_with_http_token do |token, options|
    @current_user = User.where(api_key: token).first
  end
end

And my test is

test 'User Index' do
    @user = FactoryGirl.create(:user)
    get '/api/v1/users', authorization: "Token token=\"#{@user.api_key}\""
    assert_response :success
end

This test continually fails with a response of 401 being returned.

What is the correct way of setting the authorization token so that I can actually connect to the API?

Aucun commentaire:

Enregistrer un commentaire