|
1 | 1 | import pytest |
| 2 | +from unittest.mock import patch, MagicMock, AsyncMock |
2 | 3 |
|
3 | 4 | from tests.conftest import BaseTest |
4 | 5 | from python3_capsolver.control import Control |
@@ -36,3 +37,139 @@ def test_get_balance_api_key_err(self): |
36 | 37 | async def test_aio_get_balance_api_key_err(self): |
37 | 38 | with pytest.raises(ValueError): |
38 | 39 | await Control(api_key=self.get_random_string(36)).aio_get_balance() |
| 40 | + |
| 41 | + |
| 42 | +class TestControlMock(BaseTest): |
| 43 | + """ |
| 44 | + Mocked Unit tests for Control class |
| 45 | + """ |
| 46 | + |
| 47 | + @patch("python3_capsolver.core.sio_captcha_instrument.requests.Session.post") |
| 48 | + def test_create_task(self, mock_post): |
| 49 | + mock_response = MagicMock() |
| 50 | + mock_response.status_code = 200 |
| 51 | + mock_response.json.return_value = {"errorId": 0, "taskId": "test-task-id"} |
| 52 | + mock_post.return_value = mock_response |
| 53 | + |
| 54 | + control = Control(api_key="test-key") |
| 55 | + result = control.create_task({"type": "ImageToTextTask", "body": "base64..."}) |
| 56 | + |
| 57 | + assert result["taskId"] == "test-task-id" |
| 58 | + assert result["errorId"] == 0 |
| 59 | + mock_post.assert_called_once() |
| 60 | + |
| 61 | + @patch("python3_capsolver.core.aio_captcha_instrument.aiohttp.ClientSession.post") |
| 62 | + async def test_aio_create_task(self, mock_post): |
| 63 | + mock_resp = MagicMock() |
| 64 | + mock_resp.status = 200 |
| 65 | + mock_resp.json = AsyncMock( |
| 66 | + return_value={"errorId": 0, "taskId": "test-task-id"} |
| 67 | + ) |
| 68 | + # Mock async context manager |
| 69 | + mock_resp.__aenter__.return_value = mock_resp |
| 70 | + mock_post.return_value = mock_resp |
| 71 | + |
| 72 | + control = Control(api_key="test-key") |
| 73 | + result = await control.aio_create_task( |
| 74 | + {"type": "ImageToTextTask", "body": "base64..."} |
| 75 | + ) |
| 76 | + |
| 77 | + assert result["taskId"] == "test-task-id" |
| 78 | + assert result["errorId"] == 0 |
| 79 | + |
| 80 | + @patch("python3_capsolver.core.sio_captcha_instrument.requests.Session.post") |
| 81 | + def test_get_task_result(self, mock_post): |
| 82 | + mock_response = MagicMock() |
| 83 | + mock_response.status_code = 200 |
| 84 | + mock_response.json.return_value = { |
| 85 | + "errorId": 0, |
| 86 | + "status": "ready", |
| 87 | + "solution": {"text": "abc"}, |
| 88 | + } |
| 89 | + mock_post.return_value = mock_response |
| 90 | + |
| 91 | + control = Control(api_key="test-key") |
| 92 | + result = control.get_task_result(task_id="test-id") |
| 93 | + |
| 94 | + assert result["status"] == "ready" |
| 95 | + assert result["solution"]["text"] == "abc" |
| 96 | + |
| 97 | + @patch("python3_capsolver.core.aio_captcha_instrument.aiohttp.ClientSession.post") |
| 98 | + async def test_aio_get_task_result(self, mock_post): |
| 99 | + mock_resp = MagicMock() |
| 100 | + mock_resp.status = 200 |
| 101 | + mock_resp.json = AsyncMock( |
| 102 | + return_value={ |
| 103 | + "errorId": 0, |
| 104 | + "status": "ready", |
| 105 | + "solution": {"text": "abc"}, |
| 106 | + } |
| 107 | + ) |
| 108 | + mock_resp.__aenter__.return_value = mock_resp |
| 109 | + mock_post.return_value = mock_resp |
| 110 | + |
| 111 | + control = Control(api_key="test-key") |
| 112 | + result = await control.aio_get_task_result(task_id="test-id") |
| 113 | + |
| 114 | + assert result["status"] == "ready" |
| 115 | + assert result["solution"]["text"] == "abc" |
| 116 | + |
| 117 | + @patch("python3_capsolver.core.sio_captcha_instrument.requests.Session.post") |
| 118 | + def test_get_token(self, mock_post): |
| 119 | + mock_response = MagicMock() |
| 120 | + mock_response.status_code = 200 |
| 121 | + mock_response.json.return_value = {"errorId": 0, "taskId": "token-task-id"} |
| 122 | + mock_post.return_value = mock_response |
| 123 | + |
| 124 | + control = Control(api_key="test-key") |
| 125 | + result = control.get_token( |
| 126 | + {"type": "ReCaptchaV3TaskProxyLess", "websiteURL": "..."} |
| 127 | + ) |
| 128 | + |
| 129 | + assert result["taskId"] == "token-task-id" |
| 130 | + |
| 131 | + @patch("python3_capsolver.core.aio_captcha_instrument.aiohttp.ClientSession.post") |
| 132 | + async def test_aio_get_token(self, mock_post): |
| 133 | + mock_resp = MagicMock() |
| 134 | + mock_resp.status = 200 |
| 135 | + mock_resp.json = AsyncMock( |
| 136 | + return_value={"errorId": 0, "taskId": "token-task-id"} |
| 137 | + ) |
| 138 | + mock_resp.__aenter__.return_value = mock_resp |
| 139 | + mock_post.return_value = mock_resp |
| 140 | + |
| 141 | + control = Control(api_key="test-key") |
| 142 | + result = await control.aio_get_token( |
| 143 | + {"type": "ReCaptchaV3TaskProxyLess", "websiteURL": "..."} |
| 144 | + ) |
| 145 | + |
| 146 | + assert result["taskId"] == "token-task-id" |
| 147 | + |
| 148 | + @patch("python3_capsolver.core.sio_captcha_instrument.requests.Session.post") |
| 149 | + def test_feedback_task(self, mock_post): |
| 150 | + mock_response = MagicMock() |
| 151 | + mock_response.status_code = 200 |
| 152 | + mock_response.json.return_value = {"errorId": 0, "message": "okay"} |
| 153 | + mock_post.return_value = mock_response |
| 154 | + |
| 155 | + control = Control(api_key="test-key") |
| 156 | + result = control.feedback_task( |
| 157 | + task_id="test-id", result_payload={"invalid": True} |
| 158 | + ) |
| 159 | + |
| 160 | + assert result["message"] == "okay" |
| 161 | + |
| 162 | + @patch("python3_capsolver.core.aio_captcha_instrument.aiohttp.ClientSession.post") |
| 163 | + async def test_aio_feedback_task(self, mock_post): |
| 164 | + mock_resp = MagicMock() |
| 165 | + mock_resp.status = 200 |
| 166 | + mock_resp.json = AsyncMock(return_value={"errorId": 0, "message": "okay"}) |
| 167 | + mock_resp.__aenter__.return_value = mock_resp |
| 168 | + mock_post.return_value = mock_resp |
| 169 | + |
| 170 | + control = Control(api_key="test-key") |
| 171 | + result = await control.aio_feedback_task( |
| 172 | + task_id="test-id", result_payload={"invalid": True} |
| 173 | + ) |
| 174 | + |
| 175 | + assert result["message"] == "okay" |
0 commit comments