Skip to content

Commit f4fa4ac

Browse files
committed
Use populated boxes fixture in other orchestrator tests
1 parent 22fa687 commit f4fa4ac

File tree

1 file changed

+57
-105
lines changed

1 file changed

+57
-105
lines changed

tests/unit/test_orchestrator.py

Lines changed: 57 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,17 @@ class JointRig:
6565
controller: UploadOrchestrator
6666

6767

68+
async def file_upload_box_id_generator(*args, **kwargs) -> UUID:
69+
"""Return a new FileUploadBox ID"""
70+
return uuid4()
71+
72+
6873
@pytest.fixture()
6974
def rig(config: ConfigFixture) -> JointRig:
7075
"""Return a joint fixture with in-memory dependency mocks"""
7176
_config = config.config
7277
file_box_client_mock = AsyncMock()
73-
file_box_client_mock.create_file_upload_box.return_value = TEST_FILE_UPLOAD_BOX_ID
78+
file_box_client_mock.create_file_upload_box = file_upload_box_id_generator
7479
access_client_mock = AsyncMock()
7580

7681
controller = UploadOrchestrator(
@@ -99,9 +104,9 @@ async def populate_boxes(rig: JointRig):
99104
title=f"Box {chr(65 + i)}", # "Box A", "Box B", etc.
100105
description=f"Description {i}",
101106
storage_alias="HD01",
102-
data_steward_id=TEST_USER_ID1,
107+
data_steward_id=TEST_DS_ID,
103108
)
104-
await sleep(0.001)
109+
await sleep(0.001) # insert pause to ensure different timestamps for sortings
105110
box_ids.append(box_id)
106111
return box_ids
107112

@@ -123,22 +128,16 @@ async def test_create_research_data_upload_box(rig: JointRig):
123128
assert box.changed_by == TEST_DS_ID
124129
assert box.file_count == 0
125130
assert box.size == 0
126-
assert box.file_upload_box_id == TEST_FILE_UPLOAD_BOX_ID
131+
assert isinstance(box.file_upload_box_id, UUID)
127132
assert box.last_changed - now_utc_ms_prec() < timedelta(seconds=5)
128133
assert box.locked == False
129134
assert box.state == "open"
130135

131136

132-
async def test_update_research_data_upload_box_happy(rig: JointRig):
137+
async def test_update_research_data_upload_box_happy(
138+
rig: JointRig, populated_boxes: list[UUID]
139+
):
133140
"""Test the normal path of updating box attributes."""
134-
# First create a box to update
135-
box_id = await rig.controller.create_research_data_upload_box(
136-
title="Original Title",
137-
description="Original Description",
138-
storage_alias="HD01",
139-
data_steward_id=TEST_DS_ID,
140-
)
141-
142141
# Mock the access client to return that the user has access
143142
rig.access_client.check_box_access.return_value = True # type: ignore
144143

@@ -148,6 +147,7 @@ async def test_update_research_data_upload_box_happy(rig: JointRig):
148147
)
149148

150149
# Call the update method
150+
box_id = populated_boxes[0]
151151
await rig.controller.update_research_data_upload_box(
152152
box_id=box_id, request=update_request, auth_context=DATA_STEWARD_AUTH_CONTEXT
153153
)
@@ -163,28 +163,23 @@ async def test_update_research_data_upload_box_happy(rig: JointRig):
163163
rig.access_client.check_box_access.assert_not_called() # type: ignore
164164

165165

166-
async def test_update_research_data_upload_box_unauthorized(rig: JointRig):
166+
async def test_update_research_data_upload_box_unauthorized(
167+
rig: JointRig, populated_boxes: list[UUID]
168+
):
167169
"""Test the scenario where a user tries updating box attributes like title or description.
168170
169171
Regular users are not authorized to do this, so this should be blocked.
170172
"""
171173
# Mock the access client to return that the user has access (but box doesn't exist)
172174
rig.access_client.check_box_access.return_value = True # type: ignore
173175

174-
# First create a box to update
175-
box_id = await rig.controller.create_research_data_upload_box(
176-
title="Original Title",
177-
description="Original Description",
178-
storage_alias="HD01",
179-
data_steward_id=TEST_DS_ID,
180-
)
181-
182176
# Create an update request
183177
update_request = models.UpdateUploadBoxRequest(
184178
title="Updated Title", description="Updated Description"
185179
)
186180

187181
# Call the update method
182+
box_id = populated_boxes[0]
188183
with pytest.raises(rig.controller.BoxAccessError):
189184
await rig.controller.update_research_data_upload_box(
190185
box_id=box_id,
@@ -215,22 +210,14 @@ async def test_update_research_data_upload_box_not_found(rig: JointRig):
215210
)
216211

217212

218-
# TODO: Apply the fixture to most of these tests
219-
async def test_get_upload_box_files_happy(rig: JointRig):
213+
async def test_get_upload_box_files_happy(rig: JointRig, populated_boxes: list[UUID]):
220214
"""Test the normal path of getting a list of file IDs for a box from the file box service."""
221-
# First create a box
222-
box_id = await rig.controller.create_research_data_upload_box(
223-
title="Test Box",
224-
description="Test Description",
225-
storage_alias="HD01",
226-
data_steward_id=TEST_DS_ID,
227-
)
228-
229215
# Mock the file box client to return a list of file IDs
230216
test_file_ids = sorted([uuid4(), uuid4(), uuid4()])
231217
rig.file_upload_box_client.get_file_upload_list.return_value = test_file_ids # type: ignore
232218

233219
# Mock the access client for non-data steward case
220+
box_id = populated_boxes[0]
234221
rig.access_client.check_box_access.return_value = [box_id] # type: ignore
235222

236223
# Call the method
@@ -248,23 +235,17 @@ async def test_get_upload_box_files_happy(rig: JointRig):
248235
rig.access_client.check_box_access.assert_called_once() # type: ignore
249236

250237

251-
async def test_get_upload_box_files_access_error(rig: JointRig):
238+
async def test_get_upload_box_files_access_error(
239+
rig: JointRig, populated_boxes: list[UUID]
240+
):
252241
"""Test the case where getting box files fails because the user doesn't have access."""
253-
# First create a box
254-
box_id = await rig.controller.create_research_data_upload_box(
255-
title="Test Box",
256-
description="Test Description",
257-
storage_alias="HD01",
258-
data_steward_id=TEST_DS_ID,
259-
)
260-
261242
# Mock the access client to return that the user does NOT have access to this box
262243
rig.access_client.check_box_access.return_value = False # type: ignore
263244

264245
# This should raise BoxAccessError since the user doesn't have access
265246
with pytest.raises(rig.controller.BoxAccessError):
266247
await rig.controller.get_upload_box_files(
267-
box_id=box_id, auth_context=USER2_AUTH_CONTEXT
248+
box_id=populated_boxes[0], auth_context=USER2_AUTH_CONTEXT
268249
)
269250

270251
# Verify that access check was performed
@@ -293,25 +274,19 @@ async def test_get_upload_box_files_box_not_found(rig: JointRig):
293274
rig.file_upload_box_client.get_file_upload_list.assert_not_called() # type: ignore
294275

295276

296-
async def test_upsert_file_upload_box_happy(rig: JointRig):
277+
async def test_upsert_file_upload_box_happy(rig: JointRig, populated_boxes: list[UUID]):
297278
"""Test the method that consumes FileUploadBox data and uses it to update RDUBoxes."""
298-
# First create a research data upload box
299-
box_id = await rig.controller.create_research_data_upload_box(
300-
title="Test Box",
301-
description="Test Description",
302-
storage_alias="HD01",
303-
data_steward_id=TEST_DS_ID,
304-
)
305-
306279
# Get the created box to verify initial state
280+
box_id = populated_boxes[0]
307281
initial_box = await rig.box_dao.get_by_id(box_id)
308282
assert initial_box.file_count == 0
309283
assert initial_box.size == 0
310284
assert initial_box.locked == False
285+
file_upload_box_id = initial_box.file_upload_box_id
311286

312287
# Create a FileUploadBox with updated data
313288
updated_file_upload_box = models.FileUploadBox(
314-
id=TEST_FILE_UPLOAD_BOX_ID, # This should match the file_upload_box_id in our research box
289+
id=file_upload_box_id, # This should match the file_upload_box_id in our research box
315290
locked=True,
316291
file_count=5,
317292
size=1024000,
@@ -328,8 +303,8 @@ async def test_upsert_file_upload_box_happy(rig: JointRig):
328303
assert updated_box.locked == True
329304

330305
# Verify other fields remain unchanged
331-
assert updated_box.title == "Test Box"
332-
assert updated_box.description == "Test Description"
306+
assert updated_box.title == "Box A"
307+
assert updated_box.description == "Description 0"
333308
assert updated_box.storage_alias == "HD01"
334309

335310

@@ -351,29 +326,23 @@ async def test_upsert_file_upload_box_not_found(rig: JointRig):
351326
assert not [x async for x in rig.box_dao.find_all(mapping={})]
352327

353328

354-
async def test_get_research_data_upload_box_happy(rig: JointRig):
329+
async def test_get_research_data_upload_box_happy(
330+
rig: JointRig, populated_boxes: list[UUID]
331+
):
355332
"""Test the normal path of getting a research data upload box."""
356-
# First create a research data upload box
357-
box_id = await rig.controller.create_research_data_upload_box(
358-
title="Test Box",
359-
description="Test Description",
360-
storage_alias="HD01",
361-
data_steward_id=TEST_DS_ID,
362-
)
363-
364333
# Try retrieval with Data Steward credentials
365334
rig.access_client.check_box_access.return_value = True # type: ignore
335+
box_id = populated_boxes[0]
366336
result = await rig.controller.get_research_data_upload_box(
367337
box_id=box_id, auth_context=DATA_STEWARD_AUTH_CONTEXT
368338
)
369339

370340
# Verify we got the correct box back
371341
assert result.id == box_id
372-
assert result.title == "Test Box"
373-
assert result.description == "Test Description"
342+
assert result.title == "Box A"
343+
assert result.description == "Description 0"
374344
assert result.storage_alias == "HD01"
375345
assert result.changed_by == TEST_DS_ID
376-
assert result.file_upload_box_id == TEST_FILE_UPLOAD_BOX_ID
377346

378347
# Verify access check was NOT called for Data Steward
379348
rig.access_client.check_box_access.assert_not_called() # type: ignore
@@ -386,24 +355,18 @@ async def test_get_research_data_upload_box_happy(rig: JointRig):
386355
rig.access_client.check_box_access.assert_called_once() # type: ignore
387356

388357

389-
async def test_get_research_data_upload_box_access_denied(rig: JointRig):
358+
async def test_get_research_data_upload_box_access_denied(
359+
rig: JointRig, populated_boxes: list[UUID]
360+
):
390361
"""Test the case where the user doesn't have access to the box."""
391-
# First create a research data upload box
392-
box_id = await rig.controller.create_research_data_upload_box(
393-
title="Test Box",
394-
description="Test Description",
395-
storage_alias="HD01",
396-
data_steward_id=TEST_DS_ID,
397-
)
398-
399362
# Mock the access client to return that the user does NOT have access
400363
rig.access_client.check_box_access.return_value = False # type: ignore
401364

402365
# Try to get the box with a different user
403366
# This should raise BoxAccessError since the user doesn't have access
404367
with pytest.raises(rig.controller.BoxAccessError):
405368
await rig.controller.get_research_data_upload_box(
406-
box_id=box_id, auth_context=USER2_AUTH_CONTEXT
369+
box_id=populated_boxes[0], auth_context=USER2_AUTH_CONTEXT
407370
)
408371

409372
# Verify access check was called
@@ -428,25 +391,20 @@ async def test_get_research_data_upload_box_not_found(rig: JointRig):
428391
rig.access_client.check_box_access.assert_called_once() # type: ignore
429392

430393

431-
async def test_get_upload_access_grants_happy(rig: JointRig):
394+
async def test_get_upload_access_grants_happy(
395+
rig: JointRig, populated_boxes: list[UUID]
396+
):
432397
"""Test the normal path for getting upload access grants."""
433398
# First create a research data upload box
434-
box_id = await rig.controller.create_research_data_upload_box(
435-
title="Test Box",
436-
description="Test Description",
437-
storage_alias="HD01",
438-
data_steward_id=TEST_DS_ID,
439-
)
440-
399+
box_id = populated_boxes[0]
441400
# Create mock upload grants that would be returned by access client
442-
test_user_id = uuid4()
443401
test_iva_id = uuid4()
444402
test_grant_id = uuid4()
445403

446404
mock_grants = [
447405
models.UploadGrant(
448406
id=test_grant_id,
449-
user_id=test_user_id,
407+
user_id=TEST_USER_ID1,
450408
iva_id=test_iva_id,
451409
box_id=box_id,
452410
created=now_utc_ms_prec(),
@@ -463,7 +421,7 @@ async def test_get_upload_access_grants_happy(rig: JointRig):
463421

464422
# Call the method
465423
result = await rig.controller.get_upload_access_grants(
466-
user_id=test_user_id,
424+
user_id=TEST_USER_ID1,
467425
iva_id=test_iva_id,
468426
box_id=box_id,
469427
valid=True,
@@ -473,46 +431,40 @@ async def test_get_upload_access_grants_happy(rig: JointRig):
473431
assert len(result) == 1
474432
grant_with_info = result[0]
475433
assert grant_with_info.id == test_grant_id
476-
assert grant_with_info.user_id == test_user_id
434+
assert grant_with_info.user_id == TEST_USER_ID1
477435
assert grant_with_info.iva_id == test_iva_id
478436
assert grant_with_info.box_id == box_id
479437
assert grant_with_info.user_name == "Test User"
480438
assert grant_with_info.user_email == "[email protected]"
481439
assert grant_with_info.user_title == "Dr."
482440
# These should come from the box
483-
assert grant_with_info.box_title == "Test Box"
484-
assert grant_with_info.box_description == "Test Description"
441+
assert grant_with_info.box_title == "Box A"
442+
assert grant_with_info.box_description == "Description 0"
485443

486444
# Verify access client was called with correct parameters
487445
rig.access_client.get_upload_access_grants.assert_called_once_with( # type: ignore
488-
user_id=test_user_id,
446+
user_id=TEST_USER_ID1,
489447
iva_id=test_iva_id,
490448
box_id=box_id,
491449
valid=True,
492450
)
493451

494452

495-
async def test_get_upload_access_grants_box_missing(rig: JointRig, caplog):
453+
async def test_get_upload_access_grants_box_missing(
454+
rig: JointRig, caplog, populated_boxes: list[UUID]
455+
):
496456
"""Test the case where grants returned from the access API include a grant with
497457
a box ID that doesn't exist in the UOS. This test also checks that we emit a
498458
WARNING log (but don't raise an error).
499459
"""
500-
# Create one valid box
501-
valid_box_id = await rig.controller.create_research_data_upload_box(
502-
title="Valid Box",
503-
description="Valid Description",
504-
storage_alias="HD01",
505-
data_steward_id=TEST_DS_ID,
506-
)
507-
508460
# Create mock upload grants - one with a valid box ID, one with an invalid box ID
509-
test_user_id = uuid4()
461+
valid_box_id = populated_boxes[0]
510462
invalid_box_id = uuid4() # This box doesn't/won't exist
511463

512464
mock_grants = [
513465
models.UploadGrant(
514466
id=uuid4(),
515-
user_id=test_user_id,
467+
user_id=TEST_USER_ID1,
516468
iva_id=uuid4(),
517469
box_id=valid_box_id, # This box exists
518470
created=now_utc_ms_prec(),
@@ -524,7 +476,7 @@ async def test_get_upload_access_grants_box_missing(rig: JointRig, caplog):
524476
),
525477
models.UploadGrant(
526478
id=uuid4(),
527-
user_id=test_user_id,
479+
user_id=TEST_USER_ID1,
528480
iva_id=uuid4(),
529481
box_id=invalid_box_id, # This box doesn't exist
530482
created=now_utc_ms_prec(),
@@ -546,8 +498,8 @@ async def test_get_upload_access_grants_box_missing(rig: JointRig, caplog):
546498
assert len(result) == 1
547499
grant_with_info = result[0]
548500
assert grant_with_info.box_id == valid_box_id
549-
assert grant_with_info.box_title == "Valid Box"
550-
assert grant_with_info.box_description == "Valid Description"
501+
assert grant_with_info.box_title == "Box A"
502+
assert grant_with_info.box_description == "Description 0"
551503

552504
# Verify a warning was logged for the invalid box
553505
assert caplog.records

0 commit comments

Comments
 (0)