Sha256: 1eb4996bd16ea97a4a15c4c9a7e8dca9bd1a3bea8bf6a81d18691678a88d3e8f

Contents?: true

Size: 1.9 KB

Versions: 9

Compression:

Stored size: 1.9 KB

Contents

# frozen_string_literal: true

require_relative 'actions_matcher'

module Pundit
  module Matchers
    # This matcher tests whether a policy permits only the expected actions.
    class PermitOnlyActionsMatcher < ActionsMatcher
      # A description of the matcher.
      #
      # @return [String] A description of the matcher.
      def description
        "permit only #{expected_actions}"
      end

      # Checks if the given policy permits only the expected actions.
      #
      # @param policy [Object] The policy to test.
      # @return [Boolean] True if the policy permits only the expected actions, false otherwise.
      def matches?(policy)
        setup_policy_info! policy
        check_actions!

        @actual_actions = policy_info.permitted_actions - expected_actions
        @extra_actions = policy_info.forbidden_actions & expected_actions

        actual_actions.empty? && extra_actions.empty?
      end

      # Raises a NotImplementedError
      # @raise NotImplementedError
      # @return [void]
      def does_not_match?(_policy)
        raise NotImplementedError, format(AMBIGUOUS_NEGATED_MATCHER_ERROR, name: 'permit_only_actions')
      end

      # The failure message when the expected actions and the permitted actions do not match.
      #
      # @return [String] A failure message when the expected actions and the permitted actions do not match.
      def failure_message
        message = +"expected '#{policy_info}' to permit only #{expected_actions},"
        message << " but permitted #{actual_actions}" unless actual_actions.empty?
        message << extra_message unless extra_actions.empty?
        message << user_message
      end

      private

      attr_reader :actual_actions, :extra_actions

      def extra_message
        if actual_actions.empty?
          " but forbade #{extra_actions}"
        else
          " and forbade #{extra_actions}"
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
pundit-matchers-3.1.2 lib/pundit/matchers/permit_only_actions_matcher.rb
pundit-matchers-3.1.1 lib/pundit/matchers/permit_only_actions_matcher.rb
pundit-matchers-3.1.0 lib/pundit/matchers/permit_only_actions_matcher.rb
pundit-matchers-3.0.1 lib/pundit/matchers/permit_only_actions_matcher.rb
pundit-matchers-3.0.0 lib/pundit/matchers/permit_only_actions_matcher.rb
pundit-matchers-3.0.0.beta4 lib/pundit/matchers/permit_only_actions_matcher.rb
pundit-matchers-3.0.0.beta3 lib/pundit/matchers/permit_only_actions_matcher.rb
pundit-matchers-3.0.0.beta2 lib/pundit/matchers/permit_only_actions_matcher.rb
pundit-matchers-3.0.0.beta1 lib/pundit/matchers/permit_only_actions_matcher.rb