Changeset - 500f5d252fbd
[Not reviewed]
0 5 0
Branko Majic (branko) - 6 years ago 2018-03-12 22:44:56
branko@majic.rs
GC-16: Implemented scenario for issuing client certificates when hierarchy is not initialised:

- Added functional test that checks if an error is shown to user in
case the hierarchy hasn't been initialised prior to issuing a client
certificate.
- Added initial dummy client command implementation.
5 files changed with 66 insertions and 3 deletions:
0 comments (0 inline, 0 general)
functional_tests/test_client.py
Show inline comments
 
@@ -45,3 +45,19 @@ def test_client_command_available_with_help():
 
    assert stderr == ""
 
    assert stdout.startswith("usage: gimmecert client")
 
    assert stdout.split('\n')[0].endswith(" entity_name")  # First line of help.
 

	
 

	
 
def test_client_command_requires_initialised_hierarchy(tmpdir):
 
    # John is about to issue a client certificate. He switches to his
 
    # project directory.
 
    tmpdir.chdir()
 

	
 
    # John tries to issue a client certificate.
 
    stdout, stderr, exit_code = run_command("gimmecert", "client", "myclient")
 

	
 
    # Unfortunately, John has forgotten to initialise the CA hierarchy
 
    # from within this directory, and is instead presented with an
 
    # error.
 
    assert stdout == ""
 
    assert stderr == "CA hierarchy must be initialised prior to issuing client certificates. Run the gimmecert init command first.\n"
 
    assert exit_code != 0
gimmecert/cli.py
Show inline comments
 
@@ -24,7 +24,7 @@ import os
 
import sys
 

	
 
from .decorators import subcommand_parser, get_subcommand_parser_setup_functions
 
from .commands import init, server, help_, usage, ExitCode
 
from .commands import client, help_, init, server, usage, ExitCode
 

	
 

	
 
ERROR_GENERIC = 10
 
@@ -102,7 +102,9 @@ def setup_client_subcommand_parser(parser, subparsers):
 
    subparser.add_argument('entity_name', help='Name of the client entity.')
 

	
 
    def client_wrapper(args):
 
        return ExitCode.SUCCESS
 
        project_directory = os.getcwd()
 

	
 
        return client(sys.stdout, sys.stderr, project_directory)
 

	
 
    subparser.set_defaults(func=client_wrapper)
 

	
gimmecert/commands.py
Show inline comments
 
@@ -186,3 +186,9 @@ def usage(stdout, stderr, parser):
 
    parser.print_usage(stdout)
 

	
 
    return ExitCode.SUCCESS
 

	
 

	
 
def client(stdout, stderr, project_directory):
 
    if not gimmecert.storage.is_initialised(project_directory):
 
        print("CA hierarchy must be initialised prior to issuing client certificates. Run the gimmecert init command first.", file=stderr)
 
        return ExitCode.ERROR_NOT_INITIALISED
tests/test_cli.py
Show inline comments
 
@@ -500,11 +500,15 @@ def test_setup_client_subcommand_fails_without_arguments(tmpdir):
 

	
 

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

	
 
    # We are just testing the parsing here.
 
    mock_client.return_value = gimmecert.commands.ExitCode.SUCCESS
 

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

	
 

	
 
@@ -515,3 +519,17 @@ def test_setup_client_subcommand_sets_function_callback():
 
    subparser = gimmecert.cli.setup_client_subcommand_parser(parser, subparsers)
 

	
 
    assert callable(subparser.get_default('func'))
 

	
 

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

	
 
    mock_client.return_value = gimmecert.commands.ExitCode.SUCCESS
 

	
 
    gimmecert.cli.main()
 

	
 
    mock_client.assert_called_once_with(sys.stdout, sys.stderr, tmpdir.strpath)
tests/test_commands.py
Show inline comments
 
@@ -315,3 +315,24 @@ def test_usage_command_outputs_usage():
 
    assert stderr == ""
 
    assert "usage:" in stdout
 
    assert len(stdout.splitlines()) == 1
 

	
 

	
 
def test_client_reports_error_if_directory_is_not_initialised(tmpdir):
 

	
 
    stdout_stream = io.StringIO()
 
    stderr_stream = io.StringIO()
 

	
 
    status_code = gimmecert.commands.client(stdout_stream, stderr_stream, tmpdir.strpath)
 

	
 
    stdout = stdout_stream.getvalue()
 
    stderr = stderr_stream.getvalue()
 

	
 
    assert "must be initialised" in stderr
 
    assert stdout == ""
 
    assert status_code == gimmecert.commands.ExitCode.ERROR_NOT_INITIALISED
 

	
 

	
 
def test_client_returns_status_code(tmpdir):
 
    status_code = gimmecert.commands.client(io.StringIO(), io.StringIO(), tmpdir.strpath)
 

	
 
    assert isinstance(status_code, int)
0 comments (0 inline, 0 general)