|
| 1 | +from unittest.mock import patch, MagicMock |
| 2 | +import pytest |
| 3 | + |
| 4 | +from python_anticaptcha.base import AnticaptchaClient, Job, SLEEP_EVERY_CHECK_FINISHED |
| 5 | +from python_anticaptcha.exceptions import AnticaptchaException |
| 6 | + |
| 7 | + |
| 8 | +class TestAnticaptchaClientInit: |
| 9 | + def test_https_url(self): |
| 10 | + client = AnticaptchaClient("key123") |
| 11 | + assert client.base_url == "https://api.anti-captcha.com/" |
| 12 | + assert client.client_key == "key123" |
| 13 | + |
| 14 | + def test_http_url(self): |
| 15 | + client = AnticaptchaClient("key123", use_ssl=False) |
| 16 | + assert client.base_url == "http://api.anti-captcha.com/" |
| 17 | + |
| 18 | + def test_custom_host(self): |
| 19 | + client = AnticaptchaClient("key123", host="custom.host.com") |
| 20 | + assert client.base_url == "https://custom.host.com/" |
| 21 | + |
| 22 | + def test_language_pool(self): |
| 23 | + client = AnticaptchaClient("key123", language_pool="rn") |
| 24 | + assert client.language_pool == "rn" |
| 25 | + |
| 26 | + |
| 27 | +class TestCheckResponse: |
| 28 | + def setup_method(self): |
| 29 | + self.client = AnticaptchaClient("key123") |
| 30 | + |
| 31 | + def test_success_passthrough(self): |
| 32 | + response = {"errorId": 0, "taskId": 42} |
| 33 | + self.client._check_response(response) |
| 34 | + |
| 35 | + def test_error_raises(self): |
| 36 | + response = { |
| 37 | + "errorId": 1, |
| 38 | + "errorCode": "ERROR_KEY_DOES_NOT_EXIST", |
| 39 | + "errorDescription": "Account authorization key not found", |
| 40 | + } |
| 41 | + with pytest.raises(AnticaptchaException) as exc_info: |
| 42 | + self.client._check_response(response) |
| 43 | + assert exc_info.value.error_id == 1 |
| 44 | + assert exc_info.value.error_code == "ERROR_KEY_DOES_NOT_EXIST" |
| 45 | + |
| 46 | + def test_error_id_11_appends_ip(self): |
| 47 | + response = { |
| 48 | + "errorId": 11, |
| 49 | + "errorCode": "ERROR_IP_NOT_ALLOWED", |
| 50 | + "errorDescription": "IP not allowed", |
| 51 | + } |
| 52 | + with patch.object( |
| 53 | + type(self.client), "client_ip", new_callable=lambda: property(lambda self: "5.6.7.8") |
| 54 | + ): |
| 55 | + with pytest.raises(AnticaptchaException) as exc_info: |
| 56 | + self.client._check_response(response) |
| 57 | + assert "5.6.7.8" in exc_info.value.error_description |
| 58 | + |
| 59 | + |
| 60 | +class TestCreateTask: |
| 61 | + def test_payload_structure(self): |
| 62 | + client = AnticaptchaClient("key123") |
| 63 | + mock_task = MagicMock() |
| 64 | + mock_task.serialize.return_value = {"type": "NoCaptchaTaskProxyless", "websiteURL": "https://example.com"} |
| 65 | + |
| 66 | + mock_response = MagicMock() |
| 67 | + mock_response.json.return_value = {"errorId": 0, "taskId": 99} |
| 68 | + |
| 69 | + with patch.object(client.session, "post", return_value=mock_response) as mock_post: |
| 70 | + job = client.createTask(mock_task) |
| 71 | + |
| 72 | + call_kwargs = mock_post.call_args |
| 73 | + payload = call_kwargs[1]["json"] if "json" in call_kwargs[1] else call_kwargs.kwargs["json"] |
| 74 | + assert payload["clientKey"] == "key123" |
| 75 | + assert payload["task"] == {"type": "NoCaptchaTaskProxyless", "websiteURL": "https://example.com"} |
| 76 | + assert payload["softId"] == 847 |
| 77 | + assert payload["languagePool"] == "en" |
| 78 | + assert isinstance(job, Job) |
| 79 | + assert job.task_id == 99 |
| 80 | + |
| 81 | + |
| 82 | +class TestGetBalance: |
| 83 | + def test_returns_balance(self): |
| 84 | + client = AnticaptchaClient("key123") |
| 85 | + mock_response = MagicMock() |
| 86 | + mock_response.json.return_value = {"errorId": 0, "balance": 3.21} |
| 87 | + |
| 88 | + with patch.object(client.session, "post", return_value=mock_response): |
| 89 | + balance = client.getBalance() |
| 90 | + assert balance == 3.21 |
| 91 | + |
| 92 | + |
| 93 | +class TestJobCheckIsReady: |
| 94 | + def test_ready(self): |
| 95 | + client = MagicMock() |
| 96 | + client.getTaskResult.return_value = {"status": "ready", "solution": {}} |
| 97 | + job = Job(client, task_id=1) |
| 98 | + assert job.check_is_ready() is True |
| 99 | + |
| 100 | + def test_processing(self): |
| 101 | + client = MagicMock() |
| 102 | + client.getTaskResult.return_value = {"status": "processing"} |
| 103 | + job = Job(client, task_id=1) |
| 104 | + assert job.check_is_ready() is False |
| 105 | + |
| 106 | + |
| 107 | +class TestJobSolutionGetters: |
| 108 | + def setup_method(self): |
| 109 | + self.client = MagicMock() |
| 110 | + self.job = Job(self.client, task_id=1) |
| 111 | + self.job._last_result = { |
| 112 | + "status": "ready", |
| 113 | + "solution": { |
| 114 | + "gRecaptchaResponse": "recaptcha-token", |
| 115 | + "token": "funcaptcha-token", |
| 116 | + "text": "captcha text", |
| 117 | + "cellNumbers": [1, 3, 5], |
| 118 | + "answers": {"q1": "a1"}, |
| 119 | + }, |
| 120 | + } |
| 121 | + |
| 122 | + def test_get_solution_response(self): |
| 123 | + assert self.job.get_solution_response() == "recaptcha-token" |
| 124 | + |
| 125 | + def test_get_token_response(self): |
| 126 | + assert self.job.get_token_response() == "funcaptcha-token" |
| 127 | + |
| 128 | + def test_get_captcha_text(self): |
| 129 | + assert self.job.get_captcha_text() == "captcha text" |
| 130 | + |
| 131 | + def test_get_cells_numbers(self): |
| 132 | + assert self.job.get_cells_numbers() == [1, 3, 5] |
| 133 | + |
| 134 | + def test_get_answers(self): |
| 135 | + assert self.job.get_answers() == {"q1": "a1"} |
| 136 | + |
| 137 | + def test_get_solution(self): |
| 138 | + assert self.job.get_solution() == self.job._last_result["solution"] |
| 139 | + |
| 140 | + |
| 141 | +class TestJobJoinTimeout: |
| 142 | + @patch("python_anticaptcha.base.time.sleep") |
| 143 | + def test_timeout_raises(self, mock_sleep): |
| 144 | + client = MagicMock() |
| 145 | + client.getTaskResult.return_value = {"status": "processing"} |
| 146 | + job = Job(client, task_id=1) |
| 147 | + with pytest.raises(AnticaptchaException) as exc_info: |
| 148 | + job.join(maximum_time=SLEEP_EVERY_CHECK_FINISHED) |
| 149 | + assert "exceeded" in str(exc_info.value).lower() |
0 commit comments