新增赋能中心一楼设备使用教程

This commit is contained in:
2026-11-22 22:48:45 +08:00
parent 2fae181fae
commit 1bb8f73f1e
4 changed files with 185 additions and 5 deletions
+67
View File
@@ -0,0 +1,67 @@
import tempfile
from pathlib import Path
from unittest.mock import patch
from sqlalchemy import select
from test_api import ApiTestCase, auth, register
from server.models import center_tutorials
class CenterTutorialTests(ApiTestCase):
def setUp(self):
super().setUp()
self.files = tempfile.TemporaryDirectory()
self.root = Path(self.files.name)
self.path_patch = patch.object(self.m, '_db_path', self.root / 'test.db')
self.path_patch.start()
def tearDown(self):
self.path_patch.stop()
self.files.cleanup()
super().tearDown()
async def upload(self, user, classroom='101', device='投影机', content=b'0123456789', name='guide.mp4'):
return await self.client.post('/api/center/tutorials', headers=auth(user['token']), data={
'classroom': classroom, 'device_name': device,
}, files={'video': (name, content, 'video/mp4')})
async def test_upload_search_stream_and_owner_delete(self):
owner = await register(self.client, 'tutorialowner')
other = await register(self.client, 'tutorialother')
self.assertEqual((await self.client.get('/api/center/tutorials')).status_code, 401)
response = await self.upload(owner)
self.assertEqual(response.status_code, 200, response.text)
self.assertEqual((await self.upload(owner, name='guide.exe')).status_code, 422)
self.assertEqual((await self.upload(owner, classroom=' ', device='')).status_code, 422)
items = (await self.client.get('/api/center/tutorials?q=投影', headers=auth(other['token']))).json()['items']
self.assertEqual(len(items), 1)
tutorial = items[0]
self.assertEqual(tutorial['classroom'], '101')
self.assertEqual(tutorial['device_name'], '投影机')
self.assertTrue(tutorial['can_delete'] is False)
self.assertEqual((await self.client.get('/api/center/tutorials?q=202', headers=auth(other['token']))).json()['items'], [])
self.assertEqual((await self.client.delete('/api/center/tutorials/' + tutorial['id'], headers=auth(other['token']))).status_code, 403)
self.assertEqual((await self.client.get('/api/center/tutorials/' + tutorial['id'] + '/video')).status_code, 401)
playback = await self.client.post('/api/center/tutorials/' + tutorial['id'] + '/playback', headers=auth(other['token']))
self.assertEqual(playback.status_code, 200, playback.text)
url = playback.json()['url']
self.assertNotIn(owner['token'], url)
video = await self.client.get(url, headers={**auth(other['token']), 'Range': 'bytes=2-5'})
self.assertEqual(video.status_code, 206)
self.assertEqual(video.content, b'2345')
self.assertEqual(video.headers['Content-Range'], 'bytes 2-5/10')
self.assertEqual((await self.client.get(url, headers={**auth(other['token']), 'Range': 'bytes=20-25'})).status_code, 416)
async with self.sessions() as db:
row = (await db.execute(select(center_tutorials))).mappings().one()
path = self.root / 'uploads' / 'tutorials' / (row['video_key'] + '.mp4')
self.assertEqual(path.read_bytes(), b'0123456789')
self.assertEqual((await self.client.delete('/api/center/tutorials/' + tutorial['id'], headers=auth(owner['token']))).status_code, 200)
self.assertFalse(path.exists())
self.assertEqual((await self.client.get('/api/center/tutorials/' + tutorial['id'] + '/video', headers=auth(owner['token']))).status_code, 404)
async def test_video_limit_removes_partial_file(self):
user = await register(self.client, 'tutoriallimit')
with patch.object(self.m.center, 'MAX_TUTORIAL_BYTES', 5):
response = await self.upload(user)
self.assertEqual(response.status_code, 413)
self.assertEqual(list((self.root / 'uploads' / 'tutorials').glob('*')), [])