简化邮箱换绑流程,登录后仅验证新邮箱

This commit is contained in:
2026-11-06 15:49:31 +08:00
parent e096294955
commit b32ef79384
5 changed files with 36 additions and 57 deletions
+5 -8
View File
@@ -5,11 +5,9 @@ from test_api import ApiTestCase, TEST_PASSWORD, auth, get_code, register
class ChangeEmailTests(ApiTestCase):
async def codes(self, user, email):
headers = auth(user['token'])
old = await self.client.post('/api/auth/email/old-code', headers=headers)
new = await self.client.post('/api/auth/email/new-code', headers=headers, json={'email': email})
self.assertEqual(old.status_code, 200, old.text)
self.assertEqual(new.status_code, 200, new.text)
return {'email': email, 'old_code': old.json()['dev_code'], 'new_code': new.json()['dev_code']}
return {'email': email, 'new_code': new.json()['dev_code']}
async def test_change_preserves_identity_and_current_session_but_revokes_other_devices(self):
user = await register(self.client, 'emailowner')
@@ -31,16 +29,15 @@ class ChangeEmailTests(ApiTestCase):
self.assertFalse(rows)
async def test_unauthenticated_same_email_and_occupied_email_rejected(self):
self.assertEqual((await self.client.post('/api/auth/email/old-code')).status_code, 401)
self.assertEqual((await self.client.post('/api/auth/email/new-code', json={'email': 'x@example.test'})).status_code, 401)
self.assertEqual((await self.client.post('/api/auth/email/change', json={'email': 'x@example.test', 'old_code': '123456', 'new_code': '123456'})).status_code, 401)
self.assertEqual((await self.client.post('/api/auth/email/change', json={'email': 'x@example.test', 'new_code': '123456'})).status_code, 401)
owner = await register(self.client, 'sameemail')
other = await register(self.client, 'occupiedemail')
for email in [owner['user']['email'].upper(), other['user']['email'].upper()]:
r = await self.client.post('/api/auth/email/new-code', headers=auth(owner['token']), json={'email': email})
self.assertEqual(r.status_code, 409)
async def test_wrong_new_code_does_not_consume_valid_old_code(self):
async def test_wrong_new_code_can_retry_with_correct_code(self):
owner = await register(self.client, 'retryemail')
payload = await self.codes(owner, 'retry-new@example.test')
bad = '000000' if payload['new_code'] != '000000' else '111111'
@@ -77,7 +74,7 @@ class ChangeEmailTests(ApiTestCase):
async def test_code_attempt_limit_is_persistent(self):
owner = await register(self.client, 'attemptemail')
payload = await self.codes(owner, 'attempt-new@example.test')
bad = '000000' if payload['old_code'] != '000000' else '111111'
bad = '000000' if payload['new_code'] != '000000' else '111111'
for _ in range(5):
self.assertEqual((await self.client.post('/api/auth/email/change', headers=auth(owner['token']), json={**payload, 'old_code': bad})).status_code, 400)
self.assertEqual((await self.client.post('/api/auth/email/change', headers=auth(owner['token']), json={**payload, 'new_code': bad})).status_code, 400)
self.assertEqual((await self.client.post('/api/auth/email/change', headers=auth(owner['token']), json=payload)).status_code, 429)