Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/certbot/certbot.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'certbot-nginx/certbot_nginx/tests')
-rw-r--r--certbot-nginx/certbot_nginx/tests/configurator_test.py77
-rw-r--r--certbot-nginx/certbot_nginx/tests/http_01_test.py36
-rw-r--r--certbot-nginx/certbot_nginx/tests/parser_obj_test.py253
3 files changed, 336 insertions, 30 deletions
diff --git a/certbot-nginx/certbot_nginx/tests/configurator_test.py b/certbot-nginx/certbot_nginx/tests/configurator_test.py
index 4d23f3518..2814cbb8c 100644
--- a/certbot-nginx/certbot_nginx/tests/configurator_test.py
+++ b/certbot-nginx/certbot_nginx/tests/configurator_test.py
@@ -103,7 +103,7 @@ class NginxConfiguratorTest(util.NginxTest):
errors.PluginError, self.config.enhance, 'myhost', 'unknown_enhancement')
def test_get_chall_pref(self):
- self.assertEqual([challenges.TLSSNI01, challenges.HTTP01],
+ self.assertEqual([challenges.HTTP01, challenges.TLSSNI01],
self.config.get_chall_pref('myhost'))
def test_save(self):
@@ -128,22 +128,39 @@ class NginxConfiguratorTest(util.NginxTest):
['#', parser.COMMENT]]]],
parsed[0])
- def test_choose_vhosts(self):
- localhost_conf = set(['localhost', r'~^(www\.)?(example|bar)\.'])
- server_conf = set(['somename', 'another.alias', 'alias'])
- example_conf = set(['.example.com', 'example.*'])
- foo_conf = set(['*.www.foo.com', '*.www.example.com'])
- ipv6_conf = set(['ipv6.com'])
-
- results = {'localhost': localhost_conf,
- 'alias': server_conf,
- 'example.com': example_conf,
- 'example.com.uk.test': example_conf,
- 'www.example.com': example_conf,
- 'test.www.example.com': foo_conf,
- 'abc.www.foo.com': foo_conf,
- 'www.bar.co.uk': localhost_conf,
- 'ipv6.com': ipv6_conf}
+ def test_choose_vhosts_alias(self):
+ self._test_choose_vhosts_common('alias', 'server_conf')
+
+ def test_choose_vhosts_example_com(self):
+ self._test_choose_vhosts_common('example.com', 'example_conf')
+
+ def test_choose_vhosts_localhost(self):
+ self._test_choose_vhosts_common('localhost', 'localhost_conf')
+
+ def test_choose_vhosts_example_com_uk_test(self):
+ self._test_choose_vhosts_common('example.com.uk.test', 'example_conf')
+
+ def test_choose_vhosts_www_example_com(self):
+ self._test_choose_vhosts_common('www.example.com', 'example_conf')
+
+ def test_choose_vhosts_test_www_example_com(self):
+ self._test_choose_vhosts_common('test.www.example.com', 'foo_conf')
+
+ def test_choose_vhosts_abc_www_foo_com(self):
+ self._test_choose_vhosts_common('abc.www.foo.com', 'foo_conf')
+
+ def test_choose_vhosts_www_bar_co_uk(self):
+ self._test_choose_vhosts_common('www.bar.co.uk', 'localhost_conf')
+
+ def test_choose_vhosts_ipv6_com(self):
+ self._test_choose_vhosts_common('ipv6.com', 'ipv6_conf')
+
+ def _test_choose_vhosts_common(self, name, conf):
+ conf_names = {'localhost_conf': set(['localhost', r'~^(www\.)?(example|bar)\.']),
+ 'server_conf': set(['somename', 'another.alias', 'alias']),
+ 'example_conf': set(['.example.com', 'example.*']),
+ 'foo_conf': set(['*.www.foo.com', '*.www.example.com']),
+ 'ipv6_conf': set(['ipv6.com'])}
conf_path = {'localhost': "etc_nginx/nginx.conf",
'alias': "etc_nginx/nginx.conf",
@@ -155,22 +172,22 @@ class NginxConfiguratorTest(util.NginxTest):
'www.bar.co.uk': "etc_nginx/nginx.conf",
'ipv6.com': "etc_nginx/sites-enabled/ipv6.com"}
+ vhost = self.config.choose_vhosts(name)[0]
+ path = os.path.relpath(vhost.filep, self.temp_dir)
+
+ self.assertEqual(conf_names[conf], vhost.names)
+ self.assertEqual(conf_path[name], path)
+ # IPv6 specific checks
+ if name == "ipv6.com":
+ self.assertTrue(vhost.ipv6_enabled())
+ # Make sure that we have SSL enabled also for IPv6 addr
+ self.assertTrue(
+ any([True for x in vhost.addrs if x.ssl and x.ipv6]))
+
+ def test_choose_vhosts_bad(self):
bad_results = ['www.foo.com', 'example', 't.www.bar.co',
'69.255.225.155']
- for name in results:
- vhost = self.config.choose_vhosts(name)[0]
- path = os.path.relpath(vhost.filep, self.temp_dir)
-
- self.assertEqual(results[name], vhost.names)
- self.assertEqual(conf_path[name], path)
- # IPv6 specific checks
- if name == "ipv6.com":
- self.assertTrue(vhost.ipv6_enabled())
- # Make sure that we have SSL enabled also for IPv6 addr
- self.assertTrue(
- any([True for x in vhost.addrs if x.ssl and x.ipv6]))
-
for name in bad_results:
self.assertRaises(errors.MisconfigurationError,
self.config.choose_vhosts, name)
diff --git a/certbot-nginx/certbot_nginx/tests/http_01_test.py b/certbot-nginx/certbot_nginx/tests/http_01_test.py
index 0f764e92e..ed3c257ee 100644
--- a/certbot-nginx/certbot_nginx/tests/http_01_test.py
+++ b/certbot-nginx/certbot_nginx/tests/http_01_test.py
@@ -12,6 +12,7 @@ from certbot import achallenges
from certbot.plugins import common_test
from certbot.tests import acme_util
+from certbot_nginx.obj import Addr
from certbot_nginx.tests import util
@@ -108,6 +109,41 @@ class HttpPerformTest(util.NginxTest):
# self.assertEqual(vhost.addrs, set(v_addr2_print))
# self.assertEqual(vhost.names, set([response.z_domain.decode('ascii')]))
+ @mock.patch("certbot_nginx.configurator.NginxConfigurator.ipv6_info")
+ def test_default_listen_addresses_no_memoization(self, ipv6_info):
+ # pylint: disable=protected-access
+ ipv6_info.return_value = (True, True)
+ self.http01._default_listen_addresses()
+ self.assertEqual(ipv6_info.call_count, 1)
+ ipv6_info.return_value = (False, False)
+ self.http01._default_listen_addresses()
+ self.assertEqual(ipv6_info.call_count, 2)
+
+ @mock.patch("certbot_nginx.configurator.NginxConfigurator.ipv6_info")
+ def test_default_listen_addresses_t_t(self, ipv6_info):
+ # pylint: disable=protected-access
+ ipv6_info.return_value = (True, True)
+ addrs = self.http01._default_listen_addresses()
+ http_addr = Addr.fromstring("80")
+ http_ipv6_addr = Addr.fromstring("[::]:80")
+ self.assertEqual(addrs, [http_addr, http_ipv6_addr])
+
+ @mock.patch("certbot_nginx.configurator.NginxConfigurator.ipv6_info")
+ def test_default_listen_addresses_t_f(self, ipv6_info):
+ # pylint: disable=protected-access
+ ipv6_info.return_value = (True, False)
+ addrs = self.http01._default_listen_addresses()
+ http_addr = Addr.fromstring("80")
+ http_ipv6_addr = Addr.fromstring("[::]:80 ipv6only=on")
+ self.assertEqual(addrs, [http_addr, http_ipv6_addr])
+
+ @mock.patch("certbot_nginx.configurator.NginxConfigurator.ipv6_info")
+ def test_default_listen_addresses_f_f(self, ipv6_info):
+ # pylint: disable=protected-access
+ ipv6_info.return_value = (False, False)
+ addrs = self.http01._default_listen_addresses()
+ http_addr = Addr.fromstring("80")
+ self.assertEqual(addrs, [http_addr])
if __name__ == "__main__":
unittest.main() # pragma: no cover
diff --git a/certbot-nginx/certbot_nginx/tests/parser_obj_test.py b/certbot-nginx/certbot_nginx/tests/parser_obj_test.py
new file mode 100644
index 000000000..c9c9dd440
--- /dev/null
+++ b/certbot-nginx/certbot_nginx/tests/parser_obj_test.py
@@ -0,0 +1,253 @@
+""" Tests for functions and classes in parser_obj.py """
+
+import unittest
+import mock
+
+from certbot_nginx.parser_obj import parse_raw
+from certbot_nginx.parser_obj import COMMENT_BLOCK
+
+class CommentHelpersTest(unittest.TestCase):
+ def test_is_comment(self):
+ from certbot_nginx.parser_obj import _is_comment
+ self.assertTrue(_is_comment(parse_raw(['#'])))
+ self.assertTrue(_is_comment(parse_raw(['#', ' literally anything else'])))
+ self.assertFalse(_is_comment(parse_raw(['not', 'even', 'a', 'comment'])))
+
+ def test_is_certbot_comment(self):
+ from certbot_nginx.parser_obj import _is_certbot_comment
+ self.assertTrue(_is_certbot_comment(
+ parse_raw(COMMENT_BLOCK)))
+ self.assertFalse(_is_certbot_comment(
+ parse_raw(['#', ' not a certbot comment'])))
+ self.assertFalse(_is_certbot_comment(
+ parse_raw(['#', ' managed by Certbot', ' also not a certbot comment'])))
+ self.assertFalse(_is_certbot_comment(
+ parse_raw(['not', 'even', 'a', 'comment'])))
+
+ def test_certbot_comment(self):
+ from certbot_nginx.parser_obj import _certbot_comment, _is_certbot_comment
+ comment = _certbot_comment(None)
+ self.assertTrue(_is_certbot_comment(comment))
+ self.assertEqual(comment.dump(), COMMENT_BLOCK)
+ self.assertEqual(comment.dump(True), [' '] + COMMENT_BLOCK)
+ self.assertEqual(_certbot_comment(None, 2).dump(True),
+ [' '] + COMMENT_BLOCK)
+
+class ParsingHooksTest(unittest.TestCase):
+ def test_is_sentence(self):
+ from certbot_nginx.parser_obj import Sentence
+ self.assertFalse(Sentence.should_parse([]))
+ self.assertTrue(Sentence.should_parse(['']))
+ self.assertTrue(Sentence.should_parse(['word']))
+ self.assertTrue(Sentence.should_parse(['two', 'words']))
+ self.assertFalse(Sentence.should_parse([[]]))
+ self.assertFalse(Sentence.should_parse(['word', []]))
+
+ def test_is_block(self):
+ from certbot_nginx.parser_obj import Block
+ self.assertFalse(Block.should_parse([]))
+ self.assertFalse(Block.should_parse(['']))
+ self.assertFalse(Block.should_parse(['two', 'words']))
+ self.assertFalse(Block.should_parse([[[]], []]))
+ self.assertFalse(Block.should_parse([['block_name'], ['hi', []], []]))
+ self.assertFalse(Block.should_parse([['block_name'], 'lol']))
+ self.assertTrue(Block.should_parse([['block_name'], ['hi', []]]))
+ self.assertTrue(Block.should_parse([['hello'], []]))
+ self.assertTrue(Block.should_parse([['block_name'], [['many'], ['statements'], 'here']]))
+ self.assertTrue(Block.should_parse([['if', ' ', '(whatever)'], ['hi']]))
+
+ def test_parse_raw(self):
+ fake_parser1 = mock.Mock()
+ fake_parser1.should_parse = lambda x: True
+ fake_parser2 = mock.Mock()
+ fake_parser2.should_parse = lambda x: False
+ # First encountered "match" should parse.
+ parse_raw([])
+ fake_parser1.called_once()
+ fake_parser2.not_called()
+ fake_parser1.reset_mock()
+ # "match" that returns False shouldn't parse.
+ parse_raw([])
+ fake_parser1.not_called()
+ fake_parser2.called_once()
+
+ @mock.patch("certbot_nginx.parser_obj.Parsable.parsing_hooks")
+ def test_parse_raw_no_match(self, parsing_hooks):
+ from certbot import errors
+ fake_parser1 = mock.Mock()
+ fake_parser1.should_parse = lambda x: False
+ parsing_hooks.return_value = (fake_parser1,)
+ self.assertRaises(errors.MisconfigurationError, parse_raw, [])
+ parsing_hooks.return_value = tuple()
+ self.assertRaises(errors.MisconfigurationError, parse_raw, [])
+
+ def test_parse_raw_passes_add_spaces(self):
+ fake_parser1 = mock.Mock()
+ fake_parser1.should_parse = lambda x: True
+ parse_raw([])
+ fake_parser1.parse.called_with([None])
+ parse_raw([], add_spaces=True)
+ fake_parser1.parse.called_with([None, True])
+
+class SentenceTest(unittest.TestCase):
+ def setUp(self):
+ from certbot_nginx.parser_obj import Sentence
+ self.sentence = Sentence(None)
+
+ def test_parse_bad_sentence_raises_error(self):
+ from certbot import errors
+ self.assertRaises(errors.MisconfigurationError, self.sentence.parse, 'lol')
+ self.assertRaises(errors.MisconfigurationError, self.sentence.parse, [[]])
+ self.assertRaises(errors.MisconfigurationError, self.sentence.parse, [5])
+
+ def test_parse_sentence_words_hides_spaces(self):
+ og_sentence = ['\r\n', 'hello', ' ', ' ', '\t\n ', 'lol', ' ', 'spaces']
+ self.sentence.parse(og_sentence)
+ self.assertEquals(self.sentence.words, ['hello', 'lol', 'spaces'])
+ self.assertEquals(self.sentence.dump(), ['hello', 'lol', 'spaces'])
+ self.assertEquals(self.sentence.dump(True), og_sentence)
+
+ def test_parse_sentence_with_add_spaces(self):
+ self.sentence.parse(['hi', 'there'], add_spaces=True)
+ self.assertEquals(self.sentence.dump(True), ['hi', ' ', 'there'])
+ self.sentence.parse(['one', ' ', 'space', 'none'], add_spaces=True)
+ self.assertEquals(self.sentence.dump(True), ['one', ' ', 'space', ' ', 'none'])
+
+ def test_iterate(self):
+ expected = [['1', '2', '3']]
+ self.sentence.parse(['1', ' ', '2', ' ', '3'])
+ for i, sentence in enumerate(self.sentence.iterate()):
+ self.assertEquals(sentence.dump(), expected[i])
+
+ def test_set_tabs(self):
+ self.sentence.parse(['tabs', 'pls'], add_spaces=True)
+ self.sentence.set_tabs()
+ self.assertEquals(self.sentence.dump(True)[0], '\n ')
+ self.sentence.parse(['tabs', 'pls'], add_spaces=True)
+
+ def test_get_tabs(self):
+ self.sentence.parse(['no', 'tabs'])
+ self.assertEquals(self.sentence.get_tabs(), '')
+ self.sentence.parse(['\n \n ', 'tabs'])
+ self.assertEquals(self.sentence.get_tabs(), ' ')
+ self.sentence.parse(['\n\t ', 'tabs'])
+ self.assertEquals(self.sentence.get_tabs(), '\t ')
+ self.sentence.parse(['\n\t \n', 'tabs'])
+ self.assertEquals(self.sentence.get_tabs(), '')
+
+class BlockTest(unittest.TestCase):
+ def setUp(self):
+ from certbot_nginx.parser_obj import Block
+ self.bloc = Block(None)
+ self.name = ['server', 'name']
+ self.contents = [['thing', '1'], ['thing', '2'], ['another', 'one']]
+ self.bloc.parse([self.name, self.contents])
+
+ def test_iterate(self):
+ # Iterates itself normally
+ self.assertEquals(self.bloc, next(self.bloc.iterate()))
+ # Iterates contents while expanded
+ expected = [self.bloc.dump()] + self.contents
+ for i, elem in enumerate(self.bloc.iterate(expanded=True)):
+ self.assertEquals(expected[i], elem.dump())
+
+ def test_iterate_match(self):
+ # can match on contents while expanded
+ from certbot_nginx.parser_obj import Block, Sentence
+ expected = [['thing', '1'], ['thing', '2']]
+ for i, elem in enumerate(self.bloc.iterate(expanded=True,
+ match=lambda x: isinstance(x, Sentence) and 'thing' in x.words)):
+ self.assertEquals(expected[i], elem.dump())
+ # can match on self
+ self.assertEquals(self.bloc, next(self.bloc.iterate(
+ expanded=True,
+ match=lambda x: isinstance(x, Block) and 'server' in x.names)))
+
+ def test_parse_with_added_spaces(self):
+ import copy
+ self.bloc.parse([copy.copy(self.name), self.contents], add_spaces=True)
+ self.assertEquals(self.bloc.dump(), [self.name, self.contents])
+ self.assertEquals(self.bloc.dump(True), [
+ ['server', ' ', 'name', ' '],
+ [['thing', ' ', '1'],
+ ['thing', ' ', '2'],
+ ['another', ' ', 'one']]])
+
+ def test_bad_parse_raises_error(self):
+ from certbot import errors
+ self.assertRaises(errors.MisconfigurationError, self.bloc.parse, [[[]], [[]]])
+ self.assertRaises(errors.MisconfigurationError, self.bloc.parse, ['lol'])
+ self.assertRaises(errors.MisconfigurationError, self.bloc.parse, ['fake', 'news'])
+
+ def test_set_tabs(self):
+ self.bloc.set_tabs()
+ self.assertEquals(self.bloc.names.dump(True)[0], '\n ')
+ for elem in self.bloc.contents.dump(True)[:-1]:
+ self.assertEquals(elem[0], '\n ')
+ self.assertEquals(self.bloc.contents.dump(True)[-1][0], '\n')
+
+ def test_get_tabs(self):
+ self.bloc.parse([[' \n \t', 'lol'], []])
+ self.assertEquals(self.bloc.get_tabs(), ' \t')
+
+class StatementsTest(unittest.TestCase):
+ def setUp(self):
+ from certbot_nginx.parser_obj import Statements
+ self.statements = Statements(None)
+ self.raw = [
+ ['sentence', 'one'],
+ ['sentence', 'two'],
+ ['and', 'another']
+ ]
+ self.raw_spaced = [
+ ['\n ', 'sentence', ' ', 'one'],
+ ['\n ', 'sentence', ' ', 'two'],
+ ['\n ', 'and', ' ', 'another'],
+ '\n\n'
+ ]
+
+ def test_set_tabs(self):
+ self.statements.parse(self.raw)
+ self.statements.set_tabs()
+ for statement in self.statements.iterate():
+ self.assertEquals(statement.dump(True)[0], '\n ')
+
+ def test_set_tabs_with_parent(self):
+ # Trailing whitespace should inherit from parent tabbing.
+ self.statements.parse(self.raw)
+ self.statements.parent = mock.Mock()
+ self.statements.parent.get_tabs.return_value = '\t\t'
+ self.statements.set_tabs()
+ for statement in self.statements.iterate():
+ self.assertEquals(statement.dump(True)[0], '\n ')
+ self.assertEquals(self.statements.dump(True)[-1], '\n\t\t')
+
+ def test_get_tabs(self):
+ self.raw[0].insert(0, '\n \n \t')
+ self.statements.parse(self.raw)
+ self.assertEquals(self.statements.get_tabs(), ' \t')
+ self.statements.parse([])
+ self.assertEquals(self.statements.get_tabs(), '')
+
+ def test_parse_with_added_spaces(self):
+ self.statements.parse(self.raw, add_spaces=True)
+ self.assertEquals(self.statements.dump(True)[0], ['sentence', ' ', 'one'])
+
+ def test_parse_bad_list_raises_error(self):
+ from certbot import errors
+ self.assertRaises(errors.MisconfigurationError, self.statements.parse, 'lol not a list')
+
+ def test_parse_hides_trailing_whitespace(self):
+ self.statements.parse(self.raw + ['\n\n '])
+ self.assertTrue(isinstance(self.statements.dump()[-1], list))
+ self.assertTrue(self.statements.dump(True)[-1].isspace())
+ self.assertEquals(self.statements.dump(True)[-1], '\n\n ')
+
+ def test_iterate(self):
+ self.statements.parse(self.raw)
+ expected = [['sentence', 'one'], ['sentence', 'two']]
+ for i, elem in enumerate(self.statements.iterate(match=lambda x: 'sentence' in x)):
+ self.assertEquals(expected[i], elem.dump())
+
+if __name__ == "__main__":
+ unittest.main() # pragma: no cover