Skip to content

Commit c0c9dc1

Browse files
hiroshinishioclaude
andcommitted
Fix test_main webhook test and add unique delivery IDs
- Add unique random delivery IDs to mock_github_request fixture to prevent duplicate webhook detection - Update test assertions to handle delivery_id in lambda_info - Fix pyright error by using getattr() instead of direct attribute access on route.path - Grant sequence permissions in dev database to match production 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent e620abf commit c0c9dc1

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

test_main.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Standard imports
44
import asyncio
55
import json
6+
import random
67
import urllib.parse
78
from unittest.mock import AsyncMock, MagicMock, call, patch
89

@@ -22,7 +23,10 @@
2223
def mock_github_request():
2324
"""Create a mock request object for testing."""
2425
mock_req = MagicMock(spec=Request)
25-
mock_req.headers = {"X-GitHub-Event": "push"}
26+
mock_req.headers = {
27+
"X-GitHub-Event": "push",
28+
"X-GitHub-Delivery": f"test-delivery-{random.randint(1000000, 9999999)}",
29+
}
2630
mock_req.body = AsyncMock(return_value=b'{"key": "value"}')
2731
return mock_req
2832

@@ -222,6 +226,7 @@ def test_handler_without_trigger_type(self, mock_mangum_handler):
222226

223227

224228
class TestHandleWebhook:
229+
@patch("main.insert_webhook_delivery")
225230
@patch("main.extract_lambda_info")
226231
@patch("main.verify_webhook_signature", new_callable=AsyncMock)
227232
@patch("main.handle_webhook_event", new_callable=AsyncMock)
@@ -231,10 +236,12 @@ async def test_handle_webhook_success(
231236
mock_handle_webhook_event,
232237
mock_verify_signature,
233238
mock_extract_lambda_info,
239+
mock_insert_webhook_delivery,
234240
mock_github_request,
235241
):
236242
"""Test handle_webhook function with successful execution."""
237243
# Setup
244+
mock_insert_webhook_delivery.return_value = True
238245
mock_verify_signature.return_value = None
239246
mock_handle_webhook_event.return_value = None
240247
mock_extract_lambda_info.return_value = {
@@ -251,15 +258,21 @@ async def test_handle_webhook_success(
251258
request=mock_github_request, secret=GITHUB_WEBHOOK_SECRET
252259
)
253260
mock_extract_lambda_info.assert_called_once_with(mock_github_request)
254-
mock_handle_webhook_event.assert_called_once_with(
255-
event_name="push",
256-
payload={"key": "value"},
257-
lambda_info={
258-
"log_group": "/aws/lambda/pr-agent-prod",
259-
"log_stream": "2025/09/04/pr-agent-prod[$LATEST]841315c5",
260-
"request_id": "17921070-5cb6-43ee-8d2e-b5161ae89729",
261-
},
261+
call_args = mock_handle_webhook_event.call_args
262+
assert call_args.kwargs["event_name"] == "push"
263+
assert call_args.kwargs["payload"] == {"key": "value"}
264+
assert (
265+
call_args.kwargs["lambda_info"]["log_group"] == "/aws/lambda/pr-agent-prod"
266+
)
267+
assert (
268+
call_args.kwargs["lambda_info"]["log_stream"]
269+
== "2025/09/04/pr-agent-prod[$LATEST]841315c5"
270+
)
271+
assert (
272+
call_args.kwargs["lambda_info"]["request_id"]
273+
== "17921070-5cb6-43ee-8d2e-b5161ae89729"
262274
)
275+
assert "delivery_id" in call_args.kwargs["lambda_info"]
263276
assert response == {"message": "Webhook processed successfully"}
264277

265278
@patch("main.extract_lambda_info")
@@ -607,7 +620,9 @@ def test_mangum_handler_instance(self):
607620

608621
def test_app_routes_configuration(self):
609622
"""Test that the FastAPI app has the expected route paths."""
610-
route_paths = [route.path for route in app.routes if hasattr(route, "path")]
623+
route_paths = [
624+
getattr(route, "path") for route in app.routes if hasattr(route, "path")
625+
]
611626

612627
# Check that expected routes exist
613628
assert "/" in route_paths

0 commit comments

Comments
 (0)