test_provider_debug.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. # coding=utf-8
  2. """
  3. Unit tests for DebugProvider
  4. @author: GitHub Copilot
  5. """
  6. import sys
  7. from base_test import BaseProviderTestCase, unittest, patch, MagicMock
  8. from ddns.provider.debug import DebugProvider
  9. if sys.version_info[0] < 3:
  10. from StringIO import StringIO # 对应 bytes
  11. else:
  12. from io import StringIO # 对应 unicode, py2.7中也存在
  13. class TestDebugProvider(BaseProviderTestCase):
  14. """Test cases for DebugProvider"""
  15. def setUp(self):
  16. """Set up test fixtures"""
  17. super(TestDebugProvider, self).setUp()
  18. def test_init_with_basic_config(self):
  19. """Test DebugProvider initialization with basic configuration"""
  20. provider = DebugProvider(self.auth_id, self.auth_token)
  21. self.assertEqual(provider.auth_id, self.auth_id)
  22. self.assertEqual(provider.auth_token, self.auth_token)
  23. def test_validate_always_passes(self):
  24. """Test _validate method always passes (no validation required)"""
  25. provider = DebugProvider(self.auth_id, self.auth_token)
  26. # Should not raise any exception
  27. provider._validate()
  28. def test_validate_with_none_values(self):
  29. """Test _validate method with None values still passes"""
  30. provider = DebugProvider(None, None) # type: ignore
  31. # Should not raise any exception even with None values
  32. provider._validate()
  33. @patch("sys.stdout", new_callable=StringIO)
  34. def test_set_record_ipv4(self, mock_stdout):
  35. """Test set_record method with IPv4 address"""
  36. provider = DebugProvider(self.auth_id, self.auth_token)
  37. result = provider.set_record("example.com", "192.168.1.1", "A")
  38. # Verify the result is True
  39. self.assertTrue(result)
  40. # Check that the correct output was printed
  41. output = mock_stdout.getvalue()
  42. self.assertIn("[IPv4] 192.168.1.1", output)
  43. @patch("sys.stdout", new_callable=StringIO)
  44. def test_set_record_ipv6(self, mock_stdout):
  45. """Test set_record method with IPv6 address"""
  46. provider = DebugProvider(self.auth_id, self.auth_token)
  47. result = provider.set_record("example.com", "2001:db8::1", "AAAA")
  48. # Verify the result is True
  49. self.assertTrue(result)
  50. # Check that the correct output was printed
  51. output = mock_stdout.getvalue()
  52. self.assertIn("[IPv6] 2001:db8::1", output)
  53. @patch("sys.stdout", new_callable=StringIO)
  54. def test_set_record_other_type(self, mock_stdout):
  55. """Test set_record method with other record types (CNAME, etc.)"""
  56. provider = DebugProvider(self.auth_id, self.auth_token)
  57. result = provider.set_record("example.com", "target.example.com", "CNAME")
  58. # Verify the result is True
  59. self.assertTrue(result)
  60. # Check that the correct output was printed (empty IP type for non-IP records)
  61. output = mock_stdout.getvalue()
  62. self.assertIn("[CNAME] target.example.com", output)
  63. @patch("sys.stdout", new_callable=StringIO)
  64. def test_set_record_mx_type(self, mock_stdout):
  65. """Test set_record method with MX record type"""
  66. provider = DebugProvider(self.auth_id, self.auth_token)
  67. result = provider.set_record("example.com", "mail.example.com", "MX")
  68. # Verify the result is True
  69. self.assertTrue(result)
  70. # Check that the correct output was printed
  71. output = mock_stdout.getvalue()
  72. self.assertIn("[MX] mail.example.com", output)
  73. @patch("sys.stdout", new_callable=StringIO)
  74. def test_set_record_with_all_parameters(self, mock_stdout):
  75. """Test set_record method with all optional parameters"""
  76. provider = DebugProvider(self.auth_id, self.auth_token)
  77. result = provider.set_record(
  78. domain="test.example.com", value="10.0.0.1", record_type="A", ttl=300, line="default", extra_param="test"
  79. )
  80. # Verify the result is True
  81. self.assertTrue(result)
  82. # Check that the correct output was printed
  83. output = mock_stdout.getvalue()
  84. self.assertIn("[IPv4] 10.0.0.1", output)
  85. def test_set_record_logger_debug_called(self):
  86. """Test that logger.debug is called with correct parameters"""
  87. provider = DebugProvider(self.auth_id, self.auth_token)
  88. # Mock the logger
  89. provider.logger = MagicMock()
  90. with patch("sys.stdout", new_callable=StringIO):
  91. result = provider.set_record("example.com", "192.168.1.1", "A", 600, "telecom")
  92. # Verify the result is True
  93. self.assertTrue(result)
  94. # Verify logger.debug was called with correct parameters
  95. provider.logger.debug.assert_called_once_with(
  96. "DebugProvider: %s(%s) => %s", "example.com", "A", "192.168.1.1"
  97. )
  98. @patch("sys.stdout", new_callable=StringIO)
  99. def test_set_record_multiple_calls(self, mock_stdout):
  100. """Test multiple calls to set_record method"""
  101. provider = DebugProvider(self.auth_id, self.auth_token)
  102. # Make multiple calls
  103. result1 = provider.set_record("example1.com", "192.168.1.1", "A")
  104. result2 = provider.set_record("example2.com", "2001:db8::1", "AAAA")
  105. result3 = provider.set_record("example3.com", "target.example.com", "CNAME")
  106. # Verify all results are True
  107. self.assertTrue(result1)
  108. self.assertTrue(result2)
  109. self.assertTrue(result3)
  110. # Check that all outputs were printed
  111. output = mock_stdout.getvalue()
  112. self.assertIn("[IPv4] 192.168.1.1", output)
  113. self.assertIn("[IPv6] 2001:db8::1", output)
  114. self.assertIn("[CNAME] target.example.com", output)
  115. @patch("sys.stdout", new_callable=StringIO)
  116. def test_set_record_empty_values(self, mock_stdout):
  117. """Test set_record method with empty values"""
  118. provider = DebugProvider(self.auth_id, self.auth_token)
  119. result = provider.set_record("", "", "")
  120. # Verify the result is True
  121. self.assertTrue(result)
  122. # Check that the correct output was printed
  123. output = mock_stdout.getvalue()
  124. self.assertIn("[] ", output)
  125. @patch("sys.stdout", new_callable=StringIO)
  126. def test_set_record_none_values(self, mock_stdout):
  127. """Test set_record method with None values"""
  128. provider = DebugProvider(self.auth_id, self.auth_token)
  129. result = provider.set_record("example.com", "192.168.1.1", None) # type: ignore
  130. # Verify the result is True
  131. self.assertTrue(result)
  132. # Check that the correct output was printed (None record_type results in empty IP type)
  133. output = mock_stdout.getvalue()
  134. self.assertIn("[None] 192.168.1.1", output)
  135. class TestDebugProviderIntegration(unittest.TestCase):
  136. """Integration tests for DebugProvider"""
  137. def test_full_workflow_ipv4(self):
  138. """Test complete workflow for IPv4 record"""
  139. provider = DebugProvider("test_auth_id", "test_auth_token")
  140. with patch("sys.stdout", new_callable=StringIO) as mock_stdout:
  141. result = provider.set_record("test.com", "1.2.3.4", "A", 300, "default")
  142. self.assertTrue(result)
  143. output = mock_stdout.getvalue()
  144. self.assertIn("[IPv4] 1.2.3.4", output)
  145. def test_full_workflow_ipv6(self):
  146. """Test complete workflow for IPv6 record"""
  147. provider = DebugProvider("test_auth_id", "test_auth_token")
  148. with patch("sys.stdout", new_callable=StringIO) as mock_stdout:
  149. result = provider.set_record("test.com", "::1", "AAAA", 600, "telecom")
  150. self.assertTrue(result)
  151. output = mock_stdout.getvalue()
  152. self.assertIn("[IPv6] ::1", output)
  153. def test_full_workflow_cname(self):
  154. """Test complete workflow for CNAME record"""
  155. provider = DebugProvider("test_auth_id", "test_auth_token")
  156. with patch("sys.stdout", new_callable=StringIO) as mock_stdout:
  157. result = provider.set_record("www.test.com", "test.com", "CNAME", 3600)
  158. self.assertTrue(result)
  159. output = mock_stdout.getvalue()
  160. self.assertIn("[CNAME] test.com", output)
  161. if __name__ == "__main__":
  162. unittest.main()