GRM-20: refactor: use http.HTTPStatus constants instead of magic numbers
This commit is contained in:
@@ -4,6 +4,7 @@
|
|||||||
Usage:
|
Usage:
|
||||||
GITEA_ADMIN_TOKEN=<token> python3 scripts/configure_repo.py
|
GITEA_ADMIN_TOKEN=<token> python3 scripts/configure_repo.py
|
||||||
"""
|
"""
|
||||||
|
import http
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import click
|
import click
|
||||||
@@ -104,7 +105,7 @@ def _handle_http_error(e: requests.HTTPError) -> None:
|
|||||||
"""Raise a user-friendly Click exception for HTTP errors."""
|
"""Raise a user-friendly Click exception for HTTP errors."""
|
||||||
response = e.response
|
response = e.response
|
||||||
status = response.status_code if response else 0
|
status = response.status_code if response else 0
|
||||||
if status == 403:
|
if status == http.HTTPStatus.FORBIDDEN:
|
||||||
raise click.ClickException(
|
raise click.ClickException(
|
||||||
_(
|
_(
|
||||||
"HTTP {status} Forbidden — your token lacks admin rights.\n"
|
"HTTP {status} Forbidden — your token lacks admin rights.\n"
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
"""Unit tests for scripts/configure_repo.py."""
|
"""Unit tests for scripts/configure_repo.py."""
|
||||||
|
|
||||||
|
import http
|
||||||
from unittest.mock import MagicMock, patch
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
import click
|
import click
|
||||||
@@ -245,7 +246,7 @@ class TestMain:
|
|||||||
|
|
||||||
def test_handle_http_error_403(self) -> None:
|
def test_handle_http_error_403(self) -> None:
|
||||||
mock_response = MagicMock()
|
mock_response = MagicMock()
|
||||||
mock_response.status_code = 403
|
mock_response.status_code = http.HTTPStatus.FORBIDDEN
|
||||||
mock_response.json.return_value = {"message": "Forbidden"}
|
mock_response.json.return_value = {"message": "Forbidden"}
|
||||||
err = requests.HTTPError("403", response=mock_response)
|
err = requests.HTTPError("403", response=mock_response)
|
||||||
with pytest.raises(click.ClickException) as exc:
|
with pytest.raises(click.ClickException) as exc:
|
||||||
@@ -256,7 +257,7 @@ class TestMain:
|
|||||||
|
|
||||||
def test_handle_http_error_other(self) -> None:
|
def test_handle_http_error_other(self) -> None:
|
||||||
mock_response = MagicMock()
|
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"}
|
mock_response.json.return_value = {"message": "Internal Server Error"}
|
||||||
err = requests.HTTPError("500", response=mock_response)
|
err = requests.HTTPError("500", response=mock_response)
|
||||||
with pytest.raises(click.ClickException) as exc:
|
with pytest.raises(click.ClickException) as exc:
|
||||||
@@ -265,7 +266,7 @@ class TestMain:
|
|||||||
|
|
||||||
def test_handle_http_error_json_parse_fails(self) -> None:
|
def test_handle_http_error_json_parse_fails(self) -> None:
|
||||||
mock_response = MagicMock()
|
mock_response = MagicMock()
|
||||||
mock_response.status_code = 502
|
mock_response.status_code = http.HTTPStatus.BAD_GATEWAY
|
||||||
mock_response.json.side_effect = ValueError("not json")
|
mock_response.json.side_effect = ValueError("not json")
|
||||||
err = requests.HTTPError("502", response=mock_response)
|
err = requests.HTTPError("502", response=mock_response)
|
||||||
with pytest.raises(click.ClickException) as exc:
|
with pytest.raises(click.ClickException) as exc:
|
||||||
|
|||||||
Reference in New Issue
Block a user