|
1 | | -import runpy |
| 1 | +import importlib.util |
2 | 2 | from pathlib import Path |
3 | 3 |
|
4 | 4 | import pytest |
5 | 5 |
|
6 | 6 | CLIENT_CONFIG_PATH = Path(__file__).resolve().parent.parent / "agent-sdk-client" / "config.py" |
7 | | -config_module = runpy.run_path(CLIENT_CONFIG_PATH) |
8 | | -Config = config_module["Config"] |
9 | | -load_command_whitelist = config_module["load_command_whitelist"] |
| 7 | +spec = importlib.util.spec_from_file_location("agent_sdk_client_config", CLIENT_CONFIG_PATH) |
| 8 | +config_module = importlib.util.module_from_spec(spec) |
| 9 | +assert spec.loader is not None |
| 10 | +spec.loader.exec_module(config_module) |
| 11 | +Config = config_module.Config |
| 12 | +load_command_whitelist = config_module.load_command_whitelist |
10 | 13 |
|
11 | 14 |
|
12 | 15 | def test_load_command_whitelist(tmp_path): |
@@ -43,3 +46,62 @@ def test_is_command_allowed(text, expected): |
43 | 46 | ) |
44 | 47 |
|
45 | 48 | assert cfg.is_command_allowed(text) is expected |
| 49 | + |
| 50 | + |
| 51 | +def test_empty_whitelist_allows_commands(): |
| 52 | + cfg = Config( |
| 53 | + telegram_token="", |
| 54 | + agent_server_url="", |
| 55 | + auth_token="", |
| 56 | + queue_url="", |
| 57 | + command_whitelist=[], |
| 58 | + ) |
| 59 | + assert cfg.is_command_allowed("/anything") is False |
| 60 | + |
| 61 | + |
| 62 | +def test_none_text_treated_as_allowed(): |
| 63 | + cfg = Config( |
| 64 | + telegram_token="", |
| 65 | + agent_server_url="", |
| 66 | + auth_token="", |
| 67 | + queue_url="", |
| 68 | + command_whitelist=["/allowed"], |
| 69 | + ) |
| 70 | + assert cfg.is_command_allowed(None) |
| 71 | + |
| 72 | + |
| 73 | +def test_load_whitelist_non_string_entries(tmp_path): |
| 74 | + config_path = tmp_path / "config.toml" |
| 75 | + config_path.write_text( |
| 76 | + """[white_list_commands] |
| 77 | +whitelist = ["/ok", 123, { a = 1 }] |
| 78 | +""" |
| 79 | + ) |
| 80 | + assert load_command_whitelist(config_path) == ["/ok"] |
| 81 | + |
| 82 | + |
| 83 | +def test_load_whitelist_invalid_type_logs_and_returns_empty(tmp_path, caplog): |
| 84 | + config_path = tmp_path / "config.toml" |
| 85 | + config_path.write_text( |
| 86 | + """[white_list_commands] |
| 87 | +whitelist = "not-a-list" |
| 88 | +""" |
| 89 | + ) |
| 90 | + with caplog.at_level("WARNING"): |
| 91 | + result = load_command_whitelist(config_path) |
| 92 | + assert result == [] |
| 93 | + assert any("Command whitelist is not a list" in rec.message for rec in caplog.records) |
| 94 | + |
| 95 | + |
| 96 | +def test_load_whitelist_missing_file_returns_empty(tmp_path): |
| 97 | + missing = tmp_path / "missing.toml" |
| 98 | + assert load_command_whitelist(missing) == [] |
| 99 | + |
| 100 | + |
| 101 | +def test_load_whitelist_malformed_toml_returns_empty(tmp_path, caplog): |
| 102 | + config_path = tmp_path / "config.toml" |
| 103 | + config_path.write_text("not = [ [") |
| 104 | + with caplog.at_level("WARNING"): |
| 105 | + result = load_command_whitelist(config_path) |
| 106 | + assert result == [] |
| 107 | + assert any("Failed to load command whitelist" in rec.message for rec in caplog.records) |
0 commit comments