diff --git a/tests/test_commands.py b/tests/test_commands.py index 89e5f57dfa96be6341cf831553f419f3a4794c46..87c2d56cee23ad48b726d7b5214bb2012c36a847 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -26,52 +26,107 @@ import gimmecert.commands def test_init_sets_up_directory_structure(tmpdir): base_dir = tmpdir.join('.gimmecert') ca_dir = tmpdir.join('.gimmecert') + depth = 1 tmpdir.chdir() - gimmecert.commands.init(tmpdir.strpath, tmpdir.basename) + gimmecert.commands.init(tmpdir.strpath, tmpdir.basename, depth) assert os.path.exists(base_dir.strpath) assert os.path.exists(ca_dir.strpath) -def test_init_generates_ca_artifacts(tmpdir): +def test_init_generates_single_ca_artifact_for_depth_1(tmpdir): + depth = 1 + tmpdir.chdir() - gimmecert.commands.init(tmpdir.strpath, tmpdir.basename) + gimmecert.commands.init(tmpdir.strpath, tmpdir.basename, depth) assert os.path.exists(tmpdir.join('.gimmecert', 'ca', 'level1.key.pem').strpath) assert os.path.exists(tmpdir.join('.gimmecert', 'ca', 'level1.cert.pem').strpath) assert os.path.exists(tmpdir.join('.gimmecert', 'ca', 'chain-full.cert.pem').strpath) +def test_init_generates_three_ca_artifacts_for_depth_3(tmpdir): + depth = 3 + + tmpdir.chdir() + + gimmecert.commands.init(tmpdir.strpath, tmpdir.basename, depth) + + assert os.path.exists(tmpdir.join('.gimmecert', 'ca', 'level1.key.pem').strpath) + assert os.path.exists(tmpdir.join('.gimmecert', 'ca', 'level1.cert.pem').strpath) + assert os.path.exists(tmpdir.join('.gimmecert', 'ca', 'level2.key.pem').strpath) + assert os.path.exists(tmpdir.join('.gimmecert', 'ca', 'level2.cert.pem').strpath) + assert os.path.exists(tmpdir.join('.gimmecert', 'ca', 'level3.key.pem').strpath) + assert os.path.exists(tmpdir.join('.gimmecert', 'ca', 'level3.cert.pem').strpath) + assert os.path.exists(tmpdir.join('.gimmecert', 'ca', 'chain-full.cert.pem').strpath) + + +def test_init_outputs_full_chain_for_depth_1(tmpdir): + depth = 1 + + tmpdir.chdir() + + gimmecert.commands.init(tmpdir.strpath, tmpdir.basename, depth) + + level1_certificate = tmpdir.join('.gimmecert', 'ca', 'level1.cert.pem').read() + full_chain = tmpdir.join('.gimmecert', 'ca', 'chain-full.cert.pem').read() + assert level1_certificate == full_chain + assert full_chain.replace(level1_certificate, '') == '' + + +def test_init_outputs_full_chain_for_depth_3(tmpdir): + depth = 3 + + tmpdir.chdir() + + gimmecert.commands.init(tmpdir.strpath, tmpdir.basename, depth) + + level1_certificate = tmpdir.join('.gimmecert', 'ca', 'level1.cert.pem').read() + level2_certificate = tmpdir.join('.gimmecert', 'ca', 'level2.cert.pem').read() + level3_certificate = tmpdir.join('.gimmecert', 'ca', 'level3.cert.pem').read() + full_chain = tmpdir.join('.gimmecert', 'ca', 'chain-full.cert.pem').read() + assert level1_certificate in full_chain + assert level2_certificate in full_chain + assert level3_certificate in full_chain + assert full_chain == "%s\n%s\n%s" % (level1_certificate, level2_certificate, level3_certificate) + + def test_init_returns_true_if_directory_has_not_been_previously_initialised(tmpdir): + depth = 1 + tmpdir.chdir() - initialised = gimmecert.commands.init(tmpdir.strpath, tmpdir.basename) + initialised = gimmecert.commands.init(tmpdir.strpath, tmpdir.basename, depth) assert initialised is True def test_init_returns_false_if_directory_has_been_previously_initialised(tmpdir): + depth = 1 + tmpdir.chdir() - gimmecert.commands.init(tmpdir.strpath, tmpdir.basename) - initialised = gimmecert.commands.init(tmpdir.strpath, tmpdir.basename) + gimmecert.commands.init(tmpdir.strpath, tmpdir.basename, depth) + initialised = gimmecert.commands.init(tmpdir.strpath, tmpdir.basename, depth) assert initialised is False def test_init_does_not_overwrite_artifcats_if_already_initialised(tmpdir): + depth = 1 + tmpdir.chdir() - gimmecert.commands.init(tmpdir.strpath, tmpdir.basename) + gimmecert.commands.init(tmpdir.strpath, tmpdir.basename, depth) level1_private_key_before = tmpdir.join('.gimmecert', 'ca', 'level1.key.pem').read() level1_certificate_before = tmpdir.join('.gimmecert', 'ca', 'level1.cert.pem').read() full_chain_before = tmpdir.join('.gimmecert', 'ca', 'chain-full.cert.pem').read() - gimmecert.commands.init(tmpdir.strpath, tmpdir.basename) + gimmecert.commands.init(tmpdir.strpath, tmpdir.basename, depth) level1_private_key_after = tmpdir.join('.gimmecert', 'ca', 'level1.key.pem').read() level1_certificate_after = tmpdir.join('.gimmecert', 'ca', 'level1.cert.pem').read()