106 lines
3.7 KiB
Python
106 lines
3.7 KiB
Python
#!/usr/bin/env python3
|
|
from __future__ import annotations
|
|
|
|
import tempfile
|
|
import unittest
|
|
from argparse import Namespace
|
|
from pathlib import Path
|
|
|
|
from redmine_ticket_drafts import KIND_INTERNAL_NOTE, add_draft
|
|
from redmine_tickets import (
|
|
apply_runtime_config_to_argv,
|
|
build_parser,
|
|
inject_default_sync_command,
|
|
load_runtime_config,
|
|
print_draft_queued,
|
|
resolve_apply_base_url,
|
|
)
|
|
|
|
|
|
class ProdWritesParentFlagTest(unittest.TestCase):
|
|
def test_parent_allow_prod_writes_before_draft_apply(self):
|
|
args = build_parser().parse_args(
|
|
inject_default_sync_command(
|
|
[
|
|
"--base-url",
|
|
"https://rm.ldrprep.com",
|
|
"--allow-prod-writes",
|
|
"draft",
|
|
"apply",
|
|
"deadbeefcafe",
|
|
]
|
|
)
|
|
)
|
|
self.assertTrue(args.allow_prod_writes)
|
|
self.assertEqual("draft", args.command)
|
|
self.assertEqual("apply", args.draft_command)
|
|
|
|
|
|
class DraftBaseUrlTest(unittest.TestCase):
|
|
def test_resolve_prefers_draft_url(self):
|
|
args = Namespace(base_url="https://rm.ldrprep.com", allow_base_url_override=False)
|
|
draft = {"base_url": "http://192.168.50.170"}
|
|
# default CLI equals DEFAULT which is often prod; draft LAN should win when cli is "default-like"
|
|
# Here cli is explicit prod vs draft LAN — should refuse without override
|
|
with self.assertRaises(Exception):
|
|
resolve_apply_base_url(args, draft)
|
|
|
|
args.allow_base_url_override = True
|
|
self.assertEqual("https://rm.ldrprep.com", resolve_apply_base_url(args, draft))
|
|
|
|
def test_resolve_uses_draft_when_cli_default(self):
|
|
from redmine_contacts import DEFAULT_BASE_URL
|
|
|
|
args = Namespace(base_url=DEFAULT_BASE_URL, allow_base_url_override=False)
|
|
draft = {"base_url": "http://192.168.50.170"}
|
|
self.assertEqual("http://192.168.50.170", resolve_apply_base_url(args, draft))
|
|
|
|
|
|
class ConfigAndDraftIdTest(unittest.TestCase):
|
|
def test_config_inject(self):
|
|
with tempfile.TemporaryDirectory() as td:
|
|
cfg = Path(td) / "c.json"
|
|
cfg.write_text(
|
|
'{"base-url":"http://192.168.50.170","project":"customer-service","cache-dir":"/tmp/x"}',
|
|
encoding="utf-8",
|
|
)
|
|
out = apply_runtime_config_to_argv(["--config", str(cfg), "search", "--max-tickets", "1"])
|
|
self.assertIn("--base-url", out)
|
|
self.assertIn("http://192.168.50.170", out)
|
|
self.assertIn("--project", out)
|
|
# explicit flag not overridden
|
|
out2 = apply_runtime_config_to_argv(
|
|
["--project", "other", "--config", str(cfg), "search"]
|
|
)
|
|
# project other should remain; config injects base-url/cache-dir only if missing
|
|
self.assertIn("other", out2)
|
|
|
|
def test_print_draft_id(self):
|
|
with tempfile.TemporaryDirectory() as td:
|
|
path = add_draft(
|
|
Path(td),
|
|
issue_id=9,
|
|
kind=KIND_INTERNAL_NOTE,
|
|
body="hi",
|
|
base_url="https://rm.ldrprep.com",
|
|
)
|
|
import io
|
|
import contextlib
|
|
|
|
buf = io.StringIO()
|
|
with contextlib.redirect_stdout(buf):
|
|
did = print_draft_queued(
|
|
path,
|
|
kind=KIND_INTERNAL_NOTE,
|
|
issue_id=9,
|
|
base_url="https://rm.ldrprep.com",
|
|
)
|
|
text = buf.getvalue()
|
|
self.assertEqual(path.stem, did)
|
|
self.assertIn(f"draft_id={did}", text)
|
|
self.assertIn("base_url=https://rm.ldrprep.com", text)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|