Skip to content

Commit

Permalink
Create test_friendly_captcha.py
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiDrang committed Jan 3, 2024
1 parent 6a366e5 commit f3efb67
Showing 1 changed file with 131 additions and 0 deletions.
131 changes: 131 additions & 0 deletions tests/test_friendly_captcha.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import pytest

from tests.conftest import BaseTest
from python_rucaptcha.core.enums import FriendlyCaptchaEnm
from python_rucaptcha.core.serializer import GetTaskResultResponseSer
from python_rucaptcha.friendly_captcha import FriendlyCaptcha


class TestFriendlyCaptcha(BaseTest):
websiteURL = "https://example.cc/foo/bar.html"
websiteKey = "SAb83IIB"

kwargs_params = {
"proxyType": "socks5",
"proxyAddress": BaseTest.proxyAddress,
"proxyPort": BaseTest.proxyPort,
}

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

@pytest.mark.parametrize("method", FriendlyCaptchaEnm.list_values())
def test_args(self, method: str):
instance = FriendlyCaptcha(
rucaptcha_key=self.RUCAPTCHA_KEY,
websiteURL=self.websiteURL,
websiteKey=self.websiteKey,
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.websiteURL
assert instance.create_task_payload["task"]["websiteKey"] == self.websiteKey

def test_kwargs(self):
instance = FriendlyCaptcha(
rucaptcha_key=self.RUCAPTCHA_KEY,
websiteURL=self.websiteURL,
websiteKey=self.websiteKey,
method=FriendlyCaptchaEnm.FriendlyCaptchaTaskProxyless,
**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 = FriendlyCaptcha(
rucaptcha_key=self.RUCAPTCHA_KEY,
websiteURL=self.websiteURL,
websiteKey=self.websiteKey,
method=FriendlyCaptchaEnm.FriendlyCaptchaTaskProxyless.value,
)

result = instance.captcha_handler()

assert isinstance(result, dict) is True
if not result["errorId"]:
assert result["status"] in ("ready", "processing")
assert isinstance(result["taskId"], int) is True
else:
assert result["errorId"] in (1, 12)
assert result["errorCode"] == "ERROR_CAPTCHA_UNSOLVABLE"

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

async def test_aio_basic_data(self):
instance = FriendlyCaptcha(
rucaptcha_key=self.RUCAPTCHA_KEY,
websiteURL=self.websiteURL,
websiteKey=self.websiteKey,
method=FriendlyCaptchaEnm.FriendlyCaptchaTaskProxyless.value,
)

result = await instance.aio_captcha_handler()

assert isinstance(result, dict) is True
if not result["errorId"]:
assert result["status"] in ("ready", "processing")
assert isinstance(result["taskId"], int) is True
else:
assert result["errorId"] in (1, 12)
assert result["errorCode"] in ("ERROR_CAPTCHA_UNSOLVABLE", FriendlyCaptcha.NO_CAPTCHA_ERR)

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

def test_context_basic_data(self):
with FriendlyCaptcha(
rucaptcha_key=self.RUCAPTCHA_KEY,
websiteURL=self.websiteURL,
websiteKey=self.websiteKey,
method=FriendlyCaptchaEnm.FriendlyCaptchaTaskProxyless.value,
) as instance:
assert instance

async def test_context_aio_basic_data(self):
async with FriendlyCaptcha(
rucaptcha_key=self.RUCAPTCHA_KEY,
websiteURL=self.websiteURL,
websiteKey=self.websiteKey,
method=FriendlyCaptchaEnm.FriendlyCaptchaTaskProxyless.value,
) as instance:
assert instance

"""
Fail tests
"""

def test_wrong_method(self):
with pytest.raises(ValueError):
FriendlyCaptcha(
rucaptcha_key=self.RUCAPTCHA_KEY,
websiteURL=self.websiteURL,
websiteKey=self.websiteKey,
method=self.get_random_string(length=5),
)

def test_no_websiteURL(self):
with pytest.raises(TypeError):
FriendlyCaptcha(rucaptcha_key=self.RUCAPTCHA_KEY, websiteKey=self.websiteKey)

def test_no_websiteKey(self):
with pytest.raises(TypeError):
FriendlyCaptcha(
rucaptcha_key=self.RUCAPTCHA_KEY,
websiteURL=self.websiteURL,
)

0 comments on commit f3efb67

Please sign in to comment.