diff --git a/scripts/configure_repo.py b/scripts/configure_repo.py index 300fbc4..8e2f7d8 100644 --- a/scripts/configure_repo.py +++ b/scripts/configure_repo.py @@ -4,6 +4,7 @@ Usage: GITEA_ADMIN_TOKEN= python3 scripts/configure_repo.py """ +import http import os import click @@ -104,7 +105,7 @@ def _handle_http_error(e: requests.HTTPError) -> None: """Raise a user-friendly Click exception for HTTP errors.""" response = e.response status = response.status_code if response else 0 - if status == 403: + if status == http.HTTPStatus.FORBIDDEN: raise click.ClickException( _( "HTTP {status} Forbidden — your token lacks admin rights.\n" diff --git a/tests/unit/test_configure_repo.py b/tests/unit/test_configure_repo.py index 76fef5d..50f2e76 100644 --- a/tests/unit/test_configure_repo.py +++ b/tests/unit/test_configure_repo.py @@ -1,5 +1,6 @@ """Unit tests for scripts/configure_repo.py.""" +import http from unittest.mock import MagicMock, patch import click @@ -245,7 +246,7 @@ class TestMain: def test_handle_http_error_403(self) -> None: mock_response = MagicMock() - mock_response.status_code = 403 + mock_response.status_code = http.HTTPStatus.FORBIDDEN mock_response.json.return_value = {"message": "Forbidden"} err = requests.HTTPError("403", response=mock_response) with pytest.raises(click.ClickException) as exc: @@ -256,7 +257,7 @@ class TestMain: def test_handle_http_error_other(self) -> None: mock_response = MagicMock() - mock_response.status_code = 500 + mock_response.status_code = http.HTTPStatus.INTERNAL_SERVER_ERROR mock_response.json.return_value = {"message": "Internal Server Error"} err = requests.HTTPError("500", response=mock_response) with pytest.raises(click.ClickException) as exc: @@ -265,7 +266,7 @@ class TestMain: def test_handle_http_error_json_parse_fails(self) -> None: mock_response = MagicMock() - mock_response.status_code = 502 + mock_response.status_code = http.HTTPStatus.BAD_GATEWAY mock_response.json.side_effect = ValueError("not json") err = requests.HTTPError("502", response=mock_response) with pytest.raises(click.ClickException) as exc: