Sha256: fd83b687f071bf591d9aef4e9532c3fd94b9fa69caf69072f90ac38a0609bd0e

Contents?: true

Size: 925 Bytes

Versions: 1

Compression:

Stored size: 925 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 ClearanceBackDoor
  #     # ...
  #   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

    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

1 entries across 1 versions & 1 rubygems

Version Path
clearance-1.0.0.rc6 lib/clearance/back_door.rb