From ed407ffb6111e6bef9b93ebbadbfe0bbda3f6437 Mon Sep 17 00:00:00 2001 From: Jason McCormick Date: Mon, 2 Nov 2026 20:57:55 -0500 Subject: add --initialize-state --- tests/test_he_dyndns.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'tests') diff --git a/tests/test_he_dyndns.py b/tests/test_he_dyndns.py index 0fbe343..f7ea0e7 100644 --- a/tests/test_he_dyndns.py +++ b/tests/test_he_dyndns.py @@ -92,6 +92,50 @@ class StateTests(unittest.TestCase): def tearDown(self): self.directory.cleanup() + @mock.patch.object(he_dyndns, "update_he") + @mock.patch.object(he_dyndns, "discover_address") + def test_initialize_state_discovers_both_families_without_he(self, discover, update): + discover.side_effect = [ + ipaddress.ip_address("192.0.2.60"), + ipaddress.ip_address("2001:db8::60"), + ] + args = self.parser.parse_args([ + "home.example", "--initialize-state", "--state-file", self.state_path + ]) + + he_dyndns.run(args, self.parser) + + self.assertEqual( + [call.args[1] for call in discover.call_args_list], + [4, 6], + ) + update.assert_not_called() + with open(self.state_path, encoding="utf-8") as state_file: + self.assertEqual( + json.load(state_file)["home.example"], + {"4": "192.0.2.60", "6": "2001:db8::60"}, + ) + + @mock.patch.object(he_dyndns, "update_he") + @mock.patch.object(he_dyndns, "discover_address") + def test_initialize_failure_does_not_change_existing_state(self, discover, update): + existing = {"home.example": {"4": "192.0.2.10", "6": "2001:db8::10"}} + he_dyndns.write_state(self.state_path, existing) + discover.side_effect = [ + ipaddress.ip_address("192.0.2.60"), + RuntimeError("IPv6 unavailable"), + ] + args = self.parser.parse_args([ + "home.example", "--initialize-state", "--state-file", self.state_path + ]) + + with self.assertRaisesRegex(RuntimeError, "IPv6 unavailable"): + he_dyndns.run(args, self.parser) + + update.assert_not_called() + with open(self.state_path, encoding="utf-8") as state_file: + self.assertEqual(json.load(state_file), existing) + @mock.patch.object(he_dyndns, "update_he") @mock.patch.object(he_dyndns, "discover_address") def test_unchanged_discovered_address_skips_update(self, discover, update): -- cgit v1.2.3