Files
pipelines/scripts/test_save_ntfy_token.py
T

37 lines
1.5 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:
save_token.save_token(f"token {token} created for user pipeline-publisher, never expires\n", directory)
path = Path(directory, "secrets/ntfy-token.txt")
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", directory)
self.assertEqual(path.read_text(), token + "\n")
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, directory)
self.assertEqual(path.read_text(), "")
if __name__ == "__main__":
unittest.main()