chenzhaoyang
2025-12-17 063da0bf961e1d35e25dc107f883f7492f4c5a7c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""Validation tests for LocalFiles storage constraints."""
 
import pytest  # type: ignore[import]
from django.core.exceptions import ValidationError  # type: ignore[import]
from io_storages.localfiles.models import LocalFilesImportStorage
from projects.models import Project
 
 
@pytest.mark.django_db
def test_validate_connection_rejects_document_root_path(settings, tmp_path, project_id):
    """Ensure validate_connection fails when the storage path equals LOCAL_FILES_DOCUMENT_ROOT.
 
    This test validates step by step:
    - Creating a temporary document root and configuring Label Studio to use it
    - Initializing LocalFilesImportStorage with a path that matches the document root exactly
    - Calling validate_connection to trigger the new guard clause
    - Verifying that a ValidationError is raised with a helpful explanation so the user knows to pick a subfolder
    """
 
    settings.LOCAL_FILES_DOCUMENT_ROOT = str(tmp_path)
    settings.LOCAL_FILES_SERVING_ENABLED = True
 
    project = Project.objects.get(pk=project_id)
    storage = LocalFilesImportStorage(project=project, path=str(tmp_path))
 
    with pytest.raises(ValidationError) as exc_info:
        storage.validate_connection()
 
    error_message = str(exc_info.value)
    assert 'cannot be the same as LOCAL_FILES_DOCUMENT_ROOT' in error_message
 
 
@pytest.mark.django_db
def test_validate_connection_requires_subdirectory(settings, tmp_path, project_id):
    """Ensure validate_connection demands that storage.path stays inside LOCAL_FILES_DOCUMENT_ROOT.
 
    Steps validated:
    - Configure a document root and create a second folder outside of it
    - Assign the outside folder to LocalFilesImportStorage.path
    - Expect validate_connection to raise ValidationError
    - Confirm the error text mentions the subdirectory requirement so users understand the fix
    """
 
    document_root = tmp_path / 'root'
    document_root.mkdir()
    outside_dir = tmp_path / 'outside'
    outside_dir.mkdir()
 
    settings.LOCAL_FILES_DOCUMENT_ROOT = str(document_root)
    settings.LOCAL_FILES_SERVING_ENABLED = True
 
    project = Project.objects.get(pk=project_id)
    storage = LocalFilesImportStorage(project=project, path=str(outside_dir))
 
    with pytest.raises(ValidationError) as exc_info:
        storage.validate_connection()
 
    error_message = str(exc_info.value)
    assert 'must be a subdirectory of LOCAL_FILES_DOCUMENT_ROOT' in error_message