1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
"""Tests for letsencrypt.plugins.selection"""
import sys
from typing import List
import unittest
from unittest import mock
import pytest
from certbot import errors
from certbot import interfaces
from certbot._internal.display import obj as display_obj
from certbot._internal.plugins.disco import PluginsRegistry
from certbot.display import util as display_util
from certbot.tests import util as test_util
class ConveniencePickPluginTest(unittest.TestCase):
"""Tests for certbot._internal.plugins.selection.pick_*."""
def _test(self, fun, ifaces):
config = mock.Mock()
default = mock.Mock()
plugins = mock.Mock()
with mock.patch("certbot._internal.plugins.selection.pick_plugin") as mock_p:
mock_p.return_value = "foo"
assert "foo" == fun(config, default, plugins, "Question?")
mock_p.assert_called_once_with(
config, default, plugins, "Question?", ifaces)
def test_authenticator(self):
from certbot._internal.plugins.selection import pick_authenticator
self._test(pick_authenticator, (interfaces.Authenticator,))
def test_installer(self):
from certbot._internal.plugins.selection import pick_installer
self._test(pick_installer, (interfaces.Installer,))
def test_configurator(self):
from certbot._internal.plugins.selection import pick_configurator
self._test(pick_configurator,
(interfaces.Authenticator, interfaces.Installer))
class PickPluginTest(unittest.TestCase):
"""Tests for certbot._internal.plugins.selection.pick_plugin."""
def setUp(self):
self.config = mock.Mock(noninteractive_mode=False)
self.default = None
self.reg = mock.MagicMock()
self.question = "Question?"
self.ifaces: List[interfaces.Plugin] = []
def _call(self):
from certbot._internal.plugins.selection import pick_plugin
return pick_plugin(self.config, self.default, self.reg,
self.question, self.ifaces)
def test_default_provided(self):
self.default = "foo"
self._call()
assert 1 == self.reg.filter.call_count
def test_no_default(self):
self._call()
assert 1 == self.reg.visible().ifaces.call_count
def test_no_candidate(self):
assert self._call() is None
def test_single(self):
plugin_ep = mock.MagicMock()
plugin_ep.init.return_value = "foo"
plugin_ep.misconfigured = False
self.reg.visible().ifaces().available.return_value = {
"bar": plugin_ep}
assert "foo" == self._call()
def test_single_misconfigured(self):
plugin_ep = mock.MagicMock()
plugin_ep.init.return_value = "foo"
plugin_ep.misconfigured = True
self.reg.visible().ifaces().available.return_value = {
"bar": plugin_ep}
assert self._call() is None
def test_multiple(self):
plugin_ep = mock.MagicMock()
plugin_ep.init.return_value = "foo"
self.reg.visible().ifaces().available.return_value = {
"bar": plugin_ep,
"baz": plugin_ep,
}
with mock.patch("certbot._internal.plugins.selection.choose_plugin") as mock_choose:
mock_choose.return_value = plugin_ep
assert "foo" == self._call()
mock_choose.assert_called_once_with(
[plugin_ep, plugin_ep], self.question)
def test_choose_plugin_none(self):
self.reg.visible().ifaces().available.return_value = {
"bar": None,
"baz": None,
}
with mock.patch("certbot._internal.plugins.selection.choose_plugin") as mock_choose:
mock_choose.return_value = None
assert self._call() is None
def test_default_must_be_filtered(self):
# https://github.com/certbot/certbot/issues/9664
self.default = "foo"
filtered = mock.MagicMock()
self.reg.filter.return_value = filtered
self._call()
assert filtered.ifaces.call_count == 1
class ChoosePluginTest(unittest.TestCase):
"""Tests for certbot._internal.plugins.selection.choose_plugin."""
def setUp(self):
display_obj.set_display(display_obj.FileDisplay(sys.stdout, False))
self.mock_apache = mock.Mock(
description_with_name="a", misconfigured=True)
self.mock_apache.name = "apache"
self.mock_stand = mock.Mock(
description_with_name="s", misconfigured=False)
self.mock_stand.init().more_info.return_value = "standalone"
self.plugins = [
self.mock_apache,
self.mock_stand,
]
def _call(self):
from certbot._internal.plugins.selection import choose_plugin
return choose_plugin(self.plugins, "Question?")
@test_util.patch_display_util()
def test_selection(self, mock_util):
mock_util().menu.side_effect = [(display_util.OK, 0),
(display_util.OK, 1)]
assert self.mock_stand == self._call()
assert mock_util().notification.call_count == 1
@test_util.patch_display_util()
def test_more_info(self, mock_util):
mock_util().menu.side_effect = [
(display_util.OK, 1),
]
assert self.mock_stand == self._call()
@test_util.patch_display_util()
def test_no_choice(self, mock_util):
mock_util().menu.return_value = (display_util.CANCEL, 0)
assert self._call() is None
class GetUnpreparedInstallerTest(test_util.ConfigTestCase):
"""Tests for certbot._internal.plugins.selection.get_unprepared_installer."""
def setUp(self):
super().setUp()
self.mock_apache_fail_ep = mock.Mock(
description_with_name="afail")
self.mock_apache_fail_ep.check_name = lambda name: name == "afail"
self.mock_apache_ep = mock.Mock(
description_with_name="apache")
self.mock_apache_ep.check_name = lambda name: name == "apache"
self.mock_apache_plugin = mock.MagicMock()
self.mock_apache_ep.init.return_value = self.mock_apache_plugin
self.plugins = PluginsRegistry({
"afail": self.mock_apache_fail_ep,
"apache": self.mock_apache_ep,
})
def _call(self):
from certbot._internal.plugins.selection import get_unprepared_installer
return get_unprepared_installer(self.config, self.plugins)
def test_no_installer_defined(self):
self.config.configurator = None
assert self._call() is None
def test_no_available_installers(self):
self.config.configurator = "apache"
self.plugins = PluginsRegistry({})
with pytest.raises(errors.PluginSelectionError):
self._call()
def test_get_plugin(self):
self.config.configurator = "apache"
installer = self._call()
assert installer is self.mock_apache_plugin
def test_multiple_installers_returned(self):
self.config.configurator = "apache"
# Two plugins with the same name
self.mock_apache_fail_ep.check_name = lambda name: name == "apache"
with pytest.raises(errors.PluginSelectionError):
self._call()
class TestChooseConfiguratorPlugins(unittest.TestCase):
"""Tests for certbot._internal.plugins.selection.choose_configurator_plugins."""
def _setupMockPlugin(self, name):
mock_ep = mock.Mock(
description_with_name=name)
mock_ep.check_name = lambda n: n == name
mock_plugin = mock.MagicMock()
mock_plugin.name = name
mock_ep.init.return_value = mock_plugin
mock_ep.misconfigured = False
return mock_ep
def _parseArgs(self, args):
from certbot import configuration
from certbot._internal import cli
return cli.prepare_and_parse_args(self.plugins, args.split())
def setUp(self):
self.plugins = PluginsRegistry({
"nginx": self._setupMockPlugin("nginx"),
"apache": self._setupMockPlugin("apache"),
"manual": self._setupMockPlugin("manual"),
})
def _runWithArgs(self, args):
from certbot._internal.plugins.selection import choose_configurator_plugins
return choose_configurator_plugins(self._parseArgs(args), self.plugins, "certonly")
def test_noninteractive_configurator(self):
# For certonly, setting either the nginx or apache configurators should
# return both an installer and authenticator
inst, auth = self._runWithArgs("certonly --nginx")
assert inst.name == "nginx"
assert auth.name == "nginx"
inst, auth = self._runWithArgs("certonly --apache")
assert inst.name == "apache"
assert auth.name == "apache"
def test_noninteractive_inst_arg(self):
# For certonly, if an installer arg is set, it should be returned as expected
inst, auth = self._runWithArgs("certonly -a nginx -i nginx")
assert inst.name == "nginx"
assert auth.name == "nginx"
inst, auth = self._runWithArgs("certonly -a apache -i apache")
assert inst.name == "apache"
assert auth.name == "apache"
# if no installer arg is set (or it's set to none), one shouldn't be returned
inst, auth = self._runWithArgs("certonly -a nginx")
assert inst is None
assert auth.name == "nginx"
inst, auth = self._runWithArgs("certonly -a nginx -i none")
assert inst is None
assert auth.name == "nginx"
inst, auth = self._runWithArgs("certonly -a apache")
assert inst is None
assert auth.name == "apache"
inst, auth = self._runWithArgs("certonly -a apache -i none")
assert inst is None
assert auth.name == "apache"
if __name__ == "__main__":
sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover
|