From 4c1c2010c6197e0eff908c4e672711913e41320a Mon Sep 17 00:00:00 2001 From: Priit Laes Date: Wed, 9 Sep 2015 05:31:48 +0000 Subject: [PATCH] Add basic tests --- .gitignore | 1 + certidude/cli.py | 5 ++++- tests/test_cli.py | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 tests/test_cli.py diff --git a/.gitignore b/.gitignore index a41edc8..20c930c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ __pycache__/ *.py[cod] .goutputstream* +*.swp # C extensions *.so diff --git a/certidude/cli.py b/certidude/cli.py index 06837dc..acf97d8 100755 --- a/certidude/cli.py +++ b/certidude/cli.py @@ -474,7 +474,10 @@ def certidude_setup_authority(parent, country, state, locality, organization, or slug = os.path.basename(directory[:-1] if directory.endswith('/') else directory) if not slug: - raise ValueError("Please supply proper target path") + raise click.ClickException("Please supply proper target path") + # Make sure slug is valid + if not re.match(r"^[_a-zA-Z0-9]+$", slug): + raise click.ClickException("CA name can contain only alphanumeric and '_' characters") click.echo("CA configuration files are saved to: {}".format(os.path.abspath(slug))) diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 0000000..72a2161 --- /dev/null +++ b/tests/test_cli.py @@ -0,0 +1,34 @@ +import os +from click.testing import CliRunner +from certidude.cli import entry_point as cli + +runner = CliRunner() + +def test_cli(): + + # Authority setup + # TODO: group, parent, common-name, country, state, locality + # {authority,certificate,revocation-list}-lifetime + # organization, organizational-unit + # pkcs11 + # {crl-distribution,ocsp-responder}-url + # email-address + # inbox, outbox + with runner.isolated_filesystem(): + result = runner.invoke(cli, ['setup', 'authority', 'ca']) + + assert not result.exception + # Check whether required files were generated + for f in ('ca_key.pem', 'ca_crt.pem', 'ca_crl.pem', + 'serial', 'openssl.cnf.example'): + assert os.path.isfile(os.path.join('ca', f)) + for d in ('requests', 'revoked', 'signed'): + assert os.path.isdir(os.path.join('ca', d)) + +def test_cli_setup_authority_slug_name(): + with runner.isolated_filesystem(): + result = runner.invoke(cli, ['setup', 'authority']) + assert result.exception + + result = runner.invoke(cli, ['setup', 'authority', '""']) + assert result.exception