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.
73 lines
3.3 KiB
Python
73 lines
3.3 KiB
Python
import importlib.util
|
|
from pathlib import Path
|
|
import tempfile
|
|
import unittest
|
|
|
|
spec = importlib.util.spec_from_file_location("save_token", Path(__file__).with_name("save-ntfy-token.py"))
|
|
save_token = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(save_token)
|
|
|
|
|
|
class TokenTests(unittest.TestCase):
|
|
def test_extract_prose_and_protect_source_directory(self):
|
|
token = "tk_" + "a" * 29
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
path = Path(directory, "secrets/ntfy-token.txt")
|
|
save_token.save_token(f"token {token} created for user pipeline-publisher, never expires\n", path, 0o444)
|
|
self.assertEqual(path.read_text(), token + "\n")
|
|
self.assertEqual(path.stat().st_mode & 0o777, 0o444)
|
|
self.assertEqual(path.parent.stat().st_mode & 0o777, 0o700)
|
|
with self.assertRaises(ValueError):
|
|
save_token.save_token(f"token {token} created", path, 0o444)
|
|
self.assertEqual(path.read_text(), token + "\n")
|
|
|
|
def test_native_mode_is_private_to_the_installing_user(self):
|
|
token = "tk_" + "b" * 29
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
path = Path(directory, "secrets/publisher-token")
|
|
save_token.save_token(f"token {token} created", path, 0o600)
|
|
self.assertEqual(path.stat().st_mode & 0o777, 0o600)
|
|
|
|
def test_failures_do_not_replace_empty_placeholder(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
path = Path(directory, "secrets/ntfy-token.txt")
|
|
path.parent.mkdir()
|
|
path.write_text("")
|
|
for output in ["", "connection failed", "tk_" + "a" * 29 + " tk_" + "b" * 29]:
|
|
with self.assertRaises(ValueError):
|
|
save_token.save_token(output, path, 0o444)
|
|
self.assertEqual(path.read_text(), "")
|
|
|
|
def test_accounts_are_bootstrapped_once_with_the_shared_sequence(self):
|
|
token = "tk_" + "c" * 29
|
|
|
|
class FakeCompleted:
|
|
returncode = 0
|
|
stdout = f"token {token} created for pipeline-publisher\n"
|
|
stderr = ""
|
|
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
path = Path(directory, "secrets/publisher-token")
|
|
calls = []
|
|
|
|
def run(args, capture=False, interactive=False):
|
|
calls.append((tuple(args), capture, interactive))
|
|
return FakeCompleted()
|
|
|
|
save_token.bootstrap_accounts(run, path, 0o600, log=lambda _message: None)
|
|
self.assertEqual([args for args, _, _ in calls], [
|
|
("user", "add", "--ignore-exists", "pipeline-publisher"),
|
|
("access", "pipeline-publisher", "pipelines", "write-only"),
|
|
("token", "add", "pipeline-publisher"),
|
|
("user", "add", "--ignore-exists", "phone-reader"),
|
|
("access", "phone-reader", "pipelines", "read-only"),
|
|
])
|
|
self.assertEqual([capture for _, capture, _ in calls], [False, False, True, False, False])
|
|
self.assertEqual([interactive for _, _, interactive in calls], [True, False, False, True, False])
|
|
self.assertEqual(path.read_text(), token + "\n")
|
|
self.assertEqual(path.stat().st_mode & 0o777, 0o600)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|