Ejemplo n.º 1
0
def test_web3_retries_block_not_found(
        patched_web3: Web3, succeed_at: int,
        requests_responses: responses.RequestsMock):
    requests_responses.add_callback(
        responses.POST,
        "http://domain/",
        callback=_make_json_rpc_null_response(succeed_at))

    result = patched_web3.eth.getBlock(BlockNumber(1))
    assert result["number"] == 1
Ejemplo n.º 2
0
def mock_recaptcha_verify_api(responses: RequestsMock,
                              faker: Faker) -> RequestsMock:
    from requests.models import PreparedRequest
    from mailer import recaptcha

    def request_callback(
        request: PreparedRequest, ) -> Tuple[HTTPStatus, Dict[str, Any], str]:
        import json
        from urllib.parse import parse_qs

        headers: Dict[str, Any] = {}

        params = parse_qs(str(request.body))
        secret = params.get("secret")
        response = params.get("response")

        errors = []
        if secret and secret != [valid_recaptcha_secret]:
            errors.append("invalid-input-secret")

        if not response:
            errors.append("missing-input-response")
        elif response != [valid_recaptcha_response]:
            errors.append("invalid-input-response")

        body: Dict[str, Any] = {}
        if len(errors) > 0:
            body["success"] = False
            body["error-codes"] = errors
        else:
            body["success"] = True
            body["challenge_ts"] = faker.iso8601()
            body["hostname"] = faker.hostname()

        return (HTTPStatus.OK, headers, json.dumps(body))

    responses.add_callback(
        responses.POST,
        recaptcha.verify_url,
        callback=request_callback,
        content_type="application/json",
    )

    return responses
Ejemplo n.º 3
0
def test_fetch_urls_redirection(
    logger: logging.Logger, requests_mock: responses.RequestsMock
) -> None:
    original_url = "http://example.com/original"
    redirected_url = "https://content.example.com/redirected"

    def request_callback(
        request: requests.PreparedRequest,
    ) -> Tuple[int, Mapping[str, str], bytes]:
        if request.url == redirected_url:
            return 200, {}, b"content"
        else:
            return 301, {"location": redirected_url}, b""

    requests_mock.add_callback(
        requests_mock.GET, original_url, callback=request_callback
    )
    requests_mock.add_callback(
        requests_mock.GET, redirected_url, callback=request_callback
    )

    meta_memory_storage = MemoryStorage()
    content_memory_storage = ContentMemoryStorage()

    fetch_urls_single(
        [original_url],
        meta_storage=meta_memory_storage,
        content_storage=content_memory_storage,
        logger=logger,
    )

    meta = get_meta(redirected_url, meta_storage=meta_memory_storage, logger=logger)
    assert meta is None
    meta = get_meta(original_url, meta_storage=meta_memory_storage, logger=logger)
    assert meta is not None
    assert meta.cached_url == "memory:http://example.com/original"

    content_storage = content_memory_storage.dict_for_debug()
    print(content_storage)
    assert content_storage[original_url].value == b"content"
Ejemplo n.º 4
0
def test_custom_failure_status_exception_health_check(
        mock_http_health_datetime, responses: RequestsMock):
    def send_failure(*args, **kwargs):
        raise requests.ConnectionError()

    responses.add_callback(url="http://test/health",
                           method=responses.GET,
                           callback=send_failure)
    assert healthpy.requests.check(
        "tests",
        "http://test/health",
        error_status_extracting=lambda x: x.trigger_exception,
    ) == (
        "fail",
        {
            "tests:health": {
                "componentType": "http://test/health",
                "output": "",
                "status": "fail",
                "time": "2018-10-11T15:05:05.663979",
            }
        },
    )