test_config_ssl.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. # coding=utf-8
  2. """
  3. Unit tests for SSL configuration integration
  4. @author: GitHub Copilot
  5. """
  6. import unittest
  7. import sys
  8. import os
  9. # Add parent directory to path for imports
  10. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  11. try:
  12. from unittest.mock import patch
  13. except ImportError:
  14. # Python 2.7 compatibility
  15. from mock import patch # type: ignore
  16. from ddns.util.config import init_config, get_config # noqa: E402
  17. from ddns.__init__ import __version__, __description__, __doc__, build_date # noqa
  18. from ddns.provider._base import SimpleProvider # noqa: E402
  19. class _TestSSLProvider(SimpleProvider):
  20. """Test provider to verify SSL configuration"""
  21. API = "https://api.example.com"
  22. def set_record(self, domain, value, record_type="A", ttl=None,
  23. line=None, **extra):
  24. return True
  25. class TestSSLConfiguration(unittest.TestCase):
  26. """Test SSL configuration integration"""
  27. def setUp(self):
  28. """Set up test fixtures"""
  29. # Clear global state before each test
  30. import ddns.util.config
  31. from argparse import Namespace
  32. ddns.util.config.__cli_args = Namespace()
  33. ddns.util.config.__config = {}
  34. # Clear environment variables that might affect tests
  35. # Check both uppercase and lowercase variants
  36. for key in list(os.environ.keys()):
  37. if (key.upper().startswith('DDNS_') or
  38. key.lower().startswith('ddns_')):
  39. del os.environ[key]
  40. def tearDown(self):
  41. """Clean up after each test"""
  42. # Clear global state after each test
  43. import ddns.util.config
  44. from argparse import Namespace
  45. ddns.util.config.__cli_args = Namespace()
  46. ddns.util.config.__config = {}
  47. # Clear environment variables that might affect future tests
  48. for key in list(os.environ.keys()):
  49. if (key.upper().startswith('DDNS_') or
  50. key.lower().startswith('ddns_')):
  51. del os.environ[key]
  52. def test_cli_ssl_false(self):
  53. """Test SSL configuration via CLI argument --ssl false"""
  54. args = ['test', '--token', 'test', '--ssl', 'false']
  55. with patch.object(sys, 'argv', args):
  56. init_config(__description__, __doc__, __version__, build_date)
  57. ssl_config = get_config('ssl')
  58. self.assertEqual(ssl_config, 'false')
  59. def test_cli_ssl_true(self):
  60. """Test SSL configuration via CLI argument --ssl true"""
  61. args = ['test', '--token', 'test', '--ssl', 'true']
  62. with patch.object(sys, 'argv', args):
  63. init_config(__description__, __doc__, __version__, build_date)
  64. ssl_config = get_config('ssl')
  65. self.assertEqual(ssl_config, 'true')
  66. def test_cli_ssl_auto(self):
  67. """Test SSL configuration via CLI argument --ssl auto"""
  68. args = ['test', '--token', 'test', '--ssl', 'auto']
  69. with patch.object(sys, 'argv', args):
  70. init_config(__description__, __doc__, __version__, build_date)
  71. ssl_config = get_config('ssl')
  72. self.assertEqual(ssl_config, 'auto')
  73. def test_cli_ssl_custom_path(self):
  74. """Test SSL configuration via CLI argument --ssl /path/to/cert.pem"""
  75. args = ['test', '--token', 'test', '--ssl', '/path/to/cert.pem']
  76. with patch.object(sys, 'argv', args):
  77. init_config(__description__, __doc__, __version__, build_date)
  78. ssl_config = get_config('ssl')
  79. self.assertEqual(ssl_config, '/path/to/cert.pem')
  80. def test_env_ssl_false(self):
  81. """Test SSL configuration via environment variable DDNS_SSL=false"""
  82. # Ensure completely clean environment
  83. clean_env = {k: v for k, v in os.environ.items()
  84. if not (k.upper().startswith('DDNS_') or
  85. k.lower().startswith('ddns_'))}
  86. clean_env.update({'DDNS_SSL': 'false', 'DDNS_TOKEN': 'test'})
  87. with patch.dict(os.environ, clean_env, clear=True):
  88. with patch.object(sys, 'argv', ['test']):
  89. init_config(__description__, __doc__, __version__, build_date)
  90. ssl_config = get_config('ssl')
  91. self.assertEqual(ssl_config, 'false')
  92. def test_env_ssl_true(self):
  93. """Test SSL configuration via environment variable DDNS_SSL=true"""
  94. # Ensure completely clean environment
  95. clean_env = {k: v for k, v in os.environ.items()
  96. if not (k.upper().startswith('DDNS_') or
  97. k.lower().startswith('ddns_'))}
  98. clean_env.update({'DDNS_SSL': 'true', 'DDNS_TOKEN': 'test'})
  99. with patch.dict(os.environ, clean_env, clear=True):
  100. with patch.object(sys, 'argv', ['test']):
  101. init_config(__description__, __doc__, __version__, build_date)
  102. ssl_config = get_config('ssl')
  103. self.assertEqual(ssl_config, 'true')
  104. def test_env_ssl_auto(self):
  105. """Test SSL configuration via environment variable DDNS_SSL=auto"""
  106. # Ensure completely clean environment
  107. clean_env = {k: v for k, v in os.environ.items()
  108. if not (k.upper().startswith('DDNS_') or
  109. k.lower().startswith('ddns_'))}
  110. clean_env.update({'DDNS_SSL': 'auto', 'DDNS_TOKEN': 'test'})
  111. with patch.dict(os.environ, clean_env, clear=True):
  112. with patch.object(sys, 'argv', ['test']):
  113. init_config(__description__, __doc__, __version__, build_date)
  114. ssl_config = get_config('ssl')
  115. self.assertEqual(ssl_config, 'auto')
  116. def test_cli_overrides_env(self):
  117. """Test that CLI argument overrides environment variable"""
  118. # Ensure completely clean environment
  119. clean_env = {k: v for k, v in os.environ.items()
  120. if not (k.upper().startswith('DDNS_') or
  121. k.lower().startswith('ddns_'))}
  122. clean_env.update({'DDNS_SSL': 'false', 'DDNS_TOKEN': 'test'})
  123. with patch.dict(os.environ, clean_env, clear=True):
  124. with patch.object(sys, 'argv', ['test', '--ssl', 'true']):
  125. init_config(__description__, __doc__, __version__, build_date)
  126. ssl_config = get_config('ssl')
  127. self.assertEqual(ssl_config, 'true')
  128. def test_default_ssl_config(self):
  129. """Test default SSL configuration when none provided"""
  130. with patch.object(sys, 'argv', ['test', '--token', 'test']):
  131. init_config(__description__, __doc__, __version__, build_date)
  132. ssl_config = get_config('ssl', 'auto')
  133. self.assertEqual(ssl_config, 'auto')
  134. def test_provider_ssl_integration(self):
  135. """Test that SSL configuration is passed to provider correctly"""
  136. provider = _TestSSLProvider('test_id', 'test_token',
  137. verify_ssl='false')
  138. self.assertEqual(provider.verify_ssl, 'false')
  139. provider = _TestSSLProvider('test_id', 'test_token', verify_ssl=True)
  140. self.assertTrue(provider.verify_ssl)
  141. cert_path = '/path/to/cert.pem'
  142. provider = _TestSSLProvider('test_id', 'test_token',
  143. verify_ssl=cert_path)
  144. self.assertEqual(provider.verify_ssl, '/path/to/cert.pem')
  145. def test_case_insensitive_env_vars(self):
  146. """Test that environment variables are case insensitive"""
  147. # Ensure completely clean environment
  148. clean_env = {k: v for k, v in os.environ.items()
  149. if not (k.upper().startswith('DDNS_') or
  150. k.lower().startswith('ddns_'))}
  151. clean_env.update({'ddns_ssl': 'false', 'ddns_token': 'test'})
  152. with patch.dict(os.environ, clean_env, clear=True):
  153. with patch.object(sys, 'argv', ['test']):
  154. init_config(__description__, __doc__, __version__, build_date)
  155. ssl_config = get_config('ssl')
  156. self.assertEqual(ssl_config, 'false')
  157. if __name__ == '__main__':
  158. unittest.main()