Sha256: 2714eedecbd32adbf12f2c4ea50ad058f682a4ee135128c8ab818c56dbe14068

Contents?: true

Size: 946 Bytes

Versions: 3

Compression:

Stored size: 946 Bytes

Contents

module Clearance
  # Middleware which allows signing in by passing as=USER_ID in a query
  # parameter.
  #
  # Designed to eliminate time in integration tests wasted by visiting and
  # submitting the sign in form.
  #
  # Configuration:
  #
  #   # config/environments/test.rb
  #   MyRailsApp::Application.configure do
  #     # ...
  #     config.middleware.use Clearance::BackDoor
  #     # ...
  #   end
  #
  # Usage:
  #
  #   visit new_feedback_path(as: user)
  class BackDoor
    def initialize(app)
      @app = app
    end

    def call(env)
      sign_in_through_the_back_door(env)
      @app.call(env)
    end

    private

    # @api private
    def sign_in_through_the_back_door(env)
      params = Rack::Utils.parse_query(env['QUERY_STRING'])
      user_id = params['as']

      if user_id.present?
        user = Clearance.configuration.user_model.find(user_id)
        env[:clearance].sign_in(user)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
clearance-1.13.0 lib/clearance/back_door.rb
clearance-1.12.1 lib/clearance/back_door.rb
clearance-1.12.0 lib/clearance/back_door.rb