mardi 7 juillet 2015

How can I check or modify the actual request being sent with :get in a RSpec 3 controller test?

I am trying to test an OAuth2 flow. I have the following rspec 3 test. I want to be able to test my controller routes with get :method_name, parameters instead of get "/api/v0/user/.../".

These are my relevant routes. Basically it just passes "/api/v0/user" to "Api::V0::UsersController#show".

# config/routes.rb

api_version(module: "Api::V0", path: {value: "api/v0"}, default: true) do
  match 'user', to: 'users#show', via: :get # 
end

This works:

# spec/controllers/api/v0/users_controller_spec.rb

describe Api::V0::UsersController, type: :request do # Note I have to force it to be type: request, which I don't want to do
  describe "#show" do
    let!(:application) { bob.o_auth_applications.create!(client_id: 1, client_secret: "secret") }
    let!(:token) { application.tokens.create!.bearer_token.to_s }

    context "when valid" do
      it "shows the user's username and email" do
        get "/api/v0/user/?access_token=" + token # This works
        jsonBody = JSON.parse(response.body)
        expect(jsonBody["username"]).to eq(bob.username)
        expect(jsonBody["email"]).to eq(bob.email)
      end
    end

This does not work:

# spec/controllers/api/v0/users_controller_spec.rb

describe Api::V0::UsersController, type: :controller do
  describe "#show" do
    let!(:application) { bob.o_auth_applications.create!(client_id: 1, client_secret: "secret") }
    let!(:token) { application.tokens.create!.bearer_token.to_s }

    context "when valid" do
      it "shows the user's username and email" do
        get :show, {"access_token": "#{token}"} # Both
        get :show, {"access_token": token} # throw a Rack::OAuth2::Server::Resource::Bearer::Unauthorized error
        jsonBody = JSON.parse(response.body)
        expect(jsonBody["username"]).to eq(bob.username)
        expect(jsonBody["email"]).to eq(bob.email)
      end
    end

How can I force the get :show, parameters method to create a request that looks like "/api/v0/user/?access_token=429ad8240...32"? Or at least how can I check the actual request that is being sent? It seems like the parameters aren't being passed as a URL query.

Aucun commentaire:

Enregistrer un commentaire