André Ricardo
André Ricardo

Reputation: 3172

MiniTest with Rails Doorkeeper

What is the equivalent of this Doorkeeper protected controllers in RSpec in Minitest?

let(:token) { double :accessible? => true }

before do
  controller.stub(:doorkeeper_token) { token }
end

I want to test my with Minitest, currently all responses are "Expected response to be a , but was <401>"

Upvotes: 3

Views: 1541

Answers (3)

Habax
Habax

Reputation: 1332

Based on @riffraff answer:

Add gem 'mocha' to your Gemfile and then bundle install (https://github.com/freerange/mocha)

Add some helpers for your tests:

# test/test_helper.rb
# ...
require "mocha/test_unit"

class ActiveSupport::TestCase
  fixtures :all

  # ...

  def sign_in user
    token = Doorkeeper::AccessToken.new(resource_owner_id: user.id)
    ApplicationController.any_instance.stubs(:doorkeeper_token).returns(token)
  end
end

Use your new helper in your tests:

test 'Whatever you want' do
  sign_in users(:one) # here is an example using fixtures
  # Do your get / post / etc
  # Do your assertions
end

Upvotes: 4

Hugues
Hugues

Reputation: 171

I got it to work and this is quite simple once you find the proper way.

I created a new class in test_helper.rb

class StubToken
  def acceptable?(value)
    true
  end
end

You can then use this class to bypass the token requirement by calling the following:

@controller.instance_variable_set('@_doorkeeper_token', StubToken.new)

Since the @_doorkeeper_token is define the following code in the Doorkeeper::helpers will resolve to you stub object.

File 'lib/doorkeeper/rails/helpers.rb', line 21

@_doorkeeper_token ||= OAuth::Token.authenticate request, *Doorkeeper.configuration.access_token_methods

And invoke you stub acceptable? methods.

Upvotes: 1

riffraff
riffraff

Reputation: 2447

Try

token = Doorkeeper::AccessToken.new(resource_owner_id: some_user.id)
YourController.any_instance.stubs(:doorkeeper_token).returns(token)

or if you use a mocking library just mock the token.

Upvotes: 0

Related Questions