repositories

NixDaemon

NixOS pentest workstation as one flake — IceBreaker's successor

clonegit clone https://git.daemon-sec.xyz/NixDaemon.git
test_vault_pdf.pyPython120 lines5.6 KiBraw download
"""Run with python3 -m unittest discover -s scripts/tests -p test_vault_pdf.py."""

import importlib.util
import os
from pathlib import Path
import subprocess
import tempfile
import unittest
from unittest.mock import patch

SCRIPT = Path(__file__).resolve().parents[1] / "vault-pdf.py"
SPEC = importlib.util.spec_from_file_location("vault_pdf", SCRIPT)
pdf = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(pdf)


class VaultPdfTests(unittest.TestCase):
    def setUp(self):
        self.temp = tempfile.TemporaryDirectory()
        self.addCleanup(self.temp.cleanup)
        self.root = Path(self.temp.name)
        (self.root / "06PdfCheatSheets").mkdir()

    def test_vault_root_overrides_and_default_fallback(self):
        with patch.dict(os.environ, {}, clear=True), patch.object(pdf.Path, "home", return_value=self.root):
            new = self.root / "git" / "DAEMONSEC-VAULT"
            old = self.root / "git" / "NetrunnerVault"
            self.assertEqual(pdf.vault_root(), new)
            old.mkdir(parents=True)
            self.assertEqual(pdf.vault_root(), old)
            new.mkdir()
            self.assertEqual(pdf.vault_root(), new)
            with patch.dict(os.environ, {"NETRUNNER_VAULT": str(old)}):
                self.assertEqual(pdf.vault_root(), old)
                with patch.dict(os.environ, {"DAEMONSEC_VAULT": str(new)}):
                    self.assertEqual(pdf.vault_root(), new)
                with patch.dict(os.environ, {"DAEMONSEC_VAULT": ""}):
                    self.assertEqual(pdf.vault_root(), old)
            with patch.dict(os.environ, {"DAEMONSEC_VAULT": "~/custom-vault"}):
                self.assertEqual(pdf.vault_root(), Path("~/custom-vault").expanduser().resolve())

    def test_stale_old_default_after_rename(self):
        new = self.root / "git" / "DAEMONSEC-VAULT"
        new.mkdir(parents=True)
        with patch.object(pdf.Path, "home", return_value=self.root), \
             patch.dict(os.environ, {"NETRUNNER_VAULT": str(self.root / "git" / "NetrunnerVault")}, clear=True):
            self.assertEqual(pdf.vault_root(), new)
            with patch.dict(os.environ, {"DAEMONSEC_VAULT": str(self.root / "custom-missing")}):
                self.assertEqual(pdf.vault_root(), self.root / "custom-missing")

    def document(self, relative):
        path = self.root / "06PdfCheatSheets" / relative
        path.parent.mkdir(parents=True, exist_ok=True)
        path.touch()
        return path

    def test_all_pdfs_have_unique_safe_aliases(self):
        expected = {
            self.document("one/GPG.pdf"), self.document("two/GPG.PDF"),
            self.document("quotes/it's $(touch NEVER) [one]\nnext.pdf"),
            self.document("fzf.pdf"), self.document("list.pdf"),
            self.document("Résumé.pdf"),
        }
        self.document(".git/hidden.pdf")
        self.document("notes.txt")
        (self.root / "outside-cheatsheets.pdf").touch()
        result = pdf.catalog(self.root)
        self.assertEqual(set(result.values()), expected)
        self.assertEqual(result, pdf.catalog(self.root))
        self.assertNotIn("pdf-fzf", result)
        self.assertNotIn("pdf-list", result)
        for name in result:
            self.assertRegex(name, r"^pdf-[a-z0-9-]+$")

    def test_missing_vault_and_stale_alias_are_clear(self):
        with self.assertRaisesRegex(ValueError, "Vault not found"):
            pdf.catalog(self.root / "missing")
        with self.assertRaisesRegex(ValueError, "pdf-refresh"):
            pdf.find_document({}, "pdf-deleted")

    def test_open_passes_one_literal_path_without_a_shell(self):
        path = self.document("it's $(touch NEVER).pdf")
        with patch.object(pdf.shutil, "which", return_value="/usr/bin/xdg-open"), \
             patch.object(pdf.subprocess, "Popen") as start:
            start.return_value.wait.return_value = 0
            pdf.open_document(path)
            self.assertEqual(start.call_args.args[0], ["/usr/bin/xdg-open", str(path)])
            self.assertNotIn("shell", start.call_args.kwargs)
            self.assertTrue(start.call_args.kwargs["start_new_session"])

    def test_real_fzf_selection_opens_only_matching_documents(self):
        if not pdf.shutil.which("fzf"):
            self.skipTest("fzf not installed")
        wanted = self.document("Cheat Sheets/specific test.pdf")
        self.document("unrelated.pdf")
        with patch.dict(os.environ, {"FZF_DEFAULT_OPTS": "--filter=specific", "FZF_DEFAULT_OPTS_FILE": ""}), \
             patch.object(pdf, "open_document") as opener:
            pdf.menu(pdf.catalog(self.root), self.root, "")
            opener.assert_called_once_with(wanted)

    def test_cancel_never_opens_a_document(self):
        self.document("test.pdf")
        with patch.object(pdf.shutil, "which", return_value="fzf"), \
             patch.object(pdf.subprocess, "run", return_value=subprocess.CompletedProcess([], 130, "")), \
             patch.object(pdf, "open_document") as opener:
            pdf.menu(pdf.catalog(self.root), self.root, "")
            opener.assert_not_called()

    def test_multiple_selected_documents(self):
        first = self.document("one.pdf")
        second = self.document("two.pdf")
        with patch.object(pdf.shutil, "which", return_value="fzf"), \
             patch.object(pdf.subprocess, "run", return_value=subprocess.CompletedProcess([], 0, "pdf-one\tOne\0pdf-two\tTwo\0")), \
             patch.object(pdf, "open_document") as opener:
            pdf.menu(pdf.catalog(self.root), self.root, "")
            self.assertEqual([call.args[0] for call in opener.call_args_list], [first, second])


if __name__ == "__main__":
    unittest.main()