增加课表管理员密钥导出
This commit is contained in:
@@ -15,6 +15,8 @@ from server import timetable_ocr as ocr
|
||||
from server.models import timetables
|
||||
from test_api import ApiTestCase, auth, register
|
||||
|
||||
ADMIN_KEY = 'timetable-test-admin'
|
||||
|
||||
|
||||
def image_bytes(color='blue'):
|
||||
stream = io.BytesIO(); Image.new('RGB', (30, 30), color).save(stream, 'PNG')
|
||||
@@ -61,9 +63,10 @@ class TimetableTests(ApiTestCase):
|
||||
self.db_path_patch = patch.object(self.m, '_db_path', self.root/'test.db'); self.db_path_patch.start()
|
||||
self.ocr_patch = patch('server.timetable_ocr.recognize', side_effect=lambda raw: recognized()); self.ocr_patch.start()
|
||||
self.collector_patch = patch.dict(os.environ, WORKLOG_TIMETABLE_COLLECTORS=''); self.collector_patch.start()
|
||||
self.admin_patch = patch.dict(os.environ, WORKLOG_TIMETABLE_ADMIN_KEY=ADMIN_KEY); self.admin_patch.start()
|
||||
|
||||
def tearDown(self):
|
||||
self.collector_patch.stop(); self.ocr_patch.stop(); self.db_path_patch.stop(); self.files.cleanup()
|
||||
self.admin_patch.stop(); self.collector_patch.stop(); self.ocr_patch.stop(); self.db_path_patch.stop(); self.files.cleanup()
|
||||
super().tearDown()
|
||||
|
||||
async def upload(self, user, week='odd', color='blue'):
|
||||
@@ -77,6 +80,9 @@ class TimetableTests(ApiTestCase):
|
||||
if cell['status']=='unknown': cell['status']='free'
|
||||
return await self.client.patch('/api/timetables/me/'+timetable['week_type'],headers=auth(user['token']),json={'revision':timetable['revision'],'cells':cells,'confirm':confirm})
|
||||
|
||||
def export_headers(self, user, key=ADMIN_KEY):
|
||||
return {**auth(user['token']), 'X-Timetable-Admin-Key': key}
|
||||
|
||||
async def test_authenticated_image_only_validation(self):
|
||||
for path in ['/api/timetables','/api/timetables/schema','/api/timetables/export']:
|
||||
self.assertEqual((await self.client.get(path)).status_code,401)
|
||||
@@ -129,21 +135,22 @@ class TimetableTests(ApiTestCase):
|
||||
owner = await register(self.client,'timetableexport'); other = await register(self.client,'timetableexportother')
|
||||
item = await self.upload(owner); await self.upload(owner,'even'); await self.upload(other)
|
||||
path = '/api/timetables/me/odd'; headers = auth(owner['token'])
|
||||
self.assertEqual((await self.client.get('/api/timetables/export',headers=headers)).status_code,401)
|
||||
self.assertEqual((await self.client.get('/api/timetables/export',headers=self.export_headers(owner,'wrong-key'))).status_code,401)
|
||||
result = await self.client.patch(path,headers=headers,json={'revision':item['revision'],'cells':item['structure']['cells'],'confirm':True})
|
||||
self.assertEqual(result.status_code,422)
|
||||
duplicate = [item['structure']['cells'][0]]*20
|
||||
self.assertEqual((await self.client.patch(path,headers=headers,json={'revision':item['revision'],'cells':duplicate})).status_code,422)
|
||||
self.assertEqual((await self.client.get('/api/timetables/export',headers=headers)).json()['records'],[])
|
||||
self.assertEqual((await self.client.get('/api/timetables/export',headers=self.export_headers(owner))).json()['records'],[])
|
||||
result = await self.confirm(owner,item); self.assertEqual(result.status_code,200,result.text)
|
||||
data = await self.client.get('/api/timetables/export',headers=headers)
|
||||
data = await self.client.get('/api/timetables/export',headers=self.export_headers(owner))
|
||||
self.assertEqual(len(data.json()['records']),1); self.assertNotIn(owner['user']['email'],data.text)
|
||||
csv_response = await self.client.get('/api/timetables/export?format=csv',headers=headers)
|
||||
self.assertEqual(data.headers['cache-control'],'no-store')
|
||||
csv_response = await self.client.get('/api/timetables/export?format=csv',headers=self.export_headers(owner))
|
||||
rows = list(csv.DictReader(io.StringIO(csv_response.content.decode('utf-8-sig'))))
|
||||
self.assertEqual(len(rows),20); self.assertEqual(sum(row['eligible']=='True' for row in rows),19)
|
||||
all_own = (await self.client.get('/api/timetables/export?confirmed_only=false',headers=headers)).json()['records']
|
||||
self.assertEqual(len(all_own),2)
|
||||
with patch.dict(os.environ,WORKLOG_TIMETABLE_COLLECTORS=owner['user']['email']):
|
||||
self.assertEqual(len((await self.client.get('/api/timetables/export?confirmed_only=false',headers=headers)).json()['records']),3)
|
||||
all_records = (await self.client.get('/api/timetables/export?confirmed_only=false',headers=self.export_headers(owner))).json()['records']
|
||||
self.assertEqual(len(all_records),3)
|
||||
|
||||
async def test_recognition_failure_preserves_image_for_manual_correction(self):
|
||||
user = await register(self.client,'timetablefallback')
|
||||
|
||||
Reference in New Issue
Block a user