Three-month batch sync from internal repo (~80 commits) covering Tracks F.5a, F.7e, F.8, F.17, F.18, F.X. WordPress media pipeline - Pillow-based optimization, AI image generation (OpenAI / Stability / Replicate / Google Nano Banana / OpenRouter), chunked + resumable uploads, bulk delete/reassign, idempotent retries. Capability discovery (F.7e) - Per-site credential probe + adapters for WordPress / WooCommerce / Gitea, tier-fit unions granted ∪ roles, capability badge UI with HTMX partial re-check, install hint in every companion-unreachable error. Companion plugin overhaul - Renamed wordpress-plugin/airano-mcp-seo-bridge → wordpress-plugin/airano-mcp-bridge. - Eight new endpoints: /capabilities, /bulk-meta, /export, /cache-purge, /transient-flush, /site-health, /audit-hook, /upload-and-attach. - wp.org Plugin Check pass: i18n, WP_Filesystem, scheme allowlist on audit-hook URL. Other - Gitea ergonomics (F.17): batch files, tree, search, compare, releases, fork. - Opportunistic bcrypt upgrade for legacy SHA-256 admin keys (F.8). - n8n refactor: structured errors, capability probe, missing tools backfilled. - Idempotency-Key dedup for AI media upload retries; WP client fast-fails on unreachable sites. Docs - README + CLAUDE.md drop the fixed "633 tools" claim. The total grows with each release; per-plugin approximations + dashboard-surfaced counts replace it. - Tools/Tests badges removed in favour of "Plugins: 10". Deployment - PyPI mirror chain, optional BUILD_HTTP_PROXY, Alpine→Yandex apk mirror, Debian-slim Plan-B Dockerfile, mirror.gcr.io variant. CI - Black + Ruff clean on Python 3.12; pytest tests/ green. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
194 lines
6.0 KiB
Python
194 lines
6.0 KiB
Python
"""F.18.5 — Tests for wordpress_transient_flush."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from unittest.mock import AsyncMock
|
|
|
|
import pytest
|
|
|
|
from plugins.wordpress.client import WordPressClient
|
|
from plugins.wordpress.handlers.transient_flush import (
|
|
TransientFlushHandler,
|
|
_validate,
|
|
get_tool_specifications,
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def wp_client():
|
|
return WordPressClient(site_url="https://wp.example.com", username="u", app_password="p")
|
|
|
|
|
|
@pytest.fixture
|
|
def handler(wp_client):
|
|
return TransientFlushHandler(wp_client)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Client-side validation.
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_validate_defaults_to_expired():
|
|
assert _validate(None, None) is None
|
|
|
|
|
|
def test_validate_expired_ok():
|
|
assert _validate("expired", None) is None
|
|
|
|
|
|
def test_validate_all_ok():
|
|
assert _validate("all", None) is None
|
|
|
|
|
|
def test_validate_pattern_requires_pattern():
|
|
err = _validate("pattern", None)
|
|
assert err is not None
|
|
assert err["error"] == "pattern_required"
|
|
|
|
|
|
def test_validate_pattern_with_glob_ok():
|
|
assert _validate("pattern", "rank_math_*") is None
|
|
|
|
|
|
def test_validate_rejects_unknown_scope():
|
|
err = _validate("demolish", None)
|
|
assert err is not None
|
|
assert err["error"] == "invalid_scope"
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Handler behaviour.
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_invalid_scope_never_hits_network(handler, wp_client, monkeypatch):
|
|
post_mock = AsyncMock()
|
|
monkeypatch.setattr(wp_client, "post", post_mock)
|
|
out = json.loads(await handler.transient_flush(scope="demolish"))
|
|
assert out["ok"] is False
|
|
assert out["error"] == "invalid_scope"
|
|
post_mock.assert_not_called()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_pattern_without_pattern_rejected_client_side(handler, wp_client, monkeypatch):
|
|
post_mock = AsyncMock()
|
|
monkeypatch.setattr(wp_client, "post", post_mock)
|
|
out = json.loads(await handler.transient_flush(scope="pattern"))
|
|
assert out["ok"] is False
|
|
assert out["error"] == "pattern_required"
|
|
post_mock.assert_not_called()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_expired_happy_path(handler, wp_client, monkeypatch):
|
|
companion_response = {
|
|
"ok": True,
|
|
"scope": "expired",
|
|
"pattern": None,
|
|
"include_site_transients": True,
|
|
"deleted_count": 42,
|
|
"deleted_sample": ["foo", "bar"],
|
|
"plugin_version": "2.5.0",
|
|
}
|
|
post_mock = AsyncMock(return_value=companion_response)
|
|
monkeypatch.setattr(wp_client, "post", post_mock)
|
|
|
|
out = json.loads(await handler.transient_flush()) # default scope
|
|
assert out["ok"] is True
|
|
assert out["scope"] == "expired"
|
|
assert out["deleted_count"] == 42
|
|
|
|
call_args = post_mock.call_args
|
|
assert call_args.args[0] == "airano-mcp/v1/transient-flush"
|
|
body = call_args.kwargs["json_data"]
|
|
assert body["scope"] == "expired"
|
|
assert body["include_site_transients"] is True
|
|
assert "pattern" not in body
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_pattern_passes_pattern_through(handler, wp_client, monkeypatch):
|
|
captured: dict = {}
|
|
|
|
async def fake_post(endpoint, json_data=None, **kwargs):
|
|
captured["body"] = json_data
|
|
return {
|
|
"ok": True,
|
|
"scope": "pattern",
|
|
"pattern": "rank_math_*",
|
|
"include_site_transients": False,
|
|
"deleted_count": 3,
|
|
"deleted_sample": ["rank_math_a", "rank_math_b", "rank_math_c"],
|
|
"plugin_version": "2.5.0",
|
|
}
|
|
|
|
monkeypatch.setattr(wp_client, "post", AsyncMock(side_effect=fake_post))
|
|
|
|
out = json.loads(
|
|
await handler.transient_flush(
|
|
scope="pattern",
|
|
pattern="rank_math_*",
|
|
include_site_transients=False,
|
|
)
|
|
)
|
|
assert out["ok"] is True
|
|
assert out["pattern"] == "rank_math_*"
|
|
assert out["deleted_count"] == 3
|
|
assert captured["body"]["pattern"] == "rank_math_*"
|
|
assert captured["body"]["include_site_transients"] is False
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_include_site_transients_bool_coercion(handler, wp_client, monkeypatch):
|
|
captured: dict = {}
|
|
|
|
async def fake_post(endpoint, json_data=None, **kwargs):
|
|
captured["body"] = json_data
|
|
return {"ok": True, "scope": "all", "deleted_count": 0}
|
|
|
|
monkeypatch.setattr(wp_client, "post", AsyncMock(side_effect=fake_post))
|
|
|
|
# String "false" should coerce to False.
|
|
await handler.transient_flush(scope="all", include_site_transients="false")
|
|
assert captured["body"]["include_site_transients"] is False
|
|
|
|
# Integer 1 should coerce to True.
|
|
await handler.transient_flush(scope="all", include_site_transients=1)
|
|
assert captured["body"]["include_site_transients"] is True
|
|
|
|
# None / default → True.
|
|
await handler.transient_flush(scope="all", include_site_transients=None)
|
|
assert captured["body"]["include_site_transients"] is True
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_companion_unreachable(handler, wp_client, monkeypatch):
|
|
monkeypatch.setattr(wp_client, "post", AsyncMock(side_effect=RuntimeError("404 Not Found")))
|
|
out = json.loads(await handler.transient_flush(scope="expired"))
|
|
assert out["ok"] is False
|
|
assert out["error"] == "companion_unreachable"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_non_dict_response(handler, wp_client, monkeypatch):
|
|
monkeypatch.setattr(wp_client, "post", AsyncMock(return_value="bad"))
|
|
out = json.loads(await handler.transient_flush(scope="expired"))
|
|
assert out["ok"] is False
|
|
assert out["error"] == "invalid_response"
|
|
|
|
|
|
def test_tool_spec_is_admin_scope():
|
|
specs = get_tool_specifications()
|
|
assert len(specs) == 1
|
|
assert specs[0]["name"] == "transient_flush"
|
|
assert specs[0]["scope"] == "admin"
|
|
assert set(specs[0]["schema"]["properties"]["scope"]["enum"]) == {
|
|
"expired",
|
|
"all",
|
|
"pattern",
|
|
}
|