Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

test_metadata_nullable_bug.py 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. from unittest.mock import Mock, patch
  2. import pytest
  3. from flask_restful import reqparse
  4. from services.entities.knowledge_entities.knowledge_entities import MetadataArgs
  5. from services.metadata_service import MetadataService
  6. class TestMetadataNullableBug:
  7. """Test case to reproduce the metadata nullable validation bug."""
  8. def test_metadata_args_with_none_values_should_fail(self):
  9. """Test that MetadataArgs validation should reject None values."""
  10. # This test demonstrates the expected behavior - should fail validation
  11. with pytest.raises((ValueError, TypeError)):
  12. # This should fail because Pydantic expects non-None values
  13. MetadataArgs(type=None, name=None)
  14. def test_metadata_service_create_with_none_name_crashes(self):
  15. """Test that MetadataService.create_metadata crashes when name is None."""
  16. # Mock the MetadataArgs to bypass Pydantic validation
  17. mock_metadata_args = Mock()
  18. mock_metadata_args.name = None # This will cause len() to crash
  19. mock_metadata_args.type = "string"
  20. with patch("services.metadata_service.current_user") as mock_user:
  21. mock_user.current_tenant_id = "tenant-123"
  22. mock_user.id = "user-456"
  23. # This should crash with TypeError when calling len(None)
  24. with pytest.raises(TypeError, match="object of type 'NoneType' has no len"):
  25. MetadataService.create_metadata("dataset-123", mock_metadata_args)
  26. def test_metadata_service_update_with_none_name_crashes(self):
  27. """Test that MetadataService.update_metadata_name crashes when name is None."""
  28. with patch("services.metadata_service.current_user") as mock_user:
  29. mock_user.current_tenant_id = "tenant-123"
  30. mock_user.id = "user-456"
  31. # This should crash with TypeError when calling len(None)
  32. with pytest.raises(TypeError, match="object of type 'NoneType' has no len"):
  33. MetadataService.update_metadata_name("dataset-123", "metadata-456", None)
  34. def test_api_parser_accepts_null_values(self, app):
  35. """Test that API parser configuration incorrectly accepts null values."""
  36. # Simulate the current API parser configuration
  37. parser = reqparse.RequestParser()
  38. parser.add_argument("type", type=str, required=True, nullable=True, location="json")
  39. parser.add_argument("name", type=str, required=True, nullable=True, location="json")
  40. # Simulate request data with null values
  41. with app.test_request_context(json={"type": None, "name": None}, content_type="application/json"):
  42. # This should parse successfully due to nullable=True
  43. args = parser.parse_args()
  44. # Verify that null values are accepted
  45. assert args["type"] is None
  46. assert args["name"] is None
  47. # This demonstrates the bug: API accepts None but business logic will crash
  48. def test_integration_bug_scenario(self, app):
  49. """Test the complete bug scenario from API to service layer."""
  50. # Step 1: API parser accepts null values (current buggy behavior)
  51. parser = reqparse.RequestParser()
  52. parser.add_argument("type", type=str, required=True, nullable=True, location="json")
  53. parser.add_argument("name", type=str, required=True, nullable=True, location="json")
  54. with app.test_request_context(json={"type": None, "name": None}, content_type="application/json"):
  55. args = parser.parse_args()
  56. # Step 2: Try to create MetadataArgs with None values
  57. # This should fail at Pydantic validation level
  58. with pytest.raises((ValueError, TypeError)):
  59. metadata_args = MetadataArgs(**args)
  60. # Step 3: If we bypass Pydantic (simulating the bug scenario)
  61. # Move this outside the request context to avoid Flask-Login issues
  62. mock_metadata_args = Mock()
  63. mock_metadata_args.name = None # From args["name"]
  64. mock_metadata_args.type = None # From args["type"]
  65. with patch("services.metadata_service.current_user") as mock_user:
  66. mock_user.current_tenant_id = "tenant-123"
  67. mock_user.id = "user-456"
  68. # Step 4: Service layer crashes on len(None)
  69. with pytest.raises(TypeError, match="object of type 'NoneType' has no len"):
  70. MetadataService.create_metadata("dataset-123", mock_metadata_args)
  71. def test_correct_nullable_false_configuration_works(self, app):
  72. """Test that the correct nullable=False configuration works as expected."""
  73. # This tests the FIXED configuration
  74. parser = reqparse.RequestParser()
  75. parser.add_argument("type", type=str, required=True, nullable=False, location="json")
  76. parser.add_argument("name", type=str, required=True, nullable=False, location="json")
  77. with app.test_request_context(json={"type": None, "name": None}, content_type="application/json"):
  78. # This should fail with BadRequest due to nullable=False
  79. from werkzeug.exceptions import BadRequest
  80. with pytest.raises(BadRequest):
  81. parser.parse_args()
  82. if __name__ == "__main__":
  83. pytest.main([__file__, "-v"])