Files
pipelines/scripts/test_save_ntfy_token.py

90 lines
4.2 KiB
Python

import subprocess
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)
class NonInteractiveAccounts(unittest.TestCase):
def test_given_passwords_are_used_without_prompts(self):
calls = []
def run(args, capture=False, interactive=False, password=None):
calls.append((tuple(args), interactive, password))
out = "token tk_abcdefghijklmnopqrstuvwxyz123 created" if args[0] == "token" else ""
return subprocess.CompletedProcess(args, 0, stdout=out, stderr="")
with tempfile.TemporaryDirectory() as directory:
save_token.bootstrap_accounts(run, Path(directory) / "token", 0o600, log=lambda _m: None,
publisher_password="p1", reader_password="p2")
adds = [(args[3], interactive, password) for args, interactive, password in calls if args[:2] == ("user", "add")]
self.assertEqual(adds, [("pipeline-publisher", False, "p1"), ("phone-reader", False, "p2")])
if __name__ == "__main__":
unittest.main()