2019-11-25 12:44:40 -05:00
|
|
|
"""Test certbot_apache._internal.display_ops."""
|
2015-06-25 20:02:09 -04:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
|
import mock
|
|
|
|
|
|
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):
|
|
|
|
|
self.assertFalse(select_vhost_multiple([]))
|
|
|
|
|
|
|
|
|
|
@certbot_util.patch_get_utility()
|
|
|
|
|
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]])
|
|
|
|
|
self.assertTrue(self.vhosts[2] in vhs)
|
|
|
|
|
self.assertTrue(self.vhosts[3] in vhs)
|
|
|
|
|
self.assertFalse(self.vhosts[1] in vhs)
|
|
|
|
|
|
|
|
|
|
@certbot_util.patch_get_utility()
|
|
|
|
|
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]])
|
|
|
|
|
self.assertFalse(vhs)
|
|
|
|
|
|
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
|
|
|
|
2017-01-10 19:25:33 -05:00
|
|
|
@certbot_util.patch_get_utility()
|
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
|
|
|
|
2017-01-10 19:25:33 -05:00
|
|
|
@certbot_util.patch_get_utility()
|
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:
|
2017-02-24 21:21:21 -05:00
|
|
|
self.assertTrue("vhost ambiguity" in str(e))
|
2016-01-01 20:10:57 -05:00
|
|
|
|
2017-01-10 19:25:33 -05:00
|
|
|
@certbot_util.patch_get_utility()
|
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),
|
|
|
|
|
]
|
|
|
|
|
|
2015-06-25 21:57:10 -04:00
|
|
|
self.assertEqual(None, self._call(self.vhosts))
|
2015-06-25 20:02:09 -04:00
|
|
|
|
|
|
|
|
def test_no_vhosts(self):
|
2015-06-25 21:57:10 -04:00
|
|
|
self.assertEqual(self._call([]), None)
|
|
|
|
|
|
2019-11-25 12:44:40 -05:00
|
|
|
@mock.patch("certbot_apache._internal.display_ops.display_util")
|
2017-01-10 19:25:33 -05:00
|
|
|
@certbot_util.patch_get_utility()
|
2019-11-25 12:44:40 -05:00
|
|
|
@mock.patch("certbot_apache._internal.display_ops.logger")
|
2015-06-26 19:07:39 -04:00
|
|
|
def test_small_display(self, mock_logger, mock_util, mock_display_util):
|
2015-06-26 17:47:03 -04:00
|
|
|
mock_display_util.WIDTH = 20
|
|
|
|
|
mock_util().menu.return_value = (display_util.OK, 0)
|
|
|
|
|
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
|
|
|
|
2017-01-10 19:25:33 -05:00
|
|
|
@certbot_util.patch_get_utility()
|
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(
|
|
|
|
|
"path", "aug_path", set([obj.Addr.fromstring("*:80")]),
|
|
|
|
|
False, False,
|
|
|
|
|
"wildcard.com", set(["*.wildcard.com"])))
|
|
|
|
|
|
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
|