For the complete documentation index, see llms.txt. This page is also available as Markdown.

Testing

You can use Doorkeeper models in your application test suite. Note that starting from Doorkeeper 4.3.0 it uses ActiveSupport lazy loading hooks to load models. There are known issue with the factory_bot_rails gem (it executes factories building before ActiveRecord::Base is initialized using hooks in gem railtie, so you can catch a uninitialized constant error). It is recommended to use pure factory_bot gem to solve this problem.

Controller specs (RSpec)

In controller specs, the token is typically stubbed rather than persisted. Use instance_double to mock doorkeeper_token and doorkeeper_authorize! in a before_action.

RSpec.describe Api::V1::UsersController, type: :controller do
  let(:user) { create(:user) }

  before do
    token = instance_double('Doorkeeper::AccessToken',
                            acceptable?: true,
                            resource_owner_id: user.id,
                            scopes: [:write])
    allow(controller).to receive(:doorkeeper_token).and_return(token)
    # doorkeeper_authorize! is called as a before_action in the controller;
    # stubbing doorkeeper_token with acceptable?: true makes it pass.
  end

  it 'returns 200 with a valid token' do
    get :index
    expect(response).to have_http_status(:ok)
  end

  context 'with an invalid or expired token' do
    before do
      token = instance_double('Doorkeeper::AccessToken',
                              acceptable?: false,
                              accessible?: false,
                              revoked?: false,
                              expired?: true,
                              resource_owner_id: nil)
      allow(controller).to receive(:doorkeeper_token).and_return(token)
    end

    it 'returns 401' do
      get :index
      expect(response).to have_http_status(:unauthorized)
    end
  end
end

The key is acceptable?: true — doorkeeper_authorize! checks this method, so a false value triggers a 401 (or 403 for scope mismatches). When it fails, Doorkeeper also calls accessible? to decide between 401 and 403, and on the 401 path it then calls revoked? and expired? to pick the error reason, so a verifying double standing in for a rejected token must stub those too; otherwise RSpec raises an unexpected message error instead of returning the expected status. A double with acceptable?: true never reaches that code, so it needs none of them.

Request specs exercise the full middleware stack and are the preferred approach. Create a real Doorkeeper::AccessToken record (via FactoryBot or directly) and pass the Authorization: Bearer <token> header.

Factory definition:

Spec example:

Minitest

With Minitest, mock doorkeeper_token using Minitest::Mock and stub it on the controller with @controller.stub.

Helper reference

These are the key controller methods provided by Doorkeeper::Rails::Helpers (verified against Doorkeeper 5.9.5 lib/doorkeeper/rails/helpers.rb).

Helper
Purpose

doorkeeper_authorize!(*scopes)

before_action that requires a valid token. Accepts optional scope names, combined with a logical OR: doorkeeper_authorize! :read, :write accepts a token that has either scope. Call the helper once per scope to require several at the same time. Renders 401 (invalid/missing token) or 403 (none of the required scopes).

doorkeeper_token

Returns the current Doorkeeper::AccessToken instance (or nil). Memoized per-request via OAuth::Token.authenticate.

current_resource_owner

Available as a view helper since 5.9.1. Defined in Doorkeeper::Helpers::Controller and exposed via helper_method. Returns @current_resource_owner or evaluates Doorkeeper.config.authenticate_resource_owner.

Last updated