From ccff95b738edfed8edf7c98a17a524471df6db07 Mon Sep 17 00:00:00 2001 From: Andrei Date: Wed, 6 Dec 2023 20:33:26 +0300 Subject: [PATCH] Update test_audio.py --- tests/test_audio.py | 173 ++++++++++++++++---------------------------- 1 file changed, 64 insertions(+), 109 deletions(-) diff --git a/tests/test_audio.py b/tests/test_audio.py index 2f170006..e595d710 100644 --- a/tests/test_audio.py +++ b/tests/test_audio.py @@ -18,130 +18,116 @@ def test_methods_exists(self): assert "captcha_handler" in AudioCaptcha.__dict__.keys() assert "aio_captcha_handler" in AudioCaptcha.__dict__.keys() + def test_args(self): + instance = AudioCaptcha(rucaptcha_key=self.RUCAPTCHA_KEY) + assert instance.create_task_payload["clientKey"] == self.RUCAPTCHA_KEY + @pytest.mark.parametrize("save_format", [SaveFormatsEnm.TEMP, SaveFormatsEnm.CONST]) def test_basic_data_file(self, save_format): instance = AudioCaptcha(rucaptcha_key=self.RUCAPTCHA_KEY, save_format=save_format) - assert instance.params.rucaptcha_key == self.RUCAPTCHA_KEY - result = instance.captcha_handler(captcha_file=self.captcha_file) 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"]["token"], str) 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 result["errorBody"] == "ERROR_CAPTCHA_UNSOLVABLE" + assert result["errorId"] in (1, 12) + assert result["errorCode"] == "ERROR_CAPTCHA_UNSOLVABLE" - assert result.keys() == GetTaskResultResponseSer().dict().keys() + assert result.keys() == GetTaskResultResponseSer().to_dict().keys() @pytest.mark.parametrize("save_format", [SaveFormatsEnm.TEMP, SaveFormatsEnm.CONST]) def test_basic_data_link(self, save_format): instance = AudioCaptcha(rucaptcha_key=self.RUCAPTCHA_KEY, save_format=save_format) - assert instance.params.rucaptcha_key == self.RUCAPTCHA_KEY - result = instance.captcha_handler(captcha_link=self.captcha_link) 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"]["token"], str) 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 result["errorBody"] == "ERROR_CAPTCHA_UNSOLVABLE" + assert result["errorId"] in (1, 12) + assert result["errorCode"] == "ERROR_CAPTCHA_UNSOLVABLE" - assert result.keys() == GetTaskResultResponseSer().dict().keys() + assert result.keys() == GetTaskResultResponseSer().to_dict().keys() @pytest.mark.parametrize("save_format", [SaveFormatsEnm.TEMP, SaveFormatsEnm.CONST]) def test_basic_data_base64(self, save_format): instance = AudioCaptcha(rucaptcha_key=self.RUCAPTCHA_KEY, save_format=save_format) - assert instance.params.rucaptcha_key == self.RUCAPTCHA_KEY - with open(self.captcha_file, "rb") as f: result = instance.captcha_handler(captcha_base64=f.read()) 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"]["token"], str) 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 result["errorBody"] == "ERROR_CAPTCHA_UNSOLVABLE" + assert result["errorId"] in (1, 12) + assert result["errorCode"] == "ERROR_CAPTCHA_UNSOLVABLE" - assert result.keys() == GetTaskResultResponseSer().dict().keys() + assert result.keys() == GetTaskResultResponseSer().to_dict().keys() @pytest.mark.asyncio @pytest.mark.parametrize("save_format", [SaveFormatsEnm.TEMP, SaveFormatsEnm.CONST]) async def test_aio_basic_data_file(self, save_format): instance = AudioCaptcha(rucaptcha_key=self.RUCAPTCHA_KEY, save_format=save_format) - assert instance.params.rucaptcha_key == self.RUCAPTCHA_KEY - result = await instance.aio_captcha_handler(captcha_file=self.captcha_file) 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"]["token"], str) 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 result["errorBody"] == "ERROR_CAPTCHA_UNSOLVABLE" + assert result["errorId"] in (1, 12) + assert result["errorCode"] == "ERROR_CAPTCHA_UNSOLVABLE" - assert result.keys() == GetTaskResultResponseSer().dict().keys() + assert result.keys() == GetTaskResultResponseSer().to_dict().keys() @pytest.mark.asyncio @pytest.mark.parametrize("save_format", [SaveFormatsEnm.TEMP, SaveFormatsEnm.CONST]) async def test_aio_basic_data_link(self, save_format): instance = AudioCaptcha(rucaptcha_key=self.RUCAPTCHA_KEY, save_format=save_format) - assert instance.params.rucaptcha_key == self.RUCAPTCHA_KEY - result = await instance.aio_captcha_handler(captcha_link=self.captcha_link) 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"]["token"], str) 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 result["errorBody"] == "ERROR_CAPTCHA_UNSOLVABLE" + assert result["errorId"] in (1, 12) + assert result["errorCode"] == "ERROR_CAPTCHA_UNSOLVABLE" - assert result.keys() == GetTaskResultResponseSer().dict().keys() + assert result.keys() == GetTaskResultResponseSer().to_dict().keys() @pytest.mark.asyncio @pytest.mark.parametrize("save_format", [SaveFormatsEnm.TEMP, SaveFormatsEnm.CONST]) async def test_aio_basic_data_base64(self, save_format): instance = AudioCaptcha(rucaptcha_key=self.RUCAPTCHA_KEY, save_format=save_format) - assert instance.params.rucaptcha_key == self.RUCAPTCHA_KEY - with open(self.captcha_file, "rb") as f: result = await instance.aio_captcha_handler(captcha_base64=f.read()) 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"]["token"], str) 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 result["errorBody"] == "ERROR_CAPTCHA_UNSOLVABLE" + assert result["errorId"] in (1, 12) + assert result["errorCode"] == "ERROR_CAPTCHA_UNSOLVABLE" - assert result.keys() == GetTaskResultResponseSer().dict().keys() + assert result.keys() == GetTaskResultResponseSer().to_dict().keys() """ Fail tests @@ -150,108 +136,77 @@ async def test_aio_basic_data_base64(self, save_format): def test_no_captcha(self): instance = AudioCaptcha(rucaptcha_key=self.RUCAPTCHA_KEY) - assert instance.params.rucaptcha_key == self.RUCAPTCHA_KEY - result = instance.captcha_handler() assert isinstance(result, dict) is True - assert result["error"] is True - assert result["taskId"] is None - assert result["errorBody"] == AudioCaptcha.NO_CAPTCHA_ERR - assert result["captchaSolve"] == {} - - assert result.keys() == GetTaskResultResponseSer().dict().keys() + assert result["errorId"] == 12 + assert isinstance(result["solution"]["token"], str) is True + assert result.keys() == GetTaskResultResponseSer().to_dict().keys() @pytest.mark.asyncio async def test_aio_no_captcha(self): instance = AudioCaptcha(rucaptcha_key=self.RUCAPTCHA_KEY) - assert instance.params.rucaptcha_key == self.RUCAPTCHA_KEY - result = await instance.aio_captcha_handler() assert isinstance(result, dict) is True - assert result["error"] is True - assert result["taskId"] is None - assert result["errorBody"] == AudioCaptcha.NO_CAPTCHA_ERR - assert result["captchaSolve"] == {} - assert result.keys() == GetTaskResultResponseSer().dict().keys() + assert result["errorId"] == 12 + assert isinstance(result["solution"]["token"], str) is True + assert result.keys() == GetTaskResultResponseSer().to_dict().keys() def test_wrong_link(self): instance = AudioCaptcha(rucaptcha_key=self.RUCAPTCHA_KEY) - assert instance.params.rucaptcha_key == self.RUCAPTCHA_KEY - result = instance.captcha_handler(captcha_link=self.get_random_string(length=50)) assert isinstance(result, dict) is True - assert result["error"] is True - assert result["taskId"] is None - assert result["captchaSolve"] == {} - assert result.keys() == GetTaskResultResponseSer().dict().keys() + assert result["errorId"] == 12 + assert isinstance(result["solution"]["token"], str) is True + assert result.keys() == GetTaskResultResponseSer().to_dict().keys() def test_wrong_path(self): instance = AudioCaptcha(rucaptcha_key=self.RUCAPTCHA_KEY) - assert instance.params.rucaptcha_key == self.RUCAPTCHA_KEY - with pytest.raises(FileNotFoundError): - result = instance.captcha_handler(captcha_file=self.get_random_string(length=50)) - - assert isinstance(result, dict) is True - assert result["error"] is True - assert result["taskId"] is None - assert result["captchaSolve"] == {} - assert result.keys() == GetTaskResultResponseSer().dict().keys() + instance.captcha_handler(captcha_file=self.get_random_string(length=50)) def test_wrong_base64(self): instance = AudioCaptcha(rucaptcha_key=self.RUCAPTCHA_KEY) - assert instance.params.rucaptcha_key == self.RUCAPTCHA_KEY + result = instance.captcha_handler(captcha_base64=self.get_random_string(length=50).encode(encoding="UTF-8")) + assert isinstance(result, dict) is True - assert result["error"] is True - assert result["taskId"] is None - assert result["captchaSolve"] == {} - assert result.keys() == GetTaskResultResponseSer().dict().keys() + assert result["errorId"] == 12 + assert isinstance(result["solution"]["token"], str) is True + assert result.keys() == GetTaskResultResponseSer().to_dict().keys() @pytest.mark.asyncio async def test_aio_wrong_link(self): instance = AudioCaptcha(rucaptcha_key=self.RUCAPTCHA_KEY) - assert instance.params.rucaptcha_key == self.RUCAPTCHA_KEY - result = await instance.aio_captcha_handler(captcha_link=self.get_random_string(length=50)) assert isinstance(result, dict) is True - assert result["error"] is True - assert result["taskId"] is None - assert result["captchaSolve"] == {} - assert result.keys() == GetTaskResultResponseSer().dict().keys() + assert result["errorId"] == 12 + assert isinstance(result["solution"]["token"], str) is True + assert result.keys() == GetTaskResultResponseSer().to_dict().keys() @pytest.mark.asyncio async def test_aio_wrong_path(self): instance = AudioCaptcha(rucaptcha_key=self.RUCAPTCHA_KEY) - assert instance.params.rucaptcha_key == self.RUCAPTCHA_KEY - with pytest.raises(FileNotFoundError): - result = await instance.aio_captcha_handler(captcha_file=self.get_random_string(length=50)) - - assert isinstance(result, dict) is True - assert result["error"] is True - assert result["taskId"] is None - assert result["captchaSolve"] == {} - assert result.keys() == GetTaskResultResponseSer().dict().keys() + await instance.aio_captcha_handler(captcha_file=self.get_random_string(length=50)) @pytest.mark.asyncio async def test_aio_wrong_base64(self): instance = AudioCaptcha(rucaptcha_key=self.RUCAPTCHA_KEY) - assert instance.params.rucaptcha_key == self.RUCAPTCHA_KEY + result = await instance.aio_captcha_handler( captcha_base64=self.get_random_string(length=50).encode(encoding="UTF-8") ) + assert isinstance(result, dict) is True - assert result["error"] is True - assert result["taskId"] is None - assert result["captchaSolve"] == {} - assert result.keys() == GetTaskResultResponseSer().dict().keys() + assert result["errorId"] == 12 + assert isinstance(result["solution"]["token"], str) is True + assert result.keys() == GetTaskResultResponseSer().to_dict().keys()