Files
@ 2450d422e8af
Branch filter:
Location: gimmecert/tests/test_cli.py
2450d422e8af
18.1 KiB
text/x-python
GC-19: Added option for updating server certificate DNS names:
- Added functional test covering the new scenario.
- Added option to server command for updating DNS names for already
issued certificate. Private key is kept for this purpose.
- Implemented unit tests.
- Fixed functional test related to viewing short usage instructions.
- Added functional test covering the new scenario.
- Added option to server command for updating DNS names for already
issued certificate. Private key is kept for this purpose.
- Implemented unit tests.
- Fixed functional test related to viewing short usage instructions.
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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 | # -*- 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 argparse
import sys
import gimmecert.cli
import gimmecert.decorators
import pytest
from unittest import mock
def test_get_parser_returns_parser():
parser = gimmecert.cli.get_parser()
assert isinstance(parser, argparse.ArgumentParser)
@mock.patch('gimmecert.cli.get_parser')
def test_main_invokes_get_parser(mock_get_parser, tmpdir):
# This should ensure we don't accidentally create artifacts
# outside of test directory.
tmpdir.chdir()
# Ignore system exit. Dirty hack to avoid mocking the default
# function. We care only about whether the get_parser is invoked.
try:
gimmecert.cli.main()
except SystemExit:
pass
mock_get_parser.assert_called_once_with()
@mock.patch('gimmecert.cli.get_parser')
def test_main_invokes_argument_parsing(mock_get_parser, tmpdir):
# This should ensure we don't accidentally create artifacts
# outside of test directory.
tmpdir.chdir()
mock_parser = mock.Mock()
mock_get_parser.return_value = mock_parser
# Ignore system exit. Dirty hack to avoid mocking the default
# function. We care only about whether the parsing of arguments
# got called.x
try:
gimmecert.cli.main()
except SystemExit:
pass
mock_parser.parse_args.assert_called_once_with()
def test_cli_parser_has_description():
parser = gimmecert.cli.get_parser()
assert parser.description
def test_parser_sets_up_default_callback_function():
parser = gimmecert.cli.get_parser()
assert callable(parser.get_default('func'))
@mock.patch('gimmecert.cli.argparse.ArgumentParser.print_usage')
def test_parser_default_callback_function_calls_print_usage(mock_print_usage):
parser = gimmecert.cli.get_parser()
func = parser.get_default('func')
func(mock.Mock())
assert mock_print_usage.called
@mock.patch('gimmecert.cli.get_parser')
def test_main_invokes_parser_function(mock_get_parser, tmpdir):
# This should ensure we don't accidentally create artifacts
# outside of test directory.
tmpdir.chdir()
mock_parser = mock.Mock()
mock_args = mock.Mock()
# Avoid throws of SystemExit exception.
mock_args.func.return_value = gimmecert.commands.ExitCode.SUCCESS
mock_parser.parse_args.return_value = mock_args
mock_get_parser.return_value = mock_parser
gimmecert.cli.main()
mock_args.func.assert_called_once_with(mock_args)
def test_parser_help_contains_examples():
parser = gimmecert.cli.get_parser()
assert 'Examples' in parser.description
def test_setup_help_subcommand_parser_registered():
registered_functions = gimmecert.decorators.get_subcommand_parser_setup_functions()
assert gimmecert.cli.setup_help_subcommand_parser in registered_functions
@mock.patch('gimmecert.cli.get_subcommand_parser_setup_functions')
def test_get_parser_calls_setup_subcommand_parser_functions(mock_get_subcommand_parser_setup_functions):
mock_setup1 = mock.Mock()
mock_setup2 = mock.Mock()
mock_get_subcommand_parser_setup_functions.return_value = [mock_setup1, mock_setup2]
gimmecert.cli.get_parser()
assert mock_setup1.called
assert mock_setup2.called
def test_setup_help_subcommand_parser_adds_parser():
mock_parser = mock.Mock()
mock_subparsers = mock.Mock()
gimmecert.cli.setup_help_subcommand_parser(mock_parser, mock_subparsers)
assert mock_subparsers.add_parser.called
def test_help_subcommand_returns_parser():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
subparser = gimmecert.cli.setup_help_subcommand_parser(parser, subparsers)
assert isinstance(subparser, argparse.ArgumentParser)
def test_help_subcommand_sets_function_callback():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
subparser = gimmecert.cli.setup_help_subcommand_parser(parser, subparsers)
assert callable(subparser.get_default('func'))
def test_setup_init_subcommand_parser_registered():
registered_functions = gimmecert.decorators.get_subcommand_parser_setup_functions()
assert gimmecert.cli.setup_init_subcommand_parser in registered_functions
def test_setup_init_subcommand_returns_parser():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
subparser = gimmecert.cli.setup_init_subcommand_parser(parser, subparsers)
assert isinstance(subparser, argparse.ArgumentParser)
def test_setup_init_subcommand_sets_function_callback():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
subparser = gimmecert.cli.setup_init_subcommand_parser(parser, subparsers)
assert callable(subparser.get_default('func'))
@mock.patch('sys.argv', ['gimmecert', 'init'])
@mock.patch('gimmecert.cli.init')
def test_init_command_invoked_with_correct_parameters_no_options(mock_init, tmpdir):
# This should ensure we don't accidentally create artifacts
# outside of test directory.
tmpdir.chdir()
mock_init.return_value = gimmecert.commands.ExitCode.SUCCESS
default_depth = 1
gimmecert.cli.main()
mock_init.assert_called_once_with(sys.stdout, sys.stderr, tmpdir.strpath, tmpdir.basename, default_depth)
@mock.patch('sys.argv', ['gimmecert', 'init', '-b', 'My Project'])
@mock.patch('gimmecert.cli.init')
def test_init_command_invoked_with_correct_parameters_with_options(mock_init, tmpdir):
# This should ensure we don't accidentally create artifacts
# outside of test directory.
tmpdir.chdir()
mock_init.return_value = gimmecert.commands.ExitCode.SUCCESS
default_depth = 1
gimmecert.cli.main()
mock_init.assert_called_once_with(sys.stdout, sys.stderr, tmpdir.strpath, 'My Project', default_depth)
@mock.patch('sys.argv', ['gimmecert', 'init', '--ca-base-name', 'My Project'])
@mock.patch('gimmecert.cli.init')
def test_init_command_accepts_ca_base_name_option_long_form(mock_init, tmpdir):
# This should ensure we don't accidentally create artifacts
# outside of test directory.
tmpdir.chdir()
mock_init.return_value = gimmecert.commands.ExitCode.SUCCESS
gimmecert.cli.main() # Should not raise
@mock.patch('sys.argv', ['gimmecert', 'init', '-b', 'My Project'])
@mock.patch('gimmecert.cli.init')
def test_init_command_accepts_ca_base_name_option_short_form(mock_init, tmpdir):
# This should ensure we don't accidentally create artifacts
# outside of test directory.
tmpdir.chdir()
mock_init.return_value = gimmecert.commands.ExitCode.SUCCESS
gimmecert.cli.main() # Should not raise
@mock.patch('sys.argv', ['gimmecert', 'init', '--ca-hierarchy-depth', '3'])
@mock.patch('gimmecert.cli.init')
def test_init_command_accepts_ca_hierarchy_depth_option_long_form(mock_init, tmpdir):
# This should ensure we don't accidentally create artifacts
# outside of test directory.
tmpdir.chdir()
mock_init.return_value = gimmecert.commands.ExitCode.SUCCESS
gimmecert.cli.main() # Should not raise
@mock.patch('sys.argv', ['gimmecert', 'init', '-d', '3'])
@mock.patch('gimmecert.cli.init')
def test_init_command_accepts_ca_hierarchy_depth_option_short_form(mock_init, tmpdir):
# This should ensure we don't accidentally create artifacts
# outside of test directory.
tmpdir.chdir()
mock_init.return_value = gimmecert.commands.ExitCode.SUCCESS
gimmecert.cli.main() # Should not raise
@mock.patch('sys.argv', ['gimmecert', 'server', '-h'])
def test_server_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_server_subcommand_parser_registered():
registered_functions = gimmecert.decorators.get_subcommand_parser_setup_functions()
assert gimmecert.cli.setup_server_subcommand_parser in registered_functions
def test_setup_server_subcommand_parser_returns_parser():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
subparser = gimmecert.cli.setup_server_subcommand_parser(parser, subparsers)
assert isinstance(subparser, argparse.ArgumentParser)
@mock.patch('sys.argv', ['gimmecert', 'server'])
def test_setup_server_subcommand_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', 'server', 'myserver'])
@mock.patch('gimmecert.cli.server')
def test_setup_server_subcommand_succeeds_with_just_entity_name_argument(mock_server, tmpdir):
# This should ensure we don't accidentally create artifacts
# outside of test directory.
tmpdir.chdir()
# We are just testing the parsing here.
mock_server.return_value = gimmecert.commands.ExitCode.SUCCESS
gimmecert.cli.main() # Should not raise.
@mock.patch('sys.argv', ['gimmecert', 'server', 'myserver', 'myserver.example.com'])
@mock.patch('gimmecert.cli.server')
def test_setup_server_subcommand_succeeds_with_entity_name_argument_and_one_dns_name(mock_server, tmpdir):
# This should ensure we don't accidentally create artifacts
# outside of test directory.
tmpdir.chdir()
# We are just testing the parsing here.
mock_server.return_value = gimmecert.commands.ExitCode.SUCCESS
gimmecert.cli.main() # Should not raise.
@mock.patch('sys.argv', ['gimmecert', 'server', 'myserver', 'myserver1.example.com', 'myserver2.example.com', 'myserver3.example.com', 'myserver4.example.com'])
@mock.patch('gimmecert.cli.server')
def test_setup_server_subcommand_succeeds_with_entity_name_argument_and_four_dns_names(mock_server, tmpdir):
# This should ensure we don't accidentally create artifacts
# outside of test directory.
tmpdir.chdir()
# We are just testing the parsing here.
mock_server.return_value = gimmecert.commands.ExitCode.SUCCESS
gimmecert.cli.main() # Should not raise.
def test_setup_server_subcommand_sets_function_callback():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
subparser = gimmecert.cli.setup_server_subcommand_parser(parser, subparsers)
assert callable(subparser.get_default('func'))
@mock.patch('sys.argv', ['gimmecert', 'server', 'myserver'])
@mock.patch('gimmecert.cli.server')
def test_server_command_invoked_with_correct_parameters_without_extra_dns_names(mock_server, tmpdir):
# This should ensure we don't accidentally create artifacts
# outside of test directory.
tmpdir.chdir()
mock_server.return_value = gimmecert.commands.ExitCode.SUCCESS
gimmecert.cli.main()
mock_server.assert_called_once_with(sys.stdout, sys.stderr, tmpdir.strpath, 'myserver', [], False)
@mock.patch('sys.argv', ['gimmecert', 'server', 'myserver', 'service.local', 'service.example.com'])
@mock.patch('gimmecert.cli.server')
def test_server_command_invoked_with_correct_parameters_with_extra_dns_names(mock_server, tmpdir):
# This should ensure we don't accidentally create artifacts
# outside of test directory.
tmpdir.chdir()
mock_server.return_value = gimmecert.commands.ExitCode.SUCCESS
gimmecert.cli.main()
mock_server.assert_called_once_with(sys.stdout, sys.stderr, tmpdir.strpath, 'myserver', ['service.local', 'service.example.com'], False)
@mock.patch('sys.argv', ['gimmecert', 'help'])
@mock.patch('gimmecert.cli.help_')
def test_help_command_invoked_with_correct_parameters(mock_help_, tmpdir):
# This should ensure we don't accidentally create artifacts
# outside of test directory.
tmpdir.chdir()
mock_help_.return_value = gimmecert.commands.ExitCode.SUCCESS
gimmecert.cli.main()
assert mock_help_.called
assert mock_help_.call_count == 1
@mock.patch('sys.argv', ['gimmecert'])
@mock.patch('gimmecert.cli.usage')
def test_usage_command_invoked_with_correct_parameters(mock_usage, tmpdir):
# This should ensure we don't accidentally create artifacts
# outside of test directory.
tmpdir.chdir()
mock_usage.return_value = gimmecert.commands.ExitCode.SUCCESS
gimmecert.cli.main()
# Can't test calling arguments, since we'd need to mask
# get_parser, and if we do that we mask the set_defaults and
# what-not.
assert mock_usage.called
assert mock_usage.call_count == 1
@mock.patch('sys.argv', ['gimmecert', 'testcommand'])
def test_main_does_not_exit_if_it_calls_function_that_returns_success(tmpdir):
# This should ensure we don't accidentally create artifacts
# outside of test directory.
tmpdir.chdir()
@gimmecert.decorators.subcommand_parser
def setup_testcommand_parser(parser, subparsers):
subparser = subparsers.add_parser('testcommand', description='test command')
def testcommand_wrapper(args):
return gimmecert.commands.ExitCode.SUCCESS
subparser.set_defaults(func=testcommand_wrapper)
return subparser
gimmecert.cli.main() # Should not raise
@mock.patch('sys.argv', ['gimmecert', 'testcommand'])
def test_main_exits_if_it_calls_function_that_returns_success(tmpdir):
# This should ensure we don't accidentally create artifacts
# outside of test directory.
tmpdir.chdir()
@gimmecert.decorators.subcommand_parser
def setup_testcommand_parser(parser, subparsers):
subparser = subparsers.add_parser('testcommand', description='test command')
def testcommand_wrapper(args):
return gimmecert.commands.ExitCode.ERROR_ALREADY_INITIALISED
subparser.set_defaults(func=testcommand_wrapper)
return subparser
with pytest.raises(SystemExit) as e_info:
gimmecert.cli.main()
assert e_info.value.code == gimmecert.commands.ExitCode.ERROR_ALREADY_INITIALISED
@mock.patch('sys.argv', ['gimmecert', 'client', '-h'])
def test_client_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_client_subcommand_parser_registered():
registered_functions = gimmecert.decorators.get_subcommand_parser_setup_functions()
assert gimmecert.cli.setup_client_subcommand_parser in registered_functions
def test_setup_client_subcommand_parser_returns_parser():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
subparser = gimmecert.cli.setup_client_subcommand_parser(parser, subparsers)
assert isinstance(subparser, argparse.ArgumentParser)
@mock.patch('sys.argv', ['gimmecert', 'client'])
def test_setup_client_subcommand_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', 'client', 'myclient'])
@mock.patch('gimmecert.cli.client')
def test_setup_client_subcommand_succeeds_with_entity_name_argument(mock_client, tmpdir):
# This should ensure we don't accidentally create artifacts
# outside of test directory.
tmpdir.chdir()
# We are just testing the parsing here.
mock_client.return_value = gimmecert.commands.ExitCode.SUCCESS
gimmecert.cli.main() # Should not raise.
def test_setup_client_subcommand_sets_function_callback():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
subparser = gimmecert.cli.setup_client_subcommand_parser(parser, subparsers)
assert callable(subparser.get_default('func'))
@mock.patch('sys.argv', ['gimmecert', 'client', 'myclient'])
@mock.patch('gimmecert.cli.client')
def test_client_command_invoked_with_correct_parameters(mock_client, tmpdir):
# This should ensure we don't accidentally create artifacts
# outside of test directory.
tmpdir.chdir()
mock_client.return_value = gimmecert.commands.ExitCode.SUCCESS
gimmecert.cli.main()
mock_client.assert_called_once_with(sys.stdout, sys.stderr, tmpdir.strpath, 'myclient')
@mock.patch('sys.argv', ['gimmecert', 'server', '--update-dns-names', 'myserver'])
@mock.patch('gimmecert.cli.server')
def test_server_command_accepts_update_option_long_form(mock_server, tmpdir):
# This should ensure we don't accidentally create artifacts
# outside of test directory.
tmpdir.chdir()
mock_server.return_value = gimmecert.commands.ExitCode.SUCCESS
gimmecert.cli.main() # Should not raise
@mock.patch('sys.argv', ['gimmecert', 'server', '-u', 'myserver'])
@mock.patch('gimmecert.cli.server')
def test_server_command_accepts_update_option_short_form(mock_server, tmpdir):
# This should ensure we don't accidentally create artifacts
# outside of test directory.
tmpdir.chdir()
mock_server.return_value = gimmecert.commands.ExitCode.SUCCESS
gimmecert.cli.main() # Should not raise
@mock.patch('sys.argv', ['gimmecert', 'server', '--update-dns-names', 'myserver', 'service.local'])
@mock.patch('gimmecert.cli.server')
def test_server_command_invoked_with_correct_parameters_with_update_option(mock_server, tmpdir):
# This should ensure we don't accidentally create artifacts
# outside of test directory.
tmpdir.chdir()
mock_server.return_value = gimmecert.commands.ExitCode.SUCCESS
gimmecert.cli.main()
mock_server.assert_called_once_with(sys.stdout, sys.stderr, tmpdir.strpath, 'myserver', ['service.local'], True)
|