Changeset - 108dfefd1032
[Not reviewed]
0 5 0
Branko Majic (branko) - 6 years ago 2018-04-01 00:17:56
branko@majic.rs
GC-20: Show informative message when calling status command on uninitialised directory:

- Added small function test to cover the scenario when status command
is called on an uninitalised directory.
- Updated the status command implementation.
- Implemented the necessary unit tests.
5 files changed with 76 insertions and 3 deletions:
0 comments (0 inline, 0 general)
functional_tests/test_status.py
Show inline comments
 
@@ -49,3 +49,20 @@ def test_status_command_available_with_help():
 
    assert exit_code == 0
 
    assert stderr == ""
 
    assert stdout.split('\n')[0] == "usage: gimmecert status [-h]"
 

	
 

	
 
def test_status_on_uninitialised_directory(tmpdir):
 
    # John wants to see the status of current set of artefacts
 
    # generated by Gimmecert.
 

	
 
    # He swithces over to his project directory.
 
    tmpdir.chdir()
 

	
 
    # John runs the status command.
 
    stdout, stderr, exit_code = run_command('gimmecert', 'status')
 

	
 
    # It turns out that the directory has not been previously
 
    # initialised. In spite of this, the tool reports success, and
 
    # informs John that Gimmecert has not been initialised yet.
 
    assert exit_code == 0
 
    assert "CA hierarchy has not been initialised in current directory." in stdout
gimmecert/cli.py
Show inline comments
 
@@ -154,7 +154,9 @@ def setup_status_subcommand_parser(parser, subparsers):
 
    subparser = subparsers.add_parser(name="status", description="Shows status information about issued certificates.")
 

	
 
    def status_wrapper(args):
 
        status()
 
        project_directory = os.getcwd()
 

	
 
        status(sys.stdout, sys.stderr, project_directory)
 

	
 
        return ExitCode.SUCCESS
 

	
gimmecert/commands.py
Show inline comments
 
@@ -323,5 +323,25 @@ def renew(stdout, stderr, project_directory, entity_type, entity_name, generate_
 
    return ExitCode.SUCCESS
 

	
 

	
 
def status():
 
    pass
 
def status(stdout, stderr, project_directory):
 
    """
 
    Displays information about initialised hierarchy and issued
 
    certificates in project directory.
 

	
 
    :param stdout: Output stream where the informative messages should be written-out.
 
    :type stdout: io.IOBase
 

	
 
    :param stderr: Output stream where the error messages should be written-out.
 
    :type stderr: io.IOBase
 

	
 
    :param project_directory: Path to project directory under which the artefacts are looked-up.
 
    :type project_directory: str
 

	
 
    :returns: Status code, one from gimmecert.commands.ExitCode.
 
    :rtype: int
 
    """
 

	
 
    if not gimmecert.storage.is_initialised(project_directory):
 
        print("CA hierarchy has not been initialised in current directory.", file=stdout)
 

	
 
    return ExitCode.SUCCESS
tests/test_cli.py
Show inline comments
 
@@ -564,3 +564,17 @@ def test_renew_command_invoked_with_correct_parameters_for_client_with_new_priva
 
    gimmecert.cli.main()
 

	
 
    mock_renew.assert_called_once_with(sys.stdout, sys.stderr, tmpdir.strpath, 'client', 'myclient', True)
 

	
 

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

	
 
    mock_status.return_value = gimmecert.commands.ExitCode.SUCCESS
 

	
 
    gimmecert.cli.main()
 

	
 
    mock_status.assert_called_once_with(sys.stdout, sys.stderr, tmpdir.strpath)
tests/test_commands.py
Show inline comments
 
@@ -663,3 +663,23 @@ def test_renew_generates_new_private_key_if_requested(tmpdir):
 
    private_key_after_renewal = private_key_file.read()
 

	
 
    assert private_key_after_issuance != private_key_after_renewal
 

	
 

	
 
def test_status_returns_status_code(tmpdir):
 
    status_code = gimmecert.commands.status(io.StringIO(), io.StringIO(), tmpdir.strpath)
 

	
 
    assert isinstance(status_code, int)
 

	
 

	
 
def test_status_reports_uninitialised_directory(tmpdir):
 
    stdout_stream = io.StringIO()
 
    stderr_stream = io.StringIO()
 

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

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

	
 
    assert status_code == gimmecert.commands.ExitCode.SUCCESS
 
    assert stderr == ""
 
    assert "CA hierarchy has not been initialised in current directory." in stdout
0 comments (0 inline, 0 general)