"""Problem-Meldungen: Relay und Antwort, ohne echten Forgejo.""" import json import sys import unittest from pathlib import Path from unittest import mock from urllib.error import HTTPError, URLError ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT)) from grotti import meldung class RelayTests(unittest.TestCase): def test_relay_ok(self): cfg = {"_raw": {"meldung_relay": "http://127.0.0.1/hooks/grotti-meldung"}} class Resp: def read(self): return b'{"url":"https://git.example/issues/1"}' def __enter__(self): return self def __exit__(self, *args): return False with mock.patch("urllib.request.urlopen", return_value=Resp()) as opener: ok, message = meldung.send_relay(cfg, {"text": "kaputt", "version": "1"}) self.assertTrue(ok) self.assertIn("issues/1", message) request = opener.call_args[0][0] body = json.loads(request.data.decode("utf-8")) self.assertEqual(body["text"], "kaputt") self.assertNotIn("username", body) def test_relay_down(self): cfg = {"_raw": {"meldung_relay": "http://127.0.0.1:9/hooks"}} with mock.patch("urllib.request.urlopen", side_effect=URLError("down")): ok, message = meldung.send_relay(cfg, {"text": "x"}) self.assertFalse(ok) self.assertIn("nicht erreichbar", message) def test_relay_http_error(self): cfg = {"_raw": {"meldung_relay": "http://example/hooks"}} class Fake(HTTPError): def __init__(self): pass def read(self): return b"nope" code = 502 with mock.patch("urllib.request.urlopen", side_effect=Fake()): ok, message = meldung.send_relay(cfg, {"text": "x"}) self.assertFalse(ok) self.assertIn("502", message) if __name__ == "__main__": unittest.main()