PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/test/unit/plugins/distributors/iso_distributor/test_iso_distributor_configuration.py

https://github.com/bowlofeggs/pulp_rpm
Python | 110 lines | 54 code | 24 blank | 32 comment | 2 complexity | 43bd32c57764613be86003d9314c9334 MD5 | raw file
  1. import unittest
  2. import mock
  3. from distributor_mocks import get_basic_config
  4. from pulp_rpm.common import constants
  5. from pulp_rpm.plugins import configuration_utils
  6. from pulp_rpm.plugins.distributors.iso_distributor import configuration
  7. class TestValidate(unittest.TestCase):
  8. """
  9. Assert correct behavior from the configuration.validate() function.
  10. """
  11. @mock.patch('pulp_rpm.plugins.distributors.iso_distributor.configuration._validate_ssl_cert',
  12. side_effect=configuration._validate_ssl_cert)
  13. @mock.patch('pulp_rpm.plugins.configuration_utils.validate_non_required_bool',
  14. side_effect=configuration_utils.validate_non_required_bool)
  15. def test_validate_calls_correct_helpers(self, _validate_required_bool, _validate_ssl_cert):
  16. """
  17. Test that validate() uses all the right helpers.
  18. """
  19. config = get_basic_config(
  20. **{constants.CONFIG_SERVE_HTTP: True, constants.CONFIG_SERVE_HTTPS: False})
  21. valid, msg = configuration.validate(config)
  22. # Assert the return values
  23. self.assertEqual(valid, True)
  24. self.assertEqual(msg, None)
  25. # Assert that _validate_required_bool was called twice with the correct parameters
  26. self.assertEqual(_validate_required_bool.call_count, 2)
  27. self.assertEqual(_validate_required_bool.mock_calls[0][1][0], config)
  28. self.assertEqual(_validate_required_bool.mock_calls[0][1][1], constants.CONFIG_SERVE_HTTP)
  29. self.assertEqual(_validate_required_bool.mock_calls[1][1][0], config)
  30. self.assertEqual(_validate_required_bool.mock_calls[1][1][1], constants.CONFIG_SERVE_HTTPS)
  31. # Assert that _validate_ssl_cert was called once with the right parameters
  32. _validate_ssl_cert.assert_called_once_with(config, constants.CONFIG_SSL_AUTH_CA_CERT)
  33. def test_validate_fails(self):
  34. """
  35. Test that validate() handles a bad config correctly.
  36. """
  37. config = get_basic_config(
  38. **{constants.CONFIG_SERVE_HTTP: True, constants.CONFIG_SERVE_HTTPS: False,
  39. constants.CONFIG_SSL_AUTH_CA_CERT: 'Invalid cert.'})
  40. valid, msg = configuration.validate(config)
  41. # We passed a valid config, so validate() should have indicated that everything was cool
  42. self.assertFalse(valid)
  43. self.assertEqual(msg, 'The SSL certificate <ssl_auth_ca_cert> is not a valid certificate.')
  44. def test_validate_passes(self):
  45. """
  46. Test that validate() handles a good config correctly.
  47. """
  48. config = get_basic_config(
  49. **{constants.CONFIG_SERVE_HTTP: True, constants.CONFIG_SERVE_HTTPS: False})
  50. valid, msg = configuration.validate(config)
  51. # We passed a valid config, so validate() should have indicated that everything was cool
  52. self.assertTrue(valid)
  53. self.assertEqual(msg, None)
  54. class TestValidateSSLCert(unittest.TestCase):
  55. """
  56. Test the _validate_ssl_cert() function.
  57. """
  58. def test_bad_cert(self):
  59. """
  60. Assert that a bad cert raises an error.
  61. """
  62. config = get_basic_config(**{constants.CONFIG_SSL_AUTH_CA_CERT: 'You cannot be serious.'})
  63. try:
  64. configuration._validate_ssl_cert(config, constants.CONFIG_SSL_AUTH_CA_CERT)
  65. self.fail('The validator should have raised an Exception, but it did not.')
  66. except configuration_utils.ValidationError, e:
  67. self.assertEqual(str(e),
  68. 'The SSL certificate <ssl_auth_ca_cert> is not a valid certificate.')
  69. @mock.patch('pulp_rpm.yum_plugin.util.validate_cert', return_value=True)
  70. def test_good_cert(self, validate_cert):
  71. """
  72. Assert that a good cert passes the check.
  73. """
  74. cert = 'Good Cert (well, once mock is done with it!)'
  75. config = get_basic_config(**{constants.CONFIG_SSL_AUTH_CA_CERT: cert})
  76. # This should not raise an Exception
  77. configuration._validate_ssl_cert(config, constants.CONFIG_SSL_AUTH_CA_CERT)
  78. # Make sure the mock was called
  79. validate_cert.assert_called_once_with(cert)
  80. def test_no_cert(self):
  81. """
  82. Assert that certificates are not required.
  83. """
  84. config = get_basic_config(**{})
  85. # This should not raise an Exception
  86. configuration._validate_ssl_cert(config, constants.CONFIG_SSL_AUTH_CA_CERT)