Sha256: 75db85eaaac39899ddeb7d5c5df471c6cf61ab3d4a93687dcb284e477c8c4588

Contents?: true

Size: 1.16 KB

Versions: 7

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

require 'rspec/expectations'

RSpec::Matchers.define :be_authorized do |controller, action, params: {}, session: {}|
  match do |user|
    parameters = params.merge(controller: controller, action: action)

    access?(user, parameters, session)
  end

  match_when_negated do |user|
    parameters = params.merge(controller: controller, action: action)

    !access?(user, parameters, session)
  end

  failure_message do |user|
    maybe_params_or_session("expected #{user.class}##{user.id} to be authorized in #{data}", params, session)
  end

  failure_message_when_negated do |user|
    maybe_params_or_session("expected #{user.class}##{user.id} not to be authorized in #{data}", params, session)
  end

  private

  def access?(user, params, session)
    cop = Authorizy.config.cop.new(user, params, session)

    Authorizy::Core.new(user, params, session, cop: cop).access?
  end

  def maybe_params_or_session(message, params, session)
    message += ", params: #{params}" if params.present?
    message += ", session: #{session}" if session.present?

    message
  end

  def data
    %(controller: "#{expected[0]}", action: "#{expected[1]}")
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
authorizy-0.5.0 lib/authorizy/rspec.rb
authorizy-0.4.1 lib/authorizy/rspec.rb
authorizy-0.4.0 lib/authorizy/rspec.rb
authorizy-0.3.0 lib/authorizy/rspec.rb
authorizy-0.2.2 lib/authorizy/rspec.rb
authorizy-0.2.1 lib/authorizy/rspec.rb
authorizy-0.2.0 lib/authorizy/rspec.rb