feat: track duty email confirmations and add throttled account email tests
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import asyncio
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest.mock import AsyncMock
|
||||
from server import duty_receipts as r
|
||||
|
||||
|
||||
class ReceiptsTests(unittest.IsolatedAsyncioTestCase):
|
||||
def setUp(self):
|
||||
self.tmp = tempfile.TemporaryDirectory()
|
||||
self.path = Path(self.tmp.name) / 'state.db'
|
||||
|
||||
def tearDown(self):
|
||||
self.tmp.cleanup()
|
||||
|
||||
async def test_three_attempts_and_confirmation_suppresses_reminders(self):
|
||||
names = {'同学': ['一二节']}
|
||||
send = AsyncMock()
|
||||
tokens = []
|
||||
for now in [1000, 1061, 1122]:
|
||||
self.assertTrue(await r.send_notice(self.path, '2026-11-06', names, 'a@example.test', 9999, send, '提醒', '正文', 'https://example.test', now))
|
||||
tokens.append(send.await_args.args[2].split('/duty-confirm/')[1].split()[0])
|
||||
self.assertFalse(await r.send_notice(self.path, '2026-11-06', names, 'a@example.test', 9999, send, '提醒', '正文', 'https://example.test', 1200))
|
||||
state = r.status(self.path, '2026-11-06', '一二节', '同学', 'a@example.test', 1200)
|
||||
self.assertEqual(state['sent_count'], 3)
|
||||
self.assertEqual(state['last_sent_at'], 1122000)
|
||||
# 邮件扫描 GET 不确认,POST 确认可重复调用且不改变首次确认时间。
|
||||
self.assertIsNotNone(r.confirmation(self.path, tokens[0], 1200))
|
||||
self.assertIsNone(r.status(self.path, '2026-11-06', '一二节', '同学', 'a@example.test', 1200)['confirmed_at'])
|
||||
r.confirmation(self.path, tokens[0], 1200, True)
|
||||
r.confirmation(self.path, tokens[0], 1250, True)
|
||||
self.assertEqual(r.status(self.path, '2026-11-06', '一二节', '同学', 'a@example.test', 1250)['confirmed_at'], 1200000)
|
||||
self.assertIsNone(r.confirmation(self.path, tokens[0], 10000, True))
|
||||
self.assertIsNone(r.confirmation(self.path, 'invalid', 1200, True))
|
||||
|
||||
async def test_confirmed_person_stops_early_and_concurrent_sends_cannot_bypass_limit(self):
|
||||
names = {'同学': ['一二节']}
|
||||
async def attempt():
|
||||
return await asyncio.to_thread(r.reserve, self.path, '2026-11-06', names, 'a@example.test', 9999, 1000)
|
||||
results = await asyncio.gather(*(attempt() for _ in range(5)))
|
||||
self.assertEqual(sum(bool(token) for token, _ in results), 1)
|
||||
token = next(token for token, _ in results if token)
|
||||
r.confirmation(self.path, token, 1010, True)
|
||||
self.assertEqual(r.reserve(self.path, '2026-11-06', names, 'a@example.test', 9999, 1100), (None, {}))
|
||||
self.assertIsNotNone(r.reserve(self.path, '2026-11-06', {'同学': ['三四节']}, 'a@example.test', 9999, 1100)[0])
|
||||
|
||||
async def test_test_mail_limit_is_persistent_and_case_insensitive(self):
|
||||
self.assertTrue(r.reserve_test(self.path, 'Student@example.test', 1000))
|
||||
self.assertFalse(r.reserve_test(self.path, 'student@example.test', 1119))
|
||||
self.assertTrue(r.reserve_test(self.path, 'student@example.test', 1120))
|
||||
Reference in New Issue
Block a user