-1

I have a spec type: :request and I want to add authentication via ApiAuth. How can i do that?

ApiAuth use request object for authentication

ApiAuth.sign!(request, user.id, user.api_secret_key)

And I can use it in specs for controllers as follows

request.env['HTTP_ACCEPT'] = 'application/json'
user = defined?(current_user) ? current_user : Factory.create(:admin_user)
ApiAuth.sign!(request, user.id, user.api_secret_key)

But request object is missing in specs for requests

describe 'Cities API', type: :request do
  let(:cities) { City.all }
  let(:admin_user) { Factory.create(:admin_user) }

  context 'given an unauthorized request' do
    it 'returns 401 status' do
      get '/api/cities'

      expect(response).to have_http_status(:unauthorized)
    end
  end

  context 'given an authorized request' do
    it 'sends a list of cities' do
      # i need authorization here
      get '/api/cities'

      expect(response).to be_success
    end
  end
end

Now I stub authentication

describe 'Cities API', type: :request do
  let(:cities) { City.all }
  let(:admin_user) { Factory.create(:admin_user) }

  before { Factory.create(:route) }

  context 'given an unauthorized request' do
    it 'returns 401 status' do
      get '/api/cities'

      expect(response).to have_http_status(:unauthorized)
    end
  end

  context 'given an authorized request' do
    before(:each) do
      allow_any_instance_of(CitiesController).to receive(:authenticate_user!).and_return(true)
      allow_any_instance_of(CitiesController).to receive(:admin_user).and_return(admin_user)
    end

    it 'sends a list of cities' do
      get '/api/cities'

      expect(response).to be_success
    end
  end
end

But I want to avoid of using stubs in request specs. Does anybody have any ideas?

eugene_trebin
  • 1,485
  • 1
  • 16
  • 29
  • 1
    Stack Overflow is not Google. Please research how to add headers to an Rspec request, try some code, then come back with an appropriate question. – Wes Foster May 16 '16 at 16:21
  • 1
    Oh! thank you for very useful answer cap! But I know how to add headers to an Rspec request and i have tried some code, but it's not as easy as you think! – eugene_trebin May 16 '16 at 16:57
  • Show what you've tried and report back. Explain why you tried what you did, and also explain why it is not working as you would expect it to. – Wes Foster May 16 '16 at 16:59
  • I've added authentication in controller specs as follows: ApiAuth.sign!(request, user.id, user.api_secret_key) But it does not work in specs with type request, because in such specs object "request" is missing – eugene_trebin May 16 '16 at 17:07
  • Please edit your original question and add that information, as well as your relevant controller code. Might be able to get some traction going on this – Wes Foster May 16 '16 at 17:09
  • https://www.relishapp.com/rspec/rspec-rails/v/3-4/docs/request-specs/request-spec#requesting-a-json-response – zetetic May 16 '16 at 22:54

1 Answers1

-2

I created issue on github. You can watch my solution here

eugene_trebin
  • 1,485
  • 1
  • 16
  • 29