From e17474c5ef5d9ca85bc7bc1a99eae921b6176c5f 2018-03-02 16:42:08 From: Branko Majic Date: 2018-03-02 16:42:08 Subject: [PATCH] GC-15: Added server command parser: - Added functional test for testing new command presence and help. - Added initial parser implementation. --- diff --git a/functional_tests/test_server.py b/functional_tests/test_server.py new file mode 100644 index 0000000000000000000000000000000000000000..095fc57bba6436a1f58f2b1a52d5a19a5580012c --- /dev/null +++ b/functional_tests/test_server.py @@ -0,0 +1,45 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2018 Branko Majic +# +# This file is part of Gimmecert. +# +# Gimmecert is free software: you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the Free +# Software Foundation, either version 3 of the License, or (at your option) any +# later version. +# +# Gimmecert is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. +# +# You should have received a copy of the GNU General Public License along with +# Gimmecert. If not, see . +# + + +from .base import run_command + + +def test_server_command_available_with_help(): + # John has finally finished initialising his CA hierarchy. What he + # wants to do now is to issue a server certificate. He starts off + # by having a look at the list of available commands. + stdout, stderr, exit_code = run_command("gimmecert") + + # Looking at output, John notices the server command. + assert exit_code == 0 + assert stderr == "" + assert "server" in stdout + + # He goes ahead and has a look at the server command invocation to + # check what kind of parameters he might need to provide. + stdout, stderr, exit_code = run_command("gimmecert", "server", "-h") + + # John can see that the command accepts an entity name, and an + # optional list of DNS subject alternative names. + assert exit_code == 0 + assert stderr == "" + assert stdout.startswith("usage: gimmecert server") + assert stdout.split('\n')[0].endswith(" entity_name [dns_name [dns_name ...]]") # First line of help. diff --git a/gimmecert/cli.py b/gimmecert/cli.py index 1d3a358fa427540e8463ffbc1da5e6782b0422c0..33543ec68e0aa826a0b1ff08b7a28d8243c24e47 100644 --- a/gimmecert/cli.py +++ b/gimmecert/cli.py @@ -74,6 +74,14 @@ def setup_help_subcommand_parser(parser, subparsers): return subparser +@subcommand_parser +def setup_server_subcommand_parser(parser, subparsers): + subparser = subparsers.add_parser('server', description='Issues server certificate.') + subparser.add_argument('entity_name', help='Name of the server entity.') + subparser.add_argument('dns_name', nargs='*', help='Additional DNS names to include in subject alternative name.') + + return subparser + def get_parser(): """ Sets-up and returns a CLI argument parser. diff --git a/tests/test_cli.py b/tests/test_cli.py index 485c5d2042d4fe77049c1a6966da1b4eb127fd0c..b13ce68e6aae3c7e130efea845f717aceafe456f 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -24,6 +24,7 @@ import argparse import gimmecert.cli import gimmecert.decorators +import pytest from unittest import mock @@ -206,3 +207,52 @@ def test_init_command_accepts_ca_hierarchy_depth_option_long_form(): def test_init_command_accepts_ca_hierarchy_depth_option_short_form(): gimmecert.cli.main() # Should not raise + + +@mock.patch('sys.argv', ['gimmecert', 'server', '-h']) +def test_server_command_exists_and_accepts_help_flag(): + with pytest.raises(SystemExit) as e_info: + gimmecert.cli.main() + + assert e_info.value.code == 0 + + +def test_setup_server_subcommand_parser_registered(): + registered_functions = gimmecert.decorators.get_subcommand_parser_setup_functions() + + assert gimmecert.cli.setup_server_subcommand_parser in registered_functions + + +def test_setup_server_subcommand_parser_returns_parser(): + parser = argparse.ArgumentParser() + subparsers = parser.add_subparsers() + + subparser = gimmecert.cli.setup_server_subcommand_parser(parser, subparsers) + + assert isinstance(subparser, argparse.ArgumentParser) + + +@mock.patch('sys.argv', ['gimmecert', 'server']) +def test_setup_server_subcommand_fails_without_arguments(): + with pytest.raises(SystemExit) as e_info: + gimmecert.cli.main() + + assert e_info.value.code != 0 + + +@mock.patch('sys.argv', ['gimmecert', 'server', 'myserver']) +def test_setup_server_subcommand_succeeds_with_just_entity_name_argument(): + + gimmecert.cli.main() # Should not raise. + + +@mock.patch('sys.argv', ['gimmecert', 'server', 'myserver', 'myserver.example.com']) +def test_setup_server_subcommand_succeeds_with_entity_name_argument_and_one_dns_name(): + + gimmecert.cli.main() # Should not raise. + + +@mock.patch('sys.argv', ['gimmecert', 'server', 'myserver', 'myserver1.example.com', 'myserver2.example.com', 'myserver3.example.com', 'myserver4.example.com']) +def test_setup_server_subcommand_succeeds_with_entity_name_argument_and_four_dns_names(): + + gimmecert.cli.main() # Should not raise.