|
| 1 | +require 'abstract_unit' |
| 2 | +require 'action_controller/metal/strong_parameters' |
| 3 | + |
| 4 | +class NestedParametersTest < ActiveSupport::TestCase |
| 5 | + test "permitted nested parameters" do |
| 6 | + params = ActionController::Parameters.new({ |
| 7 | + book: { |
| 8 | + title: "Romeo and Juliet", |
| 9 | + authors: [{ |
| 10 | + name: "William Shakespeare", |
| 11 | + born: "1564-04-26" |
| 12 | + }, { |
| 13 | + name: "Christopher Marlowe" |
| 14 | + }], |
| 15 | + details: { |
| 16 | + pages: 200, |
| 17 | + genre: "Tragedy" |
| 18 | + } |
| 19 | + }, |
| 20 | + magazine: "Mjallo!" |
| 21 | + }) |
| 22 | + |
| 23 | + permitted = params.permit book: [ :title, { authors: [ :name ] }, { details: :pages } ] |
| 24 | + |
| 25 | + assert permitted.permitted? |
| 26 | + assert_equal "Romeo and Juliet", permitted[:book][:title] |
| 27 | + assert_equal "William Shakespeare", permitted[:book][:authors][0][:name] |
| 28 | + assert_equal "Christopher Marlowe", permitted[:book][:authors][1][:name] |
| 29 | + assert_equal 200, permitted[:book][:details][:pages] |
| 30 | + assert_nil permitted[:book][:details][:genre] |
| 31 | + assert_nil permitted[:book][:authors][1][:born] |
| 32 | + assert_nil permitted[:magazine] |
| 33 | + end |
| 34 | + |
| 35 | + test "nested arrays with strings" do |
| 36 | + params = ActionController::Parameters.new({ |
| 37 | + :book => { |
| 38 | + :genres => ["Tragedy"] |
| 39 | + } |
| 40 | + }) |
| 41 | + |
| 42 | + permitted = params.permit :book => :genres |
| 43 | + assert_equal ["Tragedy"], permitted[:book][:genres] |
| 44 | + end |
| 45 | + |
| 46 | + test "permit may specify symbols or strings" do |
| 47 | + params = ActionController::Parameters.new({ |
| 48 | + :book => { |
| 49 | + :title => "Romeo and Juliet", |
| 50 | + :author => "William Shakespeare" |
| 51 | + }, |
| 52 | + :magazine => "Shakespeare Today" |
| 53 | + }) |
| 54 | + |
| 55 | + permitted = params.permit({:book => ["title", :author]}, "magazine") |
| 56 | + assert_equal "Romeo and Juliet", permitted[:book][:title] |
| 57 | + assert_equal "William Shakespeare", permitted[:book][:author] |
| 58 | + assert_equal "Shakespeare Today", permitted[:magazine] |
| 59 | + end |
| 60 | + |
| 61 | + test "nested array with strings that should be hashes" do |
| 62 | + params = ActionController::Parameters.new({ |
| 63 | + book: { |
| 64 | + genres: ["Tragedy"] |
| 65 | + } |
| 66 | + }) |
| 67 | + |
| 68 | + permitted = params.permit book: { genres: :type } |
| 69 | + assert_empty permitted[:book][:genres] |
| 70 | + end |
| 71 | + |
| 72 | + test "nested array with strings that should be hashes and additional values" do |
| 73 | + params = ActionController::Parameters.new({ |
| 74 | + book: { |
| 75 | + title: "Romeo and Juliet", |
| 76 | + genres: ["Tragedy"] |
| 77 | + } |
| 78 | + }) |
| 79 | + |
| 80 | + permitted = params.permit book: [ :title, { genres: :type } ] |
| 81 | + assert_equal "Romeo and Juliet", permitted[:book][:title] |
| 82 | + assert_empty permitted[:book][:genres] |
| 83 | + end |
| 84 | + |
| 85 | + test "nested string that should be a hash" do |
| 86 | + params = ActionController::Parameters.new({ |
| 87 | + book: { |
| 88 | + genre: "Tragedy" |
| 89 | + } |
| 90 | + }) |
| 91 | + |
| 92 | + permitted = params.permit book: { genre: :type } |
| 93 | + assert_nil permitted[:book][:genre] |
| 94 | + end |
| 95 | + |
| 96 | + test "fields_for-style nested params" do |
| 97 | + params = ActionController::Parameters.new({ |
| 98 | + book: { |
| 99 | + authors_attributes: { |
| 100 | + :'0' => { name: 'William Shakespeare', age_of_death: '52' }, |
| 101 | + :'-1' => { name: 'Unattributed Assistant' } |
| 102 | + } |
| 103 | + } |
| 104 | + }) |
| 105 | + permitted = params.permit book: { authors_attributes: [ :name ] } |
| 106 | + |
| 107 | + assert_not_nil permitted[:book][:authors_attributes]['0'] |
| 108 | + assert_not_nil permitted[:book][:authors_attributes]['-1'] |
| 109 | + assert_nil permitted[:book][:authors_attributes]['0'][:age_of_death] |
| 110 | + assert_equal 'William Shakespeare', permitted[:book][:authors_attributes]['0'][:name] |
| 111 | + assert_equal 'Unattributed Assistant', permitted[:book][:authors_attributes]['-1'][:name] |
| 112 | + end |
| 113 | +end |
0 commit comments