1"""Test for certbot_apache._internal.configurator implementations of reverter"""
2import shutil
3import unittest
4
5try:
6    import mock
7except ImportError: # pragma: no cover
8    from unittest import mock # type: ignore
9
10from certbot import errors
11import util
12
13
14class ConfiguratorReverterTest(util.ApacheTest):
15    """Test for ApacheConfigurator reverter methods"""
16
17
18    def setUp(self):  # pylint: disable=arguments-differ
19        super().setUp()
20
21        self.config = util.get_apache_configurator(
22            self.config_path, self.vhost_path, self.config_dir, self.work_dir)
23
24        self.vh_truth = util.get_vh_truth(
25            self.temp_dir, "debian_apache_2_4/multiple_vhosts")
26
27    def tearDown(self):
28        shutil.rmtree(self.config_dir)
29        shutil.rmtree(self.work_dir)
30        shutil.rmtree(self.temp_dir)
31
32    def test_bad_save_checkpoint(self):
33        self.config.reverter.add_to_checkpoint = mock.Mock(
34            side_effect=errors.ReverterError)
35        self.config.parser.add_dir(
36            self.vh_truth[0].path, "Test", "bad_save_ckpt")
37        self.assertRaises(errors.PluginError, self.config.save)
38
39    def test_bad_save_finalize_checkpoint(self):
40        self.config.reverter.finalize_checkpoint = mock.Mock(
41            side_effect=errors.ReverterError)
42        self.config.parser.add_dir(
43            self.vh_truth[0].path, "Test", "bad_save_ckpt")
44        self.assertRaises(errors.PluginError, self.config.save, "Title")
45
46    def test_finalize_save(self):
47        mock_finalize = mock.Mock()
48        self.config.reverter = mock_finalize
49        self.config.save("Example Title")
50
51        self.assertTrue(mock_finalize.is_called)
52
53    def test_revert_challenge_config(self):
54        mock_load = mock.Mock()
55        self.config.parser.aug.load = mock_load
56
57        self.config.revert_challenge_config()
58        self.assertEqual(mock_load.call_count, 1)
59
60    def test_revert_challenge_config_error(self):
61        self.config.reverter.revert_temporary_config = mock.Mock(
62            side_effect=errors.ReverterError)
63
64        self.assertRaises(
65            errors.PluginError, self.config.revert_challenge_config)
66
67    def test_rollback_checkpoints(self):
68        mock_load = mock.Mock()
69        self.config.parser.aug.load = mock_load
70
71        self.config.rollback_checkpoints()
72        self.assertEqual(mock_load.call_count, 1)
73
74    def test_rollback_error(self):
75        self.config.reverter.rollback_checkpoints = mock.Mock(
76            side_effect=errors.ReverterError)
77        self.assertRaises(errors.PluginError, self.config.rollback_checkpoints)
78
79    def test_recovery_routine_reload(self):
80        mock_load = mock.Mock()
81        self.config.parser.aug.load = mock_load
82        self.config.recovery_routine()
83        self.assertEqual(mock_load.call_count, 1)
84
85
86if __name__ == "__main__":
87    unittest.main()  # pragma: no cover
88