Skip to content

Commit c56ee12

Browse files
committed
test(approval): add tests for env fallbacks, SSL toggles, and webhook crash fix
- Discord: test DISCORD_CHANNEL_ID env fallback, SSL verify default/override - Telegram: test TELEGRAM_CHAT_ID env fallback, SSL verify default/override - Webhook: test integer status does not crash str().lower()
1 parent c71e0ea commit c56ee12

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

β€Žsrc/praisonai/tests/unit/test_discord_approval.pyβ€Ž

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,27 @@ def test_repr_masks_token(self):
6868
assert "super-secret-token" not in r
6969
assert "99" in r
7070

71+
def test_env_var_channel_id(self, monkeypatch):
72+
monkeypatch.setenv("DISCORD_CHANNEL_ID", "env-chan-123")
73+
from praisonai.bots._discord_approval import DiscordApproval
74+
75+
backend = DiscordApproval(token="t")
76+
assert backend._channel_id == "env-chan-123"
77+
78+
def test_ssl_verify_default_true(self, monkeypatch):
79+
monkeypatch.delenv("PRAISONAI_DISCORD_SSL_VERIFY", raising=False)
80+
from praisonai.bots._discord_approval import DiscordApproval
81+
82+
backend = DiscordApproval(token="t", channel_id="1")
83+
assert backend._ssl_verify is True
84+
85+
def test_ssl_verify_env_false(self, monkeypatch):
86+
monkeypatch.setenv("PRAISONAI_DISCORD_SSL_VERIFY", "false")
87+
from praisonai.bots._discord_approval import DiscordApproval
88+
89+
backend = DiscordApproval(token="t", channel_id="1")
90+
assert backend._ssl_verify is False
91+
7192

7293
# ── Embed Builder ───────────────────────────────────────────────────────────
7394

β€Žsrc/praisonai/tests/unit/test_telegram_approval.pyβ€Ž

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,27 @@ def test_repr_masks_token(self):
8484
assert "1234567890:ABCDEF" not in r
8585
assert "99" in r
8686

87+
def test_env_var_chat_id(self, monkeypatch):
88+
monkeypatch.setenv("TELEGRAM_CHAT_ID", "env-chat-456")
89+
from praisonai.bots._telegram_approval import TelegramApproval
90+
91+
backend = TelegramApproval(token="t")
92+
assert backend._chat_id == "env-chat-456"
93+
94+
def test_ssl_verify_default_true(self, monkeypatch):
95+
monkeypatch.delenv("PRAISONAI_TELEGRAM_SSL_VERIFY", raising=False)
96+
from praisonai.bots._telegram_approval import TelegramApproval
97+
98+
backend = TelegramApproval(token="t", chat_id="1")
99+
assert backend._ssl_verify is True
100+
101+
def test_ssl_verify_env_false(self, monkeypatch):
102+
monkeypatch.setenv("PRAISONAI_TELEGRAM_SSL_VERIFY", "false")
103+
from praisonai.bots._telegram_approval import TelegramApproval
104+
105+
backend = TelegramApproval(token="t", chat_id="1")
106+
assert backend._ssl_verify is False
107+
87108

88109
# ── Message Builder ─────────────────────────────────────────────────────────
89110

β€Žsrc/praisonai/tests/unit/test_webhook_approval.pyβ€Ž

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,28 @@ async def mock_http(method, url, payload=None, **kwargs):
220220
assert posted_payloads[0]["tool_name"] == "rm_file"
221221
assert "request_id" in posted_payloads[0]
222222

223+
def test_non_json_status_does_not_crash(self):
224+
"""Integer status (e.g. HTTP code) must not crash .lower()."""
225+
from praisonai.bots._webhook_approval import WebhookApproval
226+
227+
backend = WebhookApproval(webhook_url="https://example.com", timeout=5, poll_interval=0.1)
228+
poll_count = {"n": 0}
229+
230+
async def mock_http(method, url, payload=None, **kwargs):
231+
if method == "POST":
232+
return {"status": "pending"}
233+
if method == "GET":
234+
poll_count["n"] += 1
235+
if poll_count["n"] <= 1:
236+
# Simulate non-JSON response where status is an int
237+
return {"status": 502}
238+
return {"approved": True, "reason": "ok"}
239+
return {}
240+
241+
backend._http_request = mock_http
242+
decision = asyncio.run(backend.request_approval(self._make_request()))
243+
assert decision.approved is True
244+
223245

224246
# ── Export / Import ─────────────────────────────────────────────────────────
225247

0 commit comments

Comments
Β (0)