ping api and tests

This commit is contained in:
Andrew Herrington
2021-12-11 23:38:03 -06:00
parent 7f1ca2d331
commit fb50209a5f
12 changed files with 719 additions and 18 deletions

View File

@@ -1,10 +1,86 @@
from copy import deepcopy
import pytest
from httpx import Request
from httpx import Response
from healthchecks_io import BadAPIRequestError
from healthchecks_io import CheckNotFoundError
from healthchecks_io import HCAPIAuthError
from healthchecks_io import HCAPIError
from healthchecks_io import HCAPIRateLimitError
from healthchecks_io import NonUniqueSlugError
from healthchecks_io.client._abstract import AbstractClient
def test_abstract_add_url_params():
AbstractClient.__abstractmethods__ = set()
abstract_client = AbstractClient("test")
url = abstract_client._add_url_params(
def test_abstract_add_url_params(test_abstract_client):
url = test_abstract_client._add_url_params(
"http://test.com/?test=test", {"test": "test2"}
)
assert url == "http://test.com/?test=test2"
def test_get_ping_url(test_abstract_client):
url = test_abstract_client._get_ping_url("test", "", "/endpoint")
assert url == f"{test_abstract_client._ping_url}test/endpoint"
# test for raising when we send both a slug and a uuid
with pytest.raises(BadAPIRequestError):
test_abstract_client._get_ping_url("uuid", "slug", "endpoint")
# test for raising when we try a slug w/o a ping_key
test_abstract_client._ping_key = ""
with pytest.raises(BadAPIRequestError):
test_abstract_client._get_ping_url("", "slug", "endpoint")
check_response_parameters = [
(
pytest.lazy_fixture("test_abstract_client"),
Response(status_code=401, request=Request("get", "http://test")),
HCAPIAuthError,
),
(
pytest.lazy_fixture("test_abstract_client"),
Response(status_code=500, request=Request("get", "http://test")),
HCAPIError,
),
(
pytest.lazy_fixture("test_abstract_client"),
Response(status_code=429, request=Request("get", "http://test")),
HCAPIRateLimitError,
),
(
pytest.lazy_fixture("test_abstract_client"),
Response(status_code=404, request=Request("get", "http://test")),
CheckNotFoundError,
),
(
pytest.lazy_fixture("test_abstract_client"),
Response(status_code=400, request=Request("get", "http://test")),
BadAPIRequestError,
),
]
@pytest.mark.parametrize("test_client, response, exception", check_response_parameters)
def test_check_resposne(test_client, response, exception):
with pytest.raises(exception):
test_client.check_response(response)
ping_response_parameters = deepcopy(check_response_parameters)
ping_response_parameters.append(
(
pytest.lazy_fixture("test_abstract_client"),
Response(status_code=409, request=Request("get", "http://test")),
NonUniqueSlugError,
)
)
@pytest.mark.parametrize("test_client, response, exception", ping_response_parameters)
def test_check_ping_resposne(test_client, response, exception):
with pytest.raises(exception):
test_client.check_ping_response(response)

View File

@@ -222,3 +222,79 @@ async def test_aget_badges(fake_badges_api_result, respx_mock, test_async_client
)
integrations = await test_async_client.get_badges()
assert integrations.keys() == fake_badges_api_result["badges"].keys()
ping_test_parameters = [
(
pytest.lazy_fixture("respx_mock"),
pytest.lazy_fixture("test_async_client"),
"test",
"success_ping",
{"uuid": "test"},
),
(
pytest.lazy_fixture("respx_mock"),
pytest.lazy_fixture("test_async_client"),
"1234/test",
"success_ping",
{"slug": "test"},
),
(
pytest.lazy_fixture("respx_mock"),
pytest.lazy_fixture("test_async_client"),
"test/start",
"start_ping",
{"uuid": "test"},
),
(
pytest.lazy_fixture("respx_mock"),
pytest.lazy_fixture("test_async_client"),
"1234/test/start",
"start_ping",
{"slug": "test"},
),
(
pytest.lazy_fixture("respx_mock"),
pytest.lazy_fixture("test_async_client"),
"test/fail",
"fail_ping",
{"uuid": "test"},
),
(
pytest.lazy_fixture("respx_mock"),
pytest.lazy_fixture("test_async_client"),
"1234/test/fail",
"fail_ping",
{"slug": "test"},
),
(
pytest.lazy_fixture("respx_mock"),
pytest.lazy_fixture("test_async_client"),
"test/0",
"exit_code_ping",
{"exit_code": 0, "uuid": "test"},
),
(
pytest.lazy_fixture("respx_mock"),
pytest.lazy_fixture("test_async_client"),
"1234/test/0",
"exit_code_ping",
{"exit_code": 0, "slug": "test"},
),
]
@pytest.mark.asyncio
@pytest.mark.respx
@pytest.mark.parametrize(
"respx_mocker, tc, url, ping_method, method_kwargs", ping_test_parameters
)
async def test_success_ping(respx_mocker, tc, url, ping_method, method_kwargs):
channels_url = urljoin(tc._ping_url, url)
respx_mocker.get(channels_url).mock(
return_value=Response(status_code=200, text="OK")
)
ping_method = getattr(tc, ping_method)
result = await ping_method(**method_kwargs)
assert result[0] is True
assert result[1] == "OK"

View File

@@ -195,3 +195,78 @@ def test_get_badges(fake_badges_api_result, respx_mock, test_client):
)
integrations = test_client.get_badges()
assert integrations.keys() == fake_badges_api_result["badges"].keys()
ping_test_parameters = [
(
pytest.lazy_fixture("respx_mock"),
pytest.lazy_fixture("test_client"),
"test",
"success_ping",
{"uuid": "test"},
),
(
pytest.lazy_fixture("respx_mock"),
pytest.lazy_fixture("test_client"),
"1234/test",
"success_ping",
{"slug": "test"},
),
(
pytest.lazy_fixture("respx_mock"),
pytest.lazy_fixture("test_client"),
"test/start",
"start_ping",
{"uuid": "test"},
),
(
pytest.lazy_fixture("respx_mock"),
pytest.lazy_fixture("test_client"),
"1234/test/start",
"start_ping",
{"slug": "test"},
),
(
pytest.lazy_fixture("respx_mock"),
pytest.lazy_fixture("test_client"),
"test/fail",
"fail_ping",
{"uuid": "test"},
),
(
pytest.lazy_fixture("respx_mock"),
pytest.lazy_fixture("test_client"),
"1234/test/fail",
"fail_ping",
{"slug": "test"},
),
(
pytest.lazy_fixture("respx_mock"),
pytest.lazy_fixture("test_client"),
"test/0",
"exit_code_ping",
{"exit_code": 0, "uuid": "test"},
),
(
pytest.lazy_fixture("respx_mock"),
pytest.lazy_fixture("test_client"),
"1234/test/0",
"exit_code_ping",
{"exit_code": 0, "slug": "test"},
),
]
@pytest.mark.respx
@pytest.mark.parametrize(
"respx_mocker, tc, url, ping_method, method_kwargs", ping_test_parameters
)
def test_success_ping(respx_mocker, tc, url, ping_method, method_kwargs):
channels_url = urljoin(tc._ping_url, url)
respx_mocker.get(channels_url).mock(
return_value=Response(status_code=200, text="OK")
)
ping_method = getattr(tc, ping_method)
result, text = ping_method(**method_kwargs)
assert result is True
assert text == "OK"

View File

@@ -6,6 +6,7 @@ import pytest
from healthchecks_io import AsyncClient
from healthchecks_io import Client
from healthchecks_io.client._abstract import AbstractClient
from healthchecks_io.schemas import checks
@@ -82,16 +83,30 @@ def fake_ro_check(fake_check: checks.Check):
yield fake_check
client_kwargs = {
"api_key": "test",
"api_url": "https://localhost/api",
"ping_url": "https://localhost/ping",
"ping_key": "1234",
}
@pytest.fixture
def test_async_client():
"""An AsyncClient for testing, set to a nonsense url so we aren't pinging healtchecks."""
yield AsyncClient(api_key="test", api_url="https://localhost/api")
yield AsyncClient(**client_kwargs)
@pytest.fixture
def test_client():
"""A Client for testing, set to a nonsense url so we aren't pinging healtchecks."""
yield Client(api_key="test", api_url="https://localhost/api")
yield Client(**client_kwargs)
@pytest.fixture
def test_abstract_client():
AbstractClient.__abstractmethods__ = set()
yield AbstractClient(**client_kwargs)
@pytest.fixture