Skip to content

Commit

Permalink
Update test_audio.py
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiDrang committed Dec 6, 2023
1 parent fbd270b commit ccff95b
Showing 1 changed file with 64 additions and 109 deletions.
173 changes: 64 additions & 109 deletions tests/test_audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()

0 comments on commit ccff95b

Please sign in to comment.