You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_metadata_nullable_bug.py 5.4KB

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