refactor: remove GitHub webhook support
This commit is contained in:
@@ -3,59 +3,7 @@ from __future__ import annotations
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from app.config import _parse_ai_config, _parse_elevenlabs_config, _parse_github_webhook_config
|
||||
|
||||
|
||||
class ConfigGitHubWebhookTests(unittest.TestCase):
|
||||
def test_github_webhook_config_parses_values(self) -> None:
|
||||
with patch.dict("os.environ", {}, clear=True):
|
||||
config = _parse_github_webhook_config(
|
||||
{
|
||||
"enabled": True,
|
||||
"host": "127.0.0.1",
|
||||
"port": 9000,
|
||||
"secret": "config-secret",
|
||||
}
|
||||
)
|
||||
|
||||
self.assertTrue(config.enabled)
|
||||
self.assertEqual(config.host, "127.0.0.1")
|
||||
self.assertEqual(config.port, 9000)
|
||||
self.assertEqual(config.secret, "config-secret")
|
||||
|
||||
def test_github_webhook_env_overrides(self) -> None:
|
||||
env = {
|
||||
"GITHUB_WEBHOOK_ENABLED": "true",
|
||||
"GITHUB_WEBHOOK_HOST": "localhost",
|
||||
"GITHUB_WEBHOOK_PORT": "7777",
|
||||
"GITHUB_WEBHOOK_SECRET": "env-secret",
|
||||
}
|
||||
with patch.dict("os.environ", env, clear=True):
|
||||
config = _parse_github_webhook_config(
|
||||
{
|
||||
"enabled": False,
|
||||
"host": "0.0.0.0",
|
||||
"port": 8080,
|
||||
"secret": "config-secret",
|
||||
}
|
||||
)
|
||||
|
||||
self.assertTrue(config.enabled)
|
||||
self.assertEqual(config.host, "localhost")
|
||||
self.assertEqual(config.port, 7777)
|
||||
self.assertEqual(config.secret, "env-secret")
|
||||
|
||||
def test_github_webhook_requires_secret_when_enabled(self) -> None:
|
||||
with patch.dict("os.environ", {}, clear=True):
|
||||
with self.assertRaisesRegex(ValueError, "github_webhook.secret"):
|
||||
_parse_github_webhook_config({"enabled": True})
|
||||
|
||||
def test_github_webhook_allows_missing_secret_when_disabled(self) -> None:
|
||||
with patch.dict("os.environ", {}, clear=True):
|
||||
config = _parse_github_webhook_config({"enabled": False})
|
||||
|
||||
self.assertFalse(config.enabled)
|
||||
self.assertIsNone(config.secret)
|
||||
from app.config import _parse_ai_config, _parse_elevenlabs_config
|
||||
|
||||
|
||||
class ConfigElevenLabsTests(unittest.TestCase):
|
||||
@@ -1,153 +0,0 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import hmac
|
||||
import json
|
||||
import unittest
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from aiohttp import web
|
||||
from aiohttp.test_utils import TestClient, TestServer
|
||||
|
||||
from app.config import GitHubWebhookConfig
|
||||
from app.github_webhook import GitHubWebhookServer, chunk_text, format_github_event
|
||||
|
||||
|
||||
def _signature(body: bytes, secret: str = "secret") -> str:
|
||||
digest = hmac.new(secret.encode("utf-8"), body, hashlib.sha256).hexdigest()
|
||||
return f"sha256={digest}"
|
||||
|
||||
|
||||
def _push_payload() -> dict:
|
||||
return {
|
||||
"ref": "refs/heads/main",
|
||||
"compare": "https://github.com/acme/project/compare/a...b",
|
||||
"repository": {
|
||||
"full_name": "acme/project",
|
||||
"html_url": "https://github.com/acme/project",
|
||||
},
|
||||
"sender": {"login": "octocat"},
|
||||
"commits": [
|
||||
{
|
||||
"id": "abcdef123456",
|
||||
"message": "Add webhook receiver\n\nDetails",
|
||||
"url": "https://github.com/acme/project/commit/abcdef1",
|
||||
"author": {"name": "Mona"},
|
||||
}
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
class GitHubWebhookFormattingTests(unittest.TestCase):
|
||||
def test_format_push_event(self) -> None:
|
||||
text = format_github_event("push", _push_payload(), delivery="delivery-1")
|
||||
|
||||
self.assertIn("GitHub push", text)
|
||||
self.assertIn("acme/project", text)
|
||||
self.assertIn("main", text)
|
||||
self.assertIn("abcdef1", text)
|
||||
self.assertIn("delivery-1", text)
|
||||
|
||||
def test_format_pull_request_event(self) -> None:
|
||||
text = format_github_event(
|
||||
"pull_request",
|
||||
{
|
||||
"action": "opened",
|
||||
"repository": {"full_name": "acme/project"},
|
||||
"sender": {"login": "octocat"},
|
||||
"pull_request": {
|
||||
"number": 42,
|
||||
"title": "Improve webhooks",
|
||||
"html_url": "https://github.com/acme/project/pull/42",
|
||||
"state": "open",
|
||||
"user": {"login": "octocat"},
|
||||
"changed_files": 3,
|
||||
"additions": 12,
|
||||
"deletions": 4,
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
self.assertIn("pull request opened", text)
|
||||
self.assertIn("#42 Improve webhooks", text)
|
||||
self.assertIn("3 files", text)
|
||||
|
||||
def test_format_unknown_event_uses_compact_json(self) -> None:
|
||||
text = format_github_event(
|
||||
"star",
|
||||
{
|
||||
"action": "created",
|
||||
"repository": {"full_name": "acme/project"},
|
||||
"sender": {"login": "octocat"},
|
||||
"extra": "value",
|
||||
},
|
||||
)
|
||||
|
||||
self.assertIn("GitHub star", text)
|
||||
self.assertIn("Action:", text)
|
||||
self.assertIn('"extra": "value"', text)
|
||||
|
||||
def test_chunk_text_splits_long_messages(self) -> None:
|
||||
chunks = chunk_text("a" * 5000, limit=1000)
|
||||
|
||||
self.assertGreater(len(chunks), 1)
|
||||
self.assertTrue(all(len(chunk) <= 1000 for chunk in chunks))
|
||||
|
||||
|
||||
class GitHubWebhookHandlerTests(unittest.IsolatedAsyncioTestCase):
|
||||
async def asyncSetUp(self) -> None:
|
||||
self.telegram_client = SimpleNamespace(send_message=AsyncMock())
|
||||
state = SimpleNamespace(
|
||||
config=SimpleNamespace(name="personal", session="personal_session"),
|
||||
client=self.telegram_client,
|
||||
status="running",
|
||||
)
|
||||
manager = SimpleNamespace(states=[state])
|
||||
self.server = GitHubWebhookServer(
|
||||
manager=manager,
|
||||
config=GitHubWebhookConfig(enabled=True, secret="secret"),
|
||||
)
|
||||
app = web.Application()
|
||||
app.add_routes([web.post("/github/{account}/{chat_id}", self.server.handle)])
|
||||
self.client = TestClient(TestServer(app))
|
||||
await self.client.start_server()
|
||||
|
||||
async def asyncTearDown(self) -> None:
|
||||
await self.client.close()
|
||||
|
||||
async def _post(self, payload: dict, *, signature: str | None = None, account: str = "personal"):
|
||||
body = json.dumps(payload).encode("utf-8")
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
"X-GitHub-Event": "push",
|
||||
"X-GitHub-Delivery": "delivery-1",
|
||||
"X-Hub-Signature-256": signature if signature is not None else _signature(body),
|
||||
}
|
||||
return await self.client.post(f"/github/{account}/-100123", data=body, headers=headers)
|
||||
|
||||
async def test_handler_accepts_valid_signature_and_sends_message(self) -> None:
|
||||
response = await self._post(_push_payload())
|
||||
|
||||
self.assertEqual(response.status, 200)
|
||||
self.telegram_client.send_message.assert_awaited_once()
|
||||
args, kwargs = self.telegram_client.send_message.await_args
|
||||
self.assertEqual(args[0], -100123)
|
||||
self.assertIn("GitHub push", args[1])
|
||||
self.assertIn("parse_mode", kwargs)
|
||||
|
||||
async def test_handler_rejects_invalid_signature(self) -> None:
|
||||
response = await self._post(_push_payload(), signature="sha256=bad")
|
||||
|
||||
self.assertEqual(response.status, 401)
|
||||
self.telegram_client.send_message.assert_not_awaited()
|
||||
|
||||
async def test_handler_rejects_unknown_account(self) -> None:
|
||||
response = await self._post(_push_payload(), account="missing")
|
||||
|
||||
self.assertEqual(response.status, 404)
|
||||
self.telegram_client.send_message.assert_not_awaited()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user