lib/pundit/matchers.rb in pundit-matchers-1.5.1 vs lib/pundit/matchers.rb in pundit-matchers-1.6.0

- old
+ new

@@ -96,48 +96,70 @@ policy.public_send(Pundit::Matchers.configuration.user_alias) .inspect + '.' end end - RSpec::Matchers.define :forbid_mass_assignment_of do |attribute| + 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] + match do |policy| - if defined? @action - !policy.send("permitted_attributes_for_#{@action}").include? attribute - else - !policy.permitted_attributes.include? attribute + return false if attributes.count < 1 + + @allowed_attributes = attributes.select do |attribute| + if defined? @action + policy.send("permitted_attributes_for_#{@action}").include? attribute + else + policy.permitted_attributes.include? attribute + end end + + @allowed_attributes.empty? end + attr_reader :allowed_attributes + 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.' + failure_message do |policy| - if defined? @action - "#{policy.class} does not forbid the mass assignment of the " \ - "#{attribute} attribute, when authorising the #{@action} action, " \ - 'for ' + + 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 " + policy.public_send(Pundit::Matchers.configuration.user_alias) .inspect + '.' else - "#{policy.class} does not forbid the mass assignment of the " \ - "#{attribute} attribute for " + + "#{policy.class} expected to forbid the mass assignment of the " \ + "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 defined? @action - "#{policy.class} does not permit the mass assignment of the " \ - "#{attribute} attribute, when authorising the #{@action} action, " \ - 'for ' + + 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 " + policy.public_send(Pundit::Matchers.configuration.user_alias) .inspect + '.' else - "#{policy.class} does not permit the mass assignment of the " \ - "#{attribute} attribute for " + + "#{policy.class} expected to permit the mass assignment of the " \ + "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 @@ -235,47 +257,69 @@ policy.public_send(Pundit::Matchers.configuration.user_alias) .inspect + '.' end end - RSpec::Matchers.define :permit_mass_assignment_of do |attribute| + 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] + match do |policy| - if defined? @action - policy.send("permitted_attributes_for_#{@action}").include? attribute - else - policy.permitted_attributes.include? attribute + return false if attributes.count < 1 + + @forbidden_attributes = attributes.select do |attribute| + if defined? @action + !policy.send("permitted_attributes_for_#{@action}").include? attribute + else + !policy.permitted_attributes.include? attribute + end end + + @forbidden_attributes.empty? end + attr_reader :forbidden_attributes + 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.' + failure_message do |policy| - if defined? @action - "#{policy.class} does not permit the mass assignment of the " \ - "#{attribute} attribute, when authorising the #{@action} action, " \ - 'for ' + + 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 " + policy.public_send(Pundit::Matchers.configuration.user_alias) .inspect + '.' else - "#{policy.class} does not permit the mass assignment of the " \ - "#{attribute} attribute for " + + "#{policy.class} expected to permit the mass assignment of the " \ + "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 defined? @action - "#{policy.class} does not forbid the mass assignment of the " \ - "#{attribute} attribute, when authorising the #{@action} action, " \ - 'for ' + + 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 " + policy.public_send(Pundit::Matchers.configuration.user_alias) .inspect + '.' else - "#{policy.class} does not forbid the mass assignment of the " \ - "#{attribute} attribute for " + + "#{policy.class} expected to forbid the mass assignment of the " \ + "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