python3-anticaptcha/tests/test_ImageToText.py

199 lines
7.7 KiB
Python
Raw Normal View History

2019-09-11 12:30:09 +00:00
import inspect
import pytest
2019-11-25 17:01:08 +00:00
import requests_mock
2019-09-11 12:30:09 +00:00
2019-09-29 22:11:51 +00:00
from tests.main import MainAntiCaptcha
2020-03-11 14:40:16 +00:00
from python3_anticaptcha import ImageToTextTask, config
2019-09-11 12:30:09 +00:00
2020-03-11 14:40:16 +00:00
class TestImageToTextCaptcha(MainAntiCaptcha):
2019-09-11 12:30:09 +00:00
WRONG_SAVE_FORMAT = "qwerty"
2019-11-25 17:01:08 +00:00
2019-09-11 12:30:09 +00:00
"""
Params check
"""
def test_customcatpcha_params(self):
default_init_params = [
"self",
"anticaptcha_key",
"sleep_time",
"save_format",
"callbackUrl",
]
2019-11-25 17:08:18 +00:00
default_handler_params = ["self", "captcha_link", "captcha_file", "captcha_base64"]
2019-09-11 12:30:09 +00:00
# get customcaptcha init and captcha_handler params
2019-11-25 17:08:18 +00:00
aioinit_params = inspect.getfullargspec(ImageToTextTask.aioImageToTextTask.__init__)
aiohandler_params = inspect.getfullargspec(ImageToTextTask.aioImageToTextTask.captcha_handler)
2019-09-11 12:30:09 +00:00
# get customcaptcha init and captcha_handler params
init_params = inspect.getfullargspec(ImageToTextTask.ImageToTextTask.__init__)
2019-11-25 17:08:18 +00:00
handler_params = inspect.getfullargspec(ImageToTextTask.ImageToTextTask.captcha_handler)
2019-09-11 12:30:09 +00:00
# check aio module params
assert default_init_params == aioinit_params[0]
assert default_handler_params == aiohandler_params[0]
# check sync module params
assert default_init_params == init_params[0]
assert default_handler_params == handler_params[0]
2019-11-25 17:01:08 +00:00
def test_create_task_payload(self):
2019-11-25 17:08:18 +00:00
customcaptcha = ImageToTextTask.ImageToTextTask(anticaptcha_key=self.anticaptcha_key_fail)
2019-11-25 17:01:08 +00:00
# check response type
assert isinstance(customcaptcha, ImageToTextTask.ImageToTextTask)
with requests_mock.Mocker() as req_mock:
2019-11-25 17:05:46 +00:00
req_mock.register_uri("GET", self.image_url, json=self.VALID_RESPONSE_JSON)
2019-11-25 17:08:18 +00:00
req_mock.register_uri("POST", config.create_task_url, json=self.ERROR_RESPONSE_JSON)
2019-11-25 17:01:08 +00:00
customcaptcha.captcha_handler(captcha_link=self.image_url)
history = req_mock.request_history
assert len(history) == 2
request_payload = history[1].json()
# check all dict keys
2020-05-30 17:02:18 +00:00
assert ["clientKey", "task", "softId"] == list(request_payload.keys())
2019-11-25 17:01:08 +00:00
assert request_payload["softId"] == config.app_key
assert ["type", "body"] == list(request_payload["task"].keys())
assert request_payload["task"]["type"] == "ImageToTextTask"
def test_get_result_payload(self):
2019-11-25 17:08:18 +00:00
customcaptcha = ImageToTextTask.ImageToTextTask(anticaptcha_key=self.anticaptcha_key_fail)
2019-11-25 17:01:08 +00:00
# check response type
assert isinstance(customcaptcha, ImageToTextTask.ImageToTextTask)
with requests_mock.Mocker() as req_mock:
2019-11-25 17:05:46 +00:00
req_mock.register_uri("GET", self.image_url, json=self.VALID_RESPONSE_JSON)
2019-11-25 17:08:18 +00:00
req_mock.register_uri("POST", config.create_task_url, json=self.VALID_RESPONSE_JSON)
req_mock.register_uri("POST", config.get_result_url, json=self.VALID_RESPONSE_RESULT_JSON)
2019-11-25 17:01:08 +00:00
customcaptcha.captcha_handler(captcha_link=self.image_url)
history = req_mock.request_history
assert len(history) == 3
request_payload = history[2].json()
# check all dict keys
assert ["clientKey", "taskId"] == list(request_payload.keys())
2019-11-25 17:05:46 +00:00
assert request_payload["taskId"] == self.VALID_RESPONSE_JSON["taskId"]
2019-11-25 17:01:08 +00:00
2019-09-11 12:30:09 +00:00
"""
Response checking
"""
def test_response_imagecaptcha(self):
2019-11-25 17:08:18 +00:00
imagecaptcha = ImageToTextTask.ImageToTextTask(anticaptcha_key=self.anticaptcha_key_fail)
2019-09-11 12:30:09 +00:00
# check response type
assert isinstance(imagecaptcha, ImageToTextTask.ImageToTextTask)
response = imagecaptcha.captcha_handler(captcha_link=self.image_url)
# check response type
assert isinstance(response, dict)
# check all dict keys
assert ["errorId", "errorCode", "errorDescription"] == list(response.keys())
@pytest.mark.asyncio
async def test_response_aioimagecaptcha(self):
imagecaptcha = ImageToTextTask.aioImageToTextTask(anticaptcha_key=self.anticaptcha_key_fail)
2019-09-11 12:30:09 +00:00
# check response type
2019-10-06 21:50:22 +00:00
assert isinstance(imagecaptcha, ImageToTextTask.aioImageToTextTask)
2019-09-11 12:30:09 +00:00
response = await imagecaptcha.captcha_handler(captcha_link=self.image_url)
# check response type
assert isinstance(response, dict)
# check all dict keys
assert ["errorId", "errorCode", "errorDescription"] == list(response.keys())
"""
Fail tests
"""
def test_fail_imagecaptcha_value(self):
with pytest.raises(ValueError):
assert ImageToTextTask.ImageToTextTask(
2019-11-25 17:08:18 +00:00
anticaptcha_key=self.anticaptcha_key_fail, save_format=self.WRONG_SAVE_FORMAT
2019-09-11 12:30:09 +00:00
)
def test_fail_imagecaptcha_const(self):
imagecaptcha = ImageToTextTask.ImageToTextTask(
2019-11-25 17:08:18 +00:00
anticaptcha_key=self.anticaptcha_key_fail, save_format=ImageToTextTask.SAVE_FORMATS[0]
2019-09-11 12:30:09 +00:00
)
response = imagecaptcha.captcha_handler(captcha_link=self.image_url)
assert 1 == response["errorId"]
def test_fail_imagecaptcha_const_context(self):
with ImageToTextTask.ImageToTextTask(
2019-11-25 17:08:18 +00:00
anticaptcha_key=self.anticaptcha_key_fail, save_format=ImageToTextTask.SAVE_FORMATS[0]
2019-09-11 12:30:09 +00:00
) as imagecaptcha:
response = imagecaptcha.captcha_handler(captcha_link=self.image_url)
assert 1 == response["errorId"]
def test_fail_imagecaptcha_temp(self):
imagecaptcha = ImageToTextTask.ImageToTextTask(
2019-11-25 17:08:18 +00:00
anticaptcha_key=self.anticaptcha_key_fail, save_format=ImageToTextTask.SAVE_FORMATS[1]
2019-09-11 12:30:09 +00:00
)
response = imagecaptcha.captcha_handler(captcha_link=self.image_url)
assert 1 == response["errorId"]
def test_fail_imagecaptcha_temp_context(self):
with ImageToTextTask.ImageToTextTask(
2019-11-25 17:08:18 +00:00
anticaptcha_key=self.anticaptcha_key_fail, save_format=ImageToTextTask.SAVE_FORMATS[1]
2019-09-11 12:30:09 +00:00
) as imagecaptcha:
response = imagecaptcha.captcha_handler(captcha_link=self.image_url)
assert 1 == response["errorId"]
@pytest.mark.asyncio
async def test_fail_aioimagecaptcha_value(self):
with pytest.raises(ValueError):
2019-10-06 15:38:28 +00:00
assert await ImageToTextTask.ImageToTextTask(
2019-11-25 17:08:18 +00:00
anticaptcha_key=self.anticaptcha_key_fail, save_format=self.WRONG_SAVE_FORMAT
2019-09-11 12:30:09 +00:00
)
@pytest.mark.asyncio
async def test_fail_aioimagecaptcha_temp(self):
imagecaptcha = ImageToTextTask.aioImageToTextTask(
2019-11-25 17:08:18 +00:00
anticaptcha_key=self.anticaptcha_key_fail, save_format=ImageToTextTask.SAVE_FORMATS[1]
2019-09-11 12:30:09 +00:00
)
response = await imagecaptcha.captcha_handler(captcha_link=self.image_url)
assert 1 == response["errorId"]
@pytest.mark.asyncio
async def test_fail_aioimagecaptcha_temp_context(self):
with ImageToTextTask.aioImageToTextTask(
2019-11-25 17:08:18 +00:00
anticaptcha_key=self.anticaptcha_key_fail, save_format=ImageToTextTask.SAVE_FORMATS[1]
2019-09-11 12:30:09 +00:00
) as imagecaptcha:
response = await imagecaptcha.captcha_handler(captcha_link=self.image_url)
assert 1 == response["errorId"]
@pytest.mark.asyncio
async def test_fail_aioimagecaptcha_const(self):
imagecaptcha = ImageToTextTask.aioImageToTextTask(
2019-11-25 17:08:18 +00:00
anticaptcha_key=self.anticaptcha_key_fail, save_format=ImageToTextTask.SAVE_FORMATS[0]
2019-09-11 12:30:09 +00:00
)
response = await imagecaptcha.captcha_handler(captcha_link=self.image_url)
assert 1 == response["errorId"]
@pytest.mark.asyncio
async def test_fail_aioimagecaptcha_const_context(self):
with ImageToTextTask.aioImageToTextTask(
2019-11-25 17:08:18 +00:00
anticaptcha_key=self.anticaptcha_key_fail, save_format=ImageToTextTask.SAVE_FORMATS[0]
2019-09-11 12:30:09 +00:00
) as imagecaptcha:
response = await imagecaptcha.captcha_handler(captcha_link=self.image_url)
assert 1 == response["errorId"]
"""
True tests
"""