Files
pipelines/scripts/test_password.py
T

125 lines
6.3 KiB
Python

import importlib.util
import json
import io
import os
from pathlib import Path
import tempfile
import unittest
from unittest.mock import patch
spec = importlib.util.spec_from_file_location("pipelines_password", Path(__file__).with_name("password.py"))
password = importlib.util.module_from_spec(spec)
spec.loader.exec_module(password)
class PasswordTests(unittest.TestCase):
def setUp(self):
self.temporary = tempfile.TemporaryDirectory(prefix="pipelines-password-test-")
self.addCleanup(self.temporary.cleanup)
self.home = Path(self.temporary.name).resolve()
self.auth = self.home / ".config/pipelines/web-auth.json"
self.auth.parent.mkdir(parents=True)
self.original = {"password": "old password", "secret": "s" * 32, "other": "preserve"}
self.auth.write_text(json.dumps(self.original))
self.auth.chmod(0o600)
def test_default_path_works_for_partial_install_without_manifest(self):
self.assertEqual(password.auth_path(home=self.home, environ={}), self.auth)
def test_manifest_custom_auth_and_explicit_environment(self):
prefix = self.home / "installation"
prefix.mkdir()
recorded = self.home / "custom-auth.json"
(prefix / "pipelines-install.json").write_text(json.dumps({
"format": 1, "prefix": str(prefix), "web": {"auth_file": str(recorded)}}))
self.assertEqual(password.auth_path(home=self.home, prefix=prefix, environ={}), recorded)
self.assertEqual(password.auth_path(home=self.home, prefix=prefix,
environ={"PIPELINES_WEB_AUTH_FILE": str(self.auth)}), self.auth)
def test_change_preserves_cookie_secret_metadata_and_private_permissions(self):
password.change_password(self.auth, "new password")
result = json.loads(self.auth.read_text())
self.assertEqual(result, dict(self.original, password="new password"))
self.assertEqual(self.auth.stat().st_mode & 0o777, 0o600)
self.assertEqual(list(self.auth.parent.glob(".pipelines-password-*")), [])
def test_short_password_does_not_modify_auth(self):
with self.assertRaises(ValueError):
password.change_password(self.auth, "short")
self.assertEqual(json.loads(self.auth.read_text()), self.original)
def test_symlinked_password_is_refused_and_target_untouched(self):
link = self.home / "link.json"
link.symlink_to(self.auth)
with self.assertRaises(ValueError):
password.change_password(link, "new password")
self.assertEqual(json.loads(self.auth.read_text()), self.original)
self.assertTrue(link.is_symlink())
def test_public_or_malformed_auth_is_not_replaced(self):
self.auth.chmod(0o644)
with self.assertRaisesRegex(ValueError, "private"):
password.change_password(self.auth, "new password")
self.auth.chmod(0o600)
self.auth.write_text("{broken")
with self.assertRaises(ValueError):
password.change_password(self.auth, "new password")
self.assertEqual(self.auth.read_text(), "{broken")
def test_replacement_failure_preserves_original_and_cleans_staging(self):
before = self.auth.read_bytes()
with patch.object(password.os, "replace", side_effect=OSError("fixture failure")):
with self.assertRaises(OSError):
password.change_password(self.auth, "new password")
self.assertEqual(self.auth.read_bytes(), before)
self.assertEqual(list(self.auth.parent.glob(".pipelines-password-*")), [])
def test_atomic_replace_occurs_on_destination_filesystem(self):
replace = os.replace
calls = []
def checked_replace(source, target):
calls.append((source, target))
self.assertEqual(Path(source).parent, self.auth.parent)
return replace(source, target)
with patch.object(password.os, "replace", side_effect=checked_replace):
password.change_password(self.auth, "new password")
self.assertEqual(len(calls), 1)
def test_file_changed_during_update_is_not_overwritten(self):
replace = os.fsync
def external_update(descriptor):
self.auth.write_text(json.dumps(dict(self.original, password="changed elsewhere")))
return replace(descriptor)
with patch.object(password.os, "fsync", side_effect=external_update):
with self.assertRaisesRegex(ValueError, "changed during"):
password.change_password(self.auth, "new password")
self.assertEqual(json.loads(self.auth.read_text())["password"], "changed elsewhere")
self.assertEqual(list(self.auth.parent.glob(".pipelines-password-*")), [])
def test_cli_retries_mismatch_and_does_not_print_password(self):
output = io.StringIO()
errors = io.StringIO()
with patch.object(password.sys, "argv", ["password"]), \
patch.object(password, "auth_path", return_value=self.auth), \
patch.object(password.sys.stdin, "isatty", return_value=True), \
patch.object(password.getpass, "getpass", side_effect=[
"first password", "wrong confirmation", "new password", "new password"]), \
patch.object(password.sys, "stdout", output), \
patch.object(password.sys, "stderr", errors):
password.main()
self.assertEqual(json.loads(self.auth.read_text())["password"], "new password")
self.assertIn("do not match", errors.getvalue())
self.assertIn("No service restart", output.getvalue())
for value in ("first password", "wrong confirmation", "new password", "old password"):
self.assertNotIn(value, output.getvalue() + errors.getvalue())
def test_cli_refuses_non_tty_before_prompting(self):
with patch.object(password.sys, "argv", ["password"]), \
patch.object(password, "auth_path", return_value=self.auth), \
patch.object(password.sys.stdin, "isatty", return_value=False), \
patch.object(password.getpass, "getpass") as prompt:
with self.assertRaisesRegex(ValueError, "interactive terminal"):
password.main()
prompt.assert_not_called()
self.assertEqual(json.loads(self.auth.read_text()), self.original)