PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/pulp_rpm/test/unit/server/test_iso_distributor_configuration.py

https://github.com/arnisoph/pulp_rpm
Python | 117 lines | 50 code | 23 blank | 44 comment | 2 complexity | 7583c0ecb1e7dfaee1b093672cf53881 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright © 2013 Red Hat, Inc.
  4. #
  5. # This software is licensed to you under the GNU General Public
  6. # License as published by the Free Software Foundation; either version
  7. # 2 of the License (GPLv2) or (at your option) any later version.
  8. # There is NO WARRANTY for this software, express or implied,
  9. # including the implied warranties of MERCHANTABILITY,
  10. # NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
  11. # have received a copy of GPLv2 along with this software; if not, see
  12. # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
  13. import unittest
  14. import mock
  15. from distributor_mocks import get_basic_config
  16. from pulp_rpm.common import constants
  17. from pulp_rpm.plugins import configuration_utils
  18. from pulp_rpm.plugins.distributors.iso_distributor import configuration
  19. class TestValidate(unittest.TestCase):
  20. """
  21. Assert correct behavior from the configuration.validate() function.
  22. """
  23. @mock.patch('pulp_rpm.plugins.distributors.iso_distributor.configuration._validate_ssl_cert',
  24. side_effect=configuration._validate_ssl_cert)
  25. @mock.patch('pulp_rpm.plugins.configuration_utils.validate_non_required_bool',
  26. side_effect=configuration_utils.validate_non_required_bool)
  27. def test_validate_calls_correct_helpers(self, _validate_required_bool, _validate_ssl_cert):
  28. """
  29. Test that validate() uses all the right helpers.
  30. """
  31. config = get_basic_config(**{constants.CONFIG_SERVE_HTTP: True, constants.CONFIG_SERVE_HTTPS: False})
  32. valid, msg = configuration.validate(config)
  33. # Assert the return values
  34. self.assertEqual(valid, True)
  35. self.assertEqual(msg, None)
  36. # Assert that _validate_required_bool was called twice with the correct parameters
  37. self.assertEqual(_validate_required_bool.call_count, 2)
  38. self.assertEqual(_validate_required_bool.mock_calls[0][1][0], config)
  39. self.assertEqual(_validate_required_bool.mock_calls[0][1][1], constants.CONFIG_SERVE_HTTP)
  40. self.assertEqual(_validate_required_bool.mock_calls[1][1][0], config)
  41. self.assertEqual(_validate_required_bool.mock_calls[1][1][1], constants.CONFIG_SERVE_HTTPS)
  42. # Assert that _validate_ssl_cert was called once with the right parameters
  43. _validate_ssl_cert.assert_called_once_with(config, constants.CONFIG_SSL_AUTH_CA_CERT)
  44. def test_validate_fails(self):
  45. """
  46. Test that validate() handles a bad config correctly.
  47. """
  48. config = get_basic_config(**{constants.CONFIG_SERVE_HTTP: True, constants.CONFIG_SERVE_HTTPS: False,
  49. constants.CONFIG_SSL_AUTH_CA_CERT: 'Invalid cert.'})
  50. valid, msg = configuration.validate(config)
  51. # We passed a valid config, so validate() should have indicated that everything was cool
  52. self.assertFalse(valid)
  53. self.assertEqual(msg, 'The SSL certificate <ssl_auth_ca_cert> is not a valid certificate.')
  54. def test_validate_passes(self):
  55. """
  56. Test that validate() handles a good config correctly.
  57. """
  58. config = get_basic_config(**{constants.CONFIG_SERVE_HTTP: True, constants.CONFIG_SERVE_HTTPS: False})
  59. valid, msg = configuration.validate(config)
  60. # We passed a valid config, so validate() should have indicated that everything was cool
  61. self.assertTrue(valid)
  62. self.assertEqual(msg, None)
  63. class TestValidateSSLCert(unittest.TestCase):
  64. """
  65. Test the _validate_ssl_cert() function.
  66. """
  67. def test_bad_cert(self):
  68. """
  69. Assert that a bad cert raises an error.
  70. """
  71. config = get_basic_config(**{constants.CONFIG_SSL_AUTH_CA_CERT: 'You cannot be serious.'})
  72. try:
  73. configuration._validate_ssl_cert(config, constants.CONFIG_SSL_AUTH_CA_CERT)
  74. self.fail('The validator should have raised an Exception, but it did not.')
  75. except configuration_utils.ValidationError, e:
  76. self.assertEqual(str(e), 'The SSL certificate <ssl_auth_ca_cert> is not a valid certificate.')
  77. @mock.patch('pulp_rpm.yum_plugin.util.validate_cert', return_value=True)
  78. def test_good_cert(self, validate_cert):
  79. """
  80. Assert that a good cert passes the check.
  81. """
  82. cert = 'Good Cert (well, once mock is done with it!)'
  83. config = get_basic_config(**{constants.CONFIG_SSL_AUTH_CA_CERT: cert})
  84. # This should not raise an Exception
  85. configuration._validate_ssl_cert(config, constants.CONFIG_SSL_AUTH_CA_CERT)
  86. # Make sure the mock was called
  87. validate_cert.assert_called_once_with(cert)
  88. def test_no_cert(self):
  89. """
  90. Assert that certificates are not required.
  91. """
  92. config = get_basic_config(**{})
  93. # This should not raise an Exception
  94. configuration._validate_ssl_cert(config, constants.CONFIG_SSL_AUTH_CA_CERT)