Skip to content

Commit 4e6c952

Browse files
committed
Fix test imports: inline helpers in test_app.py
Move sign_payload and make_webhook_body directly into test_app.py to avoid conftest import issues in pytest collection.
1 parent 9aa8173 commit 4e6c952

2 files changed

Lines changed: 38 additions & 46 deletions

File tree

tests/conftest.py

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,7 @@
1-
import hashlib
2-
import hmac
3-
import json
4-
51
import pytest
62

73

84
@pytest.fixture(autouse=True)
95
def set_env(monkeypatch):
106
monkeypatch.setenv("WEBHOOK_SECRET", "test-secret")
117
monkeypatch.setenv("GITHUB_TOKEN", "ghp_test_token")
12-
13-
14-
@pytest.fixture
15-
def webhook_secret():
16-
return "test-secret"
17-
18-
19-
def sign_payload(body: bytes, secret: str = "test-secret") -> str:
20-
return "sha256=" + hmac.new(secret.encode(), body, digestmod=hashlib.sha256).hexdigest()
21-
22-
23-
def make_webhook_body(
24-
action="queued",
25-
job_id=12345,
26-
labels=None,
27-
repo_url="https://github.com/owner/repo",
28-
repo_full_name="owner/repo",
29-
):
30-
if labels is None:
31-
labels = ["self-hosted", "modal"]
32-
return json.dumps(
33-
{
34-
"action": action,
35-
"workflow_job": {"id": job_id, "labels": labels},
36-
"repository": {"url": repo_url, "full_name": repo_full_name},
37-
}
38-
).encode()

tests/test_app.py

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,43 @@
1+
import hashlib
2+
import hmac
13
import json
24
from unittest.mock import AsyncMock, MagicMock, patch
35

46
import httpx
57
import 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
159182
async def test_verify_signature_valid():
160183
from app import verify_signature
@@ -299,7 +322,7 @@ async def test_webhook_missing_labels(_mock_modal_obj):
299322
async 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):
312335
async 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

Comments
 (0)