certbot/letsencrypt-apache/letsencrypt_apache/tests/configurator_test.py

614 lines
23 KiB
Python
Raw Normal View History

2015-07-24 18:47:38 -04:00
# pylint: disable=too-many-public-methods
2015-05-10 06:47:58 -04:00
"""Test for letsencrypt_apache.configurator."""
import os
import shutil
2015-07-24 06:22:35 -04:00
import socket
import unittest
import mock
2015-05-10 07:26:21 -04:00
from acme import challenges
2015-02-13 17:37:45 -05:00
2015-05-10 08:25:29 -04:00
from letsencrypt import achallenges
from letsencrypt import errors
2015-05-10 08:25:29 -04:00
from letsencrypt.tests import acme_util
2015-05-10 06:47:58 -04:00
from letsencrypt_apache import configurator
2015-07-19 05:22:10 -04:00
from letsencrypt_apache import obj
2014-12-09 04:21:56 -05:00
2015-05-10 06:47:58 -04:00
from letsencrypt_apache.tests import util
2015-04-23 02:17:53 -04:00
2015-01-24 08:12:45 -05:00
class TwoVhost80Test(util.ApacheTest):
2015-07-19 05:22:10 -04:00
"""Test two standard well-configured HTTP vhosts."""
2014-12-09 04:21:56 -05:00
2015-07-19 19:48:27 -04:00
def setUp(self): # pylint: disable=arguments-differ
2015-01-24 08:12:45 -05:00
super(TwoVhost80Test, self).setUp()
2015-07-17 17:09:46 -04:00
self.config = util.get_apache_configurator(
self.config_path, self.config_dir, self.work_dir)
2014-12-19 18:49:29 -05:00
2015-01-24 08:12:45 -05:00
self.vh_truth = util.get_vh_truth(
2014-12-19 18:49:29 -05:00
self.temp_dir, "debian_apache_2_4/two_vhost_80")
def tearDown(self):
shutil.rmtree(self.temp_dir)
shutil.rmtree(self.config_dir)
shutil.rmtree(self.work_dir)
2015-07-24 06:22:35 -04:00
@mock.patch("letsencrypt_apache.parser.ApacheParser")
2015-07-24 18:47:38 -04:00
def test_prepare_version(self, _):
2015-07-24 06:22:35 -04:00
self.config.version = None
self.config.config_test = mock.Mock()
self.config.get_version = mock.Mock(return_value=(1, 1))
self.assertRaises(
errors.NotSupportedError, self.config.prepare)
2015-07-24 18:47:38 -04:00
def test_add_parser_arguments(self): # pylint: disable=no-self-use
2015-07-23 04:34:51 -04:00
from letsencrypt_apache.configurator import ApacheConfigurator
# Weak test..
ApacheConfigurator.add_parser_arguments(mock.MagicMock())
def test_get_all_names(self):
names = self.config.get_all_names()
2014-12-16 04:35:46 -05:00
self.assertEqual(names, set(
2015-03-26 20:39:08 -04:00
["letsencrypt.demo", "encryption-example.demo", "ip-172-30-0-17"]))
2015-07-24 06:22:35 -04:00
@mock.patch("letsencrypt_apache.configurator.socket.gethostbyaddr")
def test_get_all_names_addrs(self, mock_gethost):
2015-07-24 18:47:38 -04:00
mock_gethost.side_effect = [("google.com", "", ""), socket.error]
vhost = obj.VirtualHost(
2015-07-24 06:22:35 -04:00
"fp", "ap",
set([obj.Addr(("8.8.8.8", "443")),
obj.Addr(("zombo.com",)),
obj.Addr(("192.168.1.2"))]),
True, False)
2015-07-24 18:47:38 -04:00
self.config.vhosts.append(vhost)
2015-07-24 06:22:35 -04:00
names = self.config.get_all_names()
self.assertEqual(len(names), 5)
self.assertTrue("zombo.com" in names)
self.assertTrue("google.com" in names)
self.assertTrue("letsencrypt.demo" in names)
def test_add_servernames_alias(self):
self.config.parser.add_dir(
self.vh_truth[2].path, "ServerAlias", ["*.le.co"])
self.config._add_servernames(self.vh_truth[2]) # pylint: disable=protected-access
self.assertEqual(
self.vh_truth[2].get_names(), set(["*.le.co", "ip-172-30-0-17"]))
def test_get_virtual_hosts(self):
"""Make sure all vhosts are being properly found.
.. note:: If test fails, only finding 1 Vhost... it is likely that
2015-07-19 05:22:10 -04:00
it is a problem with is_enabled. If finding only 3, likely is_ssl
"""
vhs = self.config.get_virtual_hosts()
2014-12-10 04:20:14 -05:00
self.assertEqual(len(vhs), 4)
found = 0
2014-12-16 04:35:46 -05:00
for vhost in vhs:
for truth in self.vh_truth:
if vhost == truth:
found += 1
break
2015-07-19 05:22:10 -04:00
else:
2015-07-23 04:34:51 -04:00
raise Exception("Missed: %s" % vhost) # pragma: no cover
self.assertEqual(found, 4)
2015-07-23 04:34:51 -04:00
@mock.patch("letsencrypt_apache.display_ops.select_vhost")
def test_choose_vhost_none_avail(self, mock_select):
mock_select.return_value = None
self.assertRaises(
errors.PluginError, self.config.choose_vhost, "none.com")
@mock.patch("letsencrypt_apache.display_ops.select_vhost")
def test_choose_vhost_select_vhost_ssl(self, mock_select):
mock_select.return_value = self.vh_truth[1]
self.assertEqual(
self.vh_truth[1], self.config.choose_vhost("none.com"))
@mock.patch("letsencrypt_apache.display_ops.select_vhost")
def test_choose_vhost_select_vhost_non_ssl(self, mock_select):
mock_select.return_value = self.vh_truth[0]
chosen_vhost = self.config.choose_vhost("none.com")
2015-07-23 04:34:51 -04:00
self.assertEqual(
self.vh_truth[0].get_names(), chosen_vhost.get_names())
# Make sure we go from HTTP -> HTTPS
self.assertFalse(self.vh_truth[0].ssl)
self.assertTrue(chosen_vhost.ssl)
@mock.patch("letsencrypt_apache.display_ops.select_vhost")
def test_choose_vhost_select_vhost_conflicting_non_ssl(self, mock_select):
mock_select.return_value = self.vh_truth[3]
conflicting_vhost = obj.VirtualHost(
"path", "aug_path", set([obj.Addr.fromstring("*:443")]), True, True)
self.config.vhosts.append(conflicting_vhost)
self.assertRaises(
errors.PluginError, self.config.choose_vhost, "none.com")
2015-07-23 04:34:51 -04:00
def test_find_best_vhost(self):
2015-07-24 18:47:38 -04:00
# pylint: disable=protected-access
2015-07-23 04:34:51 -04:00
self.assertEqual(
self.vh_truth[3], self.config._find_best_vhost("letsencrypt.demo"))
self.assertEqual(
self.vh_truth[0],
self.config._find_best_vhost("encryption-example.demo"))
self.assertTrue(
self.config._find_best_vhost("does-not-exist.com") is None)
2015-07-24 06:22:35 -04:00
def test_find_best_vhost_variety(self):
2015-07-24 18:47:38 -04:00
# pylint: disable=protected-access
2015-07-24 06:22:35 -04:00
ssl_vh = obj.VirtualHost(
"fp", "ap", set([obj.Addr(("*", "443")), obj.Addr(("zombo.com",))]),
True, False)
self.config.vhosts.append(ssl_vh)
self.assertEqual(self.config._find_best_vhost("zombo.com"), ssl_vh)
2015-07-23 04:34:51 -04:00
def test_find_best_vhost_default(self):
2015-07-24 18:47:38 -04:00
# pylint: disable=protected-access
2015-07-23 04:34:51 -04:00
# Assume only the two default vhosts.
2015-07-24 18:47:38 -04:00
self.config.vhosts = [
vh for vh in self.config.vhosts
if vh.name not in ["letsencrypt.demo", "encryption-example.demo"]
]
2015-07-23 04:34:51 -04:00
self.assertEqual(
self.config._find_best_vhost("example.demo"), self.vh_truth[2])
def test_non_default_vhosts(self):
# pylint: disable=protected-access
self.assertEqual(len(self.config._non_default_vhosts()), 3)
def test_is_site_enabled(self):
"""Test if site is enabled.
.. note:: This test currently fails for hard links
(which may happen if you move dirs incorrectly)
.. warning:: This test does not work when running using the
unittest.main() function. It incorrectly copies symlinks.
"""
self.assertTrue(self.config.is_site_enabled(self.vh_truth[0].filep))
2014-12-10 04:20:14 -05:00
self.assertFalse(self.config.is_site_enabled(self.vh_truth[1].filep))
self.assertTrue(self.config.is_site_enabled(self.vh_truth[2].filep))
self.assertTrue(self.config.is_site_enabled(self.vh_truth[3].filep))
2015-07-30 02:40:07 -04:00
@mock.patch("letsencrypt.le_util.run_script")
@mock.patch("letsencrypt.le_util.exe_exists")
2015-07-19 05:22:10 -04:00
@mock.patch("letsencrypt_apache.parser.subprocess.Popen")
2015-07-30 02:40:07 -04:00
def test_enable_mod(self, mock_popen, mock_exe_exists, mock_run_script):
2015-07-19 05:22:10 -04:00
mock_popen().communicate.return_value = ("Define: DUMP_RUN_CFG", "")
mock_popen().returncode = 0
2015-07-30 02:40:07 -04:00
mock_exe_exists.return_value = True
2015-07-19 05:22:10 -04:00
self.config.enable_mod("ssl")
self.assertTrue("ssl_module" in self.config.parser.modules)
self.assertTrue("mod_ssl.c" in self.config.parser.modules)
2015-07-30 02:40:07 -04:00
self.assertTrue(mock_run_script.called)
2015-07-24 06:22:35 -04:00
def test_enable_mod_unsupported_dirs(self):
2015-07-24 22:23:12 -04:00
shutil.rmtree(os.path.join(self.config.parser.root, "mods-enabled"))
2015-07-24 06:22:35 -04:00
self.assertRaises(
errors.NotSupportedError, self.config.enable_mod, "ssl")
2015-07-30 02:40:07 -04:00
@mock.patch("letsencrypt.le_util.exe_exists")
def test_enable_mod_no_disable(self, mock_exe_exists):
mock_exe_exists.return_value = False
2015-07-24 06:22:35 -04:00
self.assertRaises(
2015-07-30 02:40:07 -04:00
errors.MisconfigurationError, self.config.enable_mod, "ssl")
2015-07-24 06:22:35 -04:00
def test_enable_site(self):
2015-07-19 05:22:10 -04:00
# Default 443 vhost
self.assertFalse(self.vh_truth[1].enabled)
self.config.enable_site(self.vh_truth[1])
self.assertTrue(self.vh_truth[1].enabled)
2015-07-24 06:22:35 -04:00
# Go again to make sure nothing fails
self.config.enable_site(self.vh_truth[1])
def test_enable_site_failure(self):
self.assertRaises(
errors.NotSupportedError,
2015-07-24 06:22:35 -04:00
self.config.enable_site,
obj.VirtualHost("asdf", "afsaf", set(), False, False))
2015-07-30 02:40:07 -04:00
def test_deploy_cert(self):
self.config.parser.modules.add("ssl_module")
self.config.parser.modules.add("mod_ssl.c")
2015-07-19 05:22:10 -04:00
# Get the default 443 vhost
self.config.assoc["random.demo"] = self.vh_truth[1]
2014-12-05 20:31:36 -05:00
self.config.deploy_cert(
"random.demo",
2014-12-05 20:31:36 -05:00
"example/cert.pem", "example/key.pem", "example/cert_chain.pem")
self.config.save()
2014-12-05 20:31:36 -05:00
2015-07-19 05:22:10 -04:00
# Verify ssl_module was enabled.
self.assertTrue(self.vh_truth[1].enabled)
self.assertTrue("ssl_module" in self.config.parser.modules)
loc_cert = self.config.parser.find_dir(
2015-07-19 05:22:10 -04:00
"sslcertificatefile", "example/cert.pem", self.vh_truth[1].path)
loc_key = self.config.parser.find_dir(
2015-07-19 05:22:10 -04:00
"sslcertificateKeyfile", "example/key.pem", self.vh_truth[1].path)
loc_chain = self.config.parser.find_dir(
2015-07-19 05:22:10 -04:00
"SSLCertificateChainFile", "example/cert_chain.pem",
self.vh_truth[1].path)
2014-12-05 20:31:36 -05:00
# Verify one directive was found in the correct file
self.assertEqual(len(loc_cert), 1)
2014-12-19 18:49:29 -05:00
self.assertEqual(configurator.get_file_path(loc_cert[0]),
self.vh_truth[1].filep)
2014-12-05 20:31:36 -05:00
self.assertEqual(len(loc_key), 1)
2014-12-19 18:49:29 -05:00
self.assertEqual(configurator.get_file_path(loc_key[0]),
self.vh_truth[1].filep)
2014-12-05 20:31:36 -05:00
2014-12-10 04:20:14 -05:00
self.assertEqual(len(loc_chain), 1)
2014-12-19 18:49:29 -05:00
self.assertEqual(configurator.get_file_path(loc_chain[0]),
self.vh_truth[1].filep)
2015-07-23 04:34:51 -04:00
# One more time for chain directive setting
self.config.deploy_cert(
"random.demo",
"two/cert.pem", "two/key.pem", "two/cert_chain.pem")
self.assertTrue(self.config.parser.find_dir(
"SSLCertificateChainFile", "two/cert_chain.pem",
self.vh_truth[1].path))
2015-07-19 19:48:27 -04:00
def test_deploy_cert_invalid_vhost(self):
self.config.parser.modules.add("ssl_module")
mock_find = mock.MagicMock()
mock_find.return_value = []
self.config.parser.find_dir = mock_find
# Get the default 443 vhost
self.config.assoc["random.demo"] = self.vh_truth[1]
self.assertRaises(
errors.PluginError, self.config.deploy_cert, "random.demo",
"example/cert.pem", "example/key.pem", "example/cert_chain.pem")
def test_is_name_vhost(self):
2015-07-19 05:22:10 -04:00
addr = obj.Addr.fromstring("*:80")
2014-12-16 04:35:46 -05:00
self.assertTrue(self.config.is_name_vhost(addr))
2014-12-05 20:31:36 -05:00
self.config.version = (2, 2)
2014-12-16 04:35:46 -05:00
self.assertFalse(self.config.is_name_vhost(addr))
2014-12-05 20:31:36 -05:00
def test_add_name_vhost(self):
2015-07-19 05:22:10 -04:00
self.config.add_name_vhost(obj.Addr.fromstring("*:443"))
2015-07-24 06:22:35 -04:00
self.config.add_name_vhost(obj.Addr.fromstring("*:80"))
self.assertTrue(self.config.parser.find_dir(
2015-07-24 06:22:35 -04:00
"NameVirtualHost", "*:443", exclude=False))
self.assertTrue(self.config.parser.find_dir(
"NameVirtualHost", "*:80"))
2014-12-05 20:31:36 -05:00
2015-07-19 19:48:27 -04:00
def test_prepare_server_https(self):
mock_enable = mock.Mock()
self.config.enable_mod = mock_enable
2015-07-19 19:48:27 -04:00
mock_find = mock.Mock()
mock_add_dir = mock.Mock()
mock_find.return_value = []
# This will test the Add listen
self.config.parser.find_dir = mock_find
self.config.parser.add_dir_to_ifmodssl = mock_add_dir
self.config.prepare_server_https("443")
self.assertEqual(mock_enable.call_args[1], {"temp": False})
self.config.prepare_server_https("8080", temp=True)
# Enable mod is temporary
self.assertEqual(mock_enable.call_args[1], {"temp": True})
2015-07-24 06:22:35 -04:00
self.assertEqual(mock_add_dir.call_count, 2)
2015-07-19 19:48:27 -04:00
2014-12-06 05:33:06 -05:00
def test_make_vhost_ssl(self):
ssl_vhost = self.config.make_vhost_ssl(self.vh_truth[0])
2014-12-10 04:20:14 -05:00
self.assertEqual(
ssl_vhost.filep,
2014-12-06 05:33:06 -05:00
os.path.join(self.config_path, "sites-available",
"encryption-example-le-ssl.conf"))
2014-12-10 04:20:14 -05:00
self.assertEqual(ssl_vhost.path,
"/files" + ssl_vhost.filep + "/IfModule/VirtualHost")
2014-12-19 18:49:29 -05:00
self.assertEqual(len(ssl_vhost.addrs), 1)
2015-07-19 05:22:10 -04:00
self.assertEqual(set([obj.Addr.fromstring("*:443")]), ssl_vhost.addrs)
2015-07-21 20:16:46 -04:00
self.assertEqual(ssl_vhost.name, "encryption-example.demo")
2014-12-06 05:33:06 -05:00
self.assertTrue(ssl_vhost.ssl)
self.assertFalse(ssl_vhost.enabled)
self.assertTrue(self.config.parser.find_dir(
2015-07-19 05:22:10 -04:00
"SSLCertificateFile", None, ssl_vhost.path, False))
self.assertTrue(self.config.parser.find_dir(
2015-07-19 05:22:10 -04:00
"SSLCertificateKeyFile", None, ssl_vhost.path, False))
2014-12-06 05:33:06 -05:00
self.assertEqual(self.config.is_name_vhost(self.vh_truth[0]),
self.config.is_name_vhost(ssl_vhost))
2014-12-06 05:33:06 -05:00
self.assertEqual(len(self.config.vhosts), 5)
2014-12-06 05:33:06 -05:00
2015-07-24 06:22:35 -04:00
def test_make_vhost_ssl_extra_vhs(self):
self.config.aug.match = mock.Mock(return_value=["p1", "p2"])
self.assertRaises(
errors.PluginError, self.config.make_vhost_ssl, self.vh_truth[0])
def test_make_vhost_ssl_bad_write(self):
mock_open = mock.mock_open()
# This calls open
self.config.reverter.register_file_creation = mock.Mock()
mock_open.side_effect = IOError
with mock.patch("__builtin__.open", mock_open):
self.assertRaises(
errors.PluginError,
self.config.make_vhost_ssl, self.vh_truth[0])
def test_get_ssl_vhost_path(self):
2015-07-24 18:47:38 -04:00
# pylint: disable=protected-access
2015-07-24 06:22:35 -04:00
self.assertTrue(
self.config._get_ssl_vhost_path("example_path").endswith(".conf"))
def test_add_name_vhost_if_necessary(self):
2015-07-24 18:47:38 -04:00
# pylint: disable=protected-access
2015-07-24 06:22:35 -04:00
self.config.save = mock.Mock()
self.config.version = (2, 2)
self.config._add_name_vhost_if_necessary(self.vh_truth[0])
self.assertTrue(self.config.save.called)
2015-05-10 06:47:58 -04:00
@mock.patch("letsencrypt_apache.configurator.dvsni.ApacheDvsni.perform")
@mock.patch("letsencrypt_apache.configurator.ApacheConfigurator.restart")
2015-01-09 08:30:15 -05:00
def test_perform(self, mock_restart, mock_dvsni_perform):
# Only tests functionality specific to configurator.perform
# Note: As more challenges are offered this will have to be expanded
2015-08-05 18:39:31 -04:00
account_key, achall1, achall2 = self.get_achalls()
dvsni_ret_val = [
achall1.gen_response(account_key),
achall2.gen_response(account_key),
]
2015-01-09 08:30:15 -05:00
mock_dvsni_perform.return_value = dvsni_ret_val
2015-02-13 17:37:45 -05:00
responses = self.config.perform([achall1, achall2])
2015-01-09 08:30:15 -05:00
self.assertEqual(mock_dvsni_perform.call_count, 1)
self.assertEqual(responses, dvsni_ret_val)
2015-01-09 08:30:15 -05:00
self.assertEqual(mock_restart.call_count, 1)
2015-07-24 06:22:35 -04:00
@mock.patch("letsencrypt_apache.configurator.ApacheConfigurator.restart")
def test_cleanup(self, mock_restart):
2015-07-24 18:47:38 -04:00
_, achall1, achall2 = self.get_achalls()
2015-07-24 06:22:35 -04:00
2015-07-24 18:47:38 -04:00
self.config._chall_out.add(achall1) # pylint: disable=protected-access
self.config._chall_out.add(achall2) # pylint: disable=protected-access
2015-07-24 06:22:35 -04:00
self.config.cleanup([achall1])
self.assertFalse(mock_restart.called)
self.config.cleanup([achall2])
self.assertTrue(mock_restart.called)
@mock.patch("letsencrypt_apache.configurator.ApacheConfigurator.restart")
def test_cleanup_no_errors(self, mock_restart):
2015-07-24 18:47:38 -04:00
_, achall1, achall2 = self.get_achalls()
2015-07-24 06:22:35 -04:00
2015-07-24 18:47:38 -04:00
self.config._chall_out.add(achall1) # pylint: disable=protected-access
2015-07-24 06:22:35 -04:00
self.config.cleanup([achall2])
self.assertFalse(mock_restart.called)
self.config.cleanup([achall1, achall2])
self.assertTrue(mock_restart.called)
2015-07-30 02:40:07 -04:00
@mock.patch("letsencrypt.le_util.run_script")
def test_get_version(self, mock_script):
mock_script.return_value = (
2015-02-09 03:12:43 -05:00
"Server Version: Apache/2.4.2 (Debian)", "")
2015-02-10 03:55:40 -05:00
self.assertEqual(self.config.get_version(), (2, 4, 2))
2015-02-09 03:12:43 -05:00
2015-07-30 02:40:07 -04:00
mock_script.return_value = (
2015-02-09 03:12:43 -05:00
"Server Version: Apache/2 (Linux)", "")
2015-02-10 03:55:40 -05:00
self.assertEqual(self.config.get_version(), (2,))
2015-02-09 03:12:43 -05:00
2015-07-30 02:40:07 -04:00
mock_script.return_value = (
2015-02-09 03:12:43 -05:00
"Server Version: Apache (Debian)", "")
2015-06-26 12:29:40 -04:00
self.assertRaises(errors.PluginError, self.config.get_version)
2015-02-09 03:12:43 -05:00
2015-07-30 02:40:07 -04:00
mock_script.return_value = (
2015-03-26 20:39:08 -04:00
"Server Version: Apache/2.3{0} Apache/2.4.7".format(os.linesep), "")
2015-06-26 12:29:40 -04:00
self.assertRaises(errors.PluginError, self.config.get_version)
2015-02-09 03:12:43 -05:00
2015-07-30 02:40:07 -04:00
mock_script.side_effect = errors.SubprocessError("Can't find program")
2015-06-26 12:29:40 -04:00
self.assertRaises(errors.PluginError, self.config.get_version)
2015-02-10 03:55:40 -05:00
2015-07-24 06:22:35 -04:00
@mock.patch("letsencrypt_apache.configurator.subprocess.Popen")
def test_restart(self, mock_popen):
"""These will be changed soon enough with reload."""
mock_popen().returncode = 0
mock_popen().communicate.return_value = ("", "")
self.config.restart()
@mock.patch("letsencrypt_apache.configurator.subprocess.Popen")
def test_restart_bad_process(self, mock_popen):
mock_popen.side_effect = OSError
self.assertRaises(errors.MisconfigurationError, self.config.restart)
@mock.patch("letsencrypt_apache.configurator.subprocess.Popen")
def test_restart_failure(self, mock_popen):
mock_popen().communicate.return_value = ("", "")
2015-07-24 18:47:38 -04:00
mock_popen().returncode = 1
2015-07-24 06:22:35 -04:00
self.assertRaises(errors.MisconfigurationError, self.config.restart)
2015-07-30 02:40:07 -04:00
@mock.patch("letsencrypt.le_util.run_script")
def test_config_test(self, _):
2015-07-24 06:22:35 -04:00
self.config.config_test()
2015-07-30 02:40:07 -04:00
@mock.patch("letsencrypt.le_util.run_script")
def test_config_test_bad_process(self, mock_run_script):
mock_run_script.side_effect = errors.SubprocessError
2015-07-24 06:22:35 -04:00
self.assertRaises(errors.MisconfigurationError, self.config.config_test)
def test_get_all_certs_keys(self):
c_k = self.config.get_all_certs_keys()
self.assertEqual(len(c_k), 1)
cert, key, path = next(iter(c_k))
self.assertTrue("cert" in cert)
self.assertTrue("key" in key)
self.assertTrue("default-ssl.conf" in path)
def test_get_all_certs_keys_malformed_conf(self):
self.config.parser.find_dir = mock.Mock(side_effect=[["path"], []])
c_k = self.config.get_all_certs_keys()
self.assertFalse(c_k)
def test_more_info(self):
self.assertTrue(self.config.more_info())
def test_get_chall_pref(self):
self.assertTrue(isinstance(self.config.get_chall_pref(""), list))
def test_temp_install(self):
from letsencrypt_apache.configurator import temp_install
path = os.path.join(self.work_dir, "test_it")
temp_install(path)
self.assertTrue(os.path.isfile(path))
2015-07-22 05:05:01 -04:00
# TEST ENHANCEMENTS
2015-07-24 06:22:35 -04:00
def test_supported_enhancements(self):
self.assertTrue(isinstance(self.config.supported_enhancements(), list))
2015-07-22 05:05:01 -04:00
def test_enhance_unknown_enhancement(self):
self.assertRaises(
errors.PluginError,
self.config.enhance, "letsencrypt.demo", "unknown_enhancement")
2015-07-30 02:40:07 -04:00
@mock.patch("letsencrypt.le_util.run_script")
@mock.patch("letsencrypt.le_util.exe_exists")
def test_redirect_well_formed_http(self, mock_exe, _):
self.config.parser.update_runtime_variables = mock.Mock()
mock_exe.return_value = True
2015-07-22 05:05:01 -04:00
# This will create an ssl vhost for letsencrypt.demo
self.config.enhance("letsencrypt.demo", "redirect")
# These are not immediately available in find_dir even with save() and
# load(). They must be found in sites-available
rw_engine = self.config.parser.find_dir(
"RewriteEngine", "on", self.vh_truth[3].path)
rw_rule = self.config.parser.find_dir(
"RewriteRule", None, self.vh_truth[3].path)
self.assertEqual(len(rw_engine), 1)
# three args to rw_rule
self.assertEqual(len(rw_rule), 3)
self.assertTrue(rw_engine[0].startswith(self.vh_truth[3].path))
self.assertTrue(rw_rule[0].startswith(self.vh_truth[3].path))
self.assertTrue("rewrite_module" in self.config.parser.modules)
2015-07-24 06:22:35 -04:00
def test_redirect_with_conflict(self):
self.config.parser.modules.add("rewrite_module")
ssl_vh = obj.VirtualHost(
"fp", "ap", set([obj.Addr(("*", "443")), obj.Addr(("zombo.com",))]),
True, False)
# No names ^ this guy should conflict.
# pylint: disable=protected-access
self.assertRaises(
errors.PluginError, self.config._enable_redirect, ssl_vh, "")
2015-07-22 05:05:01 -04:00
def test_redirect_twice(self):
# Skip the enable mod
self.config.parser.modules.add("rewrite_module")
self.config.enhance("encryption-example.demo", "redirect")
self.assertRaises(
errors.PluginError,
self.config.enhance, "encryption-example.demo", "redirect")
def test_unknown_rewrite(self):
# Skip the enable mod
self.config.parser.modules.add("rewrite_module")
self.config.parser.add_dir(
self.vh_truth[3].path, "RewriteRule", ["Unknown"])
self.config.save()
self.assertRaises(
errors.PluginError,
self.config.enhance, "letsencrypt.demo", "redirect")
2015-09-06 05:20:41 -04:00
2015-07-24 06:22:35 -04:00
def test_unknown_rewrite2(self):
# Skip the enable mod
self.config.parser.modules.add("rewrite_module")
self.config.parser.add_dir(
self.vh_truth[3].path, "RewriteRule", ["Unknown", "2", "3"])
self.config.save()
self.assertRaises(
errors.PluginError,
self.config.enhance, "letsencrypt.demo", "redirect")
2015-07-22 05:05:01 -04:00
def test_unknown_redirect(self):
# Skip the enable mod
self.config.parser.modules.add("rewrite_module")
self.config.parser.add_dir(
self.vh_truth[3].path, "Redirect", ["Unknown"])
self.config.save()
self.assertRaises(
errors.PluginError,
self.config.enhance, "letsencrypt.demo", "redirect")
2015-07-24 06:22:35 -04:00
def test_create_own_redirect(self):
self.config.parser.modules.add("rewrite_module")
# For full testing... give names...
self.vh_truth[1].name = "default.com"
self.vh_truth[1].aliases = set(["yes.default.com"])
self.config._enable_redirect(self.vh_truth[1], "") # pylint: disable=protected-access
self.assertEqual(len(self.config.vhosts), 5)
def get_achalls(self):
2015-07-24 18:47:38 -04:00
"""Return testing achallenges."""
account_key = self.rsa512jwk
2015-07-24 06:22:35 -04:00
achall1 = achallenges.DVSNI(
challb=acme_util.chall_to_challb(
challenges.DVSNI(
2015-08-05 18:39:31 -04:00
token="jIq_Xy1mXGN37tb4L6Xj_es58fW571ZNyXekdZzhh7Q"),
2015-07-24 06:22:35 -04:00
"pending"),
domain="encryption-example.demo", account_key=account_key)
2015-07-24 06:22:35 -04:00
achall2 = achallenges.DVSNI(
challb=acme_util.chall_to_challb(
challenges.DVSNI(
2015-08-05 18:39:31 -04:00
token="uqnaPzxtrndteOqtrXb0Asl5gOJfWAnnx6QJyvcmlDU"),
2015-07-24 06:22:35 -04:00
"pending"),
domain="letsencrypt.demo", account_key=account_key)
2015-07-24 06:22:35 -04:00
return account_key, achall1, achall2
2015-07-24 06:22:35 -04:00
def test_make_addrs_sni_ready(self):
self.config.version = (2, 2)
self.config.make_addrs_sni_ready(
set([obj.Addr.fromstring("*:443"), obj.Addr.fromstring("*:80")]))
self.assertTrue(self.config.parser.find_dir(
"NameVirtualHost", "*:80", exclude=False))
self.assertTrue(self.config.parser.find_dir(
"NameVirtualHost", "*:443", exclude=False))
2015-02-09 03:12:43 -05:00
2015-03-26 20:39:08 -04:00
if __name__ == "__main__":
unittest.main() # pragma: no cover