a70e3aef4b
Add scripts/setup-native.py and make ./install (and --native) build the server, the CLI and ntfy from source into ~/.local/share/pipelines, detect an existing host OpenCode and Pi instead of installing them, install the packaged authoring skill, migrate an existing Docker database and ntfy auth store, save the host client default and start both services. ./install --docker keeps the previous Docker server + ntfy installation unchanged. ntfy is compiled from its pinned Go module on every host because upstream publishes no macOS server binary (its darwin asset is built with -tags noserver); its cgo SQLite driver is why a C compiler is now required natively. The ntfy credential bootstrap and the client default writer are shared by both installers, installation.Load resolves symlinked prefixes, the Docker migration copies the SQLite write-ahead log so the newest definitions and runs are not lost, and the account subcommands read the generated configuration through NTFY_CONFIG_FILE. Verified on macOS: 115 Python tests, go build/vet/test, and scratch installs that migrate 9 pipelines and 31 runs, build ntfy v2.30.0 and start both services healthy on spare ports before stopping them.
88 lines
4.4 KiB
Python
88 lines
4.4 KiB
Python
import importlib.util
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
spec = importlib.util.spec_from_file_location("configure", Path(__file__).with_name("configure-compose.py"))
|
|
configure = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(configure)
|
|
|
|
|
|
class ConfigureTests(unittest.TestCase):
|
|
def setUp(self):
|
|
# Container runs may have HOME=/home/pipelines, which the mount guard
|
|
# rejects; inject a disposable HOME/USERPROFILE for the fixtures.
|
|
home = tempfile.TemporaryDirectory()
|
|
self.addCleanup(home.cleanup)
|
|
environment = patch.dict(os.environ, {"HOME": home.name, "USERPROFILE": home.name})
|
|
environment.start()
|
|
self.addCleanup(environment.stop)
|
|
|
|
def test_detect_timezone_and_preserve_existing_files(self):
|
|
try:
|
|
configure.host_timezone()
|
|
except ValueError:
|
|
self.skipTest("host timezone is unavailable in this environment")
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
zone = configure.configure(directory, "http://192.168.1.20:8090", "0.0.0.0")
|
|
self.assertTrue(zone)
|
|
settings = Path(directory, ".env")
|
|
original = settings.read_bytes()
|
|
self.assertIn(f"TZ={zone}", settings.read_text())
|
|
self.assertEqual(Path(directory, "secrets/ntfy-token.txt").stat().st_mode & 0o777, 0o600)
|
|
with self.assertRaises(ValueError):
|
|
configure.configure(directory, "http://192.168.1.21:8090", "0.0.0.0")
|
|
self.assertEqual(settings.read_bytes(), original)
|
|
|
|
def test_existing_token_is_not_overwritten(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
secrets = Path(directory, "secrets")
|
|
secrets.mkdir()
|
|
token = secrets / "ntfy-token.txt"
|
|
token.write_text("existing-secret")
|
|
configure.configure(directory, "http://192.168.1.20:8090", "0.0.0.0", "Europe/London")
|
|
self.assertEqual(token.read_text(), "existing-secret")
|
|
|
|
def test_reject_bad_url_and_injected_values(self):
|
|
for url in ["file:///foo", "https://user:pass@example.com", "https://example.com/topic", "https://example.com?token=secret"]:
|
|
with tempfile.TemporaryDirectory() as directory, self.assertRaises(ValueError):
|
|
configure.configure(directory, url, "0.0.0.0", "Europe/London")
|
|
with tempfile.TemporaryDirectory() as directory, self.assertRaises(ValueError):
|
|
configure.configure(directory, "https://example.com", "0.0.0.0\nEVIL=yes", "Europe/London")
|
|
|
|
def test_client_default_is_saved_privately_and_preserves_other_keys(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
environ = {"XDG_CONFIG_HOME": directory}
|
|
path = configure.configure_client("ws://127.0.0.1:8080/connect", environ)
|
|
self.assertEqual(path, Path(directory) / "pipelines/client.json")
|
|
self.assertEqual(path.stat().st_mode & 0o777, 0o600)
|
|
self.assertEqual(json.loads(path.read_text())["server_url"], "ws://127.0.0.1:8080/connect")
|
|
# Unrelated keys survive a later reinstall with a different address.
|
|
path.write_text(json.dumps({"server_url": "ws://old/connect", "theme": "dark"}))
|
|
configure.configure_client("ws://127.0.0.1:9090/connect", environ)
|
|
saved = json.loads(path.read_text())
|
|
self.assertEqual(saved["server_url"], "ws://127.0.0.1:9090/connect")
|
|
self.assertEqual(saved["theme"], "dark")
|
|
|
|
def test_client_default_refuses_symlink_and_non_object(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
environ = {"XDG_CONFIG_HOME": directory}
|
|
target = Path(directory) / "real.json"
|
|
target.write_text("{}")
|
|
directory_path = Path(directory) / "pipelines"
|
|
directory_path.mkdir()
|
|
(directory_path / "client.json").symlink_to(target)
|
|
with self.assertRaisesRegex(ValueError, "symlinked client.json"):
|
|
configure.configure_client("ws://127.0.0.1:8080/connect", environ)
|
|
(directory_path / "client.json").unlink()
|
|
(directory_path / "client.json").write_text("[]")
|
|
with self.assertRaisesRegex(ValueError, "must contain an object"):
|
|
configure.configure_client("ws://127.0.0.1:8080/connect", environ)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|