Skip to content

Commit

Permalink
Add new tests && fix url && rename classes (#54)
Browse files Browse the repository at this point in the history
* Add new tests && fix url && rename classes

* Refactor
  • Loading branch information
AndreiDrang authored Dec 20, 2019
1 parent 458004d commit 901ee7c
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 4 deletions.
2 changes: 1 addition & 1 deletion python3_anticaptcha/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# Адрес для отправки жалобы на неверное решение капчи-изображения
incorrect_imagecaptcha_url = "https://api.anti-captcha.com/reportIncorrectImageCaptcha"
# Адрес для отправки жалобы на неверное решение ReCaptcha
incorrect_recaptcha_url = "https://api.anti-captcha.com/reportIncorrectRecaptcha "
incorrect_recaptcha_url = "https://api.anti-captcha.com/reportIncorrectRecaptcha"
# Адрес для получения информации о очереди
get_queue_status_url = "https://api.anti-captcha.com/getQueueStats"
# Адрес для получения информации о приложении
Expand Down
94 changes: 92 additions & 2 deletions tests/test_Control.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import inspect
import random

import pytest
import requests_mock

from python3_anticaptcha import AntiCaptchaControl, config

from tests.main import MainAntiCaptcha


class TestAntiCaptcha(MainAntiCaptcha):
class TestControl(MainAntiCaptcha):
WRONG_MODE = WRONG_CAPTCHA_TYPE = "qwerty"
WRONG_SOFT_ID = "-1" + config.app_key
REPORT_ID = WRONG_QUEUE_ID = -1
Expand All @@ -16,7 +18,7 @@ class TestAntiCaptcha(MainAntiCaptcha):
Params check
"""

def test_customcatpcha_params(self):
def test_params(self):
default_init_params = ["self", "anticaptcha_key"]
default_get_balance_params = ["self"]
default_app_stats_params = ["self", "softId", "mode"]
Expand Down Expand Up @@ -62,6 +64,94 @@ def test_customcatpcha_params(self):
assert default_complaint_params == complaint_params[0]
assert default_queue_status_params == queue_status_params[0]

def test_balance_payload(self):
control = AntiCaptchaControl.AntiCaptchaControl(anticaptcha_key=self.anticaptcha_key_true)
# check response type
assert isinstance(control, AntiCaptchaControl.AntiCaptchaControl)

with requests_mock.Mocker() as req_mock:
req_mock.post(config.get_balance_url, json=self.ERROR_RESPONSE_JSON)
control.get_balance()

history = req_mock.request_history

assert len(history) == 1

request_payload = history[0].json()

# check all dict keys
assert ["clientKey",] == list(request_payload.keys())
assert request_payload["clientKey"] == self.anticaptcha_key_true

def test_stats_payload(self):
control = AntiCaptchaControl.AntiCaptchaControl(anticaptcha_key=self.anticaptcha_key_true)
# check response type
assert isinstance(control, AntiCaptchaControl.AntiCaptchaControl)
mode = random.choice(AntiCaptchaControl.mods)

with requests_mock.Mocker() as req_mock:
req_mock.post(config.get_app_stats_url, json=self.ERROR_RESPONSE_JSON)
control.get_app_stats(softId=config.app_key, mode=mode)

history = req_mock.request_history

assert len(history) == 1

request_payload = history[0].json()

# check all dict keys
assert ["clientKey", "softId", "mode"] == list(request_payload.keys())
assert request_payload["clientKey"] == self.anticaptcha_key_true
assert request_payload["softId"] == config.app_key
assert request_payload["mode"] == mode

def test_complaint_image_payload(self):
control = AntiCaptchaControl.AntiCaptchaControl(anticaptcha_key=self.anticaptcha_key_true)
# check response type
assert isinstance(control, AntiCaptchaControl.AntiCaptchaControl)
task_id = 123456

with requests_mock.Mocker() as req_mock:
req_mock.post(config.incorrect_imagecaptcha_url, json=self.ERROR_RESPONSE_JSON)
control.complaint_on_result(
reported_id=task_id, captcha_type=AntiCaptchaControl.complaint_types[0]
)

history = req_mock.request_history

assert len(history) == 1

request_payload = history[0].json()

# check all dict keys
assert ["clientKey", "taskId"] == list(request_payload.keys())
assert request_payload["clientKey"] == self.anticaptcha_key_true
assert request_payload["taskId"] == task_id

def test_complaint_re_payload(self):
control = AntiCaptchaControl.AntiCaptchaControl(anticaptcha_key=self.anticaptcha_key_true)
# check response type
assert isinstance(control, AntiCaptchaControl.AntiCaptchaControl)
task_id = 123456
print(config.incorrect_recaptcha_url)
print(AntiCaptchaControl.complaint_types[1])
with requests_mock.Mocker() as req_mock:
req_mock.post(config.incorrect_recaptcha_url, json=self.ERROR_RESPONSE_JSON)
control.complaint_on_result(
reported_id=task_id, captcha_type=AntiCaptchaControl.complaint_types[1]
)

history = req_mock.request_history

assert len(history) == 1

request_payload = history[0].json()

# check all dict keys
assert ["clientKey", "taskId"] == list(request_payload.keys())
assert request_payload["clientKey"] == self.anticaptcha_key_true
assert request_payload["taskId"] == task_id

"""
Response checking
"""
Expand Down
2 changes: 1 addition & 1 deletion tests/test_CustomCaptcha.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from tests.main import MainAntiCaptcha


class TestAntiCaptcha(MainAntiCaptcha):
class TestCustom(MainAntiCaptcha):
CUSTOM_TASK = "2+2=?"

"""
Expand Down

0 comments on commit 901ee7c

Please sign in to comment.