|
| 1 | +"""Tests that the shared PROPFIND property lists are never mutated in place.""" |
| 2 | + |
| 3 | +import types |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from nc_py_api.files._files import ( |
| 8 | + PROPFIND_LOCKING_PROPERTIES, |
| 9 | + PROPFIND_PROPERTIES, |
| 10 | + PropFindType, |
| 11 | + get_propfind_properties, |
| 12 | +) |
| 13 | +from nc_py_api.files.files import FilesAPI |
| 14 | +from nc_py_api.files.files_async import AsyncFilesAPI |
| 15 | + |
| 16 | +CAPS_WITH_LOCKING = {"files": {"locking": "1.0"}} |
| 17 | +CAPS_WITHOUT_LOCKING = {"files": {}} |
| 18 | +TRASHBIN_PROPERTIES = ["nc:trashbin-filename", "nc:trashbin-original-location", "nc:trashbin-deletion-time"] |
| 19 | +# real copies taken at import: comparing against the live constants would be vacuous |
| 20 | +# if they are ever turned back into lists, since the name would alias the same object |
| 21 | +EXPECTED_PROPERTIES = tuple(PROPFIND_PROPERTIES) |
| 22 | +EXPECTED_LOCKING_PROPERTIES = tuple(PROPFIND_LOCKING_PROPERTIES) |
| 23 | + |
| 24 | + |
| 25 | +def test_propfind_constants_are_immutable(): |
| 26 | + assert isinstance(PROPFIND_PROPERTIES, tuple) |
| 27 | + assert isinstance(PROPFIND_LOCKING_PROPERTIES, tuple) |
| 28 | + with pytest.raises(AttributeError): |
| 29 | + PROPFIND_PROPERTIES.append("nc:not-allowed") |
| 30 | + |
| 31 | + |
| 32 | +def test_get_propfind_properties_does_not_mutate_constants(): |
| 33 | + for _ in range(10): |
| 34 | + get_propfind_properties(CAPS_WITH_LOCKING) |
| 35 | + get_propfind_properties(CAPS_WITHOUT_LOCKING) |
| 36 | + assert tuple(PROPFIND_PROPERTIES) == EXPECTED_PROPERTIES |
| 37 | + assert tuple(PROPFIND_LOCKING_PROPERTIES) == EXPECTED_LOCKING_PROPERTIES |
| 38 | + |
| 39 | + |
| 40 | +def test_get_propfind_properties_returns_fresh_list(): |
| 41 | + first = get_propfind_properties(CAPS_WITH_LOCKING) |
| 42 | + second = get_propfind_properties(CAPS_WITH_LOCKING) |
| 43 | + assert isinstance(first, list) |
| 44 | + assert first == second |
| 45 | + assert first is not second |
| 46 | + first.append("nc:added-by-caller") |
| 47 | + assert "nc:added-by-caller" not in second |
| 48 | + assert "nc:added-by-caller" not in PROPFIND_PROPERTIES |
| 49 | + assert tuple(PROPFIND_PROPERTIES) == EXPECTED_PROPERTIES |
| 50 | + |
| 51 | + |
| 52 | +def test_get_propfind_properties_locking_capability(): |
| 53 | + with_locking = get_propfind_properties(CAPS_WITH_LOCKING) |
| 54 | + without_locking = get_propfind_properties(CAPS_WITHOUT_LOCKING) |
| 55 | + assert with_locking == [*EXPECTED_PROPERTIES, *EXPECTED_LOCKING_PROPERTIES] |
| 56 | + assert without_locking == list(EXPECTED_PROPERTIES) |
| 57 | + |
| 58 | + |
| 59 | +def test_trashbin_list_does_not_mutate_constants(monkeypatch): |
| 60 | + requested = [] |
| 61 | + |
| 62 | + def _fake_listdir(_self, _user, _path, **kwargs): |
| 63 | + requested.append(list(kwargs["properties"])) |
| 64 | + return [] |
| 65 | + |
| 66 | + monkeypatch.setattr(FilesAPI, "_listdir", _fake_listdir) |
| 67 | + files_api = FilesAPI(types.SimpleNamespace(user="admin")) |
| 68 | + for _ in range(3): |
| 69 | + files_api.trashbin_list() |
| 70 | + assert tuple(PROPFIND_PROPERTIES) == EXPECTED_PROPERTIES |
| 71 | + for properties in requested: |
| 72 | + assert properties == [*EXPECTED_PROPERTIES, *TRASHBIN_PROPERTIES] |
| 73 | + |
| 74 | + |
| 75 | +async def test_trashbin_list_async_does_not_mutate_constants(monkeypatch): |
| 76 | + requested = [] |
| 77 | + |
| 78 | + async def _fake_listdir(_self, _user, _path, **kwargs): |
| 79 | + requested.append(list(kwargs["properties"])) |
| 80 | + return [] |
| 81 | + |
| 82 | + class _StubSession: |
| 83 | + @property |
| 84 | + async def user(self) -> str: |
| 85 | + return "admin" |
| 86 | + |
| 87 | + monkeypatch.setattr(AsyncFilesAPI, "_listdir", _fake_listdir) |
| 88 | + files_api = AsyncFilesAPI(_StubSession()) |
| 89 | + for _ in range(3): |
| 90 | + await files_api.trashbin_list() |
| 91 | + assert tuple(PROPFIND_PROPERTIES) == EXPECTED_PROPERTIES |
| 92 | + for properties in requested: |
| 93 | + assert properties == [*EXPECTED_PROPERTIES, *TRASHBIN_PROPERTIES] |
| 94 | + |
| 95 | + |
| 96 | +def test_trashbin_list_requests_trashbin_prop_type(monkeypatch): |
| 97 | + calls = [] |
| 98 | + |
| 99 | + def _fake_listdir(_self, _user, _path, **kwargs): |
| 100 | + calls.append(kwargs["prop_type"]) |
| 101 | + return [] |
| 102 | + |
| 103 | + monkeypatch.setattr(FilesAPI, "_listdir", _fake_listdir) |
| 104 | + FilesAPI(types.SimpleNamespace(user="admin")).trashbin_list() |
| 105 | + assert calls == [PropFindType.TRASHBIN] |
0 commit comments