b7e195e426
- New: mcp_server.py — FastMCP stdio server exposing 7 tools: gitea_create_issue, gitea_create_pr, gitea_close_issue, gitea_list_issues, gitea_view_issue, gitea_mark_issue, gitea_mirror_refs - New: auth.py — shared authentication and API helpers (get_credentials, get_auth_header, api_request, repo_api_url) - Refactored: create_pr.py, create_issue.py, manage_labels.py to use shared auth module (eliminates credential duplication) - New: tests/test_mcp_server.py — 17 tests for all MCP tools - Updated: tests/test_credentials.py — now tests auth.py directly - Updated: tests/test_create_issue.py — adapted for refactored imports - New: requirements.txt — frozen venv deps (mcp[cli], pytest) - Updated: README.md — MCP server as primary interface - Config: added gitea-tools to mcp_config.json Closes #1. Resolves #2, #5. Relates to #7.
161 lines
6.6 KiB
Python
161 lines
6.6 KiB
Python
"""Tests for create_issue.py.
|
|
|
|
Every test mocks auth functions so no real network calls or keychain
|
|
access are performed.
|
|
|
|
Note: create_issue.py may be inaccessible due to macOS sandbox restrictions.
|
|
If so, these tests are automatically skipped.
|
|
"""
|
|
import io
|
|
import json
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
# The module under test lives in the repo root, not a package.
|
|
sys.path.insert(0, str(__import__("pathlib").Path(__file__).resolve().parent.parent))
|
|
try:
|
|
import create_issue # noqa: E402
|
|
_SKIP = False
|
|
_REASON = ""
|
|
except (ImportError, PermissionError, OSError) as exc:
|
|
create_issue = None # type: ignore[assignment]
|
|
_SKIP = True
|
|
_REASON = f"create_issue.py not importable: {exc}"
|
|
|
|
|
|
FAKE_CREDS = ("testuser", "testpass")
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Argument parsing
|
|
# ---------------------------------------------------------------------------
|
|
@unittest.skipIf(_SKIP, _REASON)
|
|
class TestArgParsing(unittest.TestCase):
|
|
"""Ensure argparse accepts the expected flags and rejects bad input."""
|
|
|
|
@patch("create_issue.api_request", return_value={"number": 1, "html_url": "http://x/1"})
|
|
@patch("create_issue.get_credentials", return_value=FAKE_CREDS)
|
|
def test_minimal_args(self, _cred, _api):
|
|
rc = create_issue.main(["--title", "Hello"])
|
|
self.assertEqual(rc, 0)
|
|
|
|
def test_missing_title_exits(self):
|
|
with self.assertRaises(SystemExit) as ctx:
|
|
create_issue.main(["--body", "no title given"])
|
|
self.assertNotEqual(ctx.exception.code, 0)
|
|
|
|
@patch("create_issue.api_request", return_value={"number": 1, "html_url": "http://x/1"})
|
|
@patch("create_issue.get_credentials", return_value=FAKE_CREDS)
|
|
def test_remote_choices(self, _cred, _api):
|
|
for remote in ("dadeschools", "prgs"):
|
|
rc = create_issue.main(["--remote", remote, "--title", "X"])
|
|
self.assertEqual(rc, 0, f"--remote {remote} should be accepted")
|
|
|
|
def test_invalid_remote_exits(self):
|
|
with self.assertRaises(SystemExit):
|
|
create_issue.main(["--remote", "nonexistent", "--title", "X"])
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Remote resolution
|
|
# ---------------------------------------------------------------------------
|
|
@unittest.skipIf(_SKIP, _REASON)
|
|
class TestRemoteResolution(unittest.TestCase):
|
|
"""Verify the correct host/org/repo are selected per remote."""
|
|
|
|
@patch("create_issue.api_request", return_value={"number": 1, "html_url": "http://x/1"})
|
|
@patch("create_issue.get_credentials", return_value=FAKE_CREDS)
|
|
def test_dadeschools_default(self, mock_cred, _api):
|
|
create_issue.main(["--title", "T"])
|
|
mock_cred.assert_called_with("gitea.dadeschools.net")
|
|
|
|
@patch("create_issue.api_request", return_value={"number": 1, "html_url": "http://x/1"})
|
|
@patch("create_issue.get_credentials", return_value=FAKE_CREDS)
|
|
def test_prgs_remote(self, mock_cred, _api):
|
|
create_issue.main(["--remote", "prgs", "--title", "T"])
|
|
mock_cred.assert_called_with("gitea.prgs.cc")
|
|
|
|
@patch("create_issue.api_request", return_value={"number": 1, "html_url": "http://x/1"})
|
|
@patch("create_issue.get_credentials", return_value=FAKE_CREDS)
|
|
def test_host_override(self, mock_cred, _api):
|
|
create_issue.main(["--host", "custom.example.com", "--title", "T"])
|
|
mock_cred.assert_called_with("custom.example.com")
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# API payload
|
|
# ---------------------------------------------------------------------------
|
|
@unittest.skipIf(_SKIP, _REASON)
|
|
class TestAPIPayload(unittest.TestCase):
|
|
"""Ensure the JSON payload sent to Gitea is correct."""
|
|
|
|
@patch("create_issue.get_credentials", return_value=FAKE_CREDS)
|
|
def test_payload_title_and_body(self, _cred):
|
|
with patch("create_issue.api_request",
|
|
return_value={"number": 1, "html_url": "http://x/1"}) as mock_api:
|
|
create_issue.main(["--title", "My Title", "--body", "My Body"])
|
|
payload = mock_api.call_args[0][3]
|
|
self.assertEqual(payload["title"], "My Title")
|
|
self.assertEqual(payload["body"], "My Body")
|
|
|
|
@patch("create_issue.get_credentials", return_value=FAKE_CREDS)
|
|
def test_url_construction(self, _cred):
|
|
with patch("create_issue.api_request",
|
|
return_value={"number": 1, "html_url": "http://x/1"}) as mock_api:
|
|
create_issue.main(["--remote", "prgs", "--title", "X"])
|
|
url = mock_api.call_args[0][1]
|
|
self.assertEqual(
|
|
url,
|
|
"https://gitea.prgs.cc/api/v1/repos/Scaled-Tech-Consulting/Timesheet/issues",
|
|
)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Body file reading
|
|
# ---------------------------------------------------------------------------
|
|
@unittest.skipIf(_SKIP, _REASON)
|
|
class TestBodyFile(unittest.TestCase):
|
|
|
|
@patch("create_issue.get_credentials", return_value=FAKE_CREDS)
|
|
def test_body_from_file(self, _cred):
|
|
with tempfile.NamedTemporaryFile(mode="w", suffix=".md", delete=False) as f:
|
|
f.write("File body content")
|
|
f.flush()
|
|
with patch("create_issue.api_request",
|
|
return_value={"number": 1, "html_url": "http://x/1"}) as mock_api:
|
|
create_issue.main(["--title", "T", "--body-file", f.name])
|
|
payload = mock_api.call_args[0][3]
|
|
self.assertEqual(payload["body"], "File body content")
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Auth failure
|
|
# ---------------------------------------------------------------------------
|
|
@unittest.skipIf(_SKIP, _REASON)
|
|
class TestAuthFailure(unittest.TestCase):
|
|
|
|
@patch("create_issue.get_credentials", return_value=("", ""))
|
|
def test_no_credentials_returns_1(self, _cred):
|
|
rc = create_issue.main(["--title", "T"])
|
|
self.assertEqual(rc, 1)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# API error handling
|
|
# ---------------------------------------------------------------------------
|
|
@unittest.skipIf(_SKIP, _REASON)
|
|
class TestAPIError(unittest.TestCase):
|
|
|
|
@patch("create_issue.get_credentials", return_value=FAKE_CREDS)
|
|
def test_api_error_returns_1(self, _cred):
|
|
with patch("create_issue.api_request",
|
|
side_effect=RuntimeError("HTTP 422: duplicate")):
|
|
rc = create_issue.main(["--title", "Dup"])
|
|
self.assertEqual(rc, 1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|