Skip to content

Enterprise agent platform for building, deploying, and operating AI teammates.

26.6 KB
Raw Blame
import json
from pathlib import Path
from unittest.mock import Mock

import pytest
import requests

from xagent.web.tools.mcp import facebook


class MockResponse:
    def __init__(self, json_data=None, text="", status_code=200):
        self._json_data = json_data or {}
        self.text = text
        self.status_code = status_code
        self.content = text.encode("utf-8") if text else b"{}"

    def json(self):
        return self._json_data

    def raise_for_status(self):
        if self.status_code >= 400:
            error = requests.HTTPError(f"HTTP {self.status_code}")
            error.response = self
            raise error


def _payload(result: str):
    return json.loads(result)


def test_graph_path_builds_encoded_path():
    assert facebook._graph_path("page-1", "page_id", "feed") == "/page-1/feed"
    assert facebook._graph_path(" page/1 ", "page_id", "feed") == "/page%2F1/feed"


def test_graph_path_rejects_missing_value():
    for missing in (None, "", "   ", 0):
        try:
            facebook._graph_path(missing, "page_id", "feed")
        except ValueError as exc:
            assert "page_id is required" in str(exc)
        else:
            raise AssertionError(f"expected ValueError for {missing!r}")


def test_auth_status_uses_injected_meta_token(monkeypatch):
    monkeypatch.setenv("META_ACCESS_TOKEN", "user-token")
    mock_request = Mock(
        return_value=MockResponse({"id": "user-1", "name": "Alice Meta"})
    )
    monkeypatch.setattr(facebook.requests, "request", mock_request)

    result = _payload(facebook.facebook_auth_status())

    assert result == {
        "status": "success",
        "authenticated": True,
        "user": {"id": "user-1", "name": "Alice Meta", "email": None},
    }
    mock_request.assert_called_once_with(
        method="GET",
        url="https://graph.facebook.com/v25.0/me",
        headers={
            "Authorization": "Bearer user-token",
            "Accept": "application/json",
        },
        params={"fields": "id,name,email"},
        timeout=30,
    )


def test_list_pages_hides_page_access_tokens(monkeypatch):
    monkeypatch.setenv("META_ACCESS_TOKEN", "user-token")
    mock_request = Mock(
        return_value=MockResponse(
            {
                "data": [
                    {
                        "id": "page-1",
                        "name": "Launch Page",
                        "category": "Software",
                        "tasks": ["CREATE_CONTENT"],
                        "access_token": "page-token",
                    }
                ]
            }
        )
    )
    monkeypatch.setattr(facebook.requests, "request", mock_request)

    result = _payload(facebook.facebook_list_pages())

    assert result == {
        "status": "success",
        "pages": [
            {
                "id": "page-1",
                "name": "Launch Page",
                "category": "Software",
                "tasks": ["CREATE_CONTENT"],
                "has_access_token": True,
            }
        ],
    }
    assert mock_request.call_args.kwargs["url"] == (
        "https://graph.facebook.com/v25.0/me/accounts"
    )
    assert mock_request.call_args.kwargs["params"] == {
        "fields": "id,name,category,tasks,access_token"
    }


def test_list_pages_treats_non_list_data_as_empty(monkeypatch):
    monkeypatch.setenv("META_ACCESS_TOKEN", "user-token")
    monkeypatch.setattr(
        facebook.requests,
        "request",
        Mock(return_value=MockResponse({"data": None})),
    )

    result = _payload(facebook.facebook_list_pages())

    assert result == {"status": "success", "pages": []}


def test_list_page_posts_uses_page_access_token(monkeypatch):
    monkeypatch.setenv("META_ACCESS_TOKEN", "user-token")

    def request(method, url, **kwargs):
        if url.endswith("/me/accounts"):
            return MockResponse(
                {
                    "data": [
                        {
                            "id": "page-1",
                            "name": "Launch Page",
                            "access_token": "page-token",
                        }
                    ]
                }
            )
        assert method == "GET"
        assert url == "https://graph.facebook.com/v25.0/page-1/feed"
        assert kwargs["headers"]["Authorization"] == "Bearer page-token"
        assert kwargs["params"] == {
            "fields": (
                "id,message,created_time,permalink_url,full_picture,status_type,"
                "likes.limit(0).summary(true),comments.limit(0).summary(true),shares"
            ),
            "limit": 5,
        }
        return MockResponse(
            {
                "data": [{"id": "post-1", "message": "hello"}],
                "paging": {"next": "https://graph.facebook.com/next"},
            }
        )

    monkeypatch.setattr(facebook.requests, "request", Mock(side_effect=request))

    result = _payload(facebook.facebook_list_page_posts("page-1", limit=5))

    assert result == {
        "status": "success",
        "posts": [{"id": "post-1", "message": "hello"}],
        "next_link": "https://graph.facebook.com/next",
        "engagement_available": True,
    }


def test_list_page_posts_falls_back_without_engagement_on_permission_error(
    monkeypatch,
):
    """The Graph API can reject the whole request when a field in a combined
    fields= expansion needs a permission the token doesn't have (e.g.
    pages_read_user_content missing for comments.summary). Must degrade to
    posts without engagement counts rather than failing outright.
    """
    monkeypatch.setenv("META_ACCESS_TOKEN", "user-token")
    feed_calls = []

    def request(method, url, **kwargs):
        if url.endswith("/me/accounts"):
            return MockResponse(
                {"data": [{"id": "page-1", "access_token": "page-token"}]}
            )
        assert url == "https://graph.facebook.com/v25.0/page-1/feed"
        feed_calls.append(kwargs["params"]["fields"])
        if "comments" in kwargs["params"]["fields"]:
            return MockResponse(
                {"error": {"message": "permission denied", "code": 10}},
                status_code=400,
            )
        assert kwargs["params"]["fields"] == facebook._POST_FIELDS_BASE
        return MockResponse({"data": [{"id": "post-1", "message": "hello"}]})

    monkeypatch.setattr(facebook.requests, "request", Mock(side_effect=request))

    result = _payload(facebook.facebook_list_page_posts("page-1", limit=5))

    assert result == {
        "status": "success",
        "posts": [{"id": "post-1", "message": "hello"}],
        "next_link": None,
        "engagement_available": False,
    }
    assert len(feed_calls) == 2


def test_list_page_posts_does_not_mask_non_permission_errors(monkeypatch):
    """Only a permission/OAuth-scope error should trigger the engagement
    fallback. A transient or rate-limit failure on the first request must
    surface as a genuine error instead of being silently reported as
    engagement_available=False.
    """
    monkeypatch.setenv("META_ACCESS_TOKEN", "user-token")
    feed_calls = []

    def request(method, url, **kwargs):
        if url.endswith("/me/accounts"):
            return MockResponse(
                {"data": [{"id": "page-1", "access_token": "page-token"}]}
            )
        assert url == "https://graph.facebook.com/v25.0/page-1/feed"
        feed_calls.append(kwargs["params"]["fields"])
        return MockResponse(
            {"error": {"message": "rate limited", "code": 4}},
            status_code=400,
        )

    monkeypatch.setattr(facebook.requests, "request", Mock(side_effect=request))

    result = _payload(facebook.facebook_list_page_posts("page-1", limit=5))

    assert result["status"] == "error"
    assert len(feed_calls) == 1


def test_list_post_comments_uses_page_access_token(monkeypatch):
    monkeypatch.setenv("META_ACCESS_TOKEN", "user-token")

    def request(method, url, **kwargs):
        if url.endswith("/me/accounts"):
            return MockResponse(
                {
                    "data": [
                        {
                            "id": "page-1",
                            "name": "Launch Page",
                            "access_token": "page-token",
                        }
                    ]
                }
            )
        assert method == "GET"
        assert url == "https://graph.facebook.com/v25.0/page-1_post-1/comments"
        assert kwargs["headers"]["Authorization"] == "Bearer page-token"
        assert kwargs["params"] == {
            "fields": "id,message,created_time,from,parent",
            "filter": "stream",
            "limit": 5,
        }
        return MockResponse(
            {
                "data": [
                    {
                        "id": "comment-1",
                        "message": "Great news!",
                        "from": {"id": "fan-1", "name": "A Fan"},
                    }
                ],
                "paging": {"next": "https://graph.facebook.com/next"},
            }
        )

    monkeypatch.setattr(facebook.requests, "request", Mock(side_effect=request))

    result = _payload(
        facebook.facebook_list_post_comments("page-1", "page-1_post-1", limit=5)
    )

    assert result == {
        "status": "success",
        "comments": [
            {
                "id": "comment-1",
                "message": "Great news!",
                "from": {"id": "fan-1", "name": "A Fan"},
            }
        ],
        "next_link": "https://graph.facebook.com/next",
    }


def test_publish_text_post_uses_page_access_token_and_message_payload(monkeypatch):
    monkeypatch.setenv("META_ACCESS_TOKEN", "user-token")

    def request(method, url, **kwargs):
        if url.endswith("/me/accounts"):
            return MockResponse(
                {"data": [{"id": "page-1", "access_token": "page-token"}]}
            )
        assert method == "POST"
        assert url == "https://graph.facebook.com/v25.0/page-1/feed"
        assert kwargs["headers"] == {
            "Authorization": "Bearer page-token",
            "Accept": "application/json",
            "Content-Type": "application/x-www-form-urlencoded",
        }
        assert kwargs["data"] == {"message": "Launch update"}
        return MockResponse({"id": "page-1_post-1"})

    monkeypatch.setattr(facebook.requests, "request", Mock(side_effect=request))

    result = _payload(facebook.facebook_publish_text_post("page-1", "Launch update"))

    assert result == {
        "status": "success",
        "post_id": "page-1_post-1",
    }


def test_publish_image_post_uses_public_url_payload(monkeypatch):
    monkeypatch.setenv("META_ACCESS_TOKEN", "user-token")

    def request(method, url, **kwargs):
        if url.endswith("/me/accounts"):
            return MockResponse(
                {"data": [{"id": "page-1", "access_token": "page-token"}]}
            )
        assert method == "POST"
        assert url == "https://graph.facebook.com/v25.0/page-1/photos"
        assert kwargs["headers"]["Authorization"] == "Bearer page-token"
        assert kwargs["data"] == {
            "url": "https://example.com/image.png",
            "published": "true",
            "caption": "Launch visual",
        }
        return MockResponse({"id": "photo-1", "post_id": "page-1_post-1"})

    monkeypatch.setattr(facebook.requests, "request", Mock(side_effect=request))

    result = _payload(
        facebook.facebook_publish_image_post(
            "page-1", "https://example.com/image.png", caption="Launch visual"
        )
    )

    assert result == {
        "status": "success",
        "photo_id": "photo-1",
        "post_id": "page-1_post-1",
    }


def test_publish_image_post_uploads_allowed_workspace_file(monkeypatch, tmp_path: Path):
    monkeypatch.setenv("META_ACCESS_TOKEN", "user-token")
    monkeypatch.setenv("XAGENT_FACEBOOK_IMAGE_ALLOWED_DIRS", str(tmp_path))
    image_path = tmp_path / "launch.png"
    image_path.write_bytes(b"not-empty-image-fixture")

    def request(method, url, **kwargs):
        if url.endswith("/me/accounts"):
            return MockResponse(
                {"data": [{"id": "page-1", "access_token": "page-token"}]}
            )
        assert method == "POST"
        assert url == "https://graph.facebook.com/v25.0/page-1/photos"
        assert kwargs["headers"] == {
            "Authorization": "Bearer page-token",
            "Accept": "application/json",
        }
        assert kwargs["data"] == {
            "published": "false",
            "caption": "Launch visual",
        }
        filename, content, media_type = kwargs["files"]["source"]
        assert filename == "launch.png"
        assert content == b"not-empty-image-fixture"
        assert media_type == "image/png"
        return MockResponse({"id": "photo-1", "post_id": "page-1_post-1"})

    monkeypatch.setattr(facebook.requests, "request", Mock(side_effect=request))

    result = _payload(
        facebook.facebook_publish_image_post(
            "page-1",
            caption="Launch visual",
            published=False,
            file_path=str(image_path),
        )
    )

    assert result == {
        "status": "success",
        "photo_id": "photo-1",
        "post_id": "page-1_post-1",
    }


def test_publish_image_post_requires_exactly_one_source(monkeypatch, tmp_path: Path):
    monkeypatch.setenv("META_ACCESS_TOKEN", "user-token")
    mock_request = Mock()
    monkeypatch.setattr(facebook.requests, "request", mock_request)

    missing = _payload(facebook.facebook_publish_image_post("page-1"))
    duplicate = _payload(
        facebook.facebook_publish_image_post(
            "page-1",
            "https://example.com/image.png",
            file_path=str(tmp_path / "image.png"),
        )
    )

    expected = {
        "status": "error",
        "message": "Provide exactly one of image_url or file_path",
    }
    assert missing == expected
    assert duplicate == expected
    mock_request.assert_not_called()


def test_publish_image_post_rejects_outside_paths_before_existence_check(
    monkeypatch, tmp_path: Path
):
    allowed = tmp_path / "allowed"
    allowed.mkdir()
    outside = tmp_path / "outside.png"
    monkeypatch.setenv("XAGENT_FACEBOOK_IMAGE_ALLOWED_DIRS", str(allowed))

    missing = _payload(
        facebook.facebook_publish_image_post("page-1", file_path=str(outside))
    )
    outside.write_bytes(b"image")
    existing = _payload(
        facebook.facebook_publish_image_post("page-1", file_path=str(outside))
    )

    assert (
        missing
        == existing
        == {
            "status": "error",
            "message": "Image file is outside the allowed directories",
        }
    )


def test_publish_image_post_rejects_traversal_and_symlink_escape(
    monkeypatch, tmp_path: Path
):
    allowed = tmp_path / "allowed"
    allowed.mkdir()
    outside = tmp_path / "outside.png"
    outside.write_bytes(b"image")
    link = allowed / "link.png"
    try:
        link.symlink_to(outside)
    except OSError:
        pytest.skip("symlinks are not supported on this platform")
    monkeypatch.setenv("XAGENT_FACEBOOK_IMAGE_ALLOWED_DIRS", str(allowed))

    traversal = _payload(
        facebook.facebook_publish_image_post(
            "page-1", file_path=str(allowed / ".." / "outside.png")
        )
    )
    symlink = _payload(
        facebook.facebook_publish_image_post("page-1", file_path=str(link))
    )

    expected = {
        "status": "error",
        "message": "Image file is outside the allowed directories",
    }
    assert traversal == expected
    assert symlink == expected


@pytest.mark.parametrize(
    ("name", "content", "message"),
    [
        ("image.svg", b"<svg/>", "file_path must reference a supported image file"),
        ("empty.png", b"", "Image file must not be empty"),
    ],
)
def test_publish_image_post_rejects_invalid_workspace_image(
    monkeypatch, tmp_path: Path, name: str, content: bytes, message: str
):
    image_path = tmp_path / name
    image_path.write_bytes(content)
    monkeypatch.setenv("XAGENT_FACEBOOK_IMAGE_ALLOWED_DIRS", str(tmp_path))

    result = _payload(
        facebook.facebook_publish_image_post("page-1", file_path=str(image_path))
    )

    assert result == {"status": "error", "message": message}


def test_publish_image_post_rejects_directory_and_missing_file(
    monkeypatch, tmp_path: Path
):
    directory = tmp_path / "directory.png"
    directory.mkdir()
    monkeypatch.setenv("XAGENT_FACEBOOK_IMAGE_ALLOWED_DIRS", str(tmp_path))

    directory_result = _payload(
        facebook.facebook_publish_image_post("page-1", file_path=str(directory))
    )
    missing_result = _payload(
        facebook.facebook_publish_image_post(
            "page-1", file_path=str(tmp_path / "missing.png")
        )
    )

    assert directory_result == {
        "status": "error",
        "message": "The given path is not a regular file",
    }
    assert missing_result == {
        "status": "error",
        "message": "Image file was not found",
    }


def test_publish_image_post_rejects_oversized_file(monkeypatch, tmp_path: Path):
    image_path = tmp_path / "large.png"
    with image_path.open("wb") as image_file:
        image_file.truncate(facebook._MAX_IMAGE_BYTES + 1)
    monkeypatch.setenv("XAGENT_FACEBOOK_IMAGE_ALLOWED_DIRS", str(tmp_path))

    result = _payload(
        facebook.facebook_publish_image_post("page-1", file_path=str(image_path))
    )

    assert result == {
        "status": "error",
        "message": (
            "Image file exceeds the local upload limit of "
            f"{facebook._MAX_IMAGE_BYTES} bytes"
        ),
    }


def test_publish_image_post_rejects_file_outside_allowed_dirs(
    monkeypatch, tmp_path: Path
):
    monkeypatch.setenv("META_ACCESS_TOKEN", "user-token")
    allowed_dir = tmp_path / "allowed"
    allowed_dir.mkdir()
    image_path = tmp_path / "outside.png"
    image_path.write_bytes(b"image")
    monkeypatch.setenv("XAGENT_FACEBOOK_IMAGE_ALLOWED_DIRS", str(allowed_dir))

    mock_request = Mock()
    monkeypatch.setattr(facebook.requests, "request", mock_request)
    result = _payload(
        facebook.facebook_publish_image_post("page-1", file_path=str(image_path))
    )

    assert result == {
        "status": "error",
        "message": "Image file is outside the allowed directories",
    }
    mock_request.assert_not_called()


def test_graph_api_errors_are_structured_and_redact_tokens(monkeypatch):
    monkeypatch.setenv("META_ACCESS_TOKEN", "user-token")
    monkeypatch.setattr(
        facebook.requests,
        "request",
        Mock(
            return_value=MockResponse(
                {
                    "error": {
                        "message": "Bad OAuth token user-token",
                        "code": 190,
                    }
                },
                text='{"error":{"message":"Bad OAuth token user-token","code":190}}',
                status_code=400,
            )
        ),
    )

    result = _payload(facebook.facebook_auth_status())

    assert result["status"] == "error"
    assert "user-token" not in json.dumps(result)
    assert "[redacted]" in json.dumps(result)
    assert result["details"]["error"]["code"] == 190


def test_graph_api_error_message_truncates_large_response_text(monkeypatch):
    monkeypatch.setenv("META_ACCESS_TOKEN", "user-token")
    long_response_text = "x" * 1200
    monkeypatch.setattr(
        facebook.requests,
        "request",
        Mock(return_value=MockResponse(text=long_response_text, status_code=502)),
    )

    result = _payload(facebook.facebook_auth_status())

    assert result["status"] == "error"
    assert "... [truncated]" in result["message"]
    assert "x" * 1001 not in result["message"]


def test_page_token_is_redacted_from_publish_errors(monkeypatch):
    monkeypatch.setenv("META_ACCESS_TOKEN", "user-token")

    def request(method, url, **kwargs):
        if url.endswith("/me/accounts"):
            return MockResponse(
                {"data": [{"id": "page-1", "access_token": "page-token"}]}
            )
        return MockResponse(
            {
                "error": {
                    "message": "Bad Page token page-token",
                    "code": 190,
                }
            },
            text='{"error":{"message":"Bad Page token page-token","code":190}}',
            status_code=400,
        )

    monkeypatch.setattr(facebook.requests, "request", Mock(side_effect=request))

    result = _payload(facebook.facebook_publish_text_post("page-1", "Launch update"))

    serialized = json.dumps(result)
    assert result["status"] == "error"
    assert "page-token" not in serialized
    assert "[redacted]" in serialized
    assert result["details"]["error"]["code"] == 190


def test_publish_image_post_rejects_non_public_url(monkeypatch):
    monkeypatch.setenv("META_ACCESS_TOKEN", "user-token")
    mock_request = Mock()
    monkeypatch.setattr(facebook.requests, "request", mock_request)

    result = _payload(
        facebook.facebook_publish_image_post("page-1", "/tmp/local-image.png")
    )

    assert result == {
        "status": "error",
        "message": "image_url must be a public http or https URL",
    }
    mock_request.assert_not_called()


# Composio-backed grants (#3531): every Graph call goes through Composio's
# Proxy Execute endpoint, and the env carries no Meta token.
COMPOSIO_PROXY_URL = "https://backend.composio.dev/api/v3.1/tools/execute/proxy"


@pytest.fixture()
def composio_env(monkeypatch):
    monkeypatch.delenv("META_ACCESS_TOKEN", raising=False)
    monkeypatch.setenv("COMPOSIO_API_KEY", "composio-key")
    monkeypatch.setenv("COMPOSIO_CONNECTED_ACCOUNT_ID", "ca_123")
    monkeypatch.setenv("COMPOSIO_USER_ID", "actor-hmac")
    monkeypatch.setattr(
        facebook.meta_graph, "_proxy_response_secrets", set(), raising=False
    )


def _proxied(data=None, *, status=200):
    body = {"data": data, "status": status, "headers": {}}
    return MockResponse(body, text=json.dumps(body))


def _graph_call(kwargs):
    """The Graph endpoint, method and query of one proxied request."""
    assert kwargs["url"] == COMPOSIO_PROXY_URL
    payload = kwargs["json"]
    query = {
        item["name"]: item["value"]
        for item in payload.get("parameters", [])
        if item["type"] == "query"
    }
    return payload["endpoint"], payload["method"], query, payload.get("body")


_ACCOUNTS = {"data": [{"id": "page-1", "access_token": "page-token"}]}


def test_proxied_list_page_posts_still_falls_back_on_a_permission_error(
    monkeypatch, composio_env
):
    feed_fields = []

    def request(**kwargs):
        endpoint, _, query, _ = _graph_call(kwargs)
        if endpoint.endswith("/me/accounts"):
            return _proxied(_ACCOUNTS)
        assert query["access_token"] == "page-token"
        feed_fields.append(query["fields"])
        if "comments" in query["fields"]:
            return _proxied(
                {"error": {"message": "permission denied", "code": 10}}, status=400
            )
        return _proxied({"data": [{"id": "post-1", "message": "hello"}]})

    monkeypatch.setattr(facebook.requests, "request", Mock(side_effect=request))

    result = _payload(facebook.facebook_list_page_posts("page-1", limit=5))

    assert result == {
        "status": "success",
        "posts": [{"id": "post-1", "message": "hello"}],
        "next_link": None,
        "engagement_available": False,
    }
    assert len(feed_fields) == 2


def test_composio_permission_failure_does_not_trigger_the_engagement_fallback(
    monkeypatch, composio_env
):
    feed_calls = []

    def request(**kwargs):
        endpoint, _, _, _ = _graph_call(kwargs)
        if endpoint.endswith("/me/accounts"):
            return _proxied(_ACCOUNTS)
        feed_calls.append(endpoint)
        body = {"error": {"message": "forbidden", "code": 10, "status": 403}}
        return MockResponse(body, text=json.dumps(body), status_code=403)

    monkeypatch.setattr(facebook.requests, "request", Mock(side_effect=request))

    result = _payload(facebook.facebook_list_page_posts("page-1", limit=5))

    assert result["status"] == "error"
    assert "Composio" in result["message"]
    assert len(feed_calls) == 1


def test_proxied_image_post_from_a_public_url(monkeypatch, composio_env):
    def request(**kwargs):
        endpoint, method, query, body = _graph_call(kwargs)
        if endpoint.endswith("/me/accounts"):
            return _proxied(_ACCOUNTS)
        assert method == "POST"
        assert endpoint == "https://graph.facebook.com/v25.0/page-1/photos"
        assert query == {"access_token": "page-token"}
        assert body == {"published": "true", "url": "https://example.com/a.png"}
        return _proxied({"id": "photo-1", "post_id": "page-1_post-1"})

    monkeypatch.setattr(facebook.requests, "request", Mock(side_effect=request))

    result = _payload(
        facebook.facebook_publish_image_post("page-1", "https://example.com/a.png")
    )

    assert result == {
        "status": "success",
        "photo_id": "photo-1",
        "post_id": "page-1_post-1",
    }


def test_proxied_image_post_from_a_file_is_refused_before_anything_runs(
    monkeypatch, composio_env, tmp_path: Path
):
    monkeypatch.setenv("XAGENT_FACEBOOK_IMAGE_ALLOWED_DIRS", str(tmp_path))
    image_path = tmp_path / "launch.png"
    image_path.write_bytes(b"png")
    mock_request = Mock()
    monkeypatch.setattr(facebook.requests, "request", mock_request)

    result = _payload(
        facebook.facebook_publish_image_post("page-1", file_path=str(image_path))
    )

    assert result["status"] == "error"
    assert "not supported" in result["message"]
    assert "image_url" in result["message"]
    mock_request.assert_not_called()


def test_composio_failures_are_redacted_in_output_and_logs(
    monkeypatch, composio_env, caplog
):
    """The tool logs the error it returns; neither may carry the API key, the
    connected account id, the Composio user id or the Page token, even when
    Composio echoes them back."""

    def request(**kwargs):
        endpoint, _, _, _ = _graph_call(kwargs)
        if endpoint.endswith("/me/accounts"):
            return _proxied(_ACCOUNTS)
        body = {
            "error": {
                "message": 'Connected account "ca_123" (actor-hmac) not found; '
                "key composio-key, token page-token",
                "status": 404,
            }
        }
        return MockResponse(body, text=json.dumps(body), status_code=404)

    monkeypatch.setattr(facebook.requests, "request", Mock(side_effect=request))

    with caplog.at_level("DEBUG"):
        result = facebook.facebook_publish_text_post("page-1", "Launch update")

    assert "Composio" in _payload(result)["message"]
    assert "Error publishing Facebook Page text post" in caplog.text
    for secret in ("composio-key", "ca_123", "actor-hmac", "page-token"):
        assert secret not in result
        assert secret not in caplog.text