|
4 | 4 | require "test_helper" |
5 | 5 |
|
6 | 6 | module LogStruct |
7 | | - class ParamFiltersTest < Minitest::Test |
8 | | - def setup |
9 | | - # Save original configuration to restore after tests |
10 | | - @original_filter_keys = LogStruct.config.filters.filter_keys.dup |
11 | | - @original_filter_keys_with_hashes = LogStruct.config.filters.filter_keys_with_hashes.dup |
12 | | - |
13 | | - # Configure filter keys for testing |
14 | | - LogStruct.config.filters.filter_keys = [:password, :secret, :token] |
15 | | - LogStruct.config.filters.filter_keys_with_hashes = [:email] |
| 7 | + class ParamFiltersTest < ActiveSupport::TestCase |
| 8 | + setup do |
| 9 | + LogStruct.configure do |c| |
| 10 | + c.filters.filter_keys = %i[password token] |
| 11 | + c.filters.filter_keys_with_hashes = %i[email] |
| 12 | + c.filters.hash_salt = "s:" |
| 13 | + c.filters.hash_length = 6 |
| 14 | + end |
16 | 15 | end |
17 | 16 |
|
18 | | - def teardown |
19 | | - # Restore original configuration |
20 | | - LogStruct.config.filters.filter_keys = @original_filter_keys |
21 | | - LogStruct.config.filters.filter_keys_with_hashes = @original_filter_keys_with_hashes |
22 | | - end |
23 | | - |
24 | | - def test_should_filter_key |
| 17 | + test "should_filter_key? respects configured sensitive keys" do |
25 | 18 | assert ParamFilters.should_filter_key?(:password) |
26 | | - assert ParamFilters.should_filter_key?("PASSWORD") |
27 | | - assert ParamFilters.should_filter_key?("secret") |
28 | | - assert ParamFilters.should_filter_key?(:token) |
29 | | - |
| 19 | + assert ParamFilters.should_filter_key?("TOKEN") |
30 | 20 | refute ParamFilters.should_filter_key?(:username) |
31 | | - refute ParamFilters.should_filter_key?("email") |
32 | 21 | end |
33 | 22 |
|
34 | | - def test_should_include_string_hash |
| 23 | + test "should_include_string_hash? respects configured hash keys" do |
35 | 24 | assert ParamFilters.should_include_string_hash?(:email) |
36 | | - assert ParamFilters.should_include_string_hash?("EMAIL") |
37 | | - |
38 | 25 | refute ParamFilters.should_include_string_hash?(:password) |
39 | | - refute ParamFilters.should_include_string_hash?("username") |
40 | 26 | end |
41 | 27 |
|
42 | | - def test_summarize_string |
43 | | - string = "test-string" |
44 | | - |
45 | | - # Without hash |
46 | | - result = ParamFilters.summarize_string(string, false) |
47 | | - |
48 | | - assert_equal String, result[:_class] |
49 | | - assert_equal string.bytesize, result[:_bytes] |
50 | | - refute result.key?(:_hash) |
51 | | - |
52 | | - # With hash |
53 | | - result = ParamFilters.summarize_string(string, true) |
| 28 | + test "summarize_string includes hash when requested" do |
| 29 | + s = ParamFilters.summarize_string("abc@ex.com", true) |
54 | 30 |
|
55 | | - assert_equal String, result[:_class] |
56 | | - refute result.key?(:_bytes) |
57 | | - assert result.key?(:_hash) |
58 | | - assert_instance_of String, result[:_hash] |
| 31 | + assert_equal String, s[:_class] |
| 32 | + assert_match(/[0-9a-f]{6}/, s[:_hash]) |
| 33 | + refute s.key?(:_bytes) |
59 | 34 | end |
60 | 35 |
|
61 | | - def test_summarize_hash_without_sensitive_keys |
62 | | - hash = {name: "John", age: 30} |
| 36 | + test "summarize_string includes bytes when not hashing" do |
| 37 | + s = ParamFilters.summarize_string("hello", false) |
63 | 38 |
|
64 | | - result = ParamFilters.summarize_hash(hash) |
65 | | - |
66 | | - assert_equal Hash, result[:_class] |
67 | | - assert_equal 2, result[:_keys_count] |
68 | | - assert_equal [:name, :age], result[:_keys] |
69 | | - assert result.key?(:_bytes) |
| 39 | + assert_equal String, s[:_class] |
| 40 | + assert_equal 5, s[:_bytes] |
| 41 | + refute s.key?(:_hash) |
70 | 42 | end |
71 | 43 |
|
72 | | - def test_summarize_hash_with_sensitive_keys |
73 | | - hash = {name: "John", password: "secret123"} |
| 44 | + test "summarize_hash includes keys and optional bytes when no sensitive keys" do |
| 45 | + summary = ParamFilters.summarize_hash({a: 1, b: 2}) |
74 | 46 |
|
75 | | - result = ParamFilters.summarize_hash(hash) |
| 47 | + assert_equal Hash, summary[:_class] |
| 48 | + assert_equal 2, summary[:_keys_count] |
| 49 | + assert_equal [:a, :b], summary[:_keys] |
| 50 | + assert_kind_of Integer, summary[:_bytes] |
76 | 51 |
|
77 | | - assert_equal Hash, result[:_class] |
78 | | - assert_equal 2, result[:_keys_count] |
79 | | - assert_equal [:name, :password], result[:_keys] |
| 52 | + # Presence of sensitive key removes _bytes |
| 53 | + summary2 = ParamFilters.summarize_hash({password: "x", a: 1}) |
80 | 54 |
|
81 | | - # Should not include byte size for hashes with sensitive keys |
82 | | - refute result.key?(:_bytes) |
| 55 | + assert_equal Hash, summary2[:_class] |
| 56 | + refute summary2.key?(:_bytes) |
83 | 57 | end |
84 | 58 |
|
85 | | - def test_summarize_hash_with_uppercase_sensitive_keys |
86 | | - hash = {name: "John", PASSWORD: "secret123"} |
87 | | - |
88 | | - result = ParamFilters.summarize_hash(hash) |
89 | | - |
90 | | - assert_equal Hash, result[:_class] |
91 | | - assert_equal 2, result[:_keys_count] |
92 | | - assert_equal [:name, :PASSWORD], result[:_keys] |
| 59 | + test "summarize_array reports count/bytes and handles empty" do |
| 60 | + s = ParamFilters.summarize_array([1, 2, 3]) |
93 | 61 |
|
94 | | - # Should not include byte size regardless of case |
95 | | - refute result.key?(:_bytes) |
96 | | - end |
| 62 | + assert_equal Array, s[:_class] |
| 63 | + assert_equal 3, s[:_count] |
| 64 | + assert_operator s[:_bytes], :>, 0 |
97 | 65 |
|
98 | | - def test_summarize_hash_empty |
99 | | - result = ParamFilters.summarize_hash({}) |
| 66 | + empty = ParamFilters.summarize_array([]) |
100 | 67 |
|
101 | | - assert_equal "Hash", result[:_class] |
102 | | - assert result[:_empty] |
| 68 | + assert_equal "Array", empty[:_class] |
| 69 | + assert empty[:_empty] |
103 | 70 | end |
104 | 71 |
|
105 | | - def test_summarize_array |
106 | | - array = [1, 2, 3] |
| 72 | + test "summarize_json_attribute dispatches by type and honors hash keys setting" do |
| 73 | + # String + key with hash |
| 74 | + s1 = ParamFilters.summarize_json_attribute(:email, "test@x.com") |
107 | 75 |
|
108 | | - result = ParamFilters.summarize_array(array) |
| 76 | + assert s1.key?(:_hash) |
109 | 77 |
|
110 | | - assert_equal Array, result[:_class] |
111 | | - assert_equal 3, result[:_count] |
112 | | - assert result.key?(:_bytes) |
113 | | - end |
| 78 | + # String + normal key -> bytes |
| 79 | + s2 = ParamFilters.summarize_json_attribute(:message, "hello") |
114 | 80 |
|
115 | | - def test_summarize_array_empty |
116 | | - result = ParamFilters.summarize_array([]) |
| 81 | + assert s2.key?(:_bytes) |
117 | 82 |
|
118 | | - assert_equal "Array", result[:_class] |
119 | | - assert result[:_empty] |
120 | | - end |
| 83 | + # Hash |
| 84 | + s3 = ParamFilters.summarize_json_attribute(:payload, {a: 1}) |
121 | 85 |
|
122 | | - def test_summarize_json_attribute_with_string |
123 | | - result = ParamFilters.summarize_json_attribute("username", "john") |
124 | | - |
125 | | - assert_equal String, result[:_class] |
126 | | - assert_equal "john".bytesize, result[:_bytes] |
127 | | - refute result.key?(:_hash) |
128 | | - |
129 | | - result = ParamFilters.summarize_json_attribute("email", "john@example.com") |
130 | | - |
131 | | - assert_equal String, result[:_class] |
132 | | - refute result.key?(:_bytes) |
133 | | - assert result.key?(:_hash) |
134 | | - end |
135 | | - |
136 | | - def test_summarize_json_attribute_with_hash |
137 | | - hash = {name: "John", age: 30} |
138 | | - result = ParamFilters.summarize_json_attribute("user", hash) |
139 | | - |
140 | | - assert_equal Hash, result[:_class] |
141 | | - assert_equal 2, result[:_keys_count] |
142 | | - assert result.key?(:_bytes) |
143 | | - |
144 | | - hash_with_sensitive = {name: "John", password: "secret"} |
145 | | - result = ParamFilters.summarize_json_attribute("user", hash_with_sensitive) |
146 | | - |
147 | | - assert_equal Hash, result[:_class] |
148 | | - assert_equal 2, result[:_keys_count] |
149 | | - refute result.key?(:_bytes) |
150 | | - end |
151 | | - |
152 | | - def test_summarize_json_attribute_with_array |
153 | | - array = [1, 2, 3] |
154 | | - result = ParamFilters.summarize_json_attribute("numbers", array) |
155 | | - |
156 | | - assert_equal Array, result[:_class] |
157 | | - assert_equal 3, result[:_count] |
158 | | - assert result.key?(:_bytes) |
159 | | - end |
| 86 | + assert_equal Hash, s3[:_class] |
160 | 87 |
|
161 | | - def test_summarize_json_attribute_with_other_types |
162 | | - result = ParamFilters.summarize_json_attribute("age", 30) |
| 88 | + # Array |
| 89 | + s4 = ParamFilters.summarize_json_attribute(:list, [1, 2]) |
163 | 90 |
|
164 | | - assert_equal Integer, result[:_class] |
| 91 | + assert_equal Array, s4[:_class] |
165 | 92 |
|
166 | | - result = ParamFilters.summarize_json_attribute("active", true) |
| 93 | + # Other |
| 94 | + s5 = ParamFilters.summarize_json_attribute(:id, 123) |
167 | 95 |
|
168 | | - assert_equal TrueClass, result[:_class] |
| 96 | + assert_equal Integer, s5[:_class] |
169 | 97 | end |
170 | 98 | end |
171 | 99 | end |
0 commit comments