aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/forbidden_attributes_protection_test.rb
diff options
context:
space:
mode:
authorGuillermo Iguaran <guilleiguaran@gmail.com>2012-07-18 01:32:49 -0500
committerGuillermo Iguaran <guilleiguaran@gmail.com>2012-09-16 23:58:19 -0500
commit0168c7a394e1f9a0462a8fc442e3cc3b205bd6ae (patch)
tree7eb59b2c8f0c03b71e127c8c661aeb3e94f49c10 /activerecord/test/cases/forbidden_attributes_protection_test.rb
parent8020f71df120f80fd7db9ab568c8d0d6d1ad4e28 (diff)
downloadrails-0168c7a394e1f9a0462a8fc442e3cc3b205bd6ae.tar.gz
rails-0168c7a394e1f9a0462a8fc442e3cc3b205bd6ae.tar.bz2
rails-0168c7a394e1f9a0462a8fc442e3cc3b205bd6ae.zip
Add tests for ForbiddenAttributesProtection in ActiveRecord
Diffstat (limited to 'activerecord/test/cases/forbidden_attributes_protection_test.rb')
-rw-r--r--activerecord/test/cases/forbidden_attributes_protection_test.rb63
1 files changed, 63 insertions, 0 deletions
diff --git a/activerecord/test/cases/forbidden_attributes_protection_test.rb b/activerecord/test/cases/forbidden_attributes_protection_test.rb
new file mode 100644
index 0000000000..cf462297ef
--- /dev/null
+++ b/activerecord/test/cases/forbidden_attributes_protection_test.rb
@@ -0,0 +1,63 @@
+require 'cases/helper'
+require 'active_support/core_ext/hash/indifferent_access'
+require 'models/person'
+
+class ProtectedParams < ActiveSupport::HashWithIndifferentAccess
+ attr_accessor :permitted
+ alias :permitted? :permitted
+
+ def initialize(attributes)
+ super(attributes)
+ @permitted = false
+ end
+
+ def permit!
+ @permitted = true
+ self
+ end
+
+ def dup
+ super.tap do |duplicate|
+ duplicate.instance_variable_set :@permitted, @permitted
+ end
+ end
+end
+
+class ForbiddenAttributesProtectionTest < ActiveRecord::TestCase
+ def test_forbidden_attributes_cannot_be_used_for_mass_assignment
+ params = ProtectedParams.new(first_name: 'Guille', gender: 'm')
+ assert_raises(ActiveModel::ForbiddenAttributes) do
+ Person.new(params)
+ end
+ end
+
+ def test_permitted_attributes_can_be_used_for_mass_assignment
+ params = ProtectedParams.new(first_name: 'Guille', gender: 'm')
+ params.permit!
+ assert_nothing_raised do
+ person = Person.new(params)
+
+ assert_equal 'Guille', person.first_name
+ assert_equal 'm', person.gender
+ end
+ end
+
+ def test_regular_hash_should_still_be_used_for_mass_assignment
+ assert_nothing_raised do
+ person = Person.new(first_name: 'Guille', gender: 'm')
+
+ assert_equal 'Guille', person.first_name
+ assert_equal 'm', person.gender
+ end
+ end
+
+ def test_protected_attributes_cannot_be_used_for_mass_assignment
+ params = ProtectedParams.new(id: 1, first_name: 'Guille', gender: 'm')
+ params.permit!
+ person = Person.new(params)
+
+ assert_equal 'Guille', person.first_name
+ assert_equal 'm', person.gender
+ assert_not_equal 1, person.id
+ end
+end