test_provider_debug.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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.id, self.token)
  21. self.assertEqual(provider.id, self.id)
  22. self.assertEqual(provider.token, self.token)
  23. def test_validate_always_passes(self):
  24. """Test _validate method always passes (no validation required)"""
  25. provider = DebugProvider(self.id, self.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.id, self.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.id, self.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.id, self.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.id, self.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.id, self.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.id, self.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("DebugProvider: %s(%s) => %s", "example.com", "A", "192.168.1.1")
  96. @patch("sys.stdout", new_callable=StringIO)
  97. def test_set_record_multiple_calls(self, mock_stdout):
  98. """Test multiple calls to set_record method"""
  99. provider = DebugProvider(self.id, self.token)
  100. # Make multiple calls
  101. result1 = provider.set_record("example1.com", "192.168.1.1", "A")
  102. result2 = provider.set_record("example2.com", "2001:db8::1", "AAAA")
  103. result3 = provider.set_record("example3.com", "target.example.com", "CNAME")
  104. # Verify all results are True
  105. self.assertTrue(result1)
  106. self.assertTrue(result2)
  107. self.assertTrue(result3)
  108. # Check that all outputs were printed
  109. output = mock_stdout.getvalue()
  110. self.assertIn("[IPv4] 192.168.1.1", output)
  111. self.assertIn("[IPv6] 2001:db8::1", output)
  112. self.assertIn("[CNAME] target.example.com", output)
  113. @patch("sys.stdout", new_callable=StringIO)
  114. def test_set_record_empty_values(self, mock_stdout):
  115. """Test set_record method with empty values"""
  116. provider = DebugProvider(self.id, self.token)
  117. result = provider.set_record("", "", "")
  118. # Verify the result is True
  119. self.assertTrue(result)
  120. # Check that the correct output was printed
  121. output = mock_stdout.getvalue()
  122. self.assertIn("[] ", output)
  123. @patch("sys.stdout", new_callable=StringIO)
  124. def test_set_record_none_values(self, mock_stdout):
  125. """Test set_record method with None values"""
  126. provider = DebugProvider(self.id, self.token)
  127. result = provider.set_record("example.com", "192.168.1.1", None) # type: ignore
  128. # Verify the result is True
  129. self.assertTrue(result)
  130. # Check that the correct output was printed (None record_type results in empty IP type)
  131. output = mock_stdout.getvalue()
  132. self.assertIn("[None] 192.168.1.1", output)
  133. class TestDebugProviderIntegration(unittest.TestCase):
  134. """Integration tests for DebugProvider"""
  135. def test_full_workflow_ipv4(self):
  136. """Test complete workflow for IPv4 record"""
  137. provider = DebugProvider("test_auth_id", "test_token")
  138. with patch("sys.stdout", new_callable=StringIO) as mock_stdout:
  139. result = provider.set_record("test.com", "1.2.3.4", "A", 300, "default")
  140. self.assertTrue(result)
  141. output = mock_stdout.getvalue()
  142. self.assertIn("[IPv4] 1.2.3.4", output)
  143. def test_full_workflow_ipv6(self):
  144. """Test complete workflow for IPv6 record"""
  145. provider = DebugProvider("test_auth_id", "test_token")
  146. with patch("sys.stdout", new_callable=StringIO) as mock_stdout:
  147. result = provider.set_record("test.com", "::1", "AAAA", 600, "telecom")
  148. self.assertTrue(result)
  149. output = mock_stdout.getvalue()
  150. self.assertIn("[IPv6] ::1", output)
  151. def test_full_workflow_cname(self):
  152. """Test complete workflow for CNAME record"""
  153. provider = DebugProvider("test_auth_id", "test_token")
  154. with patch("sys.stdout", new_callable=StringIO) as mock_stdout:
  155. result = provider.set_record("www.test.com", "test.com", "CNAME", 3600)
  156. self.assertTrue(result)
  157. output = mock_stdout.getvalue()
  158. self.assertIn("[CNAME] test.com", output)
  159. if __name__ == "__main__":
  160. unittest.main()