Changeset - 637d64ca88f7
[Not reviewed]
0 3 0
Branko Majic (branko) - 6 years ago 2018-03-09 22:52:45
branko@majic.rs
GC-17: Removed redundant tests, cleaned-up tests for commands module and fixed wrong directory used for outputting server artifacts in server command:

- Removed CLI tests that check if command errors-out in case the
hierarchy has already been initialised (for init command) or has not
been initialised (for server command). These are part of commands
module tests already.
- Removed unnecessary changing of directory within the commands
tests.
- Fixed issue where server artifacts paths are not calculated
correctly when writing them out (parent directory was not part of
path).
3 files changed with 2 insertions and 48 deletions:
0 comments (0 inline, 0 general)
gimmecert/commands.py
Show inline comments
 
@@ -118,8 +118,8 @@ def server(stdout, stderr, project_directory, entity_name, extra_dns_names):
 
    :rtype: int
 
    """
 

	
 
    private_key_path = os.path.join('.gimmecert', 'server', '%s.key.pem' % entity_name)
 
    certificate_path = os.path.join('.gimmecert', 'server', '%s.cert.pem' % entity_name)
 
    private_key_path = os.path.join(project_directory, '.gimmecert', 'server', '%s.key.pem' % entity_name)
 
    certificate_path = os.path.join(project_directory, '.gimmecert', 'server', '%s.cert.pem' % entity_name)
 

	
 
    if not gimmecert.storage.is_initialised(project_directory):
 
        print("CA hierarchy must be initialised prior to issuing server certificates. Run the gimmecert init command first.", file=stderr)
tests/test_cli.py
Show inline comments
 
@@ -20,7 +20,6 @@
 

	
 

	
 
import argparse
 
import io
 
import sys
 

	
 
import gimmecert.cli
 
@@ -237,17 +236,6 @@ def test_init_command_accepts_ca_hierarchy_depth_option_short_form(mock_init):
 
    gimmecert.cli.main()  # Should not raise
 

	
 

	
 
@mock.patch('sys.argv', ['gimmecert', 'init'])
 
def test_init_command_exists_with_error_if_hierarchy_already_initialised(tmpdir):
 
    tmpdir.chdir()
 
    gimmecert.commands.init(io.StringIO(), io.StringIO(), tmpdir.strpath, tmpdir.basename, 1)
 

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

	
 
    assert e_info.value.code != 0
 

	
 

	
 
@mock.patch('sys.argv', ['gimmecert', 'server', '-h'])
 
def test_server_command_exists_and_accepts_help_flag():
 
    with pytest.raises(SystemExit) as e_info:
 
@@ -339,16 +327,6 @@ def test_server_command_invoked_with_correct_parameters_with_extra_dns_names(moc
 
    mock_server.assert_called_once_with(sys.stdout, sys.stderr, tmpdir.strpath, 'myserver', ['service.local', 'service.example.com'])
 

	
 

	
 
@mock.patch('sys.argv', ['gimmecert', 'server', 'myserver'])
 
def test_server_command_exists_with_error_if_hierarchy_not_initialised(tmpdir):
 
    tmpdir.chdir()
 

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

	
 
    assert e_info.value.code != 0
 

	
 

	
 
@mock.patch('sys.argv', ['gimmecert', 'help'])
 
@mock.patch('gimmecert.cli.help_')
 
def test_help_command_invoked_with_correct_parameters(mock_help_):
tests/test_commands.py
Show inline comments
 
@@ -31,8 +31,6 @@ def test_init_sets_up_directory_structure(tmpdir):
 
    server_dir = tmpdir.join('.gimmecert', 'server')
 
    depth = 1
 

	
 
    tmpdir.chdir()
 

	
 
    gimmecert.commands.init(io.StringIO(), io.StringIO(), tmpdir.strpath, tmpdir.basename, depth)
 

	
 
    assert os.path.exists(base_dir.strpath)
 
@@ -43,8 +41,6 @@ def test_init_sets_up_directory_structure(tmpdir):
 
def test_init_generates_single_ca_artifact_for_depth_1(tmpdir):
 
    depth = 1
 

	
 
    tmpdir.chdir()
 

	
 
    gimmecert.commands.init(io.StringIO(), io.StringIO(), tmpdir.strpath, tmpdir.basename, depth)
 

	
 
    assert os.path.exists(tmpdir.join('.gimmecert', 'ca', 'level1.key.pem').strpath)
 
@@ -55,8 +51,6 @@ def test_init_generates_single_ca_artifact_for_depth_1(tmpdir):
 
def test_init_generates_three_ca_artifacts_for_depth_3(tmpdir):
 
    depth = 3
 

	
 
    tmpdir.chdir()
 

	
 
    gimmecert.commands.init(io.StringIO(), io.StringIO(), tmpdir.strpath, tmpdir.basename, depth)
 

	
 
    assert os.path.exists(tmpdir.join('.gimmecert', 'ca', 'level1.key.pem').strpath)
 
@@ -71,8 +65,6 @@ def test_init_generates_three_ca_artifacts_for_depth_3(tmpdir):
 
def test_init_outputs_full_chain_for_depth_1(tmpdir):
 
    depth = 1
 

	
 
    tmpdir.chdir()
 

	
 
    gimmecert.commands.init(io.StringIO(), io.StringIO(), tmpdir.strpath, tmpdir.basename, depth)
 

	
 
    level1_certificate = tmpdir.join('.gimmecert', 'ca', 'level1.cert.pem').read()
 
@@ -84,8 +76,6 @@ def test_init_outputs_full_chain_for_depth_1(tmpdir):
 
def test_init_outputs_full_chain_for_depth_3(tmpdir):
 
    depth = 3
 

	
 
    tmpdir.chdir()
 

	
 
    gimmecert.commands.init(io.StringIO(), io.StringIO(), tmpdir.strpath, tmpdir.basename, depth)
 

	
 
    level1_certificate = tmpdir.join('.gimmecert', 'ca', 'level1.cert.pem').read()
 
@@ -101,8 +91,6 @@ def test_init_outputs_full_chain_for_depth_3(tmpdir):
 
def test_init_returns_success_if_directory_has_not_been_previously_initialised(tmpdir):
 
    depth = 1
 

	
 
    tmpdir.chdir()
 

	
 
    status_code = gimmecert.commands.init(io.StringIO(), io.StringIO(), tmpdir.strpath, tmpdir.basename, depth)
 

	
 
    assert status_code == gimmecert.commands.ExitCode.SUCCESS
 
@@ -111,8 +99,6 @@ def test_init_returns_success_if_directory_has_not_been_previously_initialised(t
 
def test_init_returns_error_code_if_directory_has_been_previously_initialised(tmpdir):
 
    depth = 1
 

	
 
    tmpdir.chdir()
 

	
 
    gimmecert.commands.init(io.StringIO(), io.StringIO(), tmpdir.strpath, tmpdir.basename, depth)
 
    status_code = gimmecert.commands.init(io.StringIO(), io.StringIO(), tmpdir.strpath, tmpdir.basename, depth)
 

	
 
@@ -122,8 +108,6 @@ def test_init_returns_error_code_if_directory_has_been_previously_initialised(tm
 
def test_init_does_not_overwrite_artifcats_if_already_initialised(tmpdir):
 
    depth = 1
 

	
 
    tmpdir.chdir()
 

	
 
    gimmecert.commands.init(io.StringIO(), io.StringIO(), tmpdir.strpath, tmpdir.basename, depth)
 

	
 
    level1_private_key_before = tmpdir.join('.gimmecert', 'ca', 'level1.key.pem').read()
 
@@ -142,15 +126,12 @@ def test_init_does_not_overwrite_artifcats_if_already_initialised(tmpdir):
 

	
 

	
 
def test_server_returns_status_code(tmpdir):
 
    tmpdir.chdir()
 

	
 
    status_code = gimmecert.commands.server(io.StringIO(), io.StringIO(), tmpdir.strpath, 'myserver', None)
 

	
 
    assert isinstance(status_code, int)
 

	
 

	
 
def test_server_reports_error_if_directory_is_not_initialised(tmpdir):
 
    tmpdir.chdir()
 

	
 
    stdout_stream = io.StringIO()
 
    stderr_stream = io.StringIO()
 
@@ -171,7 +152,6 @@ def test_server_reports_success_and_paths_to_generated_artifacts(tmpdir):
 
    stdout_stream = io.StringIO()
 
    stderr_stream = io.StringIO()
 

	
 
    tmpdir.chdir()
 
    gimmecert.commands.init(io.StringIO(), io.StringIO(), tmpdir.strpath, tmpdir.basename, depth)
 

	
 
    status_code = gimmecert.commands.server(stdout_stream, stderr_stream, tmpdir.strpath, 'myserver', None)
 
@@ -189,7 +169,6 @@ def test_server_outputs_private_key_to_file(tmpdir):
 
    depth = 1
 
    private_key_file = tmpdir.join('.gimmecert', 'server', 'myserver.key.pem')
 

	
 
    tmpdir.chdir()
 
    gimmecert.commands.init(io.StringIO(), io.StringIO(), tmpdir.strpath, tmpdir.basename, depth)
 

	
 
    gimmecert.commands.server(io.StringIO(), io.StringIO(), tmpdir.strpath, 'myserver', None)
 
@@ -206,7 +185,6 @@ def test_server_outputs_certificate_to_file(tmpdir):
 
    depth = 1
 
    certificate_file = tmpdir.join('.gimmecert', 'server', 'myserver.cert.pem')
 

	
 
    tmpdir.chdir()
 
    gimmecert.commands.init(io.StringIO(), io.StringIO(), tmpdir.strpath, tmpdir.basename, depth)
 

	
 
    gimmecert.commands.server(io.StringIO(), io.StringIO(), tmpdir.strpath, 'myserver', None)
 
@@ -220,8 +198,6 @@ def test_server_outputs_certificate_to_file(tmpdir):
 

	
 

	
 
def test_server_errors_out_if_certificate_already_issued(tmpdir):
 
    tmpdir.chdir()
 

	
 
    depth = 1
 

	
 
    stdout_stream = io.StringIO()
0 comments (0 inline, 0 general)