Commit 16874b9a authored by Chris's avatar Chris
Browse files

DNS 100% coverage

parent 7a7f2673
Pipeline #5983 passed with stage
in 39 seconds
...@@ -7,6 +7,7 @@ import dns ...@@ -7,6 +7,7 @@ import dns
import dns.resolver import dns.resolver
import isitup.dns import isitup.dns
@pytest.fixture @pytest.fixture
def mock_dns_query_response(): def mock_dns_query_response():
def wrapped_mock_query_response(*args, **kwargs): def wrapped_mock_query_response(*args, **kwargs):
...@@ -17,8 +18,10 @@ def mock_dns_query_response(): ...@@ -17,8 +18,10 @@ def mock_dns_query_response():
return [ return [
response, response,
] ]
return wrapped_mock_query_response return wrapped_mock_query_response
class TestDnsChecks: class TestDnsChecks:
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_post_empty(self, api: AsyncClient) -> None: async def test_post_empty(self, api: AsyncClient) -> None:
...@@ -60,7 +63,9 @@ class TestDnsChecks: ...@@ -60,7 +63,9 @@ class TestDnsChecks:
assert "No A record found for nonexistentdomain.tld" in res.json()["detail"] assert "No A record found for nonexistentdomain.tld" in res.json()["detail"]
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_post_no_nameserver(self, api: AsyncClient, base_url: str, mocker: MockerFixture) -> None: async def test_post_no_nameserver(
self, api: AsyncClient, base_url: str, mocker: MockerFixture
) -> None:
mocker.patch("isitup.dns.LOG.fatal") mocker.patch("isitup.dns.LOG.fatal")
with mock.patch("dns.resolver.query") as query: with mock.patch("dns.resolver.query") as query:
query.side_effect = dns.resolver.NoNameservers() query.side_effect = dns.resolver.NoNameservers()
...@@ -92,13 +97,29 @@ class TestDnsChecks: ...@@ -92,13 +97,29 @@ class TestDnsChecks:
assert res.json()["record_type"] == "a" assert res.json()["record_type"] == "a"
assert res.json()["value"][0]["address"] == "127.0.0.1" assert res.json()["value"][0]["address"] == "127.0.0.1"
@pytest.mark.asyncio
async def test_post_mocked_bad_url(
self, api: AsyncClient, base_url: str, mock_dns_query_response: callable
) -> None:
with mock.patch("dns.resolver.query") as query:
query.side_effect = mock_dns_query_response
res = await api.post("/check/dns/a/", json={"domain": "https://"})
query.assert_not_called
assert res.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
assert (
"Invalid format for domain name or URL"
in res.json()["detail"][0]["msg"]
)
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_post_mocked_existing_url( async def test_post_mocked_existing_url(
self, api: AsyncClient, base_url: str, mock_dns_query_response: callable self, api: AsyncClient, base_url: str, mock_dns_query_response: callable
) -> None: ) -> None:
with mock.patch("dns.resolver.query") as query: with mock.patch("dns.resolver.query") as query:
query.side_effect = mock_dns_query_response query.side_effect = mock_dns_query_response
res = await api.post("/check/dns/a/", json={"domain": "https://existingdomain.tld"}) res = await api.post(
"/check/dns/a/", json={"domain": "https://existingdomain.tld"}
)
query.assert_called_once_with("existingdomain.tld", "A") query.assert_called_once_with("existingdomain.tld", "A")
assert res.status_code == status.HTTP_200_OK assert res.status_code == status.HTTP_200_OK
assert res.json()["record_type"] == "a" assert res.json()["record_type"] == "a"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment