1111from syft_bg .approve .handlers .job import ApprovedJobHandler
1212from syft_bg .common .config import get_default_paths
1313from syft_rds import SyftRDSClient
14+ from syft_rds .apis import Api , ApiDefinition , FileEntry
15+ from syft_rds .apis .repr import api_collection_repr_html , api_collection_repr_str
16+ from syft_rds .client import _print_call_result
1417
1518ADDER_CODE = """import json
1619with open("params.json") as f:
3740 raise ValueError("Sum too large to share")
3841"""
3942
43+ # Shares the outputs with no one.
44+ HOLD_BACK_POLICY = """import os
45+ from pathlib import Path
46+ Path(os.environ["SYFT_POLICY_RECIPIENTS_PATH"]).write_text("[]")
47+ """
48+
4049
4150@contextmanager
4251def _temp_config_paths ():
@@ -92,7 +101,7 @@ def _ds_job(ds_manager, name: str):
92101 return next (j for j in ds_manager .jobs if j .name == name )
93102
94103
95- def test_api_policies_allow_block_and_show ():
104+ def test_api_policies_allow_block_and_show (capsys ):
96105 ds_manager , do_manager = SyftRDSClient .pair_with_mock_drive_service_connection (
97106 use_in_memory_cache = False ,
98107 sync_automatically = False ,
@@ -109,6 +118,11 @@ def test_api_policies_allow_block_and_show():
109118
110119 do_manager .sync ()
111120 ds_manager .sync ()
121+ # The api table says which policies an api has.
122+ assert "client.api.adder(a, b) | policies: input, output" in str (ds_manager .api )
123+ table = ds_manager .api ._repr_html_ ()
124+ assert '<th class="syftapi-th">Policies</th>' in table
125+ assert '<td class="syftapi-td">input, output</td>' in table
112126 api = ds_manager .api .adder
113127 assert "Rate limit reached" in repr (api )
114128 assert "Sum too large to share" in api ._repr_html_ ()
@@ -126,14 +140,34 @@ def test_api_policies_allow_block_and_show():
126140 blocked = _ds_job (ds_manager , large .name )
127141 assert blocked .status == "failed"
128142 assert blocked .policy_error == "ValueError: Sum too large to share"
143+ capsys .readouterr ()
129144 assert blocked .output_paths == []
145+ # The DS is sent to the policy's reason, not to a stderr they don't get.
146+ out = capsys .readouterr ().out
147+ assert (
148+ "The data owner's policy refused the call: "
149+ "ValueError: Sum too large to share" in out
150+ )
151+ assert "job.policy_error" in out and "job.stderr" not in out
152+ _print_call_result (blocked )
153+ assert capsys .readouterr ().out == (
154+ f"Job '{ large .name } ' finished with status: failed\n "
155+ " The data owner's policy refused the call: "
156+ "ValueError: Sum too large to share\n "
157+ )
130158
131159 # The rate limit of one call is used up.
132160 second = api (1 , b = 2 , block = False )
133161 _run_calls (do_manager , paths .config )
134162 limited = _ds_job (ds_manager , second .name )
135163 assert limited .status == "failed"
136164 assert limited .policy_error == "ValueError: Rate limit reached: 1 call"
165+ capsys .readouterr ()
166+ _print_call_result (limited )
167+ assert (
168+ "refused the call: ValueError: Rate limit reached: 1 call"
169+ in capsys .readouterr ().out
170+ )
137171
138172 # The DO's view of a call shows the api and its policies.
139173 do_job = next (j for j in do_manager .jobs if j .name == large .name )
@@ -142,3 +176,69 @@ def test_api_policies_allow_block_and_show():
142176 assert "adder" in html and "Input policy" in html and "Output policy" in html
143177 original = next (j for j in do_manager .jobs if j .name == job .name )
144178 assert original .status == "pending"
179+
180+
181+ def test_held_back_outputs_point_to_the_policy (capsys ):
182+ ds_manager , do_manager = SyftRDSClient .pair_with_mock_drive_service_connection (
183+ use_in_memory_cache = False ,
184+ sync_automatically = False ,
185+ )
186+ job = _submit_adder_job (ds_manager , do_manager , {"a" : 1 , "b" : 2 })
187+
188+ with _temp_config_paths () as (paths , tmp ):
189+ result = create_api_from_job (
190+ job , output_policy = _write (tmp / "hold_back.py" , HOLD_BACK_POLICY )
191+ )
192+ assert result .success
193+ do_manager .sync ()
194+ ds_manager .sync ()
195+ call = ds_manager .api .adder (1 , b = 2 , block = False )
196+ _run_calls (do_manager , paths .config )
197+ held = _ds_job (ds_manager , call .name )
198+
199+ assert held .status == "done" and held .outputs_withheld
200+ capsys .readouterr ()
201+ assert held .output_paths == []
202+ # The DS is told the policy held the outputs back, not that the script
203+ # wrote none.
204+ out = capsys .readouterr ().out
205+ assert "The data owner's output policy held the outputs back." in out
206+ assert "The data owner can still share them with you later." in out
207+ assert "may not have written" not in out
208+ _print_call_result (held )
209+ assert capsys .readouterr ().out == (
210+ f"Job '{ call .name } ' finished with status: done\n "
211+ " The data owner's output policy held the outputs back.\n "
212+ " The data owner can still share them with you later.\n "
213+ )
214+
215+
216+ def _policy_entry (kind : str ) -> list [FileEntry ]:
217+ return [FileEntry (relative_path = f"policies/{ kind } /run.sh" , hash = "sha256:0" )]
218+
219+
220+ def test_api_table_shows_policies (tmp_path ):
221+ definitions = {
222+ "plain" : ApiDefinition (),
223+ "checked" : ApiDefinition (input_policy = _policy_entry ("input" )),
224+ "filtered" : ApiDefinition (output_policy = _policy_entry ("output" )),
225+ "both" : ApiDefinition (
226+ input_policy = _policy_entry ("input" ),
227+ output_policy = _policy_entry ("output" ),
228+ ),
229+ }
230+ apis = [
231+ Api (name , "do@test.org" , tmp_path , definition , client = None )
232+ for name , definition in definitions .items ()
233+ ]
234+
235+ lines = api_collection_repr_str (apis ).splitlines ()[1 :]
236+ assert [line .split (" | " )[1 ] for line in lines ] == [
237+ "policies: -" ,
238+ "policies: input" ,
239+ "policies: output" ,
240+ "policies: input, output" ,
241+ ]
242+ html = api_collection_repr_html (apis )
243+ for policies in ("-" , "input" , "output" , "input, output" ):
244+ assert f'<td class="syftapi-td">{ policies } </td>' in html
0 commit comments