|
| 1 | +import pytest |
| 2 | + |
| 3 | +from tests.conftest import BaseTest |
| 4 | +from python_rucaptcha.core.enums import AltchaEnm |
| 5 | +from python_rucaptcha.altcha_captcha import AltchaCaptcha |
| 6 | +from python_rucaptcha.core.serializer import GetTaskResultResponseSer |
| 7 | + |
| 8 | + |
| 9 | +class TestAltcha(BaseTest): |
| 10 | + pageurl = "https://example.com" |
| 11 | + challenge_url = "https://example.com/altcha/challenge.js" |
| 12 | + challenge_json = '{"challenge":"test_data"}' |
| 13 | + useragent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36" |
| 14 | + kwargs_params = { |
| 15 | + "proxyType": "socks5", |
| 16 | + "proxyAddress": BaseTest.proxyAddress, |
| 17 | + "proxyPort": BaseTest.proxyPort, |
| 18 | + } |
| 19 | + |
| 20 | + def test_methods_exists(self): |
| 21 | + assert "captcha_handler" in AltchaCaptcha.__dict__.keys() |
| 22 | + assert "aio_captcha_handler" in AltchaCaptcha.__dict__.keys() |
| 23 | + |
| 24 | + @pytest.mark.parametrize("method", AltchaEnm.list_values()) |
| 25 | + def test_args(self, method: str): |
| 26 | + kwargs = {} |
| 27 | + if method == AltchaEnm.AltchaTask.value: |
| 28 | + kwargs = {"proxyType": "http", "proxyAddress": "1.2.3.4", "proxyPort": 8080} |
| 29 | + instance = AltchaCaptcha( |
| 30 | + rucaptcha_key=self.RUCAPTCHA_KEY, |
| 31 | + websiteURL=self.pageurl, |
| 32 | + challengeURL=self.challenge_url, |
| 33 | + userAgent=self.useragent, |
| 34 | + method=method, |
| 35 | + **kwargs, |
| 36 | + ) |
| 37 | + assert instance.create_task_payload["clientKey"] == self.RUCAPTCHA_KEY |
| 38 | + assert instance.create_task_payload["task"]["type"] == method |
| 39 | + assert instance.create_task_payload["task"]["websiteURL"] == self.pageurl |
| 40 | + assert instance.create_task_payload["task"]["challengeURL"] == self.challenge_url |
| 41 | + |
| 42 | + def test_kwargs(self): |
| 43 | + instance = AltchaCaptcha( |
| 44 | + rucaptcha_key=self.RUCAPTCHA_KEY, |
| 45 | + websiteURL=self.pageurl, |
| 46 | + challengeURL=self.challenge_url, |
| 47 | + method=AltchaEnm.AltchaTask, |
| 48 | + **self.kwargs_params, |
| 49 | + ) |
| 50 | + assert set(self.kwargs_params.keys()).issubset(set(instance.create_task_payload["task"].keys())) |
| 51 | + assert set(self.kwargs_params.values()).issubset(set(instance.create_task_payload["task"].values())) |
| 52 | + |
| 53 | + def test_xor_validation_both_params(self): |
| 54 | + """Test that providing both challengeURL and challengeJSON raises ValueError""" |
| 55 | + with pytest.raises(ValueError, match="challengeURL|challengeJSON"): |
| 56 | + AltchaCaptcha( |
| 57 | + rucaptcha_key=self.RUCAPTCHA_KEY, |
| 58 | + websiteURL=self.pageurl, |
| 59 | + challengeURL=self.challenge_url, |
| 60 | + challengeJSON=self.challenge_json, |
| 61 | + method=AltchaEnm.AltchaTaskProxyless, |
| 62 | + ) |
| 63 | + |
| 64 | + def test_xor_validation_neither_param(self): |
| 65 | + """Test that providing neither challengeURL nor challengeJSON raises ValueError""" |
| 66 | + with pytest.raises(ValueError, match="challengeURL|challengeJSON"): |
| 67 | + AltchaCaptcha( |
| 68 | + rucaptcha_key=self.RUCAPTCHA_KEY, |
| 69 | + websiteURL=self.pageurl, |
| 70 | + method=AltchaEnm.AltchaTaskProxyless, |
| 71 | + ) |
| 72 | + |
| 73 | + def test_valid_challenge_url(self): |
| 74 | + """Test that providing only challengeURL works correctly""" |
| 75 | + instance = AltchaCaptcha( |
| 76 | + rucaptcha_key=self.RUCAPTCHA_KEY, |
| 77 | + websiteURL=self.pageurl, |
| 78 | + challengeURL=self.challenge_url, |
| 79 | + method=AltchaEnm.AltchaTaskProxyless, |
| 80 | + ) |
| 81 | + assert instance.create_task_payload["task"]["challengeURL"] == self.challenge_url |
| 82 | + assert "challengeJSON" not in instance.create_task_payload["task"] |
| 83 | + |
| 84 | + def test_valid_challenge_json(self): |
| 85 | + """Test that providing only challengeJSON works correctly""" |
| 86 | + instance = AltchaCaptcha( |
| 87 | + rucaptcha_key=self.RUCAPTCHA_KEY, |
| 88 | + websiteURL=self.pageurl, |
| 89 | + challengeJSON=self.challenge_json, |
| 90 | + method=AltchaEnm.AltchaTaskProxyless, |
| 91 | + ) |
| 92 | + assert instance.create_task_payload["task"]["challengeJSON"] == self.challenge_json |
| 93 | + assert "challengeURL" not in instance.create_task_payload["task"] |
| 94 | + |
| 95 | + def test_proxy_params_in_payload(self): |
| 96 | + """Test that proxy params are included in payload for AltchaTask method""" |
| 97 | + instance = AltchaCaptcha( |
| 98 | + rucaptcha_key=self.RUCAPTCHA_KEY, |
| 99 | + websiteURL=self.pageurl, |
| 100 | + challengeURL=self.challenge_url, |
| 101 | + method=AltchaEnm.AltchaTask, |
| 102 | + proxyType="http", |
| 103 | + proxyAddress="1.2.3.4", |
| 104 | + proxyPort=8080, |
| 105 | + ) |
| 106 | + assert instance.create_task_payload["task"]["proxyType"] == "http" |
| 107 | + assert instance.create_task_payload["task"]["proxyAddress"] == "1.2.3.4" |
| 108 | + assert instance.create_task_payload["task"]["proxyPort"] == 8080 |
| 109 | + |
| 110 | + def test_wrong_method(self): |
| 111 | + with pytest.raises(ValueError): |
| 112 | + AltchaCaptcha( |
| 113 | + rucaptcha_key=self.RUCAPTCHA_KEY, |
| 114 | + websiteURL=self.pageurl, |
| 115 | + challengeURL=self.challenge_url, |
| 116 | + method=self.get_random_string(5), |
| 117 | + ) |
| 118 | + |
| 119 | + """ |
| 120 | + Success tests |
| 121 | + """ |
| 122 | + |
| 123 | + def test_basic_data(self): |
| 124 | + instance = AltchaCaptcha( |
| 125 | + rucaptcha_key=self.RUCAPTCHA_KEY, |
| 126 | + websiteURL=self.pageurl, |
| 127 | + challengeURL=self.challenge_url, |
| 128 | + method=AltchaEnm.AltchaTaskProxyless.value, |
| 129 | + ) |
| 130 | + |
| 131 | + result = instance.captcha_handler() |
| 132 | + |
| 133 | + assert isinstance(result, dict) is True |
| 134 | + if not result["errorId"]: |
| 135 | + assert result["status"] == "ready" |
| 136 | + assert isinstance(result["solution"], dict) is True |
| 137 | + assert isinstance(result["taskId"], int) is True |
| 138 | + else: |
| 139 | + assert result["errorId"] in (1, 12) |
| 140 | + assert result["errorCode"] == "ERROR_CAPTCHA_UNSOLVABLE" |
| 141 | + |
| 142 | + assert result.keys() == GetTaskResultResponseSer().to_dict().keys() |
| 143 | + |
| 144 | + async def test_aio_basic_data(self): |
| 145 | + instance = AltchaCaptcha( |
| 146 | + rucaptcha_key=self.RUCAPTCHA_KEY, |
| 147 | + websiteURL=self.pageurl, |
| 148 | + challengeURL=self.challenge_url, |
| 149 | + method=AltchaEnm.AltchaTaskProxyless.value, |
| 150 | + ) |
| 151 | + |
| 152 | + result = await instance.aio_captcha_handler() |
| 153 | + |
| 154 | + assert isinstance(result, dict) is True |
| 155 | + if not result["errorId"]: |
| 156 | + assert result["status"] == "ready" |
| 157 | + assert isinstance(result["solution"], dict) is True |
| 158 | + assert isinstance(result["taskId"], int) is True |
| 159 | + else: |
| 160 | + assert result["errorId"] in (1, 12) |
| 161 | + assert result["errorCode"] == "ERROR_CAPTCHA_UNSOLVABLE" |
| 162 | + |
| 163 | + assert result.keys() == GetTaskResultResponseSer().to_dict().keys() |
| 164 | + |
| 165 | + def test_context_basic_data(self): |
| 166 | + with AltchaCaptcha( |
| 167 | + rucaptcha_key=self.RUCAPTCHA_KEY, |
| 168 | + websiteURL=self.pageurl, |
| 169 | + challengeURL=self.challenge_url, |
| 170 | + method=AltchaEnm.AltchaTaskProxyless.value, |
| 171 | + ) as instance: |
| 172 | + assert instance |
| 173 | + |
| 174 | + async def test_context_aio_basic_data(self): |
| 175 | + async with AltchaCaptcha( |
| 176 | + rucaptcha_key=self.RUCAPTCHA_KEY, |
| 177 | + websiteURL=self.pageurl, |
| 178 | + challengeURL=self.challenge_url, |
| 179 | + method=AltchaEnm.AltchaTaskProxyless.value, |
| 180 | + ) as instance: |
| 181 | + assert instance |
| 182 | + |
| 183 | + """ |
| 184 | + Fail tests |
| 185 | + """ |
0 commit comments