aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/controller/parameters/mutators_test.rb
blob: 31ee7964f51a76908418df580bcc1952462523bb (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# frozen_string_literal: true

require "abstract_unit"
require "action_controller/metal/strong_parameters"

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

  test "delete retains permitted status" do
    @params.permit!
    assert_predicate @params.delete(:person), :permitted?
  end

  test "delete retains unpermitted status" do
    assert_not_predicate @params.delete(:person), :permitted?
  end

  test "delete returns the value when the key is present" do
    assert_equal "32", @params[:person].delete(:age)
  end

  test "delete removes the entry when the key present" do
    @params[:person].delete(:age)
    assert_not @params[:person].key?(:age)
  end

  test "delete returns nil when the key is not present" do
    assert_nil @params[:person].delete(:first_name)
  end

  test "delete returns the value of the given block when the key is not present" do
    assert_equal "David", @params[:person].delete(:first_name) { "David" }
  end

  test "delete yields the key to the given block when the key is not present" do
    assert_equal "first_name: David", @params[:person].delete(:first_name) { |k| "#{k}: David" }
  end

  test "delete_if retains permitted status" do
    @params.permit!
    assert_predicate @params.delete_if { |k| k == "person" }, :permitted?
  end

  test "delete_if retains unpermitted status" do
    assert_not_predicate @params.delete_if { |k| k == "person" }, :permitted?
  end

  test "extract! retains permitted status" do
    @params.permit!
    assert_predicate @params.extract!(:person), :permitted?
  end

  test "extract! retains unpermitted status" do
    assert_not_predicate @params.extract!(:person), :permitted?
  end

  test "keep_if retains permitted status" do
    @params.permit!
    assert_predicate @params.keep_if { |k, v| k == "person" }, :permitted?
  end

  test "keep_if retains unpermitted status" do
    assert_not_predicate @params.keep_if { |k, v| k == "person" }, :permitted?
  end

  test "reject! retains permitted status" do
    @params.permit!
    assert_predicate @params.reject! { |k| k == "person" }, :permitted?
  end

  test "reject! retains unpermitted status" do
    assert_not_predicate @params.reject! { |k| k == "person" }, :permitted?
  end

  test "select! retains permitted status" do
    @params.permit!
    assert_predicate @params.select! { |k| k != "person" }, :permitted?
  end

  test "select! retains unpermitted status" do
    assert_not_predicate @params.select! { |k| k != "person" }, :permitted?
  end

  test "slice! retains permitted status" do
    @params.permit!
    assert_predicate @params.slice!(:person), :permitted?
  end

  test "slice! retains unpermitted status" do
    assert_not_predicate @params.slice!(:person), :permitted?
  end

  test "transform_keys! retains permitted status" do
    @params.permit!
    assert_predicate @params.transform_keys! { |k| k }, :permitted?
  end

  test "transform_keys! retains unpermitted status" do
    assert_not_predicate @params.transform_keys! { |k| k }, :permitted?
  end

  test "transform_values! retains permitted status" do
    @params.permit!
    assert_predicate @params.transform_values! { |v| v }, :permitted?
  end

  test "transform_values! retains unpermitted status" do
    assert_not_predicate @params.transform_values! { |v| v }, :permitted?
  end

  test "deep_transform_keys! retains permitted status" do
    @params.permit!
    assert_predicate @params.deep_transform_keys! { |k| k }, :permitted?
  end

  test "deep_transform_keys! retains unpermitted status" do
    assert_not_predicate @params.deep_transform_keys! { |k| k }, :permitted?
  end
end