mercredi 28 décembre 2016

RSpec: Authenticating before test with devise_auth_token

I am using devise_auth_token to authenticate users for an API. I would like to authenticate users before each test is run, but keep getting a 401 error. When I use postman to the endpoint with the correct headers, it works, but fails to work during tests.

before(:each) do
    @user = FactoryGirl.create(:user)
end

def get_auth
  headers = @user.create_new_auth_token
  auth = Hash.new
  auth["client"] = headers["client"]
  auth["access-token"] = headers["access-token"]
  auth["uid"] = headers["uid"]
  auth["expiry"] = headers["expiry"]
  return auth
end

it "auth user should return success" do
    get 'get_tasks_for_user', params: {uid: @user.uid}, headers: get_auth
    expect(response).to have_http_status(200)
end

RSpec

TasksController auth user should return success
     Failure/Error: expect(response).to have_http_status 200
       expected the response to have status code 200 but it was 401

Aucun commentaire:

Enregistrer un commentaire