Files
ServerKit/backend/tests/test_wp_import_zip.py
T
Juan DenisandClaude Opus 4.8 a55e50bc16 feat(extensions): Phase 5 — extract WordPress into serverkit-wordpress (backend)
The largest single move in the plan (#38). The entire WordPress backend leaves
core for the bundled, default-installed serverkit-wordpress extension (D4).

Moved (12 files → builtin-extensions/serverkit-wordpress/backend/, intra-extension
imports made relative, core imports stay absolute):
  api/wordpress.py, api/wordpress_sites.py, api/environment_pipeline.py,
  wordpress_service.py (2676 lines), git_wordpress_service, wordpress_env_service,
  wordpress_plugin_library_service, wp_analytics/reports/security/update/vulnerability.

Wiring:
- Blueprints keep /api/v1/wordpress, /wordpress/projects and the /wordpress/pipelines
  alias (D9) via new manifest `extra_blueprints` support (3 blueprints / 4 mounts).
- Flagship (D4): seeded active on every panel (fresh + upgrade), uninstallable,
  never deletes tracked builtin/pre-bundled files; the `wordpress` module toggle is
  retired (MODULES now empty) — the plugin status guard is the gate.
- Loadability: the dashed package app.plugins.serverkit-wordpress is registered
  in-place via spec_from_file_location (no copy) — uniform across prod/dev/test.
- The 11 WP test files re-pointed through app.services.wordpress_bridge; the 6 lazy
  core callers were re-pointed in the prior commit.
- _regenerate_frontend_manifest() is a no-op under testing so the flagship boot
  seed can't clobber the checked-in plugins-manifest.json.

Deviations (documented, matching the Email precedent):
- WordPress *models* stay core — status_page/environment_activity/promotion_job hold
  FKs into wordpress_sites, so relocation would be a breaking data migration.
- Event catalog + Fail2ban WP filter stay core as two-speed items (extension calls
  them via core APIs); inverting adds risk without blocking the move.
- Frontend UI stays core this session (it lives in a TabGroupLayout — the tab-grouped
  conversion Phase 1 deliberately deferred); the extension is backend-only, so there
  is no route duplication. Frontend contribution is the scoped follow-up.

Verified: clean collection across all 1223 tests; 199 passed / 2 skipped / 0 failed
across the WP + plugin/extension + module + backup + core-caller parity set, incl.
the new tests/test_wordpress_extraction.py proving the move.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-02 00:15:10 -04:00

133 lines
5.3 KiB
Python

"""Tests for #17 wp-content zip import — zip-slip safety, wp-content resolution,
and the container-copy flow (docker cp mocked; extraction/resolution is real)."""
import os
import zipfile
from app.services import wordpress_bridge
def _make_zip(path, entries):
"""entries: {arcname: content_str}."""
with zipfile.ZipFile(path, 'w') as zf:
for arc, content in entries.items():
zf.writestr(arc, content)
# ---- zip-slip safety -------------------------------------------------------
def test_safe_extract_rejects_parent_traversal(tmp_path):
WordPressService = wordpress_bridge.get('wordpress_service', 'WordPressService')
z = tmp_path / 'evil.zip'
_make_zip(str(z), {'../escape.txt': 'x', 'wp-content/ok.txt': 'y'})
dest = tmp_path / 'out'
dest.mkdir()
res = WordPressService._safe_extract_zip(str(z), str(dest))
assert res['success'] is False and 'Unsafe path' in res['error']
assert not (tmp_path / 'escape.txt').exists() # nothing escaped the dest
def test_safe_extract_rejects_deep_traversal(tmp_path):
WordPressService = wordpress_bridge.get('wordpress_service', 'WordPressService')
z = tmp_path / 'evil2.zip'
_make_zip(str(z), {'wp-content/../../../../tmp/evil.txt': 'x'})
dest = tmp_path / 'out2'
dest.mkdir()
res = WordPressService._safe_extract_zip(str(z), str(dest))
assert res['success'] is False
def test_safe_extract_ok(tmp_path):
WordPressService = wordpress_bridge.get('wordpress_service', 'WordPressService')
z = tmp_path / 'good.zip'
_make_zip(str(z), {'wp-content/plugins/p/p.php': '<?php',
'wp-content/themes/t/style.css': 'body{}'})
dest = tmp_path / 'out3'
dest.mkdir()
res = WordPressService._safe_extract_zip(str(z), str(dest))
assert res['success'] is True
assert (dest / 'wp-content' / 'plugins' / 'p' / 'p.php').exists()
def test_safe_extract_rejects_bad_zip(tmp_path):
WordPressService = wordpress_bridge.get('wordpress_service', 'WordPressService')
bad = tmp_path / 'notazip.zip'
bad.write_text('this is not a zip')
dest = tmp_path / 'out4'
dest.mkdir()
res = WordPressService._safe_extract_zip(str(bad), str(dest))
assert res['success'] is False and 'valid zip' in res['error']
# ---- wp-content resolution (3 archive layouts) -----------------------------
def test_resolve_wp_content_at_root(tmp_path):
WordPressService = wordpress_bridge.get('wordpress_service', 'WordPressService')
(tmp_path / 'wp-content' / 'plugins').mkdir(parents=True)
assert WordPressService._resolve_wp_content_dir(str(tmp_path)) == str(tmp_path / 'wp-content')
def test_resolve_full_site_wrapper_folder(tmp_path):
WordPressService = wordpress_bridge.get('wordpress_service', 'WordPressService')
(tmp_path / 'mysite' / 'wp-content' / 'themes').mkdir(parents=True)
assert WordPressService._resolve_wp_content_dir(str(tmp_path)) == str(tmp_path / 'mysite' / 'wp-content')
def test_resolve_root_is_wp_content(tmp_path):
WordPressService = wordpress_bridge.get('wordpress_service', 'WordPressService')
(tmp_path / 'plugins').mkdir()
(tmp_path / 'themes').mkdir()
assert WordPressService._resolve_wp_content_dir(str(tmp_path)) == str(tmp_path)
def test_resolve_none_when_unrecognizable(tmp_path):
WordPressService = wordpress_bridge.get('wordpress_service', 'WordPressService')
(tmp_path / 'random').mkdir()
assert WordPressService._resolve_wp_content_dir(str(tmp_path)) is None
# ---- container copy flow (docker cp mocked) --------------------------------
def test_import_wp_content_zip_issues_cp_and_chown(tmp_path, monkeypatch):
WordPressService = wordpress_bridge.get('wordpress_service', 'WordPressService')
wp_mod = wordpress_bridge.load('wordpress_service')
z = tmp_path / 'c.zip'
_make_zip(str(z), {'wp-content/plugins/p/p.php': '<?php'})
calls = []
class _Res:
returncode = 0
stderr = ''
stdout = ''
monkeypatch.setattr(wp_mod.subprocess, 'run', lambda cmd, **kw: (calls.append(list(cmd)), _Res())[1])
res = WordPressService._import_wp_content_zip(str(z), 'mysite')
assert res['success'] is True
assert any(c[:2] == ['docker', 'cp'] for c in calls)
assert any(c[:3] == ['docker', 'exec', 'mysite'] for c in calls) # ownership fix
def test_import_wp_content_zip_propagates_cp_failure(tmp_path, monkeypatch):
WordPressService = wordpress_bridge.get('wordpress_service', 'WordPressService')
wp_mod = wordpress_bridge.load('wordpress_service')
z = tmp_path / 'c.zip'
_make_zip(str(z), {'wp-content/uploads/x.jpg': 'data'})
class _Res:
returncode = 1
stderr = 'no such container'
stdout = ''
monkeypatch.setattr(wp_mod.subprocess, 'run', lambda cmd, **kw: _Res())
res = WordPressService._import_wp_content_zip(str(z), 'ghost')
assert res['success'] is False and 'no such container' in res['error']
def test_import_wp_content_zip_no_wp_content_found(tmp_path):
WordPressService = wordpress_bridge.get('wordpress_service', 'WordPressService')
z = tmp_path / 'empty.zip'
_make_zip(str(z), {'readme.txt': 'hello'})
# No subprocess mock needed: resolution fails before any docker call.
res = WordPressService._import_wp_content_zip(str(z), 'site')
assert res['success'] is False and 'No wp-content' in res['error']