231 lines
8.4 KiB
Diff
231 lines
8.4 KiB
Diff
|
|
From c1a81d4b132a9e6091c42c3ebcd3144fc593c016 Mon Sep 17 00:00:00 2001
|
||
|
|
From: Elliot Winkler <elliot.winkler@gmail.com>
|
||
|
|
Date: Mon, 24 Jul 2017 21:36:47 -0500
|
||
|
|
Subject: [PATCH] Disable allow_mass_assignment_of tests under Rails 5
|
||
|
|
|
||
|
|
The `allow_mass_assignment_of` matcher tests `attr_accessible` and
|
||
|
|
`attr_protected` which was moved to the `protected_attributes` gem in
|
||
|
|
Rails 4. The gem does not work in Rails 5 and neither does the matcher,
|
||
|
|
so there is no need to test it. (We should probably remove the matcher
|
||
|
|
altogether eventually -- Strong Parameters have long replaced
|
||
|
|
`protected_attributes`.)
|
||
|
|
---
|
||
|
|
spec/support/unit/helpers/rails_versions.rb | 4 +
|
||
|
|
.../allow_mass_assignment_of_matcher_spec.rb | 158 +++++++++---------
|
||
|
|
2 files changed, 84 insertions(+), 78 deletions(-)
|
||
|
|
|
||
|
|
diff --git a/spec/support/unit/helpers/rails_versions.rb b/spec/support/unit/helpers/rails_versions.rb
|
||
|
|
index 5ac1562c..61b233a6 100644
|
||
|
|
--- a/spec/support/unit/helpers/rails_versions.rb
|
||
|
|
+++ b/spec/support/unit/helpers/rails_versions.rb
|
||
|
|
@@ -24,5 +24,9 @@ def rails_gte_4_1?
|
||
|
|
def rails_gte_4_2?
|
||
|
|
rails_version >= 4.2
|
||
|
|
end
|
||
|
|
+
|
||
|
|
+ def rails_lte_5?
|
||
|
|
+ rails_version < 5
|
||
|
|
+ end
|
||
|
|
end
|
||
|
|
end
|
||
|
|
diff --git a/spec/unit/shoulda/matchers/active_model/allow_mass_assignment_of_matcher_spec.rb b/spec/unit/shoulda/matchers/active_model/allow_mass_assignment_of_matcher_spec.rb
|
||
|
|
index 175aec92..f53c82e6 100644
|
||
|
|
--- a/spec/unit/shoulda/matchers/active_model/allow_mass_assignment_of_matcher_spec.rb
|
||
|
|
+++ b/spec/unit/shoulda/matchers/active_model/allow_mass_assignment_of_matcher_spec.rb
|
||
|
|
@@ -1,115 +1,117 @@
|
||
|
|
require 'unit_spec_helper'
|
||
|
|
|
||
|
|
describe Shoulda::Matchers::ActiveModel::AllowMassAssignmentOfMatcher, type: :model do
|
||
|
|
- context '#description' do
|
||
|
|
- context 'without a role' do
|
||
|
|
- it 'includes the attribute name' do
|
||
|
|
- expect(described_class.new(:attr).description).
|
||
|
|
- to eq 'allow mass assignment of attr'
|
||
|
|
+ if rails_lte_5?
|
||
|
|
+ context '#description' do
|
||
|
|
+ context 'without a role' do
|
||
|
|
+ it 'includes the attribute name' do
|
||
|
|
+ expect(described_class.new(:attr).description).
|
||
|
|
+ to eq 'allow mass assignment of attr'
|
||
|
|
+ end
|
||
|
|
end
|
||
|
|
- end
|
||
|
|
|
||
|
|
- if active_model_3_1?
|
||
|
|
- context 'with a role' do
|
||
|
|
- it 'includes the attribute name and the role' do
|
||
|
|
- expect(described_class.new(:attr).as(:admin).description).
|
||
|
|
- to eq 'allow mass assignment of attr as admin'
|
||
|
|
+ if active_model_3_1?
|
||
|
|
+ context 'with a role' do
|
||
|
|
+ it 'includes the attribute name and the role' do
|
||
|
|
+ expect(described_class.new(:attr).as(:admin).description).
|
||
|
|
+ to eq 'allow mass assignment of attr as admin'
|
||
|
|
+ end
|
||
|
|
end
|
||
|
|
end
|
||
|
|
end
|
||
|
|
- end
|
||
|
|
|
||
|
|
- context 'an attribute that is blacklisted from mass-assignment' do
|
||
|
|
- it 'rejects being mass-assignable' do
|
||
|
|
- model = define_model(:example, blacklisted: :string) do
|
||
|
|
- attr_protected :blacklisted
|
||
|
|
- end.new
|
||
|
|
+ context 'an attribute that is blacklisted from mass-assignment' do
|
||
|
|
+ it 'rejects being mass-assignable' do
|
||
|
|
+ model = define_model(:example, blacklisted: :string) do
|
||
|
|
+ attr_protected :blacklisted
|
||
|
|
+ end.new
|
||
|
|
|
||
|
|
- expect(model).not_to allow_mass_assignment_of(:blacklisted)
|
||
|
|
+ expect(model).not_to allow_mass_assignment_of(:blacklisted)
|
||
|
|
+ end
|
||
|
|
end
|
||
|
|
- end
|
||
|
|
|
||
|
|
- context 'an attribute that is not whitelisted for mass-assignment' do
|
||
|
|
- it 'rejects being mass-assignable' do
|
||
|
|
- model = define_model(:example, not_whitelisted: :string,
|
||
|
|
- whitelisted: :string) do
|
||
|
|
- attr_accessible :whitelisted
|
||
|
|
- end.new
|
||
|
|
+ context 'an attribute that is not whitelisted for mass-assignment' do
|
||
|
|
+ it 'rejects being mass-assignable' do
|
||
|
|
+ model = define_model(:example, not_whitelisted: :string,
|
||
|
|
+ whitelisted: :string) do
|
||
|
|
+ attr_accessible :whitelisted
|
||
|
|
+ end.new
|
||
|
|
|
||
|
|
- expect(model).not_to allow_mass_assignment_of(:not_whitelisted)
|
||
|
|
+ expect(model).not_to allow_mass_assignment_of(:not_whitelisted)
|
||
|
|
+ end
|
||
|
|
end
|
||
|
|
- end
|
||
|
|
|
||
|
|
- context 'an attribute that is whitelisted for mass-assignment' do
|
||
|
|
- it 'accepts being mass-assignable' do
|
||
|
|
- expect(define_model(:example, whitelisted: :string) do
|
||
|
|
- attr_accessible :whitelisted
|
||
|
|
- end.new).to allow_mass_assignment_of(:whitelisted)
|
||
|
|
+ context 'an attribute that is whitelisted for mass-assignment' do
|
||
|
|
+ it 'accepts being mass-assignable' do
|
||
|
|
+ expect(define_model(:example, whitelisted: :string) do
|
||
|
|
+ attr_accessible :whitelisted
|
||
|
|
+ end.new).to allow_mass_assignment_of(:whitelisted)
|
||
|
|
+ end
|
||
|
|
end
|
||
|
|
- end
|
||
|
|
|
||
|
|
- context 'an attribute not included in the mass-assignment blacklist' do
|
||
|
|
- it 'accepts being mass-assignable' do
|
||
|
|
- model = define_model(:example, not_blacklisted: :string,
|
||
|
|
- blacklisted: :string) do
|
||
|
|
- attr_protected :blacklisted
|
||
|
|
- end.new
|
||
|
|
+ context 'an attribute not included in the mass-assignment blacklist' do
|
||
|
|
+ it 'accepts being mass-assignable' do
|
||
|
|
+ model = define_model(:example, not_blacklisted: :string,
|
||
|
|
+ blacklisted: :string) do
|
||
|
|
+ attr_protected :blacklisted
|
||
|
|
+ end.new
|
||
|
|
|
||
|
|
- expect(model).to allow_mass_assignment_of(:not_blacklisted)
|
||
|
|
+ expect(model).to allow_mass_assignment_of(:not_blacklisted)
|
||
|
|
+ end
|
||
|
|
end
|
||
|
|
- end
|
||
|
|
|
||
|
|
- unless active_model_3_2? || active_model_4_0?
|
||
|
|
- context 'an attribute on a class with no protected attributes' do
|
||
|
|
- it 'accepts being mass-assignable' do
|
||
|
|
- expect(no_protected_attributes).to allow_mass_assignment_of(:attr)
|
||
|
|
- end
|
||
|
|
+ unless active_model_3_2? || active_model_4_0?
|
||
|
|
+ context 'an attribute on a class with no protected attributes' do
|
||
|
|
+ it 'accepts being mass-assignable' do
|
||
|
|
+ expect(no_protected_attributes).to allow_mass_assignment_of(:attr)
|
||
|
|
+ end
|
||
|
|
|
||
|
|
- it 'assigns a negative failure message' do
|
||
|
|
- matcher = allow_mass_assignment_of(:attr)
|
||
|
|
+ it 'assigns a negative failure message' do
|
||
|
|
+ matcher = allow_mass_assignment_of(:attr)
|
||
|
|
|
||
|
|
- expect(matcher.matches?(no_protected_attributes)).to eq true
|
||
|
|
+ expect(matcher.matches?(no_protected_attributes)).to eq true
|
||
|
|
|
||
|
|
- expect(matcher.failure_message_when_negated).not_to be_nil
|
||
|
|
+ expect(matcher.failure_message_when_negated).not_to be_nil
|
||
|
|
+ end
|
||
|
|
end
|
||
|
|
- end
|
||
|
|
-
|
||
|
|
- def no_protected_attributes
|
||
|
|
- define_model(:example, attr: :string).new
|
||
|
|
- end
|
||
|
|
- end
|
||
|
|
|
||
|
|
- context 'an attribute on a class with all protected attributes' do
|
||
|
|
- it 'rejects being mass-assignable' do
|
||
|
|
- expect(all_protected_attributes).not_to allow_mass_assignment_of(:attr)
|
||
|
|
- end
|
||
|
|
-
|
||
|
|
- def all_protected_attributes
|
||
|
|
- define_model(:example, attr: :string) do
|
||
|
|
- attr_accessible nil
|
||
|
|
- end.new
|
||
|
|
+ def no_protected_attributes
|
||
|
|
+ define_model(:example, attr: :string).new
|
||
|
|
+ end
|
||
|
|
end
|
||
|
|
- end
|
||
|
|
|
||
|
|
- if active_model_3_1?
|
||
|
|
- context 'an attribute included in the mass-assignment whitelist for admin role only' do
|
||
|
|
+ context 'an attribute on a class with all protected attributes' do
|
||
|
|
it 'rejects being mass-assignable' do
|
||
|
|
- expect(mass_assignable_as_admin).not_to allow_mass_assignment_of(:attr)
|
||
|
|
- end
|
||
|
|
-
|
||
|
|
- it 'accepts being mass-assignable for admin' do
|
||
|
|
- expect(mass_assignable_as_admin).to allow_mass_assignment_of(:attr).as(:admin)
|
||
|
|
+ expect(all_protected_attributes).not_to allow_mass_assignment_of(:attr)
|
||
|
|
end
|
||
|
|
|
||
|
|
- def mass_assignable_as_admin
|
||
|
|
+ def all_protected_attributes
|
||
|
|
define_model(:example, attr: :string) do
|
||
|
|
- attr_accessible :attr, as: :admin
|
||
|
|
+ attr_accessible nil
|
||
|
|
end.new
|
||
|
|
end
|
||
|
|
end
|
||
|
|
- end
|
||
|
|
|
||
|
|
- def define_model(name, columns, &block)
|
||
|
|
- super(name, columns, whitelist_attributes: false, &block)
|
||
|
|
+ if active_model_3_1?
|
||
|
|
+ context 'an attribute included in the mass-assignment whitelist for admin role only' do
|
||
|
|
+ it 'rejects being mass-assignable' do
|
||
|
|
+ expect(mass_assignable_as_admin).not_to allow_mass_assignment_of(:attr)
|
||
|
|
+ end
|
||
|
|
+
|
||
|
|
+ it 'accepts being mass-assignable for admin' do
|
||
|
|
+ expect(mass_assignable_as_admin).to allow_mass_assignment_of(:attr).as(:admin)
|
||
|
|
+ end
|
||
|
|
+
|
||
|
|
+ def mass_assignable_as_admin
|
||
|
|
+ define_model(:example, attr: :string) do
|
||
|
|
+ attr_accessible :attr, as: :admin
|
||
|
|
+ end.new
|
||
|
|
+ end
|
||
|
|
+ end
|
||
|
|
+ end
|
||
|
|
+
|
||
|
|
+ def define_model(name, columns, &block)
|
||
|
|
+ super(name, columns, whitelist_attributes: false, &block)
|
||
|
|
+ end
|
||
|
|
end
|
||
|
|
end
|