物品借出免审批并扩大上传限制

This commit is contained in:
2026-11-17 12:41:09 +08:00
parent 2fb2c5e1ad
commit 2c9b9b946a
12 changed files with 447 additions and 214 deletions
+31 -21
View File
@@ -8,6 +8,7 @@ from test_api import ApiTestCase, auth, register
from test_center_export import package
from test_timetables import image_bytes
from server.models import center_photos
from server import center as center_service
class DirectCenterPhotoTests(ApiTestCase):
@@ -62,57 +63,66 @@ class DirectCenterPhotoTests(ApiTestCase):
more=await c.post('/api/center/materials',headers=headers,content=package([('补充.md','补充文档')]))
self.assertEqual((await c.post('/api/activities/'+aid+'/materials',headers=headers,json={'material_id':more.json()['id']})).status_code,200)
async def test_twenty_photo_batch_limit_and_historical_photos_are_preserved(self):
async def test_photo_batch_limit_and_historical_photos_are_preserved(self):
limit=center_service.MAX_PHOTOS_PER_UPLOAD
owner=await register(self.client,'batchlimit');headers=auth(owner['token']);c=self.client
photos=await self.upload(owner,21)
photos=await self.upload(owner,limit+1)
body={'title':'照片上限','start_at':self.m.now_ms(),'record_type':'center','photo_ids':photos}
rejected=await c.post('/api/activities',headers=headers,json=body)
self.assertEqual(rejected.status_code,422,rejected.text);self.assertIn('20张',rejected.text)
body['photo_ids']=photos[:20]
self.assertEqual(rejected.status_code,422,rejected.text);self.assertIn(f'{limit}张',rejected.text)
body['photo_ids']=photos[:limit]
result=await c.post('/api/activities',headers=headers,json=body)
self.assertEqual(result.status_code,200,result.text);aid=result.json()['activity']['id']
path='/api/activities/'+aid
rejected=await c.post(path+'/materials',headers=headers,json={'photo_ids':photos})
self.assertEqual(rejected.status_code,422,rejected.text)
result=await c.post(path+'/materials',headers=headers,json={'photo_ids':photos[20:]})
self.assertEqual(result.status_code,200,result.text);self.assertEqual(len(result.json()['activity']['photo_ids']),21)
# 历史记录超过20张,旧页面提交原照片、或只改说明都能保存。
result=await c.post(path+'/materials',headers=headers,json={'photo_ids':photos[limit:]})
self.assertEqual(result.status_code,200,result.text);self.assertEqual(len(result.json()['activity']['photo_ids']),limit+1)
# 历史记录超过单次上限,旧页面提交原照片、或只改说明都能保存。
for changes in ({'description':'只改说明'},{'photo_ids':photos}):
result=await c.patch(path,headers=headers,json=changes)
self.assertEqual(result.status_code,200,result.text);self.assertEqual(len(result.json()['activity']['photo_ids']),21)
extra=await self.upload(owner,21)
self.assertEqual(result.status_code,200,result.text);self.assertEqual(len(result.json()['activity']['photo_ids']),limit+1)
extra=await self.upload(owner,limit+1)
rejected=await c.patch(path,headers=headers,json={'photo_ids':extra})
self.assertEqual(rejected.status_code,422,rejected.text)
result=await c.patch(path,headers=headers,json={'photo_ids':extra[:20]})
self.assertEqual(result.status_code,200,result.text);self.assertEqual(len(result.json()['activity']['photo_ids']),41)
result=await c.patch(path,headers=headers,json={'photo_ids':extra[:limit]})
self.assertEqual(result.status_code,200,result.text);self.assertEqual(len(result.json()['activity']['photo_ids']),limit*2+1)
detail=(await c.get(path,headers=headers)).json()['activity']
self.assertEqual(set(detail['photo_ids']),set(photos+extra[:20]))
self.assertEqual(set(detail['photo_ids']),set(photos+extra[:limit]))
async def test_zip_and_direct_photos_share_twenty_photo_batch_limit(self):
async def test_zip_and_direct_photos_share_photo_batch_limit(self):
limit=center_service.MAX_PHOTOS_PER_UPLOAD
owner=await register(self.client,'mixedlimit');headers=auth(owner['token']);c=self.client
raw=package([(f'{i}.png',image_bytes()) for i in range(21)])
raw=package([(f'{i}.png',image_bytes()) for i in range(limit+1)])
rejected=await c.post('/api/center/materials',headers=headers,content=raw)
self.assertEqual(rejected.status_code,422,rejected.text);self.assertIn('20张',rejected.text)
self.assertEqual(rejected.status_code,422,rejected.text);self.assertIn(f'{limit}张',rejected.text)
material=(await c.post('/api/center/materials',headers=headers,content=package())).json()
photos=await self.upload(owner,20)
photos=await self.upload(owner,limit)
body={'title':'混合上限','start_at':self.m.now_ms(),'record_type':'center','material_id':material['id'],'photo_ids':photos}
rejected=await c.post('/api/activities',headers=headers,json=body)
self.assertEqual(rejected.status_code,422,rejected.text)
body['photo_ids']=photos[:19]
body['photo_ids']=photos[:limit-1]
result=await c.post('/api/activities',headers=headers,json=body)
self.assertEqual(result.status_code,200,result.text);aid=result.json()['activity']['id'];path='/api/activities/'+aid
self.assertEqual(len(result.json()['activity']['photo_ids']),20)
self.assertEqual(len(result.json()['activity']['photo_ids']),limit)
new_material=(await c.post('/api/center/materials',headers=headers,content=package())).json()
batch={'material_id':new_material['id'],'photo_ids':photos}
rejected=await c.post(path+'/materials',headers=headers,json=batch)
self.assertEqual(rejected.status_code,422,rejected.text)
batch['photo_ids']=await self.upload(owner,20)
batch['photo_ids']=await self.upload(owner,limit)
rejected=await c.patch(path,headers=headers,json=batch)
self.assertEqual(rejected.status_code,422,rejected.text)
batch['photo_ids']=batch['photo_ids'][:19]
batch['photo_ids']=batch['photo_ids'][:limit-1]
result=await c.post(path+'/materials',headers=headers,json=batch)
self.assertEqual(result.status_code,200,result.text)
self.assertEqual(len(result.json()['activity']['photo_ids']),40)
self.assertEqual(len(result.json()['activity']['photo_ids']),limit*2)
async def test_zip_request_limit_uses_configured_size(self):
owner=await register(self.client,'ziplimit');headers=auth(owner['token'])
with patch.object(center_service,'MAX_ZIP_BYTES',16):
result=await self.client.post('/api/center/materials',headers=headers,content=b'x'*17)
self.assertEqual(result.status_code,413,result.text)
self.assertIn('ZIP材料包最大',result.text)
async def test_daily_allowance_allows_more_than_100_and_keeps_1000_limit(self):
owner=await register(self.client,'photolimit');uid=owner['user']['id']