Files
@ 988ac40d5cec
Branch filter:
Location: gimmecert/tests/test_commands.py
988ac40d5cec
5.3 KiB
text/x-python
GC-15: Implemented scenario for server certificate issuance where user has not initialised the CA hierarchy:
- Added functional test that tests if correct error is shown to user
in case he/she has not initialised the CA hierarchy.
- Introduced new function to check if storage is initialised.
- Added initial simplified server command implementation.
- Added functional test that tests if correct error is shown to user
in case he/she has not initialised the CA hierarchy.
- Introduced new function to check if storage is initialised.
- Added initial simplified server command implementation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | # -*- 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/>.
#
import os
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, depth)
assert os.path.exists(base_dir.strpath)
assert os.path.exists(ca_dir.strpath)
def test_init_generates_single_ca_artifact_for_depth_1(tmpdir):
depth = 1
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', '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, 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, 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, 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, depth)
level1_private_key_after = tmpdir.join('.gimmecert', 'ca', 'level1.key.pem').read()
level1_certificate_after = tmpdir.join('.gimmecert', 'ca', 'level1.cert.pem').read()
full_chain_after = tmpdir.join('.gimmecert', 'ca', 'chain-full.cert.pem').read()
assert level1_private_key_before == level1_private_key_after
assert level1_certificate_before == level1_certificate_after
assert full_chain_before == full_chain_after
def test_server_returns_status_and_message(tmpdir):
tmpdir.chdir()
status, message = gimmecert.commands.server(tmpdir.strpath, 'myserver')
assert isinstance(status, bool)
assert isinstance(message, str)
def test_server_reports_error_if_directory_is_not_initialised(tmpdir):
tmpdir.chdir()
status, message = gimmecert.commands.server(tmpdir.strpath, 'myserver')
assert status is False
assert "must be initialised" in message
|