Files
guarddog-nexus/tests/test_webhooks.py
Marker689 c4dcd79ecd fix: защита от дубликатов сканов — UPDATED-only + per-URL мьютекс
- constants.py: RELEVANT_WEBHOOK_ACTIONS теперь только UPDATED
  (CREATED игнорируется, Nexs proxy шлёт UPDATED при обновл кэша)
- harvester.py: asyncio.Lock на каждый download_url
  — при параллельных вебхуках только первый пройдёт, остальные skipped
  — lock проверяется + DB re-check внутри критической секции
- tests: обновлены фикстуры (CREATED→UPDATED), добавлен тест ignores_created
2026-05-10 05:47:35 +03:00

85 lines
3.0 KiB
Python

"""Tests for Nexus webhook receiver."""
from unittest.mock import patch
import pytest
@pytest.mark.asyncio
async def test_webhook_rejects_invalid_json(client):
resp = await client.post(
"/webhooks/nexus",
content="not json",
headers={"Content-Type": "application/json"},
)
assert resp.status_code == 400
@pytest.mark.asyncio
async def test_webhook_ignores_deleted_action(client, sample_nexus_webhook):
sample_nexus_webhook["action"] = "DELETED"
resp = await client.post("/webhooks/nexus", json=sample_nexus_webhook)
assert resp.status_code == 200
assert resp.json()["status"] == "ignored"
@pytest.mark.asyncio
async def test_webhook_ignores_created_action(client, sample_nexus_webhook):
sample_nexus_webhook["action"] = "CREATED"
resp = await client.post("/webhooks/nexus", json=sample_nexus_webhook)
assert resp.status_code == 200
assert resp.json()["status"] == "ignored"
@pytest.mark.asyncio
async def test_webhook_accepts_asset_updated(client, sample_nexus_webhook):
sample_nexus_webhook["action"] = "UPDATED"
with patch("guarddog_nexus.webhooks._scan_in_background") as _mock:
resp = await client.post("/webhooks/nexus", json=sample_nexus_webhook)
assert resp.status_code == 200
assert resp.json()["status"] == "accepted"
@pytest.mark.asyncio
async def test_webhook_skips_metadata_assets(client, sample_nexus_webhook):
sample_nexus_webhook["asset"]["name"] = "/simple/requests/"
resp = await client.post("/webhooks/nexus", json=sample_nexus_webhook)
assert resp.status_code == 200
assert resp.json()["status"] == "ignored"
@pytest.mark.asyncio
async def test_webhook_skips_non_package_extension(client, sample_nexus_webhook):
sample_nexus_webhook["asset"]["name"] = "/some/path.json"
resp = await client.post("/webhooks/nexus", json=sample_nexus_webhook)
assert resp.status_code == 200
assert resp.json()["status"] == "ignored"
@pytest.mark.asyncio
async def test_webhook_no_asset_or_component(client):
resp = await client.post(
"/webhooks/nexus",
json={"action": "CREATED", "repositoryName": "test"},
)
assert resp.status_code == 200
assert resp.json()["status"] == "ignored"
@pytest.mark.asyncio
async def test_webhook_accepts_component(client, sample_nexus_component_webhook):
with patch("guarddog_nexus.webhooks._scan_component") as _mock:
resp = await client.post("/webhooks/nexus", json=sample_nexus_component_webhook)
assert resp.status_code == 200
data = resp.json()
assert data["status"] == "accepted"
assert data["component"] == "requests==2.31.0"
@pytest.mark.asyncio
async def test_webhook_component_no_version(client, sample_nexus_component_webhook):
sample_nexus_component_webhook["component"]["version"] = ""
resp = await client.post("/webhooks/nexus", json=sample_nexus_component_webhook)
assert resp.status_code == 200
assert resp.json()["status"] == "ignored"