Skip to content

Commit

Permalink
Refactor add new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiDrang committed Mar 11, 2020
1 parent cc0dde2 commit 03c40c0
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 273 deletions.
258 changes: 0 additions & 258 deletions test.py

This file was deleted.

1 change: 0 additions & 1 deletion tests/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os


# 1. `export anticaptcha_key=274832f8168a36019895a1e1174777c0`


Expand Down
13 changes: 8 additions & 5 deletions tests/test_Control.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import inspect
import random
import inspect

import pytest
import requests_mock

from python3_anticaptcha import AntiCaptchaControl, config

from tests.main import MainAntiCaptcha
from python3_anticaptcha import AntiCaptchaControl, config


class TestControl(MainAntiCaptcha):
Expand Down Expand Up @@ -116,7 +115,9 @@ def test_complaint_image_payload(self):
task_id = 123456

with requests_mock.Mocker() as req_mock:
req_mock.post(AntiCaptchaControl.incorrect_imagecaptcha_url, json=self.ERROR_RESPONSE_JSON)
req_mock.post(
AntiCaptchaControl.incorrect_imagecaptcha_url, json=self.ERROR_RESPONSE_JSON
)
control.complaint_on_result(
reported_id=task_id, captcha_type=AntiCaptchaControl.complaint_types[0]
)
Expand All @@ -139,7 +140,9 @@ def test_complaint_re_payload(self):
task_id = 123456

with requests_mock.Mocker() as req_mock:
req_mock.post(AntiCaptchaControl.incorrect_recaptcha_url, json=self.ERROR_RESPONSE_JSON)
req_mock.post(
AntiCaptchaControl.incorrect_recaptcha_url, json=self.ERROR_RESPONSE_JSON
)
control.complaint_on_result(
reported_id=task_id, captcha_type=AntiCaptchaControl.complaint_types[1]
)
Expand Down
3 changes: 1 addition & 2 deletions tests/test_CustomCaptcha.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
import pytest
import requests_mock

from python3_anticaptcha import CustomCaptchaTask, config

from tests.main import MainAntiCaptcha
from python3_anticaptcha import CustomCaptchaTask, config


class TestCustom(MainAntiCaptcha):
Expand Down
3 changes: 1 addition & 2 deletions tests/test_CustomResultHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import pytest

from python3_anticaptcha import CustomResultHandler

from tests.main import MainAntiCaptcha
from python3_anticaptcha import CustomResultHandler


class TestAntiCaptcha(MainAntiCaptcha):
Expand Down
5 changes: 2 additions & 3 deletions tests/test_ImageToText.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
import pytest
import requests_mock

from python3_anticaptcha import ImageToTextTask, config

from tests.main import MainAntiCaptcha
from python3_anticaptcha import ImageToTextTask, config


class TestAntiCaptcha(MainAntiCaptcha):
class TestImageToTextCaptcha(MainAntiCaptcha):
WRONG_SAVE_FORMAT = "qwerty"

"""
Expand Down
58 changes: 58 additions & 0 deletions tests/test_NoCaptcha.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import inspect

import pytest
import requests_mock

from tests.main import MainAntiCaptcha
from python3_anticaptcha import NoCaptchaTask, config


class TestNoCaptcha(MainAntiCaptcha):
WEBSITE_URL = "https://www.google.com/recaptcha/api2/demo"
WEBSITE_KEY = "6Le-wvkSAAAAAPBMRTvw0Q4Muexq9bi0DJwx_mJ-"

"""
Params check
"""

def test_nocaptcha_params(self):
default_init_params = ["self", "anticaptcha_key", "sleep_time", "callbackUrl"]
default_handler_params = ["self", "websiteURL", "websiteKey"]
# get customcaptcha init and captcha_handler params
aioinit_params = inspect.getfullargspec(NoCaptchaTask.aioNoCaptchaTask.__init__)
aiohandler_params = inspect.getfullargspec(NoCaptchaTask.aioNoCaptchaTask.captcha_handler)

# get customcaptcha init and captcha_handler params
init_params = inspect.getfullargspec(NoCaptchaTask.NoCaptchaTask.__init__)
handler_params = inspect.getfullargspec(NoCaptchaTask.NoCaptchaTask.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]

"""
Request payload test MOCK
"""

def test_create_task_payload(self):
no_captcha = NoCaptchaTask.NoCaptchaTask(anticaptcha_key=self.anticaptcha_key_fail)
# check response type
assert isinstance(no_captcha, NoCaptchaTask.NoCaptchaTask)

with requests_mock.Mocker() as req_mock:
req_mock.post(config.create_task_url, json=self.ERROR_RESPONSE_JSON)
no_captcha.captcha_handler(websiteURL=self.WEBSITE_URL, websiteKey=self.WEBSITE_KEY)

history = req_mock.request_history

assert len(history) == 1

request_payload = history[0].json()

# check all dict keys
assert ["clientKey", "task", "softId"] == list(request_payload.keys())
assert request_payload["softId"] == config.app_key
assert ["type", "websiteURL", "websiteKey"] == list(request_payload["task"].keys())
assert request_payload["task"]["type"] == "NoCaptchaTask"
Loading

0 comments on commit 03c40c0

Please sign in to comment.