修复:装填前复核仓库权限

This commit is contained in:
焦龙言
2026-07-26 18:16:34 +08:00
parent f6e994d95f
commit 66890ca63c
5 changed files with 184 additions and 14 deletions
@@ -391,6 +391,16 @@ class TestFillPalletService(unittest.IsolatedAsyncioTestCase):
actions.append('strain-read')
return self.strain
async def assert_access(
_db: AsyncSession,
_user: CurrentUserModel,
_warehouse_id: int,
*,
for_update: bool = False,
) -> None:
if for_update:
actions.append('warehouse-access-read')
async def add_filling(_db: AsyncSession, filling: BizFillingRecord) -> BizFillingRecord:
actions.append('filling-flush')
filling.filling_id = 31
@@ -414,11 +424,12 @@ class TestFillPalletService(unittest.IsolatedAsyncioTestCase):
captured['filling'].__dict__.pop('filling_id', None)
self.db.commit.side_effect = commit
access = AsyncMock(side_effect=assert_access)
warehouse_read = AsyncMock(side_effect=get_warehouse)
strain_read = AsyncMock(side_effect=get_strain)
with (
patch.object(AccessDao, 'list_user_warehouse_ids', new=AsyncMock(return_value=[7])),
patch.object(AccessService, 'assert_warehouse_access', new=AsyncMock()),
patch.object(AccessService, 'assert_warehouse_access', new=access),
patch.object(OperationDao, 'get_filling_by_idempotency_key', new=AsyncMock(return_value=None)),
patch.object(InventoryDao, 'get_pallet_by_qr_token', new=AsyncMock(return_value=self.pallet)),
patch.object(InventoryDao, 'get_inventory_for_update', new=AsyncMock(side_effect=lock_inventory)),
@@ -437,6 +448,7 @@ class TestFillPalletService(unittest.IsolatedAsyncioTestCase):
'inventory-lock',
'pallet-lock',
'warehouse-read',
'warehouse-access-read',
'strain-read',
'filling-flush',
'event-flush',
@@ -444,6 +456,10 @@ class TestFillPalletService(unittest.IsolatedAsyncioTestCase):
],
actions,
)
self.assertEqual(
[False, False, True], [call.kwargs.get('for_update', False) for call in access.await_args_list]
)
self.assertEqual([7, 7, 7], [call.args[2] for call in access.await_args_list])
warehouse_read.assert_awaited_once_with(self.db, 7, for_update=True)
strain_read.assert_awaited_once_with(self.db, 11, for_update=True)
filling = captured['filling']
@@ -575,6 +591,73 @@ class TestFillPalletService(unittest.IsolatedAsyncioTestCase):
self.db.commit.assert_not_awaited()
self.db.rollback.assert_awaited_once()
async def test_fill_rechecks_actual_warehouse_mapping_after_warehouse_lock(self) -> None:
actions: list[str] = []
async def lock_inventory(*_args: Any) -> BizPalletInventory:
actions.append('inventory-lock')
return self.inventory
async def lock_pallets(*_args: Any) -> list[BizPallet]:
actions.append('pallet-lock')
return [self.pallet]
async def lock_warehouse(*_args: Any, **_kwargs: Any) -> SimpleNamespace:
actions.append('warehouse-read')
return self.warehouse
async def assert_access(
_db: AsyncSession,
_user: CurrentUserModel,
_warehouse_id: int,
*,
for_update: bool = False,
) -> None:
if for_update:
actions.append('warehouse-access-read')
raise PermissionException(data='', message='无权访问该仓库')
async def lock_strain(*_args: Any, **_kwargs: Any) -> BizStrain:
actions.append('strain-read')
return self.strain
access = AsyncMock(side_effect=assert_access)
warehouse_read = AsyncMock(side_effect=lock_warehouse)
strain_read = AsyncMock(side_effect=lock_strain)
add_filling = AsyncMock(return_value=make_filling())
add_event = AsyncMock()
with (
patch.object(AccessDao, 'list_user_warehouse_ids', new=AsyncMock(return_value=[7])),
patch.object(AccessService, 'assert_warehouse_access', new=access),
patch.object(OperationDao, 'get_filling_by_idempotency_key', new=AsyncMock(return_value=None)),
patch.object(InventoryDao, 'get_pallet_by_qr_token', new=AsyncMock(return_value=self.pallet)),
patch.object(InventoryDao, 'get_inventory_for_update', new=AsyncMock(side_effect=lock_inventory)),
patch.object(InventoryDao, 'get_pallets_by_ids', new=AsyncMock(side_effect=lock_pallets)),
patch.object(MasterDao, 'get_warehouse', new=warehouse_read),
patch.object(MasterDao, 'get_strain', new=strain_read),
patch.object(OperationDao, 'add_filling_record', new=add_filling),
patch.object(InventoryDao, 'add_event', new=add_event),
self.assertRaises(PermissionException) as raised,
):
await FillingService.fill_pallet_services(self.db, self.page, self.user)
self.assertEqual('', raised.exception.data)
self.assertEqual('无权访问该托盘', raised.exception.message)
self.assertEqual(
['inventory-lock', 'pallet-lock', 'warehouse-read', 'warehouse-access-read'],
actions,
)
self.assertEqual(
[False, False, True], [call.kwargs.get('for_update', False) for call in access.await_args_list]
)
self.assertEqual([7, 7, 7], [call.args[2] for call in access.await_args_list])
warehouse_read.assert_awaited_once_with(self.db, 7, for_update=True)
strain_read.assert_not_awaited()
add_filling.assert_not_awaited()
add_event.assert_not_awaited()
self.db.commit.assert_not_awaited()
self.db.rollback.assert_awaited_once()
async def test_fill_rejects_stopped_pallet_and_stopped_or_missing_strain(self) -> None:
await self._assert_fill_rejected(pallet=make_pallet(asset_status='1'))
self.db.reset_mock()
@@ -663,9 +746,11 @@ class TestFillPalletService(unittest.IsolatedAsyncioTestCase):
with (
patch.object(AccessDao, 'list_user_warehouse_ids', new=AsyncMock(return_value=[])),
patch.object(OperationDao, 'get_filling_by_idempotency_key', new=get_key),
self.assertRaises(PermissionException),
self.assertRaises(PermissionException) as raised,
):
await FillingService.fill_pallet_services(self.db, self.page, self.user)
self.assertEqual('', raised.exception.data)
self.assertEqual('无权访问该仓库', raised.exception.message)
get_key.assert_not_awaited()
self.db.rollback.assert_awaited_once()