"""Tests for the WordPress global plugin library service + API.""" import os import shutil import pytest from app.services import wordpress_bridge PLUGIN_PHP = """ target = os.path.join(site.application.root_path, 'wp-content', 'plugins', 'my-plugin', 'my-plugin.php') assert os.path.isfile(target) # Installation row recorded row = WordPressSitePlugin.query.filter_by( wordpress_site_id=site.id, custom_plugin_id=plugin.id).first() assert row is not None and row.status == 'active' assert ['plugin', 'activate', 'my-plugin'] in calls def test_scan_site_tags_managed(app, tmp_path, monkeypatch): S = wordpress_bridge.get('wordpress_plugin_library_service', 'WordPressPluginLibraryService') WordPressService = wordpress_bridge.get('wordpress_service', 'WordPressService') monkeypatch.setattr(S, 'CACHE_DIR', str(tmp_path / 'cache')) src = _make_local_plugin(tmp_path, 'my-plugin') added = S.add_plugin({'source_type': 'local', 'source_url': src, 'slug': 'my-plugin'}) from app.models import WordPressCustomPlugin plugin = WordPressCustomPlugin.query.get(added['id']) _, site = _make_site(app, tmp_path) # WP-CLI reports the plugin installed & active, at an older version. monkeypatch.setattr(WordPressService, 'get_plugins', classmethod(lambda cls, path: [ {'name': 'my-plugin', 'status': 'active', 'version': '1.0.0'}, {'name': 'akismet', 'status': 'inactive', 'version': '5.0'}, ])) res = S.scan_site(site) assert 'my-plugin' in res['managed_slugs'] managed = S.managed_for_site(site) entry = next(m for m in managed if m['slug'] == 'my-plugin') assert entry['installed_version'] == '1.0.0' assert entry['library_version'] == '1.2.3' assert entry['update_available'] is True # -------------------------------------------------------------------------- # API: CRUD + bulk update # -------------------------------------------------------------------------- def test_api_library_crud(app, client, auth_headers, tmp_path, monkeypatch): S = wordpress_bridge.get('wordpress_plugin_library_service', 'WordPressPluginLibraryService') monkeypatch.setattr(S, 'CACHE_DIR', str(tmp_path / 'cache')) src = _make_local_plugin(tmp_path, 'api-plugin') # Create r = client.post('/api/v1/wordpress/plugins/library', json={'source_type': 'local', 'source_url': src, 'slug': 'api-plugin'}, headers=auth_headers) assert r.status_code == 201, r.get_json() pid = r.get_json()['id'] # List r = client.get('/api/v1/wordpress/plugins/library', headers=auth_headers) assert r.status_code == 200 assert any(p['slug'] == 'api-plugin' for p in r.get_json()['plugins']) # Detail r = client.get(f'/api/v1/wordpress/plugins/library/{pid}', headers=auth_headers) assert r.status_code == 200 assert r.get_json()['version'] == '1.2.3' # Delete r = client.delete(f'/api/v1/wordpress/plugins/library/{pid}', headers=auth_headers) assert r.status_code == 200 r = client.get(f'/api/v1/wordpress/plugins/library/{pid}', headers=auth_headers) assert r.status_code == 404 def test_api_add_requires_source(app, client, auth_headers, tmp_path, monkeypatch): S = wordpress_bridge.get('wordpress_plugin_library_service', 'WordPressPluginLibraryService') monkeypatch.setattr(S, 'CACHE_DIR', str(tmp_path / 'cache')) r = client.post('/api/v1/wordpress/plugins/library', json={'source_type': 'github'}, headers=auth_headers) assert r.status_code == 400 def test_api_bulk_update(app, client, auth_headers, tmp_path, monkeypatch): mod = wordpress_bridge.load('wordpress_plugin_library_service') S = wordpress_bridge.get('wordpress_plugin_library_service', 'WordPressPluginLibraryService') WordPressService = wordpress_bridge.get('wordpress_service', 'WordPressService') monkeypatch.setattr(S, 'CACHE_DIR', str(tmp_path / 'cache')) monkeypatch.setattr(mod, 'run_privileged', _fake_privileged) monkeypatch.setattr(WordPressService, 'wp_cli', classmethod(lambda cls, path, cmd, **kw: {'success': True})) src = _make_local_plugin(tmp_path, 'bulk-plugin') added = S.add_plugin({'source_type': 'local', 'source_url': src, 'slug': 'bulk-plugin'}) from app.models import WordPressCustomPlugin plugin = WordPressCustomPlugin.query.get(added['id']) _, site = _make_site(app, tmp_path, name='bulk-site') S.install_on_site(plugin, site, activate=False) r = client.post(f'/api/v1/wordpress/plugins/library/{added["id"]}/bulk-update', headers=auth_headers) assert r.status_code == 200 body = r.get_json() assert body['total'] == 1 assert body['updated'] == 1