diff --git a/discord_rss_bot/healthcheck.py b/discord_rss_bot/healthcheck.py index 5c35a1e..7fa0231 100644 --- a/discord_rss_bot/healthcheck.py +++ b/discord_rss_bot/healthcheck.py @@ -16,6 +16,7 @@ def healthcheck() -> None: r: requests.Response = requests.get(url="http://localhost:5000", timeout=5) if r.ok: sys.exit(0) + sys.exit(1) except requests.exceptions.RequestException as e: print(f"Healthcheck failed: {e}", file=sys.stderr) # noqa: T201 sys.exit(1) diff --git a/tests/test_healthcheck.py b/tests/test_healthcheck.py new file mode 100644 index 0000000..70977df --- /dev/null +++ b/tests/test_healthcheck.py @@ -0,0 +1,69 @@ +from __future__ import annotations + +from unittest.mock import MagicMock +from unittest.mock import patch + +import pytest +import requests + +from discord_rss_bot.healthcheck import healthcheck + + +def test_healthcheck_success() -> None: + """Test that healthcheck exits with 0 when the website is up.""" + mock_response = MagicMock() + mock_response.ok = True + + with ( + patch("discord_rss_bot.healthcheck.requests.get", return_value=mock_response), + pytest.raises(SystemExit) as exc_info, + ): + healthcheck() + + assert exc_info.value.code == 0 + + +def test_healthcheck_not_ok() -> None: + """Test that healthcheck exits with 1 when the response is not ok.""" + mock_response = MagicMock() + mock_response.ok = False + + with ( + patch("discord_rss_bot.healthcheck.requests.get", return_value=mock_response), + pytest.raises(SystemExit) as exc_info, + ): + healthcheck() + + assert exc_info.value.code == 1 + + +def test_healthcheck_request_exception(capsys: pytest.CaptureFixture) -> None: + """Test that healthcheck exits with 1 on a request exception.""" + with ( + patch( + "discord_rss_bot.healthcheck.requests.get", + side_effect=requests.exceptions.ConnectionError("Connection refused"), + ), + pytest.raises(SystemExit) as exc_info, + ): + healthcheck() + + assert exc_info.value.code == 1 + captured = capsys.readouterr() + assert "Healthcheck failed" in captured.err + + +def test_healthcheck_timeout(capsys: pytest.CaptureFixture) -> None: + """Test that healthcheck exits with 1 on a timeout.""" + with ( + patch( + "discord_rss_bot.healthcheck.requests.get", + side_effect=requests.exceptions.Timeout("Request timed out"), + ), + pytest.raises(SystemExit) as exc_info, + ): + healthcheck() + + assert exc_info.value.code == 1 + captured = capsys.readouterr() + assert "Healthcheck failed" in captured.err