Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mass_assignment_security moved from AR to AMo, and minor test cleanup
Signed-off-by: José Valim <jose.valim@gmail.com>
- Loading branch information
Showing
15 changed files
with
143 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...ass_assignment_security/permission_set.rb → ...ass_assignment_security/permission_set.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...ss_assignment_security/black_list_test.rb → ...ss_assignment_security/black_list_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...ssignment_security/permission_set_test.rb → ...ssignment_security/permission_set_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
...ass_assignment_security/sanitizer_test.rb → ...ass_assignment_security/sanitizer_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...ss_assignment_security/white_list_test.rb → ...ss_assignment_security/white_list_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
require "cases/helper" | ||
require 'models/mass_assignment_specific' | ||
|
||
class MassAssignmentSecurityTest < ActiveModel::TestCase | ||
|
||
def test_attribute_protection | ||
user = User.new | ||
expected = { "name" => "John Smith", "email" => "john@smith.com" } | ||
sanitized = user.sanitize_for_mass_assignment(expected.merge("admin" => true)) | ||
assert_equal expected, sanitized | ||
end | ||
|
||
def test_attributes_accessible | ||
user = Person.new | ||
expected = { "name" => "John Smith", "email" => "john@smith.com" } | ||
sanitized = user.sanitize_for_mass_assignment(expected.merge("super_powers" => true)) | ||
assert_equal expected, sanitized | ||
end | ||
|
||
def test_attributes_protected_by_default | ||
firm = Firm.new | ||
expected = { } | ||
sanitized = firm.sanitize_for_mass_assignment({ "type" => "Client" }) | ||
assert_equal expected, sanitized | ||
end | ||
|
||
def test_mass_assignment_protection_inheritance | ||
assert LoosePerson.accessible_attributes.blank? | ||
assert_equal Set.new([ 'credit_rating', 'administrator']), LoosePerson.protected_attributes | ||
|
||
assert LooseDescendant.accessible_attributes.blank? | ||
assert_equal Set.new([ 'credit_rating', 'administrator', 'phone_number']), LooseDescendant.protected_attributes | ||
|
||
assert LooseDescendantSecond.accessible_attributes.blank? | ||
assert_equal Set.new([ 'credit_rating', 'administrator', 'phone_number', 'name']), LooseDescendantSecond.protected_attributes, | ||
'Running attr_protected twice in one class should merge the protections' | ||
|
||
assert (TightPerson.protected_attributes - TightPerson.attributes_protected_by_default).blank? | ||
assert_equal Set.new([ 'name', 'address' ]), TightPerson.accessible_attributes | ||
|
||
assert (TightDescendant.protected_attributes - TightDescendant.attributes_protected_by_default).blank? | ||
assert_equal Set.new([ 'name', 'address', 'phone_number' ]), TightDescendant.accessible_attributes | ||
end | ||
|
||
def test_mass_assignment_multiparameter_protector | ||
task = Task.new | ||
attributes = { "starting(1i)" => "2004", "starting(2i)" => "6", "starting(3i)" => "24" } | ||
sanitized = task.sanitize_for_mass_assignment(attributes) | ||
assert_equal sanitized, { } | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
class User | ||
include ActiveModel::MassAssignmentSecurity | ||
attr_protected :admin | ||
|
||
public :sanitize_for_mass_assignment | ||
end | ||
|
||
class Person | ||
include ActiveModel::MassAssignmentSecurity | ||
attr_accessible :name, :email | ||
|
||
public :sanitize_for_mass_assignment | ||
end | ||
|
||
class Firm | ||
include ActiveModel::MassAssignmentSecurity | ||
|
||
public :sanitize_for_mass_assignment | ||
|
||
def self.attributes_protected_by_default | ||
["type"] | ||
end | ||
end | ||
|
||
class Task | ||
include ActiveModel::MassAssignmentSecurity | ||
attr_protected :starting | ||
|
||
public :sanitize_for_mass_assignment | ||
end | ||
|
||
class LoosePerson | ||
include ActiveModel::MassAssignmentSecurity | ||
attr_protected :credit_rating, :administrator | ||
end | ||
|
||
class LooseDescendant < LoosePerson | ||
attr_protected :phone_number | ||
end | ||
|
||
class LooseDescendantSecond< LoosePerson | ||
attr_protected :phone_number | ||
attr_protected :name | ||
end | ||
|
||
class TightPerson | ||
include ActiveModel::MassAssignmentSecurity | ||
attr_accessible :name, :address | ||
|
||
def self.attributes_protected_by_default | ||
["mobile_number"] | ||
end | ||
end | ||
|
||
class TightDescendant < TightPerson | ||
attr_accessible :phone_number | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters