Changeset - ce28175b4896
[Not reviewed]
0 2 1
Branko Majic (branko) - 6 years ago 2018-03-12 20:59:28
branko@majic.rs
GC-16: Added client command parser:

- Added functional test for checking availability of client command
and its help.
- Added initial dummy implementation with parser.
3 files changed with 117 insertions and 0 deletions:
0 comments (0 inline, 0 general)
functional_tests/test_client.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_client_command_available_with_help():
 
    # After issuing a couple of server certificates, John has decided
 
    # to test out TLS client certificate authentication in his
 
    # project. For this, naturally, he needs a client certificate.
 
    # Hoping that the Gimmecert tool has ability to generate those
 
    # too, he runs the tool to see available commands.
 
    stdout, stderr, exit_code = run_command("gimmecert")
 

	
 
    # Looking at output, John notices the client command.
 
    assert exit_code == 0
 
    assert stderr == ""
 
    assert "client" in stdout
 

	
 
    # He goes ahead and has a look at the client command invocation to
 
    # check what kind of parameters he might need to provide.
 
    stdout, stderr, exit_code = run_command("gimmecert", "client", "-h")
 

	
 
    # John can see that the command accepts a single positional
 
    # argument - an entity name.
 
    assert exit_code == 0
 
    assert stderr == ""
 
    assert stdout.startswith("usage: gimmecert client")
 
    assert stdout.split('\n')[0].endswith(" entity_name")  # First line of help.
gimmecert/cli.py
Show inline comments
 
@@ -96,6 +96,19 @@ def setup_server_subcommand_parser(parser, subparsers):
 
    return subparser
 

	
 

	
 
@subcommand_parser
 
def setup_client_subcommand_parser(parser, subparsers):
 
    subparser = subparsers.add_parser('client', description='Issue client certificate.')
 
    subparser.add_argument('entity_name', help='Name of the client entity.')
 

	
 
    def client_wrapper(args):
 
        return ExitCode.SUCCESS
 

	
 
    subparser.set_defaults(func=client_wrapper)
 

	
 
    return subparser
 

	
 

	
 
def get_parser():
 
    """
 
    Sets-up and returns a CLI argument parser.
tests/test_cli.py
Show inline comments
 
@@ -458,3 +458,60 @@ def test_main_exits_if_it_calls_function_that_returns_success(tmpdir):
 
        gimmecert.cli.main()
 

	
 
    assert e_info.value.code == gimmecert.commands.ExitCode.ERROR_ALREADY_INITIALISED
 

	
 

	
 
@mock.patch('sys.argv', ['gimmecert', 'client', '-h'])
 
def test_client_command_exists_and_accepts_help_flag(tmpdir):
 
    # This should ensure we don't accidentally create artifacts
 
    # outside of test directory.
 
    tmpdir.chdir()
 

	
 
    with pytest.raises(SystemExit) as e_info:
 
        gimmecert.cli.main()
 

	
 
    assert e_info.value.code == 0
 

	
 

	
 
def test_setup_client_subcommand_parser_registered():
 
    registered_functions = gimmecert.decorators.get_subcommand_parser_setup_functions()
 

	
 
    assert gimmecert.cli.setup_client_subcommand_parser in registered_functions
 

	
 

	
 
def test_setup_client_subcommand_parser_returns_parser():
 
    parser = argparse.ArgumentParser()
 
    subparsers = parser.add_subparsers()
 

	
 
    subparser = gimmecert.cli.setup_client_subcommand_parser(parser, subparsers)
 

	
 
    assert isinstance(subparser, argparse.ArgumentParser)
 

	
 

	
 
@mock.patch('sys.argv', ['gimmecert', 'client'])
 
def test_setup_client_subcommand_fails_without_arguments(tmpdir):
 
    # This should ensure we don't accidentally create artifacts
 
    # outside of test directory.
 
    tmpdir.chdir()
 

	
 
    with pytest.raises(SystemExit) as e_info:
 
        gimmecert.cli.main()
 

	
 
    assert e_info.value.code != 0
 

	
 

	
 
@mock.patch('sys.argv', ['gimmecert', 'client', 'myclient'])
 
def test_setup_client_subcommand_succeeds_with_just_entity_name_argument(tmpdir):
 
    # This should ensure we don't accidentally create artifacts
 
    # outside of test directory.
 
    tmpdir.chdir()
 

	
 
    gimmecert.cli.main()  # Should not raise.
 

	
 

	
 
def test_setup_client_subcommand_sets_function_callback():
 
    parser = argparse.ArgumentParser()
 
    subparsers = parser.add_subparsers()
 

	
 
    subparser = gimmecert.cli.setup_client_subcommand_parser(parser, subparsers)
 

	
 
    assert callable(subparser.get_default('func'))
0 comments (0 inline, 0 general)