Bin
2025-12-17 d616898802dfe7e5dd648bcf53c6d1f86b6d3642
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
60
61
62
63
64
65
66
67
"""Unit tests for LocalFiles serializers and helpers."""
 
from unittest import mock
 
import pytest
from django.core.exceptions import ValidationError as DjangoValidationError  # type: ignore[import]
from io_storages.localfiles.serializers import (
    LocalFilesExportStorageSerializer,
    LocalFilesImportStorageSerializer,
    _stringify_detail,
)
from projects.models import Project
from rest_framework.exceptions import ValidationError as DRFValidationError  # type: ignore[import]
 
 
def test_stringify_detail_handles_nested_structures():
    """Ensure _stringify_detail flattens nested dict/list/tuple structures."""
    detail = {'path': ['bad', {'nested': ('inner', 'values')}]}
    assert _stringify_detail(detail) == {'path': ['bad', {'nested': ['inner', 'values']}]}
 
 
@pytest.mark.django_db
def test_import_serializer_stringifies_validation_detail(settings, tmp_path, project_id):
    """LocalFilesImportStorageSerializer should normalize paths and stringify validation errors."""
    document_root = tmp_path / 'root'
    document_root.mkdir()
    settings.LOCAL_FILES_DOCUMENT_ROOT = str(document_root)
    settings.LOCAL_FILES_SERVING_ENABLED = True
 
    project = Project.objects.get(pk=project_id)
 
    nested_detail = ['bad', {'nested': ('inner',)}]
    with mock.patch(
        'io_storages.localfiles.models.LocalFilesImportStorage.validate_connection',
        side_effect=DjangoValidationError(nested_detail),
    ) as mocked_validate:
        serializer = LocalFilesImportStorageSerializer()
 
        with pytest.raises(DRFValidationError) as excinfo:
            serializer.validate({'project': project, 'path': f'{document_root}//'})
 
    assert _stringify_detail(excinfo.value.detail) == ['bad', "('inner',)"]
    # Path is normalized before validate_connection is called
    assert mocked_validate.call_count == 1
 
 
@pytest.mark.django_db
def test_export_serializer_wraps_generic_exception(settings, tmp_path, project_id):
    """LocalFilesExportStorageSerializer should wrap unexpected exceptions with DRFValidationError."""
    document_root = tmp_path / 'root'
    document_root.mkdir()
    settings.LOCAL_FILES_DOCUMENT_ROOT = str(document_root)
    settings.LOCAL_FILES_SERVING_ENABLED = True
 
    project = Project.objects.get(pk=project_id)
 
    with mock.patch(
        'io_storages.localfiles.models.LocalFilesExportStorage.validate_connection',
        side_effect=RuntimeError('unexpected boom'),
    ):
        serializer = LocalFilesExportStorageSerializer()
 
        with pytest.raises(DRFValidationError) as excinfo:
            serializer.validate({'project': project, 'path': f'{document_root}//subdir//'})
 
    # DRF wraps scalar strings in a list for consistency
    assert _stringify_detail(excinfo.value.detail) == ['unexpected boom']