|
5 | 5 | os.environ["PRE_SYNC"] = "false" |
6 | 6 |
|
7 | 7 | from syft_enclaves import SyftEnclaveClient |
8 | | -from syft_enclaves.immutability import ( |
9 | | - is_private_dataset_path, |
10 | | - make_private_dataset_immutability_filter, |
11 | | -) |
12 | | - |
13 | | - |
14 | | -# --- Unit tests for is_private_dataset_path --- |
15 | | - |
16 | | - |
17 | | -def test_is_private_dataset_path_positive(): |
18 | | - assert is_private_dataset_path( |
19 | | - "do@example.com/private/syft_datasets/my_ds/data.csv" |
20 | | - ) |
21 | | - |
22 | | - |
23 | | -def test_is_private_dataset_path_versioned_layout(): |
24 | | - # A protocol copy holds its files under a v<n> segment. The filter must |
25 | | - # protect that layout too, and not only the flat one of protocol 0. |
26 | | - assert is_private_dataset_path( |
27 | | - "do@example.com/private/syft_datasets/v1/my_ds/data.csv" |
28 | | - ) |
29 | | - |
30 | | - |
31 | | -def test_is_private_dataset_path_public(): |
32 | | - assert not is_private_dataset_path( |
33 | | - "do@example.com/public/syft_datasets/my_ds/data.csv" |
34 | | - ) |
35 | | - |
36 | | - |
37 | | -def test_is_private_dataset_path_wrong_subdir(): |
38 | | - assert not is_private_dataset_path("do@example.com/private/other/file.txt") |
39 | | - |
40 | | - |
41 | | -def test_is_private_dataset_path_too_short(): |
42 | | - # Missing the file component — just the dataset dir itself |
43 | | - assert not is_private_dataset_path("do@example.com/private/syft_datasets/my_ds") |
44 | | - |
45 | | - |
46 | | -# --- Unit tests for the filter --- |
47 | | - |
48 | | - |
49 | | -def test_filter_allows_first_write(tmp_path): |
50 | | - f = make_private_dataset_immutability_filter(tmp_path) |
51 | | - path = "do@example.com/private/syft_datasets/my_ds/data.csv" |
52 | | - assert f(path, False) is True |
53 | | - |
54 | | - |
55 | | -def test_filter_blocks_overwrite(tmp_path): |
56 | | - f = make_private_dataset_immutability_filter(tmp_path) |
57 | | - path = "do@example.com/private/syft_datasets/my_ds/data.csv" |
58 | | - |
59 | | - full_path = tmp_path / path |
60 | | - full_path.parent.mkdir(parents=True, exist_ok=True) |
61 | | - full_path.write_text("original data") |
62 | | - |
63 | | - assert f(path, False) is False |
64 | | - |
65 | | - |
66 | | -def test_filter_blocks_delete(tmp_path): |
67 | | - f = make_private_dataset_immutability_filter(tmp_path) |
68 | | - path = "do@example.com/private/syft_datasets/my_ds/data.csv" |
69 | | - |
70 | | - full_path = tmp_path / path |
71 | | - full_path.parent.mkdir(parents=True, exist_ok=True) |
72 | | - full_path.write_text("original data") |
73 | | - |
74 | | - assert f(path, True) is False |
75 | | - |
76 | | - |
77 | | -def test_filter_allows_non_dataset_paths(tmp_path): |
78 | | - f = make_private_dataset_immutability_filter(tmp_path) |
79 | | - |
80 | | - assert f("do@example.com/public/syft_datasets/ds/file.csv", False) is True |
81 | | - assert f("do@example.com/app_data/job/inbox/ds/job1/run.sh", False) is True |
82 | | - |
83 | | - |
84 | | -def test_filter_allows_remaining_files_in_batch(tmp_path): |
85 | | - """First file creates the dir; other files in the same dataset should still be allowed.""" |
86 | | - f = make_private_dataset_immutability_filter(tmp_path) |
87 | | - base = "do@example.com/private/syft_datasets/my_ds" |
88 | | - |
89 | | - # Simulate first file already written |
90 | | - first = tmp_path / base / "data.csv" |
91 | | - first.parent.mkdir(parents=True, exist_ok=True) |
92 | | - first.write_text("data") |
93 | | - |
94 | | - # Second file doesn't exist yet — allowed |
95 | | - assert f(f"{base}/metadata.yaml", False) is True |
96 | | - |
97 | | - # First file exists — blocked |
98 | | - assert f(f"{base}/data.csv", False) is False |
99 | | - |
100 | | - |
101 | | -# --- Integration test --- |
102 | 8 |
|
103 | 9 |
|
104 | 10 | def _create_tmp_dataset_files(): |
|
0 commit comments