Changeset - e17474c5ef5d
[Not reviewed]
0 2 1
Branko Majic (branko) - 6 years ago 2018-03-02 16:42:08
branko@majic.rs
GC-15: Added server command parser:

- Added functional test for testing new command presence and help.
- Added initial parser implementation.
3 files changed with 103 insertions and 0 deletions:
0 comments (0 inline, 0 general)
functional_tests/test_server.py
Show inline comments
 
new file 100644
 
# -*- 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 <http://www.gnu.org/licenses/>.
 
#
 

	
 

	
 
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.
gimmecert/cli.py
Show inline comments
 
@@ -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.
tests/test_cli.py
Show inline comments
 
@@ -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.
0 comments (0 inline, 0 general)