python3-anticaptcha/tests/test_Control.py

368 lines
12 KiB
Python
Raw Normal View History

2019-10-03 19:52:30 +00:00
import inspect
import pytest
2019-10-04 23:31:45 +00:00
from python3_anticaptcha import AntiCaptchaControl, config
2019-10-03 19:52:30 +00:00
from tests.main import MainAntiCaptcha
class TestAntiCaptcha(MainAntiCaptcha):
2019-10-06 15:16:51 +00:00
WRONG_MODE = WRONG_CAPTCHA_TYPE = "qwerty"
WRONG_SOFT_ID = "-1" + config.app_key
REPORT_ID = WRONG_QUEUE_ID = -1
QUEUE_STATUS_KEYS = ("waiting", "load", "bid", "speed", "total")
2019-10-03 19:52:30 +00:00
"""
Params check
"""
def test_customcatpcha_params(self):
2019-10-04 23:31:45 +00:00
default_init_params = ["self", "anticaptcha_key"]
default_get_balance_params = ["self"]
default_app_stats_params = ["self", "softId", "mode"]
default_complaint_params = ["self", "reported_id", "captcha_type"]
2019-10-05 01:49:20 +00:00
default_queue_status_params = ["queue_id"]
2019-10-04 10:39:40 +00:00
# get aiocaptchacontrol init and other params
2019-11-25 17:08:18 +00:00
aio_init_params = inspect.getfullargspec(AntiCaptchaControl.aioAntiCaptchaControl.__init__)
2019-10-04 10:39:40 +00:00
aio_balance_params = inspect.getfullargspec(
AntiCaptchaControl.aioAntiCaptchaControl.get_balance
)
aio_app_stats_params = inspect.getfullargspec(
AntiCaptchaControl.aioAntiCaptchaControl.get_app_stats
)
aio_complaint_params = inspect.getfullargspec(
AntiCaptchaControl.aioAntiCaptchaControl.complaint_on_result
)
aio_queue_status_params = inspect.getfullargspec(
AntiCaptchaControl.aioAntiCaptchaControl.get_queue_status
2019-10-03 19:52:30 +00:00
)
2019-10-04 10:39:40 +00:00
# get captchacontrol init and other params
2019-11-25 17:08:18 +00:00
init_params = inspect.getfullargspec(AntiCaptchaControl.AntiCaptchaControl.__init__)
balance_params = inspect.getfullargspec(AntiCaptchaControl.AntiCaptchaControl.get_balance)
2019-10-04 10:39:40 +00:00
app_stats_params = inspect.getfullargspec(
AntiCaptchaControl.AntiCaptchaControl.get_app_stats
)
complaint_params = inspect.getfullargspec(
AntiCaptchaControl.AntiCaptchaControl.complaint_on_result
)
queue_status_params = inspect.getfullargspec(
AntiCaptchaControl.AntiCaptchaControl.get_queue_status
2019-10-03 19:52:30 +00:00
)
# check aio module params
2019-10-04 10:39:40 +00:00
assert default_init_params == aio_init_params[0]
assert default_get_balance_params == aio_balance_params[0]
assert default_app_stats_params == aio_app_stats_params[0]
assert default_complaint_params == aio_complaint_params[0]
assert default_queue_status_params == aio_queue_status_params[0]
2019-10-03 19:52:30 +00:00
# check sync module params
assert default_init_params == init_params[0]
2019-10-04 10:39:40 +00:00
assert default_get_balance_params == balance_params[0]
assert default_app_stats_params == app_stats_params[0]
assert default_complaint_params == complaint_params[0]
assert default_queue_status_params == queue_status_params[0]
2019-10-03 19:52:30 +00:00
"""
Response checking
"""
2019-10-04 10:39:40 +00:00
def test_response_control(self):
2019-10-03 19:52:30 +00:00
control_captcha = AntiCaptchaControl.AntiCaptchaControl(
anticaptcha_key=self.anticaptcha_key_fail
)
# check response type
assert isinstance(control_captcha, AntiCaptchaControl.AntiCaptchaControl)
@pytest.mark.asyncio
2019-10-04 23:31:45 +00:00
async def test_response_aiocontrol(self):
control_captcha = AntiCaptchaControl.aioAntiCaptchaControl(
2019-10-03 19:52:30 +00:00
anticaptcha_key=self.anticaptcha_key_fail
)
# check response type
2019-10-04 23:31:45 +00:00
assert isinstance(control_captcha, AntiCaptchaControl.aioAntiCaptchaControl)
2019-10-03 19:52:30 +00:00
"""
Fail tests
"""
2019-10-04 23:31:45 +00:00
def test_fail_balance(self):
captcha_control = AntiCaptchaControl.AntiCaptchaControl(
anticaptcha_key=self.anticaptcha_key_fail
2019-10-03 19:52:30 +00:00
)
2019-10-04 23:31:45 +00:00
response = captcha_control.get_balance()
assert isinstance(response, dict)
2019-10-03 19:52:30 +00:00
assert 1 == response["errorId"]
2019-10-04 23:31:45 +00:00
def test_fail_balance_context(self):
with AntiCaptchaControl.AntiCaptchaControl(
anticaptcha_key=self.anticaptcha_key_fail
) as captcha_control:
response = captcha_control.get_balance()
2019-10-03 19:52:30 +00:00
2019-10-04 23:31:45 +00:00
assert isinstance(response, dict)
2019-10-03 19:52:30 +00:00
assert 1 == response["errorId"]
2019-10-04 23:31:45 +00:00
def test_fail_key_app_stats(self):
captcha_control = AntiCaptchaControl.AntiCaptchaControl(
anticaptcha_key=self.anticaptcha_key_fail
2019-10-03 19:52:30 +00:00
)
2019-10-04 23:31:45 +00:00
response = captcha_control.get_app_stats(softId=self.WRONG_SOFT_ID)
assert isinstance(response, dict)
2019-10-03 19:52:30 +00:00
assert 1 == response["errorId"]
2019-10-05 01:49:20 +00:00
def test_fail_app_stats_context(self):
with AntiCaptchaControl.AntiCaptchaControl(
anticaptcha_key=self.anticaptcha_key_fail
) as captcha_control:
response = captcha_control.get_app_stats(softId=config.app_key)
assert isinstance(response, dict)
assert 1 == response["errorId"]
2019-10-04 23:31:45 +00:00
def test_fail_id_app_stats(self):
captcha_control = AntiCaptchaControl.AntiCaptchaControl(
anticaptcha_key=self.anticaptcha_key_true
)
2019-10-03 19:52:30 +00:00
2019-10-04 23:31:45 +00:00
response = captcha_control.get_app_stats(softId=self.WRONG_SOFT_ID)
2019-10-03 19:52:30 +00:00
2019-10-04 23:31:45 +00:00
assert isinstance(response, dict)
2019-10-03 19:52:30 +00:00
2019-10-04 23:31:45 +00:00
assert 1 == response["errorId"]
def test_fail_mode_app_stats(self):
captcha_control = AntiCaptchaControl.AntiCaptchaControl(
anticaptcha_key=self.anticaptcha_key_true
)
2019-10-03 19:52:30 +00:00
with pytest.raises(ValueError):
2019-11-25 17:08:18 +00:00
assert captcha_control.get_app_stats(softId=config.app_key, mode=self.WRONG_MODE)
2019-10-06 15:16:51 +00:00
def test_fail_key_complaint(self):
captcha_control = AntiCaptchaControl.AntiCaptchaControl(
anticaptcha_key=self.anticaptcha_key_fail
)
response = captcha_control.complaint_on_result(
2019-11-25 17:08:18 +00:00
reported_id=self.REPORT_ID, captcha_type=AntiCaptchaControl.complaint_types[0]
2019-10-06 15:16:51 +00:00
)
assert isinstance(response, dict)
assert 1 == response["errorId"]
def test_fail_id_complaint(self):
captcha_control = AntiCaptchaControl.AntiCaptchaControl(
anticaptcha_key=self.anticaptcha_key_true
)
response = captcha_control.complaint_on_result(
2019-11-25 17:08:18 +00:00
reported_id=self.REPORT_ID, captcha_type=AntiCaptchaControl.complaint_types[0]
2019-10-06 15:16:51 +00:00
)
assert isinstance(response, dict)
assert 16 == response["errorId"]
def test_fail_type_complaint(self):
captcha_control = AntiCaptchaControl.AntiCaptchaControl(
anticaptcha_key=self.anticaptcha_key_fail
)
with pytest.raises(ValueError):
assert captcha_control.complaint_on_result(
reported_id=self.REPORT_ID, captcha_type=self.WRONG_CAPTCHA_TYPE
)
def test_fail_id_queue_status(self):
captcha_control = AntiCaptchaControl.AntiCaptchaControl
with pytest.raises(ValueError):
assert captcha_control.get_queue_status(queue_id=self.WRONG_QUEUE_ID)
2019-10-03 19:52:30 +00:00
@pytest.mark.asyncio
2019-10-04 23:31:45 +00:00
async def test_fail_aiobalance(self):
captcha_control = AntiCaptchaControl.aioAntiCaptchaControl(
anticaptcha_key=self.anticaptcha_key_fail
2019-10-03 19:52:30 +00:00
)
2019-10-04 23:31:45 +00:00
response = await captcha_control.get_balance()
assert isinstance(response, dict)
2019-10-03 19:52:30 +00:00
assert 1 == response["errorId"]
2019-10-05 01:49:20 +00:00
@pytest.mark.asyncio
async def test_fail_aiobalance_context(self):
with AntiCaptchaControl.aioAntiCaptchaControl(
anticaptcha_key=self.anticaptcha_key_fail
) as captcha_control:
response = await captcha_control.get_balance()
assert isinstance(response, dict)
assert 1 == response["errorId"]
@pytest.mark.asyncio
async def test_fail_aioapp_stats_context(self):
with AntiCaptchaControl.aioAntiCaptchaControl(
anticaptcha_key=self.anticaptcha_key_fail
) as captcha_control:
response = await captcha_control.get_app_stats(softId=config.app_key)
assert isinstance(response, dict)
assert 1 == response["errorId"]
@pytest.mark.asyncio
async def test_fail_aiokey_app_stats(self):
captcha_control = AntiCaptchaControl.aioAntiCaptchaControl(
anticaptcha_key=self.anticaptcha_key_fail
)
response = await captcha_control.get_app_stats(softId=self.WRONG_SOFT_ID)
assert isinstance(response, dict)
assert 1 == response["errorId"]
@pytest.mark.asyncio
async def test_fail_aioid_app_stats(self):
captcha_control = AntiCaptchaControl.aioAntiCaptchaControl(
anticaptcha_key=self.anticaptcha_key_true
)
response = await captcha_control.get_app_stats(softId=self.WRONG_SOFT_ID)
assert isinstance(response, dict)
assert 1 == response["errorId"]
@pytest.mark.asyncio
async def test_fail_aiomode_app_stats(self):
with AntiCaptchaControl.aioAntiCaptchaControl(
anticaptcha_key=self.anticaptcha_key_fail
) as captcha_control:
response = await captcha_control.get_balance()
assert isinstance(response, dict)
assert 1 == response["errorId"]
2019-10-03 19:52:30 +00:00
2019-10-06 15:16:51 +00:00
@pytest.mark.asyncio
async def test_fail_aiokey_complaint(self):
captcha_control = AntiCaptchaControl.aioAntiCaptchaControl(
anticaptcha_key=self.anticaptcha_key_fail
)
response = await captcha_control.complaint_on_result(
2019-11-25 17:08:18 +00:00
reported_id=self.REPORT_ID, captcha_type=AntiCaptchaControl.complaint_types[0]
2019-10-06 15:16:51 +00:00
)
assert isinstance(response, dict)
assert 1 == response["errorId"]
@pytest.mark.asyncio
async def test_fail_aioid_complaint(self):
captcha_control = AntiCaptchaControl.aioAntiCaptchaControl(
anticaptcha_key=self.anticaptcha_key_true
)
response = await captcha_control.complaint_on_result(
2019-11-25 17:08:18 +00:00
reported_id=self.REPORT_ID, captcha_type=AntiCaptchaControl.complaint_types[0]
2019-10-06 15:16:51 +00:00
)
assert isinstance(response, dict)
assert 16 == response["errorId"]
@pytest.mark.asyncio
async def test_fail_aiotype_complaint(self):
captcha_control = AntiCaptchaControl.aioAntiCaptchaControl(
anticaptcha_key=self.anticaptcha_key_fail
)
with pytest.raises(ValueError):
assert await captcha_control.complaint_on_result(
reported_id=self.REPORT_ID, captcha_type=self.WRONG_CAPTCHA_TYPE
)
@pytest.mark.asyncio
async def test_fail_aioid_queue_status(self):
captcha_control = AntiCaptchaControl.aioAntiCaptchaControl
with pytest.raises(ValueError):
assert await captcha_control.get_queue_status(queue_id=self.WRONG_QUEUE_ID)
2019-10-03 19:52:30 +00:00
"""
True tests
"""
2019-10-04 23:31:45 +00:00
def test_true_mode_app_stats(self):
captcha_control = AntiCaptchaControl.AntiCaptchaControl(
anticaptcha_key=self.anticaptcha_key_true
)
for mode in AntiCaptchaControl.mods:
2019-10-05 01:49:20 +00:00
response = captcha_control.get_app_stats(softId=config.app_key, mode=mode)
2019-10-04 23:31:45 +00:00
assert isinstance(response, dict)
2019-10-06 15:16:51 +00:00
assert 0 == response["errorId"]
def test_true_mode_app_stats_context(self):
with AntiCaptchaControl.AntiCaptchaControl(
anticaptcha_key=self.anticaptcha_key_true
) as captcha_control:
for mode in AntiCaptchaControl.mods:
2019-11-25 17:08:18 +00:00
response = captcha_control.get_app_stats(softId=config.app_key, mode=mode)
2019-10-06 15:16:51 +00:00
assert isinstance(response, dict)
assert 0 == response["errorId"]
def test_true_balance(self):
captcha_control = AntiCaptchaControl.AntiCaptchaControl(
anticaptcha_key=self.anticaptcha_key_true
)
response = captcha_control.get_balance()
assert isinstance(response, dict)
assert 0 == response["errorId"]
def test_true_balance_context(self):
with AntiCaptchaControl.AntiCaptchaControl(
anticaptcha_key=self.anticaptcha_key_true
) as captcha_control:
response = captcha_control.get_balance()
assert isinstance(response, dict)
assert 0 == response["errorId"]
def test_true_queue_status(self):
captcha_control = AntiCaptchaControl.AntiCaptchaControl
for queue_id in AntiCaptchaControl.queue_ids:
response = captcha_control.get_queue_status(queue_id=queue_id)
assert isinstance(response, dict)
assert self.QUEUE_STATUS_KEYS == tuple(response.keys())