重构共享逻辑并修复值班提醒回执去重
This commit is contained in:
+55
-21
@@ -1,6 +1,7 @@
|
||||
import asyncio
|
||||
import io
|
||||
import tempfile
|
||||
from PIL import Image
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
from sqlalchemy import insert, select
|
||||
@@ -8,6 +9,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):
|
||||
@@ -26,6 +28,29 @@ class DirectCenterPhotoTests(ApiTestCase):
|
||||
self.assertEqual(result.status_code,200,result.text);ids.append(result.json()['id'])
|
||||
return ids
|
||||
|
||||
async def test_jpeg_variants_convert_to_unified_original_size_jpeg(self):
|
||||
owner=await register(self.client,'jpegvariants');headers=auth(owner['token'])
|
||||
image=Image.new('RGB',(4021,237),'white')
|
||||
raw=io.BytesIO();image.save(raw,'JPEG')
|
||||
result=await self.client.post('/api/center/photos',headers=headers,content=raw.getvalue())
|
||||
self.assertEqual(result.status_code,200,result.text)
|
||||
photo=await self.client.get('/api/center/photos/'+result.json()['id'],headers=headers)
|
||||
self.assertEqual(photo.status_code,200)
|
||||
self.assertEqual(photo.headers['content-type'],'image/jpeg')
|
||||
with Image.open(io.BytesIO(photo.content)) as stored:
|
||||
self.assertEqual(stored.format,'JPEG')
|
||||
self.assertEqual(stored.size,(4021,237))
|
||||
|
||||
async def test_mpo_multi_picture_jpg_converts_as_jpeg(self):
|
||||
owner=await register(self.client,'mpophoto');headers=auth(owner['token'])
|
||||
image=Image.new('RGB',(32,32),'blue')
|
||||
raw=io.BytesIO();image.save(raw,'MPO')
|
||||
result=await self.client.post('/api/center/photos',headers=headers,content=raw.getvalue())
|
||||
self.assertEqual(result.status_code,200,result.text)
|
||||
photo=await self.client.get('/api/center/photos/'+result.json()['id'],headers=headers)
|
||||
with Image.open(io.BytesIO(photo.content)) as stored:
|
||||
self.assertEqual(stored.format,'JPEG'); self.assertEqual(stored.size,(32,32))
|
||||
|
||||
async def test_photo_only_creation_supplement_and_edit_preserve_all_members_photos(self):
|
||||
owner=await register(self.client,'directowner');member=await register(self.client,'directmember')
|
||||
c=self.client;oh=auth(owner['token']);mh=auth(member['token'])
|
||||
@@ -62,57 +87,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']
|
||||
|
||||
Reference in New Issue
Block a user