lib/pundit/matchers.rb in pundit-matchers-2.0.0 vs lib/pundit/matchers.rb in pundit-matchers-2.1.0

- old
+ new

@@ -6,10 +6,15 @@ require_relative 'matchers/utils/all_actions/forbidden_actions_error_formatter' require_relative 'matchers/utils/all_actions/forbidden_actions_matcher' require_relative 'matchers/utils/all_actions/permitted_actions_error_formatter' require_relative 'matchers/utils/all_actions/permitted_actions_matcher' + require_relative 'matchers/utils/only_actions/forbidden_actions_error_formatter' + require_relative 'matchers/utils/only_actions/forbidden_actions_matcher' + require_relative 'matchers/utils/only_actions/permitted_actions_error_formatter' + require_relative 'matchers/utils/only_actions/permitted_actions_matcher' + class Configuration attr_accessor :user_alias def initialize @user_alias = :user @@ -51,38 +56,39 @@ RSpec::Matchers.define :forbid_actions do |*actions| actions.flatten! match do |policy| return false if actions.count < 1 + @allowed_actions = actions.select do |action| policy.public_send("#{action}?") end @allowed_actions.empty? end attr_reader :allowed_actions zero_actions_failure_message = 'At least one action must be ' \ - 'specified when using the forbid_actions matcher.' + 'specified when using the forbid_actions matcher.' failure_message do |policy| if actions.count.zero? zero_actions_failure_message else "#{policy.class} expected to forbid #{actions}, but allowed " \ - "#{allowed_actions} for " + + "#{allowed_actions} for " + policy.public_send(Pundit::Matchers.configuration.user_alias) .inspect + '.' end end failure_message_when_negated do |policy| if actions.count.zero? zero_actions_failure_message else "#{policy.class} expected to permit #{actions}, but forbade " \ - "#{allowed_actions} for " + + "#{allowed_actions} for " + policy.public_send(Pundit::Matchers.configuration.user_alias) .inspect + '.' end end end @@ -105,11 +111,11 @@ end end RSpec::Matchers.define :forbid_mass_assignment_of do |attributes| # Map single object argument to an array, if necessary - attributes = attributes.is_a?(Array) ? attributes : [attributes] + attributes = [attributes] unless attributes.is_a?(Array) match do |policy| return false if attributes.count < 1 @allowed_attributes = attributes.select do |attribute| @@ -128,45 +134,45 @@ chain :for_action do |action| @action = action end zero_attributes_failure_message = 'At least one attribute must be ' \ - 'specified when using the forbid_mass_assignment_of matcher.' + 'specified when using the forbid_mass_assignment_of matcher.' failure_message do |policy| if attributes.count.zero? zero_attributes_failure_message elsif defined? @action "#{policy.class} expected to forbid the mass assignment of the " \ - "attributes #{attributes} when authorising the #{@action} action, " \ - 'but allowed the mass assignment of the attributes ' \ - "#{allowed_attributes} for " + + "attributes #{attributes} when authorising the #{@action} action, " \ + 'but allowed the mass assignment of the attributes ' \ + "#{allowed_attributes} for " + policy.public_send(Pundit::Matchers.configuration.user_alias) .inspect + '.' else "#{policy.class} expected to forbid the mass assignment of the " \ - "attributes #{attributes}, but allowed the mass assignment of " \ - "the attributes #{allowed_attributes} for " + + "attributes #{attributes}, but allowed the mass assignment of " \ + "the attributes #{allowed_attributes} for " + policy.public_send(Pundit::Matchers.configuration.user_alias) .inspect + '.' end end failure_message_when_negated do |policy| if attributes.count.zero? zero_attributes_failure_message elsif defined? @action "#{policy.class} expected to permit the mass assignment of the " \ - "attributes #{attributes} when authorising the #{@action} action, " \ - 'but permitted the mass assignment of the attributes ' \ - "#{allowed_attributes} for " + + "attributes #{attributes} when authorising the #{@action} action, " \ + 'but permitted the mass assignment of the attributes ' \ + "#{allowed_attributes} for " + policy.public_send(Pundit::Matchers.configuration.user_alias) .inspect + '.' else "#{policy.class} expected to permit the mass assignment of the " \ - "attributes #{attributes}, but permitted the mass assignment of " \ - "the attributes #{allowed_attributes} for " + + "attributes #{attributes}, but permitted the mass assignment of " \ + "the attributes #{allowed_attributes} for " + policy.public_send(Pundit::Matchers.configuration.user_alias) .inspect + '.' end end end @@ -213,10 +219,11 @@ RSpec::Matchers.define :permit_actions do |*actions| actions.flatten! match do |policy| return false if actions.count < 1 + @forbidden_actions = actions.reject do |action| policy.public_send("#{action}?") end @forbidden_actions.empty? end @@ -233,38 +240,39 @@ In this case, edit would be true and destroy would be false, but both \ tests would pass.' return true if actions.count < 1 + @forbidden_actions = actions.reject do |action| policy.public_send("#{action}?") end !@forbidden_actions.empty? end attr_reader :forbidden_actions zero_actions_failure_message = 'At least one action must be specified ' \ - 'when using the permit_actions matcher.' + 'when using the permit_actions matcher.' failure_message do |policy| if actions.count.zero? zero_actions_failure_message else "#{policy.class} expected to permit #{actions}, but forbade " \ - "#{forbidden_actions} for " + + "#{forbidden_actions} for " + policy.public_send(Pundit::Matchers.configuration.user_alias) .inspect + '.' end end failure_message_when_negated do |policy| if actions.count.zero? zero_actions_failure_message else "#{policy.class} expected to forbid #{actions}, but allowed " \ - "#{forbidden_actions} for " + + "#{forbidden_actions} for " + policy.public_send(Pundit::Matchers.configuration.user_alias) .inspect + '.' end end end @@ -287,11 +295,11 @@ end end RSpec::Matchers.define :permit_mass_assignment_of do |attributes| # Map single object argument to an array, if necessary - attributes = attributes.is_a?(Array) ? attributes : [attributes] + attributes = [attributes] unless attributes.is_a?(Array) match do |policy| return false if attributes.count < 1 @forbidden_attributes = attributes.select do |attribute| @@ -310,45 +318,45 @@ chain :for_action do |action| @action = action end zero_attributes_failure_message = 'At least one attribute must be ' \ - 'specified when using the permit_mass_assignment_of matcher.' + 'specified when using the permit_mass_assignment_of matcher.' failure_message do |policy| if attributes.count.zero? zero_attributes_failure_message elsif defined? @action "#{policy.class} expected to permit the mass assignment of the " \ - "attributes #{attributes} when authorising the #{@action} action, " \ - 'but forbade the mass assignment of the attributes ' \ - "#{forbidden_attributes} for " + + "attributes #{attributes} when authorising the #{@action} action, " \ + 'but forbade the mass assignment of the attributes ' \ + "#{forbidden_attributes} for " + policy.public_send(Pundit::Matchers.configuration.user_alias) .inspect + '.' else "#{policy.class} expected to permit the mass assignment of the " \ - "attributes #{attributes}, but forbade the mass assignment of the " \ - "attributes #{forbidden_attributes} for " + + "attributes #{attributes}, but forbade the mass assignment of the " \ + "attributes #{forbidden_attributes} for " + policy.public_send(Pundit::Matchers.configuration.user_alias) .inspect + '.' end end failure_message_when_negated do |policy| if attributes.count.zero? zero_attributes_failure_message elsif defined? @action "#{policy.class} expected to forbid the mass assignment of the " \ - "attributes #{attributes} when authorising the #{@action} action, " \ - 'but forbade the mass assignment of the attributes ' \ - "#{forbidden_attributes} for " + + "attributes #{attributes} when authorising the #{@action} action, " \ + 'but forbade the mass assignment of the attributes ' \ + "#{forbidden_attributes} for " + policy.public_send(Pundit::Matchers.configuration.user_alias) .inspect + '.' else "#{policy.class} expected to forbid the mass assignment of the " \ - "attributes #{attributes}, but forbade the mass assignment of the " \ - "attributes #{forbidden_attributes} for " + + "attributes #{attributes}, but forbade the mass assignment of the " \ + "attributes #{forbidden_attributes} for " + policy.public_send(Pundit::Matchers.configuration.user_alias) .inspect + '.' end end end @@ -381,17 +389,41 @@ formatter = Pundit::Matchers::Utils::AllActions::PermittedActionsErrorFormatter.new(@matcher) formatter.message end end + RSpec::Matchers.define :permit_only_actions do |actions| + match do |policy| + @matcher = Pundit::Matchers::Utils::OnlyActions::PermittedActionsMatcher.new(policy, actions) + @matcher.match? + end + + failure_message do + formatter = Pundit::Matchers::Utils::OnlyActions::PermittedActionsErrorFormatter.new(@matcher) + formatter.message + end + end + RSpec::Matchers.define :forbid_all_actions do match do |policy| @matcher = Pundit::Matchers::Utils::AllActions::ForbiddenActionsMatcher.new(policy) @matcher.match? end failure_message do formatter = Pundit::Matchers::Utils::AllActions::ForbiddenActionsErrorFormatter.new(@matcher) + formatter.message + end + end + + RSpec::Matchers.define :forbid_only_actions do |actions| + match do |policy| + @matcher = Pundit::Matchers::Utils::OnlyActions::ForbiddenActionsMatcher.new(policy, actions) + @matcher.match? + end + + failure_message do + formatter = Pundit::Matchers::Utils::OnlyActions::ForbiddenActionsErrorFormatter.new(@matcher) formatter.message end end end