diff options
Diffstat (limited to 'tests/test_he_dyndns.py')
| -rw-r--r-- | tests/test_he_dyndns.py | 44 |
1 files changed, 44 insertions, 0 deletions
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 @@ -94,6 +94,50 @@ class StateTests(unittest.TestCase): @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): address = ipaddress.ip_address("192.0.2.44") discover.return_value = address |
