File size: 5,100 Bytes
dfb775d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | # SPDX-License-Identifier: Apache-2.0
# (c) 2026 BANKON — all rights reserved.
"""Tests for WordpressAgent."""
from __future__ import annotations
from datetime import datetime, timezone
import httpx
import pytest
from wordpress_agent.agent import (
AuthenticationError,
PublishError,
WordpressAgent,
)
from wordpress_agent.config import Settings
@pytest.fixture
def settings() -> Settings:
return Settings( # type: ignore[call-arg]
base_url="https://rage.example.test",
user="codephreak",
app_password="test-pass-1234-5678",
retry_count=1,
retry_backoff=0.0,
)
@pytest.fixture
async def agent(settings: Settings):
async with WordpressAgent(settings) as a:
yield a
@pytest.mark.asyncio
async def test_publish_success(httpx_mock, agent: WordpressAgent) -> None:
httpx_mock.add_response(
method="POST",
url="https://rage.example.test/wp-json/wp/v2/posts",
json={
"id": 42,
"link": "https://rage.example.test/?p=42",
"status": "publish",
"slug": "hello-world",
"date_gmt": "2026-05-09T22:00:00",
},
status_code=201,
)
result = await agent.publish(title="Hello", content="<p>World</p>")
assert result.post_id == 42
assert result.url == "https://rage.example.test/?p=42"
assert result.status == "publish"
@pytest.mark.asyncio
async def test_publish_scheduled_requires_tz_aware_date(agent: WordpressAgent) -> None:
naive_date = datetime(2026, 6, 1, 9, 0, 0)
with pytest.raises(ValueError, match="timezone-aware"):
await agent.publish(
title="Scheduled",
content="<p>Future post</p>",
status="future",
date=naive_date,
)
@pytest.mark.asyncio
async def test_publish_empty_title_rejected(agent: WordpressAgent) -> None:
with pytest.raises(ValueError, match="title"):
await agent.publish(title=" ", content="<p>Body</p>")
@pytest.mark.asyncio
async def test_publish_empty_content_rejected(agent: WordpressAgent) -> None:
with pytest.raises(ValueError, match="content"):
await agent.publish(title="Title", content="")
@pytest.mark.asyncio
async def test_publish_authentication_failure(httpx_mock, agent: WordpressAgent) -> None:
httpx_mock.add_response(
method="POST",
url="https://rage.example.test/wp-json/wp/v2/posts",
status_code=401,
json={"code": "rest_cannot_create"},
)
with pytest.raises(AuthenticationError):
await agent.publish(title="Hello", content="<p>World</p>")
@pytest.mark.asyncio
async def test_publish_retries_on_5xx(httpx_mock, agent: WordpressAgent) -> None:
httpx_mock.add_response(
method="POST",
url="https://rage.example.test/wp-json/wp/v2/posts",
status_code=503,
)
httpx_mock.add_response(
method="POST",
url="https://rage.example.test/wp-json/wp/v2/posts",
status_code=201,
json={
"id": 7,
"link": "https://rage.example.test/?p=7",
"status": "publish",
"slug": "retry-success",
"date_gmt": "2026-05-09T22:00:00",
},
)
result = await agent.publish(title="Retry", content="<p>Body</p>")
assert result.post_id == 7
@pytest.mark.asyncio
async def test_publish_gives_up_after_max_retries(httpx_mock, agent: WordpressAgent) -> None:
httpx_mock.add_response(
method="POST",
url="https://rage.example.test/wp-json/wp/v2/posts",
status_code=503,
)
httpx_mock.add_response(
method="POST",
url="https://rage.example.test/wp-json/wp/v2/posts",
status_code=503,
)
with pytest.raises(PublishError):
await agent.publish(title="Down", content="<p>Body</p>")
@pytest.mark.asyncio
async def test_health_check_ok(httpx_mock, agent: WordpressAgent) -> None:
httpx_mock.add_response(
method="GET",
url="https://rage.example.test/wp-json/wp/v2/users/me",
status_code=200,
json={"id": 1, "name": "codephreak"},
)
result = await agent.health_check()
assert result["ok"] is True
assert result["wp_user_id"] == 1
@pytest.mark.asyncio
async def test_publish_includes_scheduled_date_in_payload(
httpx_mock, agent: WordpressAgent
) -> None:
httpx_mock.add_response(
method="POST",
url="https://rage.example.test/wp-json/wp/v2/posts",
status_code=201,
json={
"id": 99,
"link": "https://rage.example.test/?p=99",
"status": "future",
"slug": "scheduled",
"date_gmt": "2026-06-01T09:00:00",
},
)
when = datetime(2026, 6, 1, 9, 0, 0, tzinfo=timezone.utc)
await agent.publish(
title="Scheduled",
content="<p>Body</p>",
status="future",
date=when,
)
request = httpx_mock.get_request()
assert request is not None
body = request.read().decode()
assert "date_gmt" in body
assert "future" in body
|