mirror of
https://github.com/langgenius/dify.git
synced 2026-01-14 06:07:33 +08:00
Signed-off-by: -LAN- <laipz8200@outlook.com> Signed-off-by: kenwoodjw <blackxin55+@gmail.com> Signed-off-by: Yongtao Huang <yongtaoh2022@gmail.com> Signed-off-by: yihong0618 <zouzou0208@gmail.com> Signed-off-by: zhanluxianshen <zhanluxianshen@163.com> Co-authored-by: -LAN- <laipz8200@outlook.com> Co-authored-by: GuanMu <ballmanjq@gmail.com> Co-authored-by: Davide Delbianco <davide.delbianco@outlook.com> Co-authored-by: NeatGuyCoding <15627489+NeatGuyCoding@users.noreply.github.com> Co-authored-by: kenwoodjw <blackxin55+@gmail.com> Co-authored-by: Yongtao Huang <yongtaoh2022@gmail.com> Co-authored-by: Yongtao Huang <99629139+hyongtao-db@users.noreply.github.com> Co-authored-by: Qiang Lee <18018968632@163.com> Co-authored-by: 李强04 <liqiang04@gaotu.cn> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Asuka Minato <i@asukaminato.eu.org> Co-authored-by: Matri Qi <matrixdom@126.com> Co-authored-by: huayaoyue6 <huayaoyue@163.com> Co-authored-by: Bowen Liang <liangbowen@gf.com.cn> Co-authored-by: znn <jubinkumarsoni@gmail.com> Co-authored-by: crazywoola <427733928@qq.com> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: yihong <zouzou0208@gmail.com> Co-authored-by: Muke Wang <shaodwaaron@gmail.com> Co-authored-by: wangmuke <wangmuke@kingsware.cn> Co-authored-by: Wu Tianwei <30284043+WTW0313@users.noreply.github.com> Co-authored-by: quicksand <quicksandzn@gmail.com> Co-authored-by: 非法操作 <hjlarry@163.com> Co-authored-by: zxhlyh <jasonapring2015@outlook.com> Co-authored-by: Eric Guo <eric.guocz@gmail.com> Co-authored-by: Zhedong Cen <cenzhedong2@126.com> Co-authored-by: jiangbo721 <jiangbo721@163.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: hjlarry <25834719+hjlarry@users.noreply.github.com> Co-authored-by: lxsummer <35754229+lxjustdoit@users.noreply.github.com> Co-authored-by: 湛露先生 <zhanluxianshen@163.com> Co-authored-by: Guangdong Liu <liugddx@gmail.com> Co-authored-by: QuantumGhost <obelisk.reg+git@gmail.com> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Yessenia-d <yessenia.contact@gmail.com> Co-authored-by: huangzhuo1949 <167434202+huangzhuo1949@users.noreply.github.com> Co-authored-by: huangzhuo <huangzhuo1@xiaomi.com> Co-authored-by: 17hz <0x149527@gmail.com> Co-authored-by: Amy <1530140574@qq.com> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Nite Knite <nkCoding@gmail.com> Co-authored-by: Yeuoly <45712896+Yeuoly@users.noreply.github.com> Co-authored-by: Petrus Han <petrus.hanks@gmail.com> Co-authored-by: iamjoel <2120155+iamjoel@users.noreply.github.com> Co-authored-by: Kalo Chin <frog.beepers.0n@icloud.com> Co-authored-by: Ujjwal Maurya <ujjwalsbx@gmail.com> Co-authored-by: Maries <xh001x@hotmail.com>
109 lines
5.2 KiB
Python
109 lines
5.2 KiB
Python
from unittest.mock import Mock, patch
|
|
|
|
import pytest
|
|
from flask_restx import reqparse
|
|
|
|
from services.entities.knowledge_entities.knowledge_entities import MetadataArgs
|
|
from services.metadata_service import MetadataService
|
|
|
|
|
|
class TestMetadataNullableBug:
|
|
"""Test case to reproduce the metadata nullable validation bug."""
|
|
|
|
def test_metadata_args_with_none_values_should_fail(self):
|
|
"""Test that MetadataArgs validation should reject None values."""
|
|
# This test demonstrates the expected behavior - should fail validation
|
|
with pytest.raises((ValueError, TypeError)):
|
|
# This should fail because Pydantic expects non-None values
|
|
MetadataArgs(type=None, name=None)
|
|
|
|
def test_metadata_service_create_with_none_name_crashes(self):
|
|
"""Test that MetadataService.create_metadata crashes when name is None."""
|
|
# Mock the MetadataArgs to bypass Pydantic validation
|
|
mock_metadata_args = Mock()
|
|
mock_metadata_args.name = None # This will cause len() to crash
|
|
mock_metadata_args.type = "string"
|
|
|
|
with patch("services.metadata_service.current_user") as mock_user:
|
|
mock_user.current_tenant_id = "tenant-123"
|
|
mock_user.id = "user-456"
|
|
|
|
# This should crash with TypeError when calling len(None)
|
|
with pytest.raises(TypeError, match="object of type 'NoneType' has no len"):
|
|
MetadataService.create_metadata("dataset-123", mock_metadata_args)
|
|
|
|
def test_metadata_service_update_with_none_name_crashes(self):
|
|
"""Test that MetadataService.update_metadata_name crashes when name is None."""
|
|
with patch("services.metadata_service.current_user") as mock_user:
|
|
mock_user.current_tenant_id = "tenant-123"
|
|
mock_user.id = "user-456"
|
|
|
|
# This should crash with TypeError when calling len(None)
|
|
with pytest.raises(TypeError, match="object of type 'NoneType' has no len"):
|
|
MetadataService.update_metadata_name("dataset-123", "metadata-456", None)
|
|
|
|
def test_api_parser_accepts_null_values(self, app):
|
|
"""Test that API parser configuration incorrectly accepts null values."""
|
|
# Simulate the current API parser configuration
|
|
parser = reqparse.RequestParser()
|
|
parser.add_argument("type", type=str, required=True, nullable=True, location="json")
|
|
parser.add_argument("name", type=str, required=True, nullable=True, location="json")
|
|
|
|
# Simulate request data with null values
|
|
with app.test_request_context(json={"type": None, "name": None}, content_type="application/json"):
|
|
# This should parse successfully due to nullable=True
|
|
args = parser.parse_args()
|
|
|
|
# Verify that null values are accepted
|
|
assert args["type"] is None
|
|
assert args["name"] is None
|
|
|
|
# This demonstrates the bug: API accepts None but business logic will crash
|
|
|
|
def test_integration_bug_scenario(self, app):
|
|
"""Test the complete bug scenario from API to service layer."""
|
|
# Step 1: API parser accepts null values (current buggy behavior)
|
|
parser = reqparse.RequestParser()
|
|
parser.add_argument("type", type=str, required=True, nullable=True, location="json")
|
|
parser.add_argument("name", type=str, required=True, nullable=True, location="json")
|
|
|
|
with app.test_request_context(json={"type": None, "name": None}, content_type="application/json"):
|
|
args = parser.parse_args()
|
|
|
|
# Step 2: Try to create MetadataArgs with None values
|
|
# This should fail at Pydantic validation level
|
|
with pytest.raises((ValueError, TypeError)):
|
|
metadata_args = MetadataArgs(**args)
|
|
|
|
# Step 3: If we bypass Pydantic (simulating the bug scenario)
|
|
# Move this outside the request context to avoid Flask-Login issues
|
|
mock_metadata_args = Mock()
|
|
mock_metadata_args.name = None # From args["name"]
|
|
mock_metadata_args.type = None # From args["type"]
|
|
|
|
with patch("services.metadata_service.current_user") as mock_user:
|
|
mock_user.current_tenant_id = "tenant-123"
|
|
mock_user.id = "user-456"
|
|
|
|
# Step 4: Service layer crashes on len(None)
|
|
with pytest.raises(TypeError, match="object of type 'NoneType' has no len"):
|
|
MetadataService.create_metadata("dataset-123", mock_metadata_args)
|
|
|
|
def test_correct_nullable_false_configuration_works(self, app):
|
|
"""Test that the correct nullable=False configuration works as expected."""
|
|
# This tests the FIXED configuration
|
|
parser = reqparse.RequestParser()
|
|
parser.add_argument("type", type=str, required=True, nullable=False, location="json")
|
|
parser.add_argument("name", type=str, required=True, nullable=False, location="json")
|
|
|
|
with app.test_request_context(json={"type": None, "name": None}, content_type="application/json"):
|
|
# This should fail with BadRequest due to nullable=False
|
|
from werkzeug.exceptions import BadRequest
|
|
|
|
with pytest.raises(BadRequest):
|
|
parser.parse_args()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
pytest.main([__file__, "-v"])
|