Changeset - 95c7e343aa57
[Not reviewed]
0 2 1
Branko Majic (branko) - 6 years ago 2018-03-22 20:41:41
branko@majic.rs
GC-18: Added initial dummy implementation of the renew command:

- Added functional test covering testing of available help for the
renew command.
- Implemented unit tests
- Registered a renew command parser setup function with CLI.
3 files changed with 115 insertions and 0 deletions:
0 comments (0 inline, 0 general)
functional_tests/test_renew.py
Show inline comments
 
new file 100644
 
# -*- 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/>.
 
#
 

	
 

	
 
from .base import run_command
 

	
 

	
 
def test_renew_command_available_with_help():
 
    # John has been issuing server and client certificates using
 
    # Gimmecert for a while now. The project has been in use for quite
 
    # some time, and John has realised the certificates might be about
 
    # to expire. Thinking how tedious it would be to generate
 
    # everything again from scratch, he tries to figure out if there
 
    # is an easier way to do it instead of providing information for
 
    # all of the entities instead.
 
    stdout, stderr, exit_code = run_command("gimmecert")
 

	
 
    # Looking at output, John notices the renew command.
 
    assert exit_code == 0
 
    assert stderr == ""
 
    assert "renew" in stdout
 

	
 
    # He goes ahead and has a look at command invocation to check what
 
    # kind of parameters he might need to provide.
 
    stdout, stderr, exit_code = run_command("gimmecert", "renew", "-h")
 

	
 
    # John can see that the command accepts two positional argument -
 
    # type of entity, and entity name.
 
    assert exit_code == 0
 
    assert stderr == ""
 
    assert stdout.startswith("usage: gimmecert renew")
 
    assert stdout.split('\n')[0].endswith("{server,client} entity_name")  # First line of help
gimmecert/cli.py
Show inline comments
 
@@ -125,6 +125,15 @@ def setup_client_subcommand_parser(parser, subparsers):
 
    return subparser
 

	
 

	
 
@subcommand_parser
 
def setup_renew_subcommand_parser(parser, subparsers):
 
    subparser = subparsers.add_parser('renew', description='Renews existing certificates.')
 
    subparser.add_argument('entity_type', help='Type of entity to renew.', choices=['server', 'client'])
 
    subparser.add_argument('entity_name', help='Name of the entity')
 

	
 
    return subparser
 

	
 

	
 
def get_parser():
 
    """
 
    Sets-up and returns a CLI argument parser.
tests/test_cli.py
Show inline comments
 
@@ -571,3 +571,60 @@ def test_server_command_invoked_with_correct_parameters_with_update_option(mock_
 
    gimmecert.cli.main()
 

	
 
    mock_server.assert_called_once_with(sys.stdout, sys.stderr, tmpdir.strpath, 'myserver', ['service.local'], True)
 

	
 

	
 
@mock.patch('sys.argv', ['gimmecert', 'renew', '-h'])
 
def test_renew_command_exists_and_accepts_help_flag(tmpdir):
 
    # This should ensure we don't accidentally create artifacts
 
    # outside of test directory.
 
    tmpdir.chdir()
 

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

	
 
    assert e_info.value.code == 0
 

	
 

	
 
def test_setup_renew_subcommand_parser_registered():
 
    registered_functions = gimmecert.decorators.get_subcommand_parser_setup_functions()
 

	
 
    assert gimmecert.cli.setup_renew_subcommand_parser in registered_functions
 

	
 

	
 
def test_setup_renew_subcommand_parser_returns_parser():
 
    parser = argparse.ArgumentParser()
 
    subparsers = parser.add_subparsers()
 

	
 
    subparser = gimmecert.cli.setup_renew_subcommand_parser(parser, subparsers)
 

	
 
    assert isinstance(subparser, argparse.ArgumentParser)
 

	
 

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

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

	
 
    assert e_info.value.code != 0
 

	
 

	
 
@mock.patch('sys.argv', ['gimmecert', 'renew', 'server', 'myserver'])
 
def test_renew_command_accepts_entity_type_server_and_entity_name(tmpdir):
 
    # This should ensure we don't accidentally create artifacts
 
    # outside of test directory.
 
    tmpdir.chdir()
 

	
 
    gimmecert.cli.main()  # Should not raise
 

	
 

	
 
@mock.patch('sys.argv', ['gimmecert', 'renew', 'client', 'myclient'])
 
def test_renew_command_accepts_entity_type_client_and_entity_name(tmpdir):
 
    # This should ensure we don't accidentally create artifacts
 
    # outside of test directory.
 
    tmpdir.chdir()
 

	
 
    gimmecert.cli.main()  # Should not raise
0 comments (0 inline, 0 general)