- client: add raise_for_status() in get_latest_release() for non-404 errors (FINDING-001) - client: add timeout parameter (default 30s) passed to all session.get() calls (FINDING-004/SEC-002) - cli: replace return with sys.exit(1) in all except blocks (FINDING-002) - test_cli: remove duplicate test_exits_when_token_missing, assert GITEA_TOKEN in stderr (FINDING-006) - test_cli: update connection error tests to expect SystemExit(1) after exit code fix - test_cli: rework token masking test to inject token into exception message (FINDING-007) - test_client: add test_raises_on_server_error for HTTP 500 path (FINDING-001) - test_client: add tests for default and custom timeout values (FINDING-004) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
161 lines
5.9 KiB
Python
161 lines
5.9 KiB
Python
"""Tests for GiteaClient API client."""
|
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
|
|
from gitea_dashboard.client import GiteaClient
|
|
|
|
|
|
class TestGiteaClientInit:
|
|
"""Test client initialization and auth."""
|
|
|
|
def test_auth_header_is_set(self):
|
|
"""Session must carry Authorization header with token prefix."""
|
|
client = GiteaClient("http://gitea.local:3000", "my-secret-token")
|
|
assert client.session.headers["Authorization"] == "token my-secret-token"
|
|
|
|
def test_base_url_stored(self):
|
|
"""Base URL is stored without trailing slash."""
|
|
client = GiteaClient("http://gitea.local:3000/", "tok")
|
|
assert client.base_url == "http://gitea.local:3000"
|
|
|
|
def test_default_timeout_is_30(self):
|
|
"""Default timeout is 30 seconds."""
|
|
client = GiteaClient("http://gitea.local:3000", "tok")
|
|
assert client.timeout == 30
|
|
|
|
def test_custom_timeout_is_stored(self):
|
|
"""Custom timeout is stored and used."""
|
|
client = GiteaClient("http://gitea.local:3000", "tok", timeout=10)
|
|
assert client.timeout == 10
|
|
|
|
|
|
class TestGetPaginated:
|
|
"""Test internal pagination logic."""
|
|
|
|
def _make_client(self):
|
|
return GiteaClient("http://gitea.local:3000", "tok")
|
|
|
|
def test_single_page(self):
|
|
"""When response has fewer items than limit, stop after one request."""
|
|
client = self._make_client()
|
|
# Return 3 items (< 50 limit) -> single page
|
|
mock_resp = MagicMock()
|
|
mock_resp.raise_for_status = MagicMock()
|
|
mock_resp.json.return_value = [{"id": 1}, {"id": 2}, {"id": 3}]
|
|
|
|
with patch.object(client.session, "get", return_value=mock_resp) as mock_get:
|
|
result = client._get_paginated("/api/v1/user/repos")
|
|
|
|
assert result == [{"id": 1}, {"id": 2}, {"id": 3}]
|
|
# Called exactly once (single page)
|
|
mock_get.assert_called_once()
|
|
|
|
def test_two_pages(self):
|
|
"""When first page is full (limit items), fetch second page."""
|
|
client = self._make_client()
|
|
|
|
page1 = [{"id": i} for i in range(50)] # Exactly limit=50
|
|
page2 = [{"id": i} for i in range(50, 60)] # 10 items -> last page
|
|
|
|
mock_resp1 = MagicMock()
|
|
mock_resp1.raise_for_status = MagicMock()
|
|
mock_resp1.json.return_value = page1
|
|
|
|
mock_resp2 = MagicMock()
|
|
mock_resp2.raise_for_status = MagicMock()
|
|
mock_resp2.json.return_value = page2
|
|
|
|
with patch.object(client.session, "get", side_effect=[mock_resp1, mock_resp2]):
|
|
result = client._get_paginated("/api/v1/user/repos")
|
|
|
|
assert len(result) == 60
|
|
assert result == page1 + page2
|
|
|
|
def test_pagination_params_forwarded(self):
|
|
"""Extra params are merged with pagination params."""
|
|
client = self._make_client()
|
|
mock_resp = MagicMock()
|
|
mock_resp.raise_for_status = MagicMock()
|
|
mock_resp.json.return_value = []
|
|
|
|
with patch.object(client.session, "get", return_value=mock_resp) as mock_get:
|
|
client._get_paginated("/api/v1/repos/o/r/milestones", params={"state": "open"})
|
|
|
|
call_params = mock_get.call_args[1]["params"]
|
|
assert call_params["state"] == "open"
|
|
assert call_params["limit"] == 50
|
|
assert call_params["page"] == 1
|
|
|
|
|
|
class TestGetRepos:
|
|
"""Test get_repos method."""
|
|
|
|
def test_get_repos_calls_paginated(self):
|
|
"""get_repos delegates to _get_paginated with correct endpoint."""
|
|
client = GiteaClient("http://gitea.local:3000", "tok")
|
|
with patch.object(client, "_get_paginated", return_value=[{"id": 1}]) as mock_pag:
|
|
result = client.get_repos()
|
|
|
|
mock_pag.assert_called_once_with("/api/v1/user/repos")
|
|
assert result == [{"id": 1}]
|
|
|
|
|
|
class TestGetLatestRelease:
|
|
"""Test get_latest_release method."""
|
|
|
|
def test_returns_release_on_success(self):
|
|
"""Returns release dict when repo has a release."""
|
|
client = GiteaClient("http://gitea.local:3000", "tok")
|
|
mock_resp = MagicMock()
|
|
mock_resp.status_code = 200
|
|
mock_resp.json.return_value = {"tag_name": "v1.0", "published_at": "2026-01-01"}
|
|
|
|
with patch.object(client.session, "get", return_value=mock_resp):
|
|
result = client.get_latest_release("admin", "my-repo")
|
|
|
|
assert result == {"tag_name": "v1.0", "published_at": "2026-01-01"}
|
|
|
|
def test_returns_none_on_404(self):
|
|
"""Returns None when repo has no release (404)."""
|
|
client = GiteaClient("http://gitea.local:3000", "tok")
|
|
mock_resp = MagicMock()
|
|
mock_resp.status_code = 404
|
|
|
|
with patch.object(client.session, "get", return_value=mock_resp):
|
|
result = client.get_latest_release("admin", "no-release-repo")
|
|
|
|
assert result is None
|
|
|
|
def test_raises_on_server_error(self):
|
|
"""HTTP 500 raises an exception instead of silently returning bad data."""
|
|
import pytest
|
|
import requests as req
|
|
|
|
client = GiteaClient("http://gitea.local:3000", "tok")
|
|
mock_resp = MagicMock()
|
|
mock_resp.status_code = 500
|
|
mock_resp.raise_for_status.side_effect = req.HTTPError("500 Server Error")
|
|
|
|
with patch.object(client.session, "get", return_value=mock_resp):
|
|
with pytest.raises(req.HTTPError):
|
|
client.get_latest_release("admin", "my-repo")
|
|
|
|
|
|
class TestGetMilestones:
|
|
"""Test get_milestones method."""
|
|
|
|
def test_get_milestones_calls_paginated_with_state_open(self):
|
|
"""get_milestones delegates to _get_paginated with state=open."""
|
|
client = GiteaClient("http://gitea.local:3000", "tok")
|
|
milestones = [{"title": "v2.0", "open_issues": 3, "closed_issues": 2}]
|
|
|
|
with patch.object(client, "_get_paginated", return_value=milestones) as mock_pag:
|
|
result = client.get_milestones("admin", "my-repo")
|
|
|
|
mock_pag.assert_called_once_with(
|
|
"/api/v1/repos/admin/my-repo/milestones",
|
|
params={"state": "open"},
|
|
)
|
|
assert result == milestones
|