fix: make helper script ask for confirmation on overwrite
This commit is contained in:
3 files changed
+84
-13
No files matched your search
@@ -8,6 +8,7 @@ import stat
|
||||
import tempfile
|
||||
import tomllib
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
import snap_dyndns_host as sh
|
||||
|
||||
@@ -31,10 +32,14 @@ token_sha256 = "bbbb" # trailing
|
||||
|
||||
class SetHostTest(unittest.TestCase):
|
||||
def test_new_host_is_appended(self):
|
||||
text, created = sh.set_host(CONFIG, "c.example.co.uk", "cccc", zone="example.co.uk")
|
||||
text, created = sh.set_host(
|
||||
CONFIG, "c.example.co.uk", "cccc", zone="example.co.uk"
|
||||
)
|
||||
self.assertTrue(created)
|
||||
hosts = tomllib.loads(text)["hosts"]
|
||||
self.assertEqual(hosts["c.example.co.uk"], {"token_sha256": "cccc", "zone": "example.co.uk"})
|
||||
self.assertEqual(
|
||||
hosts["c.example.co.uk"], {"token_sha256": "cccc", "zone": "example.co.uk"}
|
||||
)
|
||||
self.assertEqual(hosts["a.example.com"], {"token_sha256": "aaaa"})
|
||||
self.assertTrue(text.startswith(CONFIG.rstrip("\n")))
|
||||
|
||||
@@ -42,7 +47,9 @@ class SetHostTest(unittest.TestCase):
|
||||
text, created = sh.set_host(CONFIG, "b.example.com", "new", None)
|
||||
self.assertFalse(created)
|
||||
parsed = tomllib.loads(text)["hosts"]
|
||||
self.assertEqual(parsed["b.example.com"], {"zone": "example.com", "token_sha256": "new"})
|
||||
self.assertEqual(
|
||||
parsed["b.example.com"], {"zone": "example.com", "token_sha256": "new"}
|
||||
)
|
||||
self.assertEqual(parsed["a.example.com"]["token_sha256"], "aaaa")
|
||||
self.assertIn("# keep this comment", text)
|
||||
|
||||
@@ -56,9 +63,13 @@ class SetHostTest(unittest.TestCase):
|
||||
sh.set_host(CONFIG, "a.example.com", "new", zone="example.com")
|
||||
|
||||
def test_config_without_hosts(self):
|
||||
text, created = sh.set_host('[inwx]\nuser = "u"\npass = "p"', "a.example.com", "aaaa")
|
||||
text, created = sh.set_host(
|
||||
'[inwx]\nuser = "u"\npass = "p"', "a.example.com", "aaaa"
|
||||
)
|
||||
self.assertTrue(created)
|
||||
self.assertEqual(tomllib.loads(text)["hosts"]["a.example.com"]["token_sha256"], "aaaa")
|
||||
self.assertEqual(
|
||||
tomllib.loads(text)["hosts"]["a.example.com"]["token_sha256"], "aaaa"
|
||||
)
|
||||
|
||||
|
||||
class MainTest(unittest.TestCase):
|
||||
@@ -85,11 +96,36 @@ class MainTest(unittest.TestCase):
|
||||
self.assertEqual(stored, hashlib.sha256(token.encode()).hexdigest())
|
||||
|
||||
def test_rotation_changes_the_hash_and_keeps_permissions(self):
|
||||
self.run_main("a.example.com")
|
||||
self.run_main("a.example.com", "--yes")
|
||||
with open(self.path, "rb") as f:
|
||||
self.assertNotEqual(tomllib.load(f)["hosts"]["a.example.com"]["token_sha256"], "aaaa")
|
||||
self.assertNotEqual(
|
||||
tomllib.load(f)["hosts"]["a.example.com"]["token_sha256"], "aaaa"
|
||||
)
|
||||
self.assertEqual(stat.S_IMODE(os.stat(self.path).st_mode), 0o600)
|
||||
|
||||
def test_existing_host_asks_and_replaces_on_yes(self):
|
||||
with mock.patch("builtins.input", return_value="y") as ask:
|
||||
self.run_main("a.example.com")
|
||||
self.assertIn("already exists", ask.call_args.args[0])
|
||||
self.assertNotEqual(self.stored_hash("a.example.com"), "aaaa")
|
||||
|
||||
def test_existing_host_is_kept_on_no_or_without_input(self):
|
||||
for answer in ("", "n", EOFError()):
|
||||
with (
|
||||
mock.patch("builtins.input", side_effect=[answer]),
|
||||
self.assertRaises(SystemExit),
|
||||
):
|
||||
self.run_main("a.example.com")
|
||||
self.assertEqual(self.stored_hash("a.example.com"), "aaaa")
|
||||
|
||||
def test_new_host_does_not_ask(self):
|
||||
with mock.patch("builtins.input", side_effect=AssertionError("asked")):
|
||||
self.run_main("fresh.example.com")
|
||||
|
||||
def stored_hash(self, host):
|
||||
with open(self.path, "rb") as f:
|
||||
return tomllib.load(f)["hosts"][host]["token_sha256"]
|
||||
|
||||
def test_invalid_hostname_leaves_config_untouched(self):
|
||||
for bad in ('x"] \n[evil', "no_dots", "a..b", "-a.example.com"):
|
||||
with self.assertRaises(SystemExit):
|
||||
|
||||
Reference in new issue
Block a user