1+ import hashlib
2+ import hmac
13import json
24from unittest .mock import AsyncMock , MagicMock , patch
35
46import httpx
57import pytest
6- from conftest import make_webhook_body , sign_payload
8+
9+
10+ def _sign (body : bytes , secret : str = "test-secret" ) -> str :
11+ return "sha256=" + hmac .new (secret .encode (), body , digestmod = hashlib .sha256 ).hexdigest ()
12+
13+
14+ def _make_request (body : bytes , secret : str = "test-secret" , delivery_id : str = "test-delivery-123" ):
15+ request = MagicMock ()
16+ request .headers = {
17+ "Content-Type" : "application/json" ,
18+ "X-Hub-Signature-256" : _sign (body , secret ),
19+ "X-GitHub-Delivery" : delivery_id ,
20+ }
21+ request .body = AsyncMock (return_value = body )
22+ return request
23+
24+
25+ def _make_webhook_body (
26+ action = "queued" ,
27+ job_id = 12345 ,
28+ labels = None ,
29+ repo_url = "https://github.com/owner/repo" ,
30+ repo_full_name = "owner/repo" ,
31+ ):
32+ if labels is None :
33+ labels = ["self-hosted" , "modal" ]
34+ return json .dumps (
35+ {
36+ "action" : action ,
37+ "workflow_job" : {"id" : job_id , "labels" : labels },
38+ "repository" : {"url" : repo_url , "full_name" : repo_full_name },
39+ }
40+ ).encode ()
741
842# =============================================================================
943# _call_github_api retry tests
@@ -144,17 +178,6 @@ async def test_call_github_api_exhausts_retries():
144178# =============================================================================
145179
146180
147- def _make_request (body : bytes , secret : str = "test-secret" , delivery_id : str = "test-delivery-123" ):
148- request = MagicMock ()
149- request .headers = {
150- "Content-Type" : "application/json" ,
151- "X-Hub-Signature-256" : sign_payload (body , secret ),
152- "X-GitHub-Delivery" : delivery_id ,
153- }
154- request .body = AsyncMock (return_value = body )
155- return request
156-
157-
158181@pytest .mark .asyncio
159182async def test_verify_signature_valid ():
160183 from app import verify_signature
@@ -299,7 +322,7 @@ async def test_webhook_missing_labels(_mock_modal_obj):
299322async def test_webhook_ignores_non_modal_labels (_mock_modal_obj ):
300323 from app import github_webhook
301324
302- body = make_webhook_body (labels = ["self-hosted" , "ubuntu" ])
325+ body = _make_webhook_body (labels = ["self-hosted" , "ubuntu" ])
303326 request = _make_request (body )
304327
305328 result = await github_webhook (request )
@@ -312,7 +335,7 @@ async def test_webhook_ignores_non_modal_labels(_mock_modal_obj):
312335async def test_webhook_ignores_non_queued_action (_mock_modal_obj ):
313336 from app import github_webhook
314337
315- body = make_webhook_body (action = "started" )
338+ body = _make_webhook_body (action = "started" )
316339 request = _make_request (body )
317340
318341 result = await github_webhook (request )
@@ -342,7 +365,7 @@ async def test_webhook_rejects_invalid_repo_url(_mock_modal_obj):
342365
343366 from app import github_webhook
344367
345- body = make_webhook_body (repo_url = "https://evil.com/fake" )
368+ body = _make_webhook_body (repo_url = "https://evil.com/fake" )
346369 request = _make_request (body )
347370
348371 with pytest .raises (HTTPException ) as exc_info :
0 commit comments