-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_bridge.py
More file actions
70 lines (55 loc) · 2.39 KB
/
test_bridge.py
File metadata and controls
70 lines (55 loc) · 2.39 KB
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
"""Tests for bridge.py startup validation and core helpers."""
import os
import subprocess
import sys
from pathlib import Path
BRIDGE_DIR = Path(__file__).parent
def _import_bridge(env_overrides: dict) -> subprocess.CompletedProcess:
"""Import bridge.py in a subprocess with the given environment overrides.
pass (password-store) may be unavailable in CI, so TELEGRAM_BOT_TOKEN is
provided directly to bypass the secret lookup and the BOT_TOKEN check.
"""
env = {
**os.environ,
"TELEGRAM_BOT_TOKEN": "fake_token_for_tests",
**env_overrides,
}
return subprocess.run(
[sys.executable, "-c", "import bridge"],
env=env,
capture_output=True,
text=True,
cwd=str(BRIDGE_DIR),
)
class TestAllowedUserIdsParsing:
def test_valid_single_id(self):
result = _import_bridge({"ALLOWED_USER_IDS": "123456789"})
assert result.returncode == 0, result.stderr
def test_valid_multiple_ids(self):
result = _import_bridge({"ALLOWED_USER_IDS": "123456789,987654321"})
assert result.returncode == 0, result.stderr
def test_empty_value_is_allowed(self):
result = _import_bridge({"ALLOWED_USER_IDS": ""})
assert result.returncode == 0, result.stderr
def test_whitespace_around_ids(self):
result = _import_bridge({"ALLOWED_USER_IDS": " 123 , 456 "})
assert result.returncode == 0, result.stderr
def test_non_integer_rejects_with_exit_1(self):
result = _import_bridge({"ALLOWED_USER_IDS": "123,not_a_number"})
assert result.returncode == 1
def test_non_integer_error_message(self):
result = _import_bridge({"ALLOWED_USER_IDS": "123,not_a_number"})
assert "ALLOWED_USER_IDS" in result.stderr
assert "not_a_number" in result.stderr
def test_float_rejects(self):
result = _import_bridge({"ALLOWED_USER_IDS": "123,45.6"})
assert result.returncode == 1
assert "45.6" in result.stderr
def test_leading_non_integer_rejects(self):
result = _import_bridge({"ALLOWED_USER_IDS": "abc,123"})
assert result.returncode == 1
assert "abc" in result.stderr
def test_trailing_comma_ignored(self):
"""Trailing comma produces an empty token, which should be silently skipped."""
result = _import_bridge({"ALLOWED_USER_IDS": "123,"})
assert result.returncode == 0, result.stderr