feat: guarddog-nexus — webhook-based PyPI scanner with web UI
This commit is contained in:
131
tests/conftest.py
Normal file
131
tests/conftest.py
Normal file
@@ -0,0 +1,131 @@
|
||||
"""Test fixtures for guarddog-nexus."""
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
from httpx import ASGITransport, AsyncClient
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
||||
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
|
||||
|
||||
os.environ["DATABASE_PATH"] = ":memory:"
|
||||
os.environ["NEXUS_URL"] = "http://nexus:8081"
|
||||
os.environ["NEXUS_USERNAME"] = "admin"
|
||||
os.environ["NEXUS_PASSWORD"] = "admin123"
|
||||
os.environ["LOG_SYSLOG_HOST"] = ""
|
||||
os.environ["TEMP_DIR"] = "/tmp/guarddog-nexus-test"
|
||||
|
||||
from guarddog_nexus.database import Base, get_session # noqa: E402
|
||||
from guarddog_nexus.main import app # noqa: E402
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def db_engine():
|
||||
engine = create_async_engine("sqlite+aiosqlite:///file:guarddog_test?mode=memory&cache=shared&uri=true")
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
yield engine
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.drop_all)
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def db_session(db_engine):
|
||||
maker = async_sessionmaker(db_engine, class_=AsyncSession, expire_on_commit=False)
|
||||
async with maker() as session:
|
||||
yield session
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def client(db_engine):
|
||||
maker = async_sessionmaker(db_engine, class_=AsyncSession, expire_on_commit=False)
|
||||
|
||||
async def override_get_session():
|
||||
async with maker() as session:
|
||||
yield session
|
||||
|
||||
app.dependency_overrides[get_session] = override_get_session
|
||||
|
||||
transport = ASGITransport(app=app)
|
||||
async with AsyncClient(transport=transport, base_url="http://test") as ac:
|
||||
yield ac
|
||||
|
||||
app.dependency_overrides.clear()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sample_nexus_webhook():
|
||||
return {
|
||||
"timestamp": "2026-05-09T12:00:00.000+00:00",
|
||||
"nodeId": "test-node",
|
||||
"initiator": "admin",
|
||||
"action": "CREATED",
|
||||
"repositoryName": "pypi-proxy",
|
||||
"asset": {
|
||||
"name": "requests-2.31.0.tar.gz",
|
||||
"format": "pypi",
|
||||
"path": "packages/requests/2.31.0/requests-2.31.0.tar.gz",
|
||||
"downloadUrl": "http://nexus:8081/repository/pypi-proxy/packages/requests/2.31.0/requests-2.31.0.tar.gz",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def guarddog_output_clean():
|
||||
return {
|
||||
"results": [],
|
||||
"errors": [],
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def guarddog_output_flagged():
|
||||
return {
|
||||
"results": [
|
||||
{
|
||||
"rule": "shady-links",
|
||||
"severity": "WARNING",
|
||||
"message": "Package contains URL to suspicious domain",
|
||||
"location": "setup.py:15",
|
||||
},
|
||||
{
|
||||
"rule": "exec-base64",
|
||||
"severity": "ERROR",
|
||||
"message": "Base64-encoded code execution detected",
|
||||
"location": "core.py:42",
|
||||
},
|
||||
],
|
||||
"errors": [],
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def guarddog_normalized_flagged():
|
||||
return {
|
||||
"findings": [
|
||||
{
|
||||
"rule": "shady-links",
|
||||
"severity": "WARNING",
|
||||
"message": "Suspicious URL",
|
||||
"location": "setup.py:15",
|
||||
},
|
||||
{
|
||||
"rule": "exec-base64",
|
||||
"severity": "ERROR",
|
||||
"message": "Base64 exec",
|
||||
"location": "core.py:42",
|
||||
},
|
||||
],
|
||||
"errors": [],
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def guarddog_normalized_clean():
|
||||
return {
|
||||
"findings": [],
|
||||
"errors": [],
|
||||
}
|
||||
Reference in New Issue
Block a user