2019-11-25 12:44:40 -05:00
|
|
|
"""Test certbot_apache._internal.display_ops."""
|
2015-06-25 20:02:09 -04:00
|
|
|
import unittest
|
|
|
|
|
|
2020-04-15 14:30:08 -04:00
|
|
|
try:
|
|
|
|
|
import mock
|
2021-07-19 20:09:06 -04:00
|
|
|
except ImportError: # pragma: no cover
|
|
|
|
|
from unittest import mock # type: ignore
|
2015-06-25 20:02:09 -04:00
|
|
|
|
2016-04-13 19:30:57 -04:00
|
|
|
from certbot import errors
|
2017-01-10 19:25:33 -05:00
|
|
|
from certbot.display import util as display_util
|
|
|
|
|
from certbot.tests import util as certbot_util
|
2019-11-25 12:44:40 -05:00
|
|
|
from certbot_apache._internal import obj
|
|
|
|
|
from certbot_apache._internal.display_ops import select_vhost_multiple
|
2019-11-27 12:57:35 -05:00
|
|
|
import util
|
2015-07-24 20:05:25 -04:00
|
|
|
|
2015-06-25 20:02:09 -04:00
|
|
|
|
2018-02-28 14:31:47 -05:00
|
|
|
class SelectVhostMultiTest(unittest.TestCase):
|
2019-11-25 12:44:40 -05:00
|
|
|
"""Tests for certbot_apache._internal.display_ops.select_vhost_multiple."""
|
2018-02-28 14:31:47 -05:00
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.base_dir = "/example_path"
|
|
|
|
|
self.vhosts = util.get_vh_truth(
|
|
|
|
|
self.base_dir, "debian_apache_2_4/multiple_vhosts")
|
|
|
|
|
|
|
|
|
|
def test_select_no_input(self):
|
2022-01-03 16:05:21 -05:00
|
|
|
self.assertEqual(len(select_vhost_multiple([])), 0)
|
2018-02-28 14:31:47 -05:00
|
|
|
|
2021-07-19 20:09:06 -04:00
|
|
|
@certbot_util.patch_display_util()
|
2018-02-28 14:31:47 -05:00
|
|
|
def test_select_correct(self, mock_util):
|
|
|
|
|
mock_util().checklist.return_value = (
|
|
|
|
|
display_util.OK, [self.vhosts[3].display_repr(),
|
|
|
|
|
self.vhosts[2].display_repr()])
|
|
|
|
|
vhs = select_vhost_multiple([self.vhosts[3],
|
|
|
|
|
self.vhosts[2],
|
|
|
|
|
self.vhosts[1]])
|
2022-01-03 16:05:21 -05:00
|
|
|
self.assertIn(self.vhosts[2], vhs)
|
|
|
|
|
self.assertIn(self.vhosts[3], vhs)
|
|
|
|
|
self.assertNotIn(self.vhosts[1], vhs)
|
2018-02-28 14:31:47 -05:00
|
|
|
|
2021-07-19 20:09:06 -04:00
|
|
|
@certbot_util.patch_display_util()
|
2018-02-28 14:31:47 -05:00
|
|
|
def test_select_cancel(self, mock_util):
|
|
|
|
|
mock_util().checklist.return_value = (display_util.CANCEL, "whatever")
|
|
|
|
|
vhs = select_vhost_multiple([self.vhosts[2], self.vhosts[3]])
|
2022-01-03 16:05:21 -05:00
|
|
|
self.assertEqual(vhs, [])
|
2018-02-28 14:31:47 -05:00
|
|
|
|
2021-07-19 20:09:06 -04:00
|
|
|
|
2015-06-25 20:02:09 -04:00
|
|
|
class SelectVhostTest(unittest.TestCase):
|
2019-11-25 12:44:40 -05:00
|
|
|
"""Tests for certbot_apache._internal.display_ops.select_vhost."""
|
2015-06-25 20:02:09 -04:00
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.base_dir = "/example_path"
|
|
|
|
|
self.vhosts = util.get_vh_truth(
|
2016-03-20 15:57:52 -04:00
|
|
|
self.base_dir, "debian_apache_2_4/multiple_vhosts")
|
2015-06-25 20:02:09 -04:00
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def _call(cls, vhosts):
|
2019-11-25 12:44:40 -05:00
|
|
|
from certbot_apache._internal.display_ops import select_vhost
|
2015-06-25 21:57:10 -04:00
|
|
|
return select_vhost("example.com", vhosts)
|
2015-06-25 20:02:09 -04:00
|
|
|
|
2021-07-19 20:09:06 -04:00
|
|
|
@certbot_util.patch_display_util()
|
2015-06-25 20:02:09 -04:00
|
|
|
def test_successful_choice(self, mock_util):
|
2015-06-25 21:57:10 -04:00
|
|
|
mock_util().menu.return_value = (display_util.OK, 3)
|
|
|
|
|
self.assertEqual(self.vhosts[3], self._call(self.vhosts))
|
2015-06-25 20:02:09 -04:00
|
|
|
|
2021-07-19 20:09:06 -04:00
|
|
|
@certbot_util.patch_display_util()
|
2016-01-01 20:10:57 -05:00
|
|
|
def test_noninteractive(self, mock_util):
|
|
|
|
|
mock_util().menu.side_effect = errors.MissingCommandlineFlag("no vhost default")
|
|
|
|
|
try:
|
|
|
|
|
self._call(self.vhosts)
|
2016-01-30 22:53:50 -05:00
|
|
|
except errors.MissingCommandlineFlag as e:
|
2022-01-03 16:05:21 -05:00
|
|
|
self.assertIn("vhost ambiguity", str(e))
|
2016-01-01 20:10:57 -05:00
|
|
|
|
2021-07-19 20:09:06 -04:00
|
|
|
@certbot_util.patch_display_util()
|
2015-06-25 20:02:09 -04:00
|
|
|
def test_more_info_cancel(self, mock_util):
|
|
|
|
|
mock_util().menu.side_effect = [
|
|
|
|
|
(display_util.CANCEL, -1),
|
|
|
|
|
]
|
|
|
|
|
|
2022-01-03 16:05:21 -05:00
|
|
|
self.assertIsNone(self._call(self.vhosts))
|
2015-06-25 20:02:09 -04:00
|
|
|
|
|
|
|
|
def test_no_vhosts(self):
|
2022-01-03 16:05:21 -05:00
|
|
|
self.assertIsNone(self._call([]))
|
2015-06-25 21:57:10 -04:00
|
|
|
|
2019-11-25 12:44:40 -05:00
|
|
|
@mock.patch("certbot_apache._internal.display_ops.display_util")
|
|
|
|
|
@mock.patch("certbot_apache._internal.display_ops.logger")
|
2021-07-19 20:09:06 -04:00
|
|
|
def test_small_display(self, mock_logger, mock_display_util):
|
2015-06-26 17:47:03 -04:00
|
|
|
mock_display_util.WIDTH = 20
|
2021-07-19 20:09:06 -04:00
|
|
|
mock_display_util.menu.return_value = (display_util.OK, 0)
|
2015-06-26 17:47:03 -04:00
|
|
|
self._call(self.vhosts)
|
|
|
|
|
|
2015-06-26 19:07:39 -04:00
|
|
|
self.assertEqual(mock_logger.debug.call_count, 1)
|
2015-06-26 17:47:03 -04:00
|
|
|
|
2021-07-19 20:09:06 -04:00
|
|
|
@certbot_util.patch_display_util()
|
2015-07-24 20:05:25 -04:00
|
|
|
def test_multiple_names(self, mock_util):
|
2015-11-06 03:56:50 -05:00
|
|
|
mock_util().menu.return_value = (display_util.OK, 5)
|
2015-07-24 20:05:25 -04:00
|
|
|
|
|
|
|
|
self.vhosts.append(
|
|
|
|
|
obj.VirtualHost(
|
2020-04-13 13:41:39 -04:00
|
|
|
"path", "aug_path", {obj.Addr.fromstring("*:80")},
|
2015-07-24 20:05:25 -04:00
|
|
|
False, False,
|
2020-04-13 13:41:39 -04:00
|
|
|
"wildcard.com", {"*.wildcard.com"}))
|
2015-07-24 20:05:25 -04:00
|
|
|
|
2015-11-06 03:56:50 -05:00
|
|
|
self.assertEqual(self.vhosts[5], self._call(self.vhosts))
|
2015-07-24 20:05:25 -04:00
|
|
|
|
2015-06-26 17:47:03 -04:00
|
|
|
|
2015-06-25 21:57:10 -04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
|
unittest.main() # pragma: no cover
|