GRM-20: feat: user-friendly click errors with i18n in configure_repo
CI / lint (push) Has been cancelled
CI / unit-tests (push) Has been cancelled
CI / molecule-tests (push) Has been cancelled
Post-merge Vikunja update / vikunja (push) Has been cancelled

This commit is contained in:
Emil Simeonov
2026-06-19 19:33:04 +02:00
parent 4fa3aeb54c
commit e1b589efab
3 changed files with 187 additions and 23 deletions
+35 -3
View File
@@ -2,6 +2,7 @@
from unittest.mock import MagicMock, patch
import click
import pytest
import requests
@@ -9,6 +10,7 @@ from scripts.configure_repo import (
BRANCH_PROTECTION_CONFIG,
LABEL_CONFIG,
GiteaRepoConfig,
_handle_http_error,
main,
)
@@ -203,9 +205,9 @@ class TestGiteaRepoConfig:
class TestMain:
def test_main_missing_token(self) -> None:
with patch.dict("os.environ", {}, clear=True):
with pytest.raises(SystemExit) as exc:
with pytest.raises(click.ClickException) as exc:
main()
assert exc.value.code == 1
assert "GITEA_ADMIN_TOKEN" in str(exc.value)
def test_main_success(self) -> None:
with patch.dict("os.environ", {"GITEA_ADMIN_TOKEN": "tok"}, clear=True):
@@ -237,8 +239,38 @@ class TestMain:
mock_cfg.ensure_branch_protection.side_effect = requests.HTTPError("403")
mock_cfg_class.return_value = mock_cfg
with pytest.raises(requests.HTTPError):
with pytest.raises(click.ClickException) as exc:
main()
assert "HTTP" in str(exc.value)
def test_handle_http_error_403(self) -> None:
mock_response = MagicMock()
mock_response.status_code = 403
mock_response.json.return_value = {"message": "Forbidden"}
err = requests.HTTPError("403", response=mock_response)
with pytest.raises(click.ClickException) as exc:
_handle_http_error(err)
msg = str(exc.value)
assert "admin rights" in msg
assert "Settings → Branches" in msg
def test_handle_http_error_other(self) -> None:
mock_response = MagicMock()
mock_response.status_code = 500
mock_response.json.return_value = {"message": "Internal Server Error"}
err = requests.HTTPError("500", response=mock_response)
with pytest.raises(click.ClickException) as exc:
_handle_http_error(err)
assert "500" in str(exc.value)
def test_handle_http_error_json_parse_fails(self) -> None:
mock_response = MagicMock()
mock_response.status_code = 502
mock_response.json.side_effect = ValueError("not json")
err = requests.HTTPError("502", response=mock_response)
with pytest.raises(click.ClickException) as exc:
_handle_http_error(err)
assert "502" in str(exc.value)
def test_main_module_block() -> None: