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
|
"""Tests for pyvyos.utils.json module."""
import pytest
from pyvyos.utils.json import redact_key, safe_dumps
def test_redact_key_single_key():
"""Test redacting a single key."""
data = {"key": "secret", "other": "visible"}
result = redact_key(data)
assert result["key"] == "***REDACTED***"
assert result["other"] == "visible"
def test_redact_key_multiple_keys():
"""Test redacting multiple keys."""
data = {"key": "secret", "password": "pass123", "apikey": "api_key", "visible": "data"}
result = redact_key(data, keys=["key", "password", "apikey"])
assert result["key"] == "***REDACTED***"
assert result["password"] == "***REDACTED***"
assert result["apikey"] == "***REDACTED***"
assert result["visible"] == "data"
def test_redact_key_default_keys():
"""Test redacting with default keys."""
data = {"key": "secret", "apikey": "api", "password": "pass", "data": "ok"}
result = redact_key(data)
assert result["key"] == "***REDACTED***"
assert result["apikey"] == "***REDACTED***"
assert result["password"] == "***REDACTED***"
assert result["data"] == "ok"
def test_redact_key_copies_data():
"""Test that redact_key returns a copy, not original."""
data = {"key": "secret"}
result = redact_key(data)
result["new"] = "value"
assert "new" not in data
assert data["key"] == "secret" # Original unchanged
def test_safe_dumps_with_dict():
"""Test safe_dumps with dictionary."""
data = {"key": "secret", "data": "visible"}
result = safe_dumps(data)
assert "***REDACTED***" in result
assert "secret" not in result
assert "visible" in result
def test_safe_dumps_with_list():
"""Test safe_dumps with list."""
data = [1, 2, 3]
result = safe_dumps(data)
assert result == "[1, 2, 3]"
def test_safe_dumps_with_custom_keys():
"""Test safe_dumps with custom redaction keys."""
data = {"sensitive": "hide", "public": "show"}
result = safe_dumps(data, redact_keys=["sensitive"])
assert "***REDACTED***" in result
assert "hide" not in result
assert "show" in result
|