Coverage for mlos_bench/mlos_bench/tests/test_sanitize_confs.py: 100%
28 statements
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-14 00:55 +0000
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-14 00:55 +0000
1#
2# Copyright (c) Microsoft Corporation.
3# Licensed under the MIT License.
4#
5"""
6Unit tests for sanitize_conf utility function.
8Tests cover obfuscation of sensitive keys and recursive sanitization.
9"""
10from mlos_bench.util import sanitize_config
13def test_sanitize_config_simple() -> None:
14 """Test sanitization of a simple configuration dictionary."""
15 config = {
16 "username": "user1",
17 "password": "mypassword",
18 "token": "abc123",
19 "api_key": "key",
20 "secret": "shh",
21 "other": 42,
22 }
23 sanitized = sanitize_config(config)
24 assert sanitized["username"] == "user1"
25 assert sanitized["password"] == "[REDACTED]"
26 assert sanitized["token"] == "[REDACTED]"
27 assert sanitized["api_key"] == "[REDACTED]"
28 assert sanitized["secret"] == "[REDACTED]"
29 assert sanitized["other"] == 42
32def test_sanitize_config_nested() -> None:
33 """Test sanitization of nested dictionaries."""
34 config = {
35 "outer": {
36 "password": "pw",
37 "inner": {"token": "tok", "foo": "bar"},
38 },
39 "api_key": "key",
40 }
41 sanitized = sanitize_config(config)
42 assert sanitized["outer"]["password"] == "[REDACTED]"
43 assert sanitized["outer"]["inner"]["token"] == "[REDACTED]"
44 assert sanitized["outer"]["inner"]["foo"] == "bar"
45 assert sanitized["api_key"] == "[REDACTED]"
48def test_sanitize_config_no_sensitive_keys() -> None:
49 """Test that no changes are made if no sensitive keys are present."""
50 config = {"foo": 1, "bar": {"baz": 2}}
51 sanitized = sanitize_config(config)
52 assert sanitized == config
55def test_sanitize_config_mixed_types() -> None:
56 """Test sanitization with mixed types including lists and dicts."""
57 config = {
58 "password": None,
59 "token": 123,
60 "secret": ["a", "b"],
61 "api_key": {"nested": "val"},
62 }
63 sanitized = sanitize_config(config)
64 assert sanitized["password"] == "[REDACTED]"
65 assert sanitized["token"] == "[REDACTED]"
66 assert sanitized["secret"] == "[REDACTED]"
67 assert sanitized["api_key"] == "[REDACTED]"