Skip to content

Commit

Permalink
Update test_turnstile.py
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiDrang committed Dec 7, 2023
1 parent 7a5f0bf commit e8015d5
Showing 1 changed file with 64 additions and 42 deletions.
106 changes: 64 additions & 42 deletions tests/test_turnstile.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,92 +9,113 @@
class TestTurnstile(BaseTest):
pageurl = "https://rucaptcha.com/demo/cloudflare-turnstile"
sitekey = "0x4AAAAAAAC3DHQFLr1GavRN"

"""
Success tests
"""
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"
kwargs_params = {
"proxyType": "socks5",
"proxyAddress": BaseTest.proxyAddress,
"proxyPort": BaseTest.proxyPort,
}

def test_methods_exists(self):
assert "captcha_handler" in Turnstile.__dict__.keys()
assert "aio_captcha_handler" in Turnstile.__dict__.keys()

@pytest.mark.parametrize("method", TurnstileCaptchaEnm.list_values())
def test_args(self, method: str):
instance = Turnstile(
rucaptcha_key=self.RUCAPTCHA_KEY,
websiteURL=self.pageurl,
websiteKey=self.sitekey,
userAgent=self.useragent,
method=method,
)
assert instance.create_task_payload["clientKey"] == self.RUCAPTCHA_KEY
assert instance.create_task_payload["task"]["type"] == method
assert instance.create_task_payload["task"]["websiteURL"] == self.pageurl
assert instance.create_task_payload["task"]["websiteKey"] == self.sitekey
assert instance.create_task_payload["task"]["userAgent"] == self.useragent

def test_kwargs(self):
instance = Turnstile(
rucaptcha_key=self.RUCAPTCHA_KEY,
websiteURL=self.pageurl,
websiteKey=self.sitekey,
userAgent=self.useragent,
method=TurnstileCaptchaEnm.TurnstileTaskProxyless,
**self.kwargs_params,
)
assert set(self.kwargs_params.keys()).issubset(set(instance.create_task_payload["task"].keys()))
assert set(self.kwargs_params.values()).issubset(set(instance.create_task_payload["task"].values()))

"""
Success tests
"""

def test_basic_data(self):
instance = Turnstile(
rucaptcha_key=self.RUCAPTCHA_KEY,
pageurl=self.pageurl,
sitekey=self.sitekey,
websiteURL=self.pageurl,
websiteKey=self.sitekey,
userAgent=self.useragent,
method=TurnstileCaptchaEnm.TurnstileTaskProxyless.value,
)

assert instance.create_task_payload["method"] == TurnstileCaptchaEnm.TurnstileTaskProxyless.value
assert instance.create_task_payload["pageurl"] == self.pageurl
assert instance.create_task_payload["sitekey"] == self.sitekey

result = instance.captcha_handler()

assert isinstance(result, dict) is True
if result["error"] is False:
assert result["error"] is False
if not result["errorId"]:
assert result["status"] == "ready"
assert isinstance(result["solution"], dict) is True
assert isinstance(result["taskId"], int) is True
assert result["errorBody"] is None
assert isinstance(result["captchaSolve"], str) is True
else:
assert result["error"] is True
assert isinstance(result["taskId"], int) is True
assert result["errorBody"] == "ERROR_CAPTCHA_UNSOLVABLE"
assert result["errorId"] in (1, 12)
assert result["errorCode"] == "ERROR_CAPTCHA_UNSOLVABLE"

assert result.keys() == GetTaskResultResponseSer().to_dict().keys()

@pytest.mark.asyncio
async def test_aio_basic_data(self):
instance = Turnstile(
rucaptcha_key=self.RUCAPTCHA_KEY,
pageurl=self.pageurl,
sitekey=self.sitekey,
websiteURL=self.pageurl,
websiteKey=self.sitekey,
userAgent=self.useragent,
method=TurnstileCaptchaEnm.TurnstileTaskProxyless.value,
)

assert instance.create_task_payload["method"] == TurnstileCaptchaEnm.TurnstileTaskProxyless.value
assert instance.create_task_payload["pageurl"] == self.pageurl
assert instance.create_task_payload["sitekey"] == self.sitekey

result = await instance.aio_captcha_handler()

assert isinstance(result, dict) is True
if result["error"] is False:
assert result["error"] is False
if not result["errorId"]:
assert result["status"] == "ready"
assert isinstance(result["solution"], dict) is True
assert isinstance(result["taskId"], int) is True
assert result["errorBody"] is None
assert isinstance(result["captchaSolve"], str) is True
else:
assert result["error"] is True
assert isinstance(result["taskId"], int) is True
assert result["errorBody"] == "ERROR_CAPTCHA_UNSOLVABLE"
assert result["errorId"] in (1, 12)
assert result["errorCode"] == "ERROR_CAPTCHA_UNSOLVABLE"

assert result.keys() == GetTaskResultResponseSer().to_dict().keys()

def test_context_basic_data(self):
with Turnstile(
rucaptcha_key=self.RUCAPTCHA_KEY,
pageurl=self.pageurl,
sitekey=self.sitekey,
websiteURL=self.pageurl,
websiteKey=self.sitekey,
userAgent=self.useragent,
method=TurnstileCaptchaEnm.TurnstileTaskProxyless.value,
) as instance:
assert instance.create_task_payload["method"] == TurnstileCaptchaEnm.TurnstileTaskProxyless.value
assert instance.create_task_payload["pageurl"] == self.pageurl
assert instance.create_task_payload["sitekey"] == self.sitekey
assert instance.captcha_handler()

@pytest.mark.asyncio
async def test_context_aio_basic_data(self):
async with Turnstile(
rucaptcha_key=self.RUCAPTCHA_KEY,
pageurl=self.pageurl,
sitekey=self.sitekey,
websiteURL=self.pageurl,
websiteKey=self.sitekey,
userAgent=self.useragent,
method=TurnstileCaptchaEnm.TurnstileTaskProxyless.value,
) as instance:
assert instance.create_task_payload["method"] == TurnstileCaptchaEnm.TurnstileTaskProxyless.value
assert instance.create_task_payload["pageurl"] == self.pageurl
assert instance.create_task_payload["sitekey"] == self.sitekey
assert await instance.aio_captcha_handler()

"""
Fail tests
Expand All @@ -104,7 +125,8 @@ def test_wrong_method(self):
with pytest.raises(ValueError):
Turnstile(
rucaptcha_key=self.RUCAPTCHA_KEY,
pageurl=self.pageurl,
sitekey=self.sitekey,
websiteURL=self.pageurl,
websiteKey=self.sitekey,
userAgent=self.useragent,
method=self.get_random_string(5),
)

0 comments on commit e8015d5

Please sign in to comment.