|
| 1 | +"""Tests for distinguishing policy-blocked errors from token-expiry errors. |
| 2 | +
|
| 3 | +Meta returns code 368 ("action disallowed") and code 190 / subcode 459 |
| 4 | +("user checkpointed") for cases where the access token is still valid but |
| 5 | +the requested action / account is restricted. The library must NOT |
| 6 | +invalidate the token in those cases and MUST surface a distinct |
| 7 | +is_account_disabled flag so callers can branch on it. |
| 8 | +
|
| 9 | +Other code 190 subcodes (458, 460, 463, 464, 467) are genuine session |
| 10 | +errors and keep the existing invalidate-token + reauth path. |
| 11 | +""" |
| 12 | + |
| 13 | +import httpx |
| 14 | +import pytest |
| 15 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 16 | + |
| 17 | +from meta_ads_mcp.core import api as api_module |
| 18 | +from meta_ads_mcp.core.api import ( |
| 19 | + GraphAPIError, |
| 20 | + _is_account_disabled_error, |
| 21 | + make_api_request, |
| 22 | +) |
| 23 | + |
| 24 | + |
| 25 | +class TestIsAccountDisabledError: |
| 26 | + """Pure helper — covers the code/subcode classification.""" |
| 27 | + |
| 28 | + def test_code_368_is_account_disabled(self): |
| 29 | + assert _is_account_disabled_error(368, None) is True |
| 30 | + assert _is_account_disabled_error(368, 1404082) is True |
| 31 | + |
| 32 | + def test_code_190_subcode_459_is_account_disabled(self): |
| 33 | + assert _is_account_disabled_error(190, 459) is True |
| 34 | + |
| 35 | + @pytest.mark.parametrize("subcode", [458, 460, 463, 464, 467]) |
| 36 | + def test_code_190_other_subcodes_are_not_account_disabled(self, subcode): |
| 37 | + # Genuine session errors — keep the reauth path |
| 38 | + assert _is_account_disabled_error(190, subcode) is False |
| 39 | + |
| 40 | + def test_code_190_no_subcode_is_not_account_disabled(self): |
| 41 | + assert _is_account_disabled_error(190, None) is False |
| 42 | + |
| 43 | + def test_unrelated_codes_are_not_account_disabled(self): |
| 44 | + assert _is_account_disabled_error(4, None) is False |
| 45 | + assert _is_account_disabled_error(102, None) is False |
| 46 | + assert _is_account_disabled_error(100, 33) is False |
| 47 | + assert _is_account_disabled_error(None, None) is False |
| 48 | + |
| 49 | + |
| 50 | +class TestGraphAPIError: |
| 51 | + """The exception type checks codes at __init__ time and decides whether |
| 52 | + to invalidate the token.""" |
| 53 | + |
| 54 | + def test_code_368_does_not_invalidate_token(self): |
| 55 | + with patch.object(api_module.auth_manager, "invalidate_token") as mock_inv: |
| 56 | + GraphAPIError({ |
| 57 | + "code": 368, |
| 58 | + "message": "The action attempted has been deemed abusive", |
| 59 | + }) |
| 60 | + mock_inv.assert_not_called() |
| 61 | + |
| 62 | + def test_code_190_subcode_459_does_not_invalidate_token(self): |
| 63 | + with patch.object(api_module.auth_manager, "invalidate_token") as mock_inv: |
| 64 | + GraphAPIError({ |
| 65 | + "code": 190, |
| 66 | + "error_subcode": 459, |
| 67 | + "message": "User has been checkpointed", |
| 68 | + }) |
| 69 | + mock_inv.assert_not_called() |
| 70 | + |
| 71 | + def test_code_190_subcode_463_still_invalidates_token(self): |
| 72 | + with patch.object(api_module.auth_manager, "invalidate_token") as mock_inv: |
| 73 | + GraphAPIError({ |
| 74 | + "code": 190, |
| 75 | + "error_subcode": 463, |
| 76 | + "message": "Session has expired", |
| 77 | + }) |
| 78 | + mock_inv.assert_called_once() |
| 79 | + |
| 80 | + def test_code_190_no_subcode_still_invalidates_token(self): |
| 81 | + with patch.object(api_module.auth_manager, "invalidate_token") as mock_inv: |
| 82 | + GraphAPIError({ |
| 83 | + "code": 190, |
| 84 | + "message": "Error validating access token", |
| 85 | + }) |
| 86 | + mock_inv.assert_called_once() |
| 87 | + |
| 88 | + def test_code_4_does_not_invalidate_token(self): |
| 89 | + with patch.object(api_module.auth_manager, "invalidate_token") as mock_inv: |
| 90 | + GraphAPIError({ |
| 91 | + "code": 4, |
| 92 | + "error_subcode": 1504022, |
| 93 | + "message": "Application request limit reached", |
| 94 | + }) |
| 95 | + mock_inv.assert_not_called() |
| 96 | + |
| 97 | + |
| 98 | +def _build_http_error(status: int, body: dict) -> httpx.HTTPStatusError: |
| 99 | + request = httpx.Request("GET", "https://graph.facebook.com/v24.0/act_123/insights") |
| 100 | + response = httpx.Response(status, request=request, json=body) |
| 101 | + return httpx.HTTPStatusError("error", request=request, response=response) |
| 102 | + |
| 103 | + |
| 104 | +class TestMakeApiRequestHttpErrors: |
| 105 | + """End-to-end on the HTTP error handler: account-disabled errors flow |
| 106 | + through with the is_account_disabled flag, token errors trigger |
| 107 | + invalidate_token and DO NOT set the flag.""" |
| 108 | + |
| 109 | + @pytest.mark.asyncio |
| 110 | + async def test_code_368_returns_is_account_disabled_flag(self): |
| 111 | + body = { |
| 112 | + "error": { |
| 113 | + "message": "The action attempted has been deemed abusive", |
| 114 | + "type": "OAuthException", |
| 115 | + "code": 368, |
| 116 | + "error_subcode": 1404082, |
| 117 | + "fbtrace_id": "abc", |
| 118 | + } |
| 119 | + } |
| 120 | + http_err = _build_http_error(400, body) |
| 121 | + |
| 122 | + client = MagicMock() |
| 123 | + client.__aenter__ = AsyncMock(return_value=client) |
| 124 | + client.__aexit__ = AsyncMock(return_value=False) |
| 125 | + client.get = AsyncMock(side_effect=http_err) |
| 126 | + |
| 127 | + with patch.object(api_module.auth_manager, "invalidate_token") as mock_inv, \ |
| 128 | + patch.object(api_module, "httpx") as mock_httpx: |
| 129 | + mock_httpx.AsyncClient = MagicMock(return_value=client) |
| 130 | + mock_httpx.HTTPStatusError = httpx.HTTPStatusError |
| 131 | + result = await make_api_request("act_123/insights", "tok") |
| 132 | + |
| 133 | + assert isinstance(result, dict) |
| 134 | + assert "error" in result |
| 135 | + assert result["error"].get("is_account_disabled") is True |
| 136 | + assert result["error"].get("error_code") == 368 |
| 137 | + assert result["error"].get("error_subcode") == 1404082 |
| 138 | + # Original Meta error details preserved |
| 139 | + assert result["error"]["details"]["error"]["code"] == 368 |
| 140 | + mock_inv.assert_not_called() |
| 141 | + |
| 142 | + @pytest.mark.asyncio |
| 143 | + async def test_code_190_subcode_459_returns_is_account_disabled_flag(self): |
| 144 | + body = { |
| 145 | + "error": { |
| 146 | + "message": "User has been checkpointed", |
| 147 | + "type": "OAuthException", |
| 148 | + "code": 190, |
| 149 | + "error_subcode": 459, |
| 150 | + } |
| 151 | + } |
| 152 | + http_err = _build_http_error(400, body) |
| 153 | + |
| 154 | + client = MagicMock() |
| 155 | + client.__aenter__ = AsyncMock(return_value=client) |
| 156 | + client.__aexit__ = AsyncMock(return_value=False) |
| 157 | + client.get = AsyncMock(side_effect=http_err) |
| 158 | + |
| 159 | + with patch.object(api_module.auth_manager, "invalidate_token") as mock_inv, \ |
| 160 | + patch.object(api_module, "httpx") as mock_httpx: |
| 161 | + mock_httpx.AsyncClient = MagicMock(return_value=client) |
| 162 | + mock_httpx.HTTPStatusError = httpx.HTTPStatusError |
| 163 | + result = await make_api_request("act_123/insights", "tok") |
| 164 | + |
| 165 | + assert result["error"].get("is_account_disabled") is True |
| 166 | + assert result["error"].get("error_code") == 190 |
| 167 | + assert result["error"].get("error_subcode") == 459 |
| 168 | + mock_inv.assert_not_called() |
| 169 | + |
| 170 | + @pytest.mark.asyncio |
| 171 | + async def test_code_190_subcode_463_does_not_set_flag_and_invalidates(self): |
| 172 | + body = { |
| 173 | + "error": { |
| 174 | + "message": "Session has expired", |
| 175 | + "type": "OAuthException", |
| 176 | + "code": 190, |
| 177 | + "error_subcode": 463, |
| 178 | + } |
| 179 | + } |
| 180 | + http_err = _build_http_error(400, body) |
| 181 | + |
| 182 | + client = MagicMock() |
| 183 | + client.__aenter__ = AsyncMock(return_value=client) |
| 184 | + client.__aexit__ = AsyncMock(return_value=False) |
| 185 | + client.get = AsyncMock(side_effect=http_err) |
| 186 | + |
| 187 | + with patch.object(api_module.auth_manager, "invalidate_token") as mock_inv, \ |
| 188 | + patch.object(api_module, "httpx") as mock_httpx: |
| 189 | + mock_httpx.AsyncClient = MagicMock(return_value=client) |
| 190 | + mock_httpx.HTTPStatusError = httpx.HTTPStatusError |
| 191 | + result = await make_api_request("act_123/insights", "tok") |
| 192 | + |
| 193 | + assert "is_account_disabled" not in result["error"] |
| 194 | + mock_inv.assert_called_once() |
| 195 | + |
| 196 | + @pytest.mark.asyncio |
| 197 | + async def test_code_190_no_subcode_does_not_set_flag_and_invalidates(self): |
| 198 | + body = { |
| 199 | + "error": { |
| 200 | + "message": "Error validating access token", |
| 201 | + "type": "OAuthException", |
| 202 | + "code": 190, |
| 203 | + } |
| 204 | + } |
| 205 | + http_err = _build_http_error(400, body) |
| 206 | + |
| 207 | + client = MagicMock() |
| 208 | + client.__aenter__ = AsyncMock(return_value=client) |
| 209 | + client.__aexit__ = AsyncMock(return_value=False) |
| 210 | + client.get = AsyncMock(side_effect=http_err) |
| 211 | + |
| 212 | + with patch.object(api_module.auth_manager, "invalidate_token") as mock_inv, \ |
| 213 | + patch.object(api_module, "httpx") as mock_httpx: |
| 214 | + mock_httpx.AsyncClient = MagicMock(return_value=client) |
| 215 | + mock_httpx.HTTPStatusError = httpx.HTTPStatusError |
| 216 | + result = await make_api_request("act_123/insights", "tok") |
| 217 | + |
| 218 | + assert "is_account_disabled" not in result["error"] |
| 219 | + mock_inv.assert_called_once() |
0 commit comments