您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

test_metadata_bug_complete.py 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. from unittest.mock import Mock, create_autospec, patch
  2. import pytest
  3. from flask_restx import reqparse
  4. from werkzeug.exceptions import BadRequest
  5. from models.account import Account
  6. from services.entities.knowledge_entities.knowledge_entities import MetadataArgs
  7. from services.metadata_service import MetadataService
  8. class TestMetadataBugCompleteValidation:
  9. """Complete test suite to verify the metadata nullable bug and its fix."""
  10. def test_1_pydantic_layer_validation(self):
  11. """Test Layer 1: Pydantic model validation correctly rejects None values."""
  12. # Pydantic should reject None values for required fields
  13. with pytest.raises((ValueError, TypeError)):
  14. MetadataArgs(type=None, name=None)
  15. with pytest.raises((ValueError, TypeError)):
  16. MetadataArgs(type="string", name=None)
  17. with pytest.raises((ValueError, TypeError)):
  18. MetadataArgs(type=None, name="test")
  19. # Valid values should work
  20. valid_args = MetadataArgs(type="string", name="test_name")
  21. assert valid_args.type == "string"
  22. assert valid_args.name == "test_name"
  23. def test_2_business_logic_layer_crashes_on_none(self):
  24. """Test Layer 2: Business logic crashes when None values slip through."""
  25. # Create mock that bypasses Pydantic validation
  26. mock_metadata_args = Mock()
  27. mock_metadata_args.name = None
  28. mock_metadata_args.type = "string"
  29. mock_user = create_autospec(Account, instance=True)
  30. mock_user.current_tenant_id = "tenant-123"
  31. mock_user.id = "user-456"
  32. with patch("services.metadata_service.current_user", mock_user):
  33. # Should crash with TypeError
  34. with pytest.raises(TypeError, match="object of type 'NoneType' has no len"):
  35. MetadataService.create_metadata("dataset-123", mock_metadata_args)
  36. # Test update method as well
  37. mock_user = create_autospec(Account, instance=True)
  38. mock_user.current_tenant_id = "tenant-123"
  39. mock_user.id = "user-456"
  40. with patch("services.metadata_service.current_user", mock_user):
  41. with pytest.raises(TypeError, match="object of type 'NoneType' has no len"):
  42. MetadataService.update_metadata_name("dataset-123", "metadata-456", None)
  43. def test_3_database_constraints_verification(self):
  44. """Test Layer 3: Verify database model has nullable=False constraints."""
  45. from sqlalchemy import inspect
  46. from models.dataset import DatasetMetadata
  47. # Get table info
  48. mapper = inspect(DatasetMetadata)
  49. # Check that type and name columns are not nullable
  50. type_column = mapper.columns["type"]
  51. name_column = mapper.columns["name"]
  52. assert type_column.nullable is False, "type column should be nullable=False"
  53. assert name_column.nullable is False, "name column should be nullable=False"
  54. def test_4_fixed_api_layer_rejects_null(self, app):
  55. """Test Layer 4: Fixed API configuration properly rejects null values."""
  56. # Test Console API create endpoint (fixed)
  57. parser = reqparse.RequestParser()
  58. parser.add_argument("type", type=str, required=True, nullable=False, location="json")
  59. parser.add_argument("name", type=str, required=True, nullable=False, location="json")
  60. with app.test_request_context(json={"type": None, "name": None}, content_type="application/json"):
  61. with pytest.raises(BadRequest):
  62. parser.parse_args()
  63. # Test with just name being null
  64. with app.test_request_context(json={"type": "string", "name": None}, content_type="application/json"):
  65. with pytest.raises(BadRequest):
  66. parser.parse_args()
  67. # Test with just type being null
  68. with app.test_request_context(json={"type": None, "name": "test"}, content_type="application/json"):
  69. with pytest.raises(BadRequest):
  70. parser.parse_args()
  71. def test_5_fixed_api_accepts_valid_values(self, app):
  72. """Test that fixed API still accepts valid non-null values."""
  73. parser = reqparse.RequestParser()
  74. parser.add_argument("type", type=str, required=True, nullable=False, location="json")
  75. parser.add_argument("name", type=str, required=True, nullable=False, location="json")
  76. with app.test_request_context(json={"type": "string", "name": "valid_name"}, content_type="application/json"):
  77. args = parser.parse_args()
  78. assert args["type"] == "string"
  79. assert args["name"] == "valid_name"
  80. def test_6_simulated_buggy_behavior(self, app):
  81. """Test simulating the original buggy behavior with nullable=True."""
  82. # Simulate the old buggy configuration
  83. buggy_parser = reqparse.RequestParser()
  84. buggy_parser.add_argument("type", type=str, required=True, nullable=True, location="json")
  85. buggy_parser.add_argument("name", type=str, required=True, nullable=True, location="json")
  86. with app.test_request_context(json={"type": None, "name": None}, content_type="application/json"):
  87. # This would pass in the buggy version
  88. args = buggy_parser.parse_args()
  89. assert args["type"] is None
  90. assert args["name"] is None
  91. # But would crash when trying to create MetadataArgs
  92. with pytest.raises((ValueError, TypeError)):
  93. MetadataArgs(**args)
  94. def test_7_end_to_end_validation_layers(self):
  95. """Test all validation layers work together correctly."""
  96. # Layer 1: API should reject null at parameter level (with fix)
  97. # Layer 2: Pydantic should reject null at model level
  98. # Layer 3: Business logic expects non-null
  99. # Layer 4: Database enforces non-null
  100. # Test that valid data flows through all layers
  101. valid_data = {"type": "string", "name": "test_metadata"}
  102. # Should create valid Pydantic object
  103. metadata_args = MetadataArgs(**valid_data)
  104. assert metadata_args.type == "string"
  105. assert metadata_args.name == "test_metadata"
  106. # Should not crash in business logic length check
  107. assert len(metadata_args.name) <= 255 # This should not crash
  108. assert len(metadata_args.type) > 0 # This should not crash
  109. def test_8_verify_specific_fix_locations(self):
  110. """Verify that the specific locations mentioned in bug report are fixed."""
  111. # Read the actual files to verify fixes
  112. import os
  113. # Console API create
  114. console_create_file = "api/controllers/console/datasets/metadata.py"
  115. if os.path.exists(console_create_file):
  116. with open(console_create_file) as f:
  117. content = f.read()
  118. # Should contain nullable=False, not nullable=True
  119. assert "nullable=True" not in content.split("class DatasetMetadataCreateApi")[1].split("class")[0]
  120. # Service API create
  121. service_create_file = "api/controllers/service_api/dataset/metadata.py"
  122. if os.path.exists(service_create_file):
  123. with open(service_create_file) as f:
  124. content = f.read()
  125. # Should contain nullable=False, not nullable=True
  126. create_api_section = content.split("class DatasetMetadataCreateServiceApi")[1].split("class")[0]
  127. assert "nullable=True" not in create_api_section
  128. class TestMetadataValidationSummary:
  129. """Summary tests that demonstrate the complete validation architecture."""
  130. def test_validation_layer_architecture(self):
  131. """Document and test the 4-layer validation architecture."""
  132. # Layer 1: API Parameter Validation (Flask-RESTful reqparse)
  133. # - Role: First line of defense, validates HTTP request parameters
  134. # - Fixed: nullable=False ensures null values are rejected at API boundary
  135. # Layer 2: Pydantic Model Validation
  136. # - Role: Validates data structure and types before business logic
  137. # - Working: Required fields without Optional[] reject None values
  138. # Layer 3: Business Logic Validation
  139. # - Role: Domain-specific validation (length checks, uniqueness, etc.)
  140. # - Vulnerable: Direct len() calls crash on None values
  141. # Layer 4: Database Constraints
  142. # - Role: Final data integrity enforcement
  143. # - Working: nullable=False prevents None values in database
  144. # The bug was: Layer 1 allowed None, but Layers 2-4 expected non-None
  145. # The fix: Make Layer 1 consistent with Layers 2-4
  146. assert True # This test documents the architecture
  147. if __name__ == "__main__":
  148. pytest.main([__file__, "-v"])