import tempfile from pathlib import Path from unittest.mock import AsyncMock, patch from sqlalchemy import insert, select, update, func from test_api import ApiTestCase, auth, register from test_center_export import package class CenterRecordTests(ApiTestCase): async def make_material(self, user): response = await self.client.post('/api/center/materials', headers=auth(user['token']), content=package()) self.assertEqual(response.status_code, 200, response.text) return response.json() async def test_public_record_has_no_participation_and_legacy_rows_do_not_affect_stats(self): m = self.m; c = self.client owner = await register(c, 'recordowner'); other = await register(c, 'recordviewer') oh = auth(owner['token']); vh = auth(other['token']) normal = (await c.post('/api/activities', headers=oh, json={'title':'普通活动', 'start_at':m.now_ms()+3600000})).json()['activity'] self.assertEqual((await c.post('/api/activities/'+normal['id']+'/claim', headers=vh)).status_code, 200) baseline = (await c.get('/api/stats/overview', headers=oh)).json() with tempfile.TemporaryDirectory() as directory, patch.object(m, '_db_path', Path(directory)/'test.db'): material = await self.make_material(owner) with patch.object(m, 'DEV_MODE', False), patch.object(m, 'NOTIFY_ENABLED', True), patch.object(m, '_notify_new_activity', new_callable=AsyncMock) as notify: response = await c.post('/api/activities', headers=oh, json={ 'title':'全员可见记录', 'start_at':m.now_ms()+3600000, 'record_type':'center', 'visibility':'private', 'capacity':1, 'material_id':material['id'], }) self.assertEqual(response.status_code, 200, response.text) notify.assert_not_called() activity = response.json()['activity']; aid = activity['id'] self.assertEqual(activity['visibility'], 'public'); self.assertIsNone(activity['capacity']) self.assertEqual(activity['participants'], []); self.assertIsNone(activity['my_participation']) self.assertEqual((await c.get('/api/activities/'+aid, headers=vh)).status_code, 200) for method, suffix, body in [('POST','/claim',{}),('POST','/leave',{}),('PATCH','/participants/'+other['user']['id'],{'status':'attended'}),('DELETE','/participants/'+other['user']['id'],{})]: result = await c.request(method, '/api/activities/'+aid+suffix, headers=vh, json=body) self.assertEqual(result.status_code, 409, result.text) edited = await c.patch('/api/activities/'+aid, headers=oh, json={'visibility':'private','capacity':2}) self.assertEqual(edited.json()['activity']['visibility'], 'public') self.assertIsNone(edited.json()['activity']['capacity']) # 旧版遗留的私密状态、参加记录及邮件链接也不能重新产生参加行为。 token = 'legacy-center-link' async with self.sessions() as db: self.assertIsNone(await m._require_participant(db, aid, owner['user']['id'])) await db.execute(insert(m.participations).values(id=m.new_id(),activity_id=aid,user_id=other['user']['id'],role='member',status='attended',note='历史备注',joined_at=m.now_ms(),updated_at=m.now_ms())) await db.execute(update(m.activities).where(m.activities.c.id==aid).values(visibility='private')) await db.execute(insert(m.join_links).values(id=m.new_id(),token=m.token_digest(token),activity_id=aid,user_id=owner['user']['id'],expires_at=m.now_ms()+86400000,used=0,created_at=m.now_ms())) await db.commit() for method in ('GET','POST'): page = await c.request(method, '/join/'+token) self.assertIn('仅作记录', page.text); self.assertNotIn('