2323}
2424
2525
26- class FakeDB :
26+ class FakeJobDB :
2727 async def summary (self , * args ): ...
2828
2929
30+ class FakeSBMetadataDB :
31+ async def get_owner_id (self , * args ): ...
32+ async def get_sandbox_owner_id (self , * args ): ...
33+
34+
3035@pytest .fixture
3136def job_db ():
32- yield FakeDB ()
37+ yield FakeJobDB ()
38+
39+
40+ @pytest .fixture
41+ def sandbox_metadata_db ():
42+ yield FakeSBMetadataDB ()
3343
3444
3545WMS_POLICY_NAME = "WMSAccessPolicy_AlthoughItDoesNotMatter"
@@ -220,7 +230,7 @@ async def summary_other_vo(*args):
220230)
221231
222232
223- async def test_sandbox_access_policy_create ():
233+ async def test_sandbox_access_policy_create (sandbox_metadata_db ):
224234
225235 admin_user = AuthorizedUserInfo (properties = [JOB_ADMINISTRATOR ], ** base_payload )
226236 normal_user = AuthorizedUserInfo (properties = [NORMAL_USER ], ** base_payload )
@@ -230,6 +240,7 @@ async def test_sandbox_access_policy_create():
230240 await SandboxAccessPolicy .policy (
231241 SANDBOX_POLICY_NAME ,
232242 normal_user ,
243+ sandbox_metadata_db = sandbox_metadata_db ,
233244 )
234245
235246 # An admin cannot create any resource
@@ -238,6 +249,7 @@ async def test_sandbox_access_policy_create():
238249 SANDBOX_POLICY_NAME ,
239250 admin_user ,
240251 action = ActionType .CREATE ,
252+ sandbox_metadata_db = sandbox_metadata_db ,
241253 pfns = [USER_SANDBOX_PFN ],
242254 )
243255
@@ -246,13 +258,14 @@ async def test_sandbox_access_policy_create():
246258 SANDBOX_POLICY_NAME ,
247259 normal_user ,
248260 action = ActionType .CREATE ,
261+ sandbox_metadata_db = sandbox_metadata_db ,
249262 pfns = [USER_SANDBOX_PFN ],
250263 )
251264
252265 ##############
253266
254267
255- async def test_sandbox_access_policy_read ():
268+ async def test_sandbox_access_policy_read (sandbox_metadata_db , monkeypatch ):
256269
257270 admin_user = AuthorizedUserInfo (properties = [JOB_ADMINISTRATOR ], ** base_payload )
258271 normal_user = AuthorizedUserInfo (properties = [NORMAL_USER ], ** base_payload )
@@ -261,6 +274,7 @@ async def test_sandbox_access_policy_read():
261274 SANDBOX_POLICY_NAME ,
262275 admin_user ,
263276 action = ActionType .READ ,
277+ sandbox_metadata_db = sandbox_metadata_db ,
264278 pfns = [USER_SANDBOX_PFN ],
265279 required_prefix = SANDBOX_PREFIX ,
266280 )
@@ -269,6 +283,7 @@ async def test_sandbox_access_policy_read():
269283 SANDBOX_POLICY_NAME ,
270284 admin_user ,
271285 action = ActionType .READ ,
286+ sandbox_metadata_db = sandbox_metadata_db ,
272287 pfns = [OTHER_USER_SANDBOX_PFN ],
273288 required_prefix = SANDBOX_PREFIX ,
274289 )
@@ -279,24 +294,43 @@ async def test_sandbox_access_policy_read():
279294 SANDBOX_POLICY_NAME ,
280295 normal_user ,
281296 action = ActionType .READ ,
297+ sandbox_metadata_db = sandbox_metadata_db ,
282298 pfns = [USER_SANDBOX_PFN ],
283299 )
284300
285301 # User can act on his own sandbox
302+ async def get_owner_id (* args ):
303+ return 1
304+
305+ async def get_sandbox_owner_id (* args ):
306+ return 1
307+
308+ monkeypatch .setattr (sandbox_metadata_db , "get_owner_id" , get_owner_id )
309+ monkeypatch .setattr (
310+ sandbox_metadata_db , "get_sandbox_owner_id" , get_sandbox_owner_id
311+ )
312+
286313 await SandboxAccessPolicy .policy (
287314 SANDBOX_POLICY_NAME ,
288315 normal_user ,
289316 action = ActionType .READ ,
317+ sandbox_metadata_db = sandbox_metadata_db ,
290318 pfns = [USER_SANDBOX_PFN ],
291319 required_prefix = SANDBOX_PREFIX ,
292320 )
293321
294322 # User cannot act on others
323+ async def get_owner_id (* args ):
324+ return 2
325+
326+ monkeypatch .setattr (sandbox_metadata_db , "get_owner_id" , get_owner_id )
327+
295328 with pytest .raises (HTTPException ):
296329 await SandboxAccessPolicy .policy (
297330 SANDBOX_POLICY_NAME ,
298331 normal_user ,
299332 action = ActionType .READ ,
333+ sandbox_metadata_db = sandbox_metadata_db ,
300334 pfns = [OTHER_USER_SANDBOX_PFN ],
301335 required_prefix = SANDBOX_PREFIX ,
302336 )
0 commit comments