@@ -552,3 +552,248 @@ def test_has_many_create_with_bang_without_protection
552552 end
553553
554554end
555+
556+
557+ class MassAssignmentSecurityNestedAttributesTest < ActiveRecord ::TestCase
558+ include MassAssignmentTestHelpers
559+
560+ def nested_attributes_hash ( association , collection = false , except = [ :id ] )
561+ if collection
562+ { :first_name => 'David' } . merge ( :"#{ association } _attributes" => [ attributes_hash . except ( *except ) ] )
563+ else
564+ { :first_name => 'David' } . merge ( :"#{ association } _attributes" => attributes_hash . except ( *except ) )
565+ end
566+ end
567+
568+ # build
569+
570+ def test_has_one_new_with_attr_protected_attributes
571+ person = LoosePerson . new ( nested_attributes_hash ( :best_friend ) )
572+ assert_default_attributes ( person . best_friend )
573+ end
574+
575+ def test_has_one_new_with_attr_accessible_attributes
576+ person = TightPerson . new ( nested_attributes_hash ( :best_friend ) )
577+ assert_default_attributes ( person . best_friend )
578+ end
579+
580+ def test_has_one_new_with_admin_role_with_attr_protected_attributes
581+ person = LoosePerson . new ( nested_attributes_hash ( :best_friend ) , :as => :admin )
582+ assert_admin_attributes ( person . best_friend )
583+ end
584+
585+ def test_has_one_new_with_admin_role_with_attr_accessible_attributes
586+ person = TightPerson . new ( nested_attributes_hash ( :best_friend ) , :as => :admin )
587+ assert_admin_attributes ( person . best_friend )
588+ end
589+
590+ def test_has_one_new_without_protection
591+ person = LoosePerson . new ( nested_attributes_hash ( :best_friend , false , nil ) , :without_protection => true )
592+ assert_all_attributes ( person . best_friend )
593+ end
594+
595+ def test_belongs_to_new_with_attr_protected_attributes
596+ person = LoosePerson . new ( nested_attributes_hash ( :best_friend_of ) )
597+ assert_default_attributes ( person . best_friend_of )
598+ end
599+
600+ def test_belongs_to_new_with_attr_accessible_attributes
601+ person = TightPerson . new ( nested_attributes_hash ( :best_friend_of ) )
602+ assert_default_attributes ( person . best_friend_of )
603+ end
604+
605+ def test_belongs_to_new_with_admin_role_with_attr_protected_attributes
606+ person = LoosePerson . new ( nested_attributes_hash ( :best_friend_of ) , :as => :admin )
607+ assert_admin_attributes ( person . best_friend_of )
608+ end
609+
610+ def test_belongs_to_new_with_admin_role_with_attr_accessible_attributes
611+ person = TightPerson . new ( nested_attributes_hash ( :best_friend_of ) , :as => :admin )
612+ assert_admin_attributes ( person . best_friend_of )
613+ end
614+
615+ def test_belongs_to_new_without_protection
616+ person = LoosePerson . new ( nested_attributes_hash ( :best_friend_of , false , nil ) , :without_protection => true )
617+ assert_all_attributes ( person . best_friend_of )
618+ end
619+
620+ def test_has_many_new_with_attr_protected_attributes
621+ person = LoosePerson . new ( nested_attributes_hash ( :best_friends , true ) )
622+ assert_default_attributes ( person . best_friends . first )
623+ end
624+
625+ def test_has_many_new_with_attr_accessible_attributes
626+ person = TightPerson . new ( nested_attributes_hash ( :best_friends , true ) )
627+ assert_default_attributes ( person . best_friends . first )
628+ end
629+
630+ def test_has_many_new_with_admin_role_with_attr_protected_attributes
631+ person = LoosePerson . new ( nested_attributes_hash ( :best_friends , true ) , :as => :admin )
632+ assert_admin_attributes ( person . best_friends . first )
633+ end
634+
635+ def test_has_many_new_with_admin_role_with_attr_accessible_attributes
636+ person = TightPerson . new ( nested_attributes_hash ( :best_friends , true ) , :as => :admin )
637+ assert_admin_attributes ( person . best_friends . first )
638+ end
639+
640+ def test_has_many_new_without_protection
641+ person = LoosePerson . new ( nested_attributes_hash ( :best_friends , true , nil ) , :without_protection => true )
642+ assert_all_attributes ( person . best_friends . first )
643+ end
644+
645+ # create
646+
647+ def test_has_one_create_with_attr_protected_attributes
648+ person = LoosePerson . create ( nested_attributes_hash ( :best_friend ) )
649+ assert_default_attributes ( person . best_friend , true )
650+ end
651+
652+ def test_has_one_create_with_attr_accessible_attributes
653+ person = TightPerson . create ( nested_attributes_hash ( :best_friend ) )
654+ assert_default_attributes ( person . best_friend , true )
655+ end
656+
657+ def test_has_one_create_with_admin_role_with_attr_protected_attributes
658+ person = LoosePerson . create ( nested_attributes_hash ( :best_friend ) , :as => :admin )
659+ assert_admin_attributes ( person . best_friend , true )
660+ end
661+
662+ def test_has_one_create_with_admin_role_with_attr_accessible_attributes
663+ person = TightPerson . create ( nested_attributes_hash ( :best_friend ) , :as => :admin )
664+ assert_admin_attributes ( person . best_friend , true )
665+ end
666+
667+ def test_has_one_create_without_protection
668+ person = LoosePerson . create ( nested_attributes_hash ( :best_friend , false , nil ) , :without_protection => true )
669+ assert_all_attributes ( person . best_friend )
670+ end
671+
672+ def test_belongs_to_create_with_attr_protected_attributes
673+ person = LoosePerson . create ( nested_attributes_hash ( :best_friend_of ) )
674+ assert_default_attributes ( person . best_friend_of , true )
675+ end
676+
677+ def test_belongs_to_create_with_attr_accessible_attributes
678+ person = TightPerson . create ( nested_attributes_hash ( :best_friend_of ) )
679+ assert_default_attributes ( person . best_friend_of , true )
680+ end
681+
682+ def test_belongs_to_create_with_admin_role_with_attr_protected_attributes
683+ person = LoosePerson . create ( nested_attributes_hash ( :best_friend_of ) , :as => :admin )
684+ assert_admin_attributes ( person . best_friend_of , true )
685+ end
686+
687+ def test_belongs_to_create_with_admin_role_with_attr_accessible_attributes
688+ person = TightPerson . create ( nested_attributes_hash ( :best_friend_of ) , :as => :admin )
689+ assert_admin_attributes ( person . best_friend_of , true )
690+ end
691+
692+ def test_belongs_to_create_without_protection
693+ person = LoosePerson . create ( nested_attributes_hash ( :best_friend_of , false , nil ) , :without_protection => true )
694+ assert_all_attributes ( person . best_friend_of )
695+ end
696+
697+ def test_has_many_create_with_attr_protected_attributes
698+ person = LoosePerson . create ( nested_attributes_hash ( :best_friends , true ) )
699+ assert_default_attributes ( person . best_friends . first , true )
700+ end
701+
702+ def test_has_many_create_with_attr_accessible_attributes
703+ person = TightPerson . create ( nested_attributes_hash ( :best_friends , true ) )
704+ assert_default_attributes ( person . best_friends . first , true )
705+ end
706+
707+ def test_has_many_create_with_admin_role_with_attr_protected_attributes
708+ person = LoosePerson . create ( nested_attributes_hash ( :best_friends , true ) , :as => :admin )
709+ assert_admin_attributes ( person . best_friends . first , true )
710+ end
711+
712+ def test_has_many_create_with_admin_role_with_attr_accessible_attributes
713+ person = TightPerson . create ( nested_attributes_hash ( :best_friends , true ) , :as => :admin )
714+ assert_admin_attributes ( person . best_friends . first , true )
715+ end
716+
717+ def test_has_many_create_without_protection
718+ person = LoosePerson . create ( nested_attributes_hash ( :best_friends , true , nil ) , :without_protection => true )
719+ assert_all_attributes ( person . best_friends . first )
720+ end
721+
722+ # create!
723+
724+ def test_has_one_create_with_bang_with_attr_protected_attributes
725+ person = LoosePerson . create! ( nested_attributes_hash ( :best_friend ) )
726+ assert_default_attributes ( person . best_friend , true )
727+ end
728+
729+ def test_has_one_create_with_bang_with_attr_accessible_attributes
730+ person = TightPerson . create! ( nested_attributes_hash ( :best_friend ) )
731+ assert_default_attributes ( person . best_friend , true )
732+ end
733+
734+ def test_has_one_create_with_bang_with_admin_role_with_attr_protected_attributes
735+ person = LoosePerson . create! ( nested_attributes_hash ( :best_friend ) , :as => :admin )
736+ assert_admin_attributes ( person . best_friend , true )
737+ end
738+
739+ def test_has_one_create_with_bang_with_admin_role_with_attr_accessible_attributes
740+ person = TightPerson . create! ( nested_attributes_hash ( :best_friend ) , :as => :admin )
741+ assert_admin_attributes ( person . best_friend , true )
742+ end
743+
744+ def test_has_one_create_with_bang_without_protection
745+ person = LoosePerson . create! ( nested_attributes_hash ( :best_friend , false , nil ) , :without_protection => true )
746+ assert_all_attributes ( person . best_friend )
747+ end
748+
749+ def test_belongs_to_create_with_bang_with_attr_protected_attributes
750+ person = LoosePerson . create! ( nested_attributes_hash ( :best_friend_of ) )
751+ assert_default_attributes ( person . best_friend_of , true )
752+ end
753+
754+ def test_belongs_to_create_with_bang_with_attr_accessible_attributes
755+ person = TightPerson . create! ( nested_attributes_hash ( :best_friend_of ) )
756+ assert_default_attributes ( person . best_friend_of , true )
757+ end
758+
759+ def test_belongs_to_create_with_bang_with_admin_role_with_attr_protected_attributes
760+ person = LoosePerson . create! ( nested_attributes_hash ( :best_friend_of ) , :as => :admin )
761+ assert_admin_attributes ( person . best_friend_of , true )
762+ end
763+
764+ def test_belongs_to_create_with_bang_with_admin_role_with_attr_accessible_attributes
765+ person = TightPerson . create! ( nested_attributes_hash ( :best_friend_of ) , :as => :admin )
766+ assert_admin_attributes ( person . best_friend_of , true )
767+ end
768+
769+ def test_belongs_to_create_with_bang_without_protection
770+ person = LoosePerson . create! ( nested_attributes_hash ( :best_friend_of , false , nil ) , :without_protection => true )
771+ assert_all_attributes ( person . best_friend_of )
772+ end
773+
774+ def test_has_many_create_with_bang_with_attr_protected_attributes
775+ person = LoosePerson . create! ( nested_attributes_hash ( :best_friends , true ) )
776+ assert_default_attributes ( person . best_friends . first , true )
777+ end
778+
779+ def test_has_many_create_with_bang_with_attr_accessible_attributes
780+ person = TightPerson . create! ( nested_attributes_hash ( :best_friends , true ) )
781+ assert_default_attributes ( person . best_friends . first , true )
782+ end
783+
784+ def test_has_many_create_with_bang_with_admin_role_with_attr_protected_attributes
785+ person = LoosePerson . create! ( nested_attributes_hash ( :best_friends , true ) , :as => :admin )
786+ assert_admin_attributes ( person . best_friends . first , true )
787+ end
788+
789+ def test_has_many_create_with_bang_with_admin_role_with_attr_accessible_attributes
790+ person = TightPerson . create! ( nested_attributes_hash ( :best_friends , true ) , :as => :admin )
791+ assert_admin_attributes ( person . best_friends . first , true )
792+ end
793+
794+ def test_has_many_create_with_bang_without_protection
795+ person = LoosePerson . create! ( nested_attributes_hash ( :best_friends , true , nil ) , :without_protection => true )
796+ assert_all_attributes ( person . best_friends . first )
797+ end
798+
799+ end
0 commit comments