|
1 | | -from unittest.mock import patch |
| 1 | +from unittest.mock import Mock, patch |
2 | 2 |
|
3 | 3 | import pytest |
4 | 4 |
|
@@ -119,3 +119,67 @@ async def test_appstore_async_context_manager(): |
119 | 119 |
|
120 | 120 | # Verify session is closed after exiting context |
121 | 121 | assert validator._session is None |
| 122 | + |
| 123 | + |
| 124 | +@pytest.mark.asyncio |
| 125 | +async def test_appstore_async_http_error_includes_raw_response(appstore_validator: AppStoreValidator): |
| 126 | + """Test that async HTTP errors include raw_response with status code and content""" |
| 127 | + from unittest.mock import AsyncMock, Mock |
| 128 | + |
| 129 | + # Create a mock response |
| 130 | + mock_resp = Mock() |
| 131 | + mock_resp.status = 503 |
| 132 | + mock_resp.text = AsyncMock(return_value="Service Unavailable") |
| 133 | + |
| 134 | + # Create a mock session post that returns our mock response |
| 135 | + def mock_post(*args, **kwargs): |
| 136 | + class MockContext: |
| 137 | + async def __aenter__(self): |
| 138 | + return mock_resp |
| 139 | + |
| 140 | + async def __aexit__(self, *args): |
| 141 | + pass |
| 142 | + |
| 143 | + return MockContext() |
| 144 | + |
| 145 | + with pytest.raises(InAppPyValidationError) as exc_info: |
| 146 | + appstore_validator._session = Mock() |
| 147 | + appstore_validator._session.post = mock_post |
| 148 | + # Mock json.loads to raise ValueError (simulating invalid JSON) |
| 149 | + with patch("json.loads", side_effect=ValueError("Invalid JSON")): |
| 150 | + await appstore_validator.post_json({"receipt-data": "test"}) |
| 151 | + |
| 152 | + # Verify raw_response is not None and contains useful information |
| 153 | + assert exc_info.value.raw_response is not None |
| 154 | + assert "error" in exc_info.value.raw_response |
| 155 | + assert "status_code" in exc_info.value.raw_response |
| 156 | + assert exc_info.value.raw_response["status_code"] == 503 |
| 157 | + assert "content" in exc_info.value.raw_response |
| 158 | + assert exc_info.value.raw_response["content"] == "Service Unavailable" |
| 159 | + |
| 160 | + |
| 161 | +@pytest.mark.asyncio |
| 162 | +async def test_appstore_async_network_error_includes_raw_response(appstore_validator: AppStoreValidator): |
| 163 | + """Test that async network errors include raw_response with error details""" |
| 164 | + from aiohttp import ClientError |
| 165 | + |
| 166 | + # Create a mock session that raises ClientError |
| 167 | + def mock_post(*args, **kwargs): |
| 168 | + class MockContext: |
| 169 | + async def __aenter__(self): |
| 170 | + raise ClientError("Connection timeout") |
| 171 | + |
| 172 | + async def __aexit__(self, *args): |
| 173 | + pass |
| 174 | + |
| 175 | + return MockContext() |
| 176 | + |
| 177 | + with pytest.raises(InAppPyValidationError) as exc_info: |
| 178 | + appstore_validator._session = Mock() |
| 179 | + appstore_validator._session.post = mock_post |
| 180 | + await appstore_validator.post_json({"receipt-data": "test"}) |
| 181 | + |
| 182 | + # Verify raw_response is not None and contains error information |
| 183 | + assert exc_info.value.raw_response is not None |
| 184 | + assert "error" in exc_info.value.raw_response |
| 185 | + assert "Connection timeout" in exc_info.value.raw_response["error"] |
0 commit comments