Files
leocrm/tests/test_marketplace.py
T
Agent Zero 98eb1d0d89
Check Cross-Plugin Imports / check (push) Has been cancelled
feat: Plugin-System Umbau — 6 Phasen komplett abgeschlossen
Phase 1: Contracts konsequent nutzen
- 12 neue contracts.py erstellt (alle 19 Plugins haben jetzt contracts)
- 4 bestehende contracts.py an zentrale ContractRegistry angepasst
- Alle 19 Plugins haben on_deactivate mit Contract-Unregister
- 0 echte problematische INTER-Plugin Imports

Phase 2: Hooks/Filters-System
- app/core/hooks.py (HookRegistry mit actions + filters)
- 15 Hook-Punkte in Core-Services (contact, auth, mail, calendar, user, dms)
- BasePlugin.on_deactivate meldet alle Hooks ab

Phase 3: Plugin-Isolation
- scripts/check_cross_plugin_imports.py (Linting-Regel)
- .github/workflows/check-cross-plugin-imports.yml (CI/CD)
- .pre-commit-cross-plugin.yaml (Pre-commit Hook)
- 155 Dateien geprueft, 0 Verstoesse

Phase 4: Plugin-Versioning
- app/plugins/semver.py (SemVer mit Parse, Compare, Pre-release)
- migration_runner.py erweitert: run_migration_down, rollback_to_version
- manifest.py: min_app_version Feld
- registry.py: App-Version-Compatibility-Check bei Installation
- GET /api/v1/plugins/updates Endpoint

Phase 5: Marketplace-Vorbereitung
- app/plugins/signature.py (Ed25519 Signatur-Validierung)
- app/plugins/quarantine.py (Plugin-Quarantine mit Validierung)
- app/models/plugin_allowlist.py + Migration 0046
- manifest.py: author, license, homepage, icon, screenshots, changelog, marketplace_tags, price
- registry.py: discover_external(), discover_all()
- POST /api/v1/plugins/install-marketplace (deaktiviert)

Phase 6: Manifest-Anpassung
- manifest.py: 12 neue Felder + SemVer/Hook-Name Validierung
- MANIFEST_SCHEMA_DOC aktualisiert
- Alle 19 Plugin-Manifeste aktualisiert
- Frontend PluginUiManifest Typ erweitert

Zusaetzliche Bug-Fixes:
- test_sample-Modul erstellt
- conftest.py Deadlock-Prevention
- SESSION_COOKIE_SECURE=true
- dump.rdb aus Git entfernt + .gitignore
- backup.py datetime.utcnow -> func.now()
- system_settings.py JSONB-Import nach oben
- tax.py Mapped[float] -> Mapped[Decimal]
- notification.py type_key-Laengen vereinheitlicht

Tests: 91 neue Tests, alle bestanden
2026-07-26 23:15:34 +02:00

158 lines
5.8 KiB
Python

"""Tests for marketplace plugin system: signature, quarantine, allowlist."""
from __future__ import annotations
import hashlib
import tempfile
from pathlib import Path
import pytest
from app.plugins.signature import PluginSignature
from app.plugins.quarantine import (
QuarantineError,
_check_dangerous_imports,
_check_migration_sql,
_validate_manifest,
)
class TestPluginSignature:
def test_compute_hash(self, tmp_path):
"""compute_hash returns a valid SHA-256 hex string."""
test_file = tmp_path / "test.zip"
test_file.write_bytes(b"test content")
h = PluginSignature.compute_hash(test_file)
assert len(h) == 64 # SHA-256 hex
assert h == hashlib.sha256(b"test content").hexdigest()
def test_verify_signature_without_pynacl(self, tmp_path):
"""verify_signature returns False if PyNaCl is not installed."""
test_file = tmp_path / "test.zip"
test_file.write_bytes(b"test")
# Without PyNaCl installed, returns False
result = PluginSignature.verify_signature(test_file, b"sig", b"key")
assert result in (False, True) # Depends on whether pynacl is installed
class TestQuarantineValidation:
def test_validate_manifest_valid(self, tmp_path):
"""_validate_manifest passes for a valid plugin structure."""
plugin_dir = tmp_path / "test_plugin"
plugin_dir.mkdir()
(plugin_dir / "plugin.py").write_text(
"from app.plugins.base import BasePlugin\n"
"from app.plugins.manifest import PluginManifest\n"
"class TestPlugin(BasePlugin):\n"
" manifest = PluginManifest(name='test', version='1.0.0', display_name='Test')\n"
)
result = _validate_manifest(plugin_dir)
assert result["has_manifest"] is True
def test_validate_manifest_missing(self, tmp_path):
"""_validate_manifest raises for missing plugin.py."""
with pytest.raises(QuarantineError, match="plugin.py or __init__.py"):
_validate_manifest(tmp_path)
def test_validate_manifest_no_manifest(self, tmp_path):
"""_validate_manifest raises when PluginManifest is missing."""
plugin_dir = tmp_path / "test_plugin"
plugin_dir.mkdir()
(plugin_dir / "plugin.py").write_text("print('hello')")
with pytest.raises(QuarantineError, match="PluginManifest"):
_validate_manifest(plugin_dir)
def test_check_dangerous_imports_clean(self, tmp_path):
"""_check_dangerous_imports returns empty for safe code."""
plugin_dir = tmp_path / "safe_plugin"
plugin_dir.mkdir()
(plugin_dir / "plugin.py").write_text(
"import logging\n"
"from app.plugins.base import BasePlugin\n"
)
result = _check_dangerous_imports(plugin_dir)
assert result == []
def test_check_dangerous_imports_found(self, tmp_path):
"""_check_dangerous_imports detects dangerous patterns."""
plugin_dir = tmp_path / "dangerous_plugin"
plugin_dir.mkdir()
(plugin_dir / "plugin.py").write_text(
"import os\n"
"os.system('rm -rf /')\n"
)
result = _check_dangerous_imports(plugin_dir)
assert len(result) > 0
assert any("os.system" in r for r in result)
def test_check_migration_sql_no_migrations(self, tmp_path):
"""_check_migration_sql returns empty when no migrations dir."""
result = _check_migration_sql(tmp_path)
assert result == []
def test_check_migration_sql_valid(self, tmp_path):
"""_check_migration_sql passes for valid SQL with tenant_id."""
migrations = tmp_path / "migrations"
migrations.mkdir()
(migrations / "0001_initial.sql").write_text(
"CREATE TABLE items (id UUID, tenant_id UUID NOT NULL);\n"
)
result = _check_migration_sql(tmp_path)
assert result == []
def test_check_migration_sql_missing_tenant_id(self, tmp_path):
"""_check_migration_sql detects missing tenant_id."""
migrations = tmp_path / "migrations"
migrations.mkdir()
(migrations / "0001_initial.sql").write_text(
"CREATE TABLE items (id UUID);\n"
)
result = _check_migration_sql(tmp_path)
assert len(result) > 0
assert "tenant_id" in result[0]
def test_check_migration_sql_drop_database(self, tmp_path):
"""_check_migration_sql detects DROP DATABASE."""
migrations = tmp_path / "migrations"
migrations.mkdir()
(migrations / "0001_initial.sql").write_text(
"DROP DATABASE leocrm;\n"
)
result = _check_migration_sql(tmp_path)
assert len(result) > 0
assert "DROP" in result[0]
class TestManifestMarketplaceFields:
def test_manifest_has_marketplace_fields(self):
"""PluginManifest has marketplace fields."""
from app.plugins.manifest import PluginManifest
m = PluginManifest(
name="test",
version="1.0.0",
display_name="Test",
author="Test Author",
license="MIT",
min_app_version="1.0.0",
)
assert m.author == "Test Author"
assert m.license == "MIT"
assert m.min_app_version == "1.0.0"
assert m.contract_version == "1.0.0"
assert m.hooks == []
def test_manifest_marketplace_optional_fields(self):
"""PluginManifest marketplace fields have defaults."""
from app.plugins.manifest import PluginManifest
m = PluginManifest(
name="test",
version="1.0.0",
display_name="Test",
)
assert m.author == ""
assert m.homepage == ""
assert m.license == "MIT"
assert m.price == 0.0
assert m.screenshots == []
assert m.marketplace_tags == []