From 4c91f6d0811e5496a569c0338bbc3ca215f98aa5 Mon Sep 17 00:00:00 2001 From: Andrei Date: Fri, 6 Sep 2019 00:07:36 +0300 Subject: [PATCH] Move tests to separate folder --- tests/main.py | 11 ++ tests/test_Callback.py | 16 +++ tests/test_CustomCaptcha.py | 154 ++++++++++++++++++++++++++++ tests/test_CustomResultHandler.py | 164 ++++++++++++++++++++++++++++++ 4 files changed, 345 insertions(+) create mode 100644 tests/main.py create mode 100644 tests/test_Callback.py create mode 100644 tests/test_CustomCaptcha.py create mode 100644 tests/test_CustomResultHandler.py diff --git a/tests/main.py b/tests/main.py new file mode 100644 index 0000000..27ecc96 --- /dev/null +++ b/tests/main.py @@ -0,0 +1,11 @@ +import os + + +class MainAntiCaptcha(object): + WRONG_QUEUE_ID = WRONG_TASK_ID = -1 + + def setup_class(self): + self.anticaptcha_key_fail = os.getenv("anticaptcha_key")[:5] + self.anticaptcha_key_true = os.getenv("anticaptcha_key") + self.server_ip = "85.255.8.26" + self.image_url = "https://raw.githubusercontent.com/AndreiDrang/python3-anticaptcha/master/anticaptcha_examples/088636.png" diff --git a/tests/test_Callback.py b/tests/test_Callback.py new file mode 100644 index 0000000..224ab65 --- /dev/null +++ b/tests/test_Callback.py @@ -0,0 +1,16 @@ +import requests + +from main import MainAntiCaptcha + + +class TestAntiCaptcha(MainAntiCaptcha): + def test_callback_server(self): + # test server alive + response = requests.get(f"http://{self.server_ip}:8001/ping") + assert response.status_code == 200 + # try register new queue + response = requests.post( + f"http://{self.server_ip}:8001/register_key", + json={"key": "fwefefefopewofkewopfkop", "vhost": "anticaptcha_vhost"}, + ) + assert response.status_code == 200 diff --git a/tests/test_CustomCaptcha.py b/tests/test_CustomCaptcha.py new file mode 100644 index 0000000..26dbb6e --- /dev/null +++ b/tests/test_CustomCaptcha.py @@ -0,0 +1,154 @@ +import inspect +import asyncio + +import pytest + +from python3_anticaptcha import CustomCaptchaTask + +from main import MainAntiCaptcha + + +class TestAntiCaptcha(MainAntiCaptcha): + CUSTOM_TASK = "2+2=?" + """ + Params check + """ + + def test_customcatpcha_params(self): + default_init_params = [ + "self", + "anticaptcha_key", + "sleep_time", + "assignment", + "forms", + "callbackUrl", + ] + default_handler_params = ["self", "imageUrl"] + # get customcaptcha init and captcha_handler params + aioinit_params = inspect.getfullargspec( + CustomCaptchaTask.aioCustomCaptchaTask.__init__ + ) + aiohandler_params = inspect.getfullargspec( + CustomCaptchaTask.aioCustomCaptchaTask.captcha_handler + ) + + # get customcaptcha init and captcha_handler params + init_params = inspect.getfullargspec( + CustomCaptchaTask.CustomCaptchaTask.__init__ + ) + handler_params = inspect.getfullargspec( + CustomCaptchaTask.CustomCaptchaTask.captcha_handler + ) + # 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] + + """ + Response checking + """ + + def test_response_customcaptcha(self): + customcaptcha = CustomCaptchaTask.CustomCaptchaTask( + anticaptcha_key=self.anticaptcha_key_fail, assignment=self.CUSTOM_TASK + ) + # check response type + assert type(customcaptcha) is CustomCaptchaTask.CustomCaptchaTask + + response = customcaptcha.captcha_handler(imageUrl=self.image_url) + # check response type + assert type(response) is dict + # check all dict keys + assert ["errorId", "errorCode", "errorDescription"] == list(response.keys()) + + @pytest.mark.asyncio + async def test_response_aiocustomcaptcha(self): + customcaptcha = CustomCaptchaTask.aioCustomCaptchaTask( + anticaptcha_key=self.anticaptcha_key_fail, assignment=self.CUSTOM_TASK + ) + # check response type + assert type(customcaptcha) is CustomCaptchaTask.CustomCaptchaTask + + response = await customcaptcha.captcha_handler(imageUrl=self.image_url) + # check response type + assert type(response) is dict + # check all dict keys + assert ["errorId", "errorCode", "errorDescription"] == list(response.keys()) + + """ + Fail tests + """ + + def test_fail_customcaptcha(self): + customcaptcha = CustomCaptchaTask.CustomCaptchaTask( + anticaptcha_key=self.anticaptcha_key_fail, assignment=self.CUSTOM_TASK + ) + + response = customcaptcha.captcha_handler(imageUrl=self.image_url) + + assert 1 == response["errorId"] + + def test_fail_customcaptcha_context(self): + with CustomCaptchaTask.CustomCaptchaTask( + anticaptcha_key=self.anticaptcha_key_fail, assignment=self.CUSTOM_TASK + ) as customcaptcha: + + response = customcaptcha.captcha_handler(imageUrl=self.image_url) + + assert 1 == response["errorId"] + + @pytest.mark.asyncio + async def test_fail_aiocustomcaptcha(self): + customcaptcha = CustomCaptchaTask.aioCustomCaptchaTask( + anticaptcha_key=self.anticaptcha_key_fail, assignment=self.CUSTOM_TASK + ) + response = await customcaptcha.captcha_handler(imageUrl=self.image_url) + assert 1 == response["errorId"] + + @pytest.mark.asyncio + async def test_fail_aiocustomcaptcha_context(self): + with CustomCaptchaTask.aioCustomCaptchaTask( + anticaptcha_key=self.anticaptcha_key_fail, assignment=self.CUSTOM_TASK + ) as customcaptcha: + response = await customcaptcha.captcha_handler(imageUrl=self.image_url) + assert 1 == response["errorId"] + + """ + True tests + """ + + def test_true_customcaptcha(self): + customcaptcha = CustomCaptchaTask.CustomCaptchaTask( + anticaptcha_key=self.anticaptcha_key_true, assignment=self.CUSTOM_TASK + ) + + response = customcaptcha.captcha_handler(imageUrl=self.image_url) + + assert 0 == response["errorId"] + + def test_true_customcaptcha_context(self): + with CustomCaptchaTask.CustomCaptchaTask( + anticaptcha_key=self.anticaptcha_key_true, assignment=self.CUSTOM_TASK + ) as customcaptcha: + + response = customcaptcha.captcha_handler(imageUrl=self.image_url) + + assert 0 == response["errorId"] + + @pytest.mark.asyncio + async def test_true_aiocustomcaptcha(self): + customcaptcha = CustomCaptchaTask.aioCustomCaptchaTask( + anticaptcha_key=self.anticaptcha_key_true, assignment=self.CUSTOM_TASK + ) + response = await customcaptcha.captcha_handler(imageUrl=self.image_url) + assert 0 == response["errorId"] + + @pytest.mark.asyncio + async def test_true_aiocustomcaptcha_context(self): + with CustomCaptchaTask.aioCustomCaptchaTask( + anticaptcha_key=self.anticaptcha_key_true, assignment=self.CUSTOM_TASK + ) as customcaptcha: + response = await customcaptcha.captcha_handler(imageUrl=self.image_url) + assert 0 == response["errorId"] diff --git a/tests/test_CustomResultHandler.py b/tests/test_CustomResultHandler.py new file mode 100644 index 0000000..c74c2f7 --- /dev/null +++ b/tests/test_CustomResultHandler.py @@ -0,0 +1,164 @@ +import inspect +import asyncio + +import pytest + +from python3_anticaptcha import CustomResultHandler + +from main import MainAntiCaptcha + + +class TestAntiCaptcha(MainAntiCaptcha): + """ + Params check + """ + + def test_result_handler_params(self): + default_init_params = ["self", "anticaptcha_key", "sleep_time"] + default_handler_params = ["self", "task_id"] + # get customcaptcha init and task_handler params + aioinit_params = inspect.getfullargspec( + CustomResultHandler.aioCustomResultHandler.__init__ + ) + aiohandler_params = inspect.getfullargspec( + CustomResultHandler.aioCustomResultHandler.task_handler + ) + + # get customcaptcha init and task_handler params + init_params = inspect.getfullargspec( + CustomResultHandler.CustomResultHandler.__init__ + ) + handler_params = inspect.getfullargspec( + CustomResultHandler.CustomResultHandler.task_handler + ) + # 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] + + """ + Response checking + """ + + def test_response_result_handler(self): + # prepare client + custom_result = CustomResultHandler.CustomResultHandler( + anticaptcha_key=self.anticaptcha_key_true + ) + # get balance + response = custom_result.task_handler(task_id=self.WRONG_TASK_ID) + # check response type + assert type(response) is dict + # check all dict keys + assert ["errorId", "errorCode", "errorDescription", "taskId"] == list( + response.keys() + ) + + @pytest.mark.asyncio + async def test_response_aioresult_handler(self): + # prepare client + custom_result = CustomResultHandler.aioCustomResultHandler( + anticaptcha_key=self.anticaptcha_key_true + ) + # get balance + response = await custom_result.task_handler(task_id=self.WRONG_TASK_ID) + # check response type + assert type(response) is dict + # check all dict keys + assert ["errorId", "errorCode", "errorDescription", "taskId"] == list( + response.keys() + ) + + """ + Fail tests + """ + + def test_fail_result_handler(self): + # prepare client + custom_result = CustomResultHandler.CustomResultHandler( + anticaptcha_key=self.anticaptcha_key_fail + ) + # get balance + response = custom_result.task_handler(task_id=self.WRONG_TASK_ID) + # check error code + assert 1 == response["errorId"] + + def test_fail_result_handler_context(self): + # prepare client + with CustomResultHandler.CustomResultHandler( + anticaptcha_key=self.anticaptcha_key_fail + ) as custom_result: + # get balance + response = custom_result.task_handler(task_id=self.WRONG_TASK_ID) + # check error code + assert 1 == response["errorId"] + + @pytest.mark.asyncio + async def test_fail_aioresult_handler(self): + # prepare client + custom_result = CustomResultHandler.aioCustomResultHandler( + anticaptcha_key=self.anticaptcha_key_fail + ) + # get balance + response = await custom_result.task_handler(task_id=self.WRONG_TASK_ID) + # check error code + assert 1 == response["errorId"] + + @pytest.mark.asyncio + async def test_fail_aioresult_handler_context(self): + # prepare client + with CustomResultHandler.aioCustomResultHandler( + anticaptcha_key=self.anticaptcha_key_fail + ) as custom_result: + # get balance + response = await custom_result.task_handler(task_id=self.WRONG_TASK_ID) + # check error code + assert 1 == response["errorId"] + + """ + True tests + """ + + def test_true_result_handler(self): + # prepare client + custom_result = CustomResultHandler.CustomResultHandler( + anticaptcha_key=self.anticaptcha_key_true + ) + # get balance + response = custom_result.task_handler(task_id=self.WRONG_TASK_ID) + # check error code + assert 16 == response["errorId"] + + def test_true_result_handler_context(self): + # prepare client + with CustomResultHandler.CustomResultHandler( + anticaptcha_key=self.anticaptcha_key_true + ) as custom_result: + # get balance + response = custom_result.task_handler(task_id=self.WRONG_TASK_ID) + # check error code + assert 16 == response["errorId"] + + @pytest.mark.asyncio + async def test_true_aioresult_handler(self): + # prepare client + custom_result = CustomResultHandler.aioCustomResultHandler( + anticaptcha_key=self.anticaptcha_key_true + ) + # get balance + response = await custom_result.task_handler(task_id=self.WRONG_TASK_ID) + # check error code + assert 16 == response["errorId"] + + @pytest.mark.asyncio + async def test_true_aioresult_handler_context(self): + # prepare client + with CustomResultHandler.aioCustomResultHandler( + anticaptcha_key=self.anticaptcha_key_true + ) as custom_result: + # get balance + response = await custom_result.task_handler(task_id=self.WRONG_TASK_ID) + # check error code + assert 16 == response["errorId"]