aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/controller/parameters/dup_test.rb
blob: fb707a13543238e50366cf8ea9ef6faf1d7377dd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
require "abstract_unit"
require "action_controller/metal/strong_parameters"
require "active_support/core_ext/object/deep_dup"

class ParametersDupTest < ActiveSupport::TestCase
  setup do
    ActionController::Parameters.permit_all_parameters = false

    @params = ActionController::Parameters.new(
      person: {
        age: "32",
        name: {
          first: "David",
          last: "Heinemeier Hansson"
        },
        addresses: [{ city: "Chicago", state: "Illinois" }]
      }
    )
  end

  test "a duplicate maintains the original's permitted status" do
    @params.permit!
    dupped_params = @params.dup
    assert dupped_params.permitted?
  end

  test "a duplicate maintains the original's parameters" do
    @params.permit!
    dupped_params = @params.dup
    assert_equal @params.to_h, dupped_params.to_h
  end

  test "changes to a duplicate's parameters do not affect the original" do
    dupped_params = @params.dup
    dupped_params.delete(:person)
    assert_not_equal @params, dupped_params
  end

  test "changes to a duplicate's permitted status do not affect the original" do
    dupped_params = @params.dup
    dupped_params.permit!
    assert_not_equal @params, dupped_params
  end

  test "deep_dup content" do
    dupped_params = @params.deep_dup
    dupped_params[:person][:age] = "45"
    dupped_params[:person][:addresses].clear

    assert_not_equal @params[:person][:age], dupped_params[:person][:age]
    assert_not_equal @params[:person][:addresses], dupped_params[:person][:addresses]
  end

  test "deep_dup @permitted" do
    dupped_params = @params.deep_dup
    dupped_params.permit!

    assert_not @params.permitted?
  end

  test "deep_dup @permitted is being copied" do
    @params.permit!
    assert @params.deep_dup.permitted?
  end
end