Files
worklog/tests/test_change_email.py

81 lines
5.6 KiB
Python

from sqlalchemy import select, update
from test_api import ApiTestCase, TEST_PASSWORD, auth, get_code, register
class ChangeEmailTests(ApiTestCase):
async def codes(self, user, email):
headers = auth(user['token'])
new = await self.client.post('/api/auth/email/new-code', headers=headers, json={'email': email})
self.assertEqual(new.status_code, 200, new.text)
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')
old_email = user['user']['email']
login = await self.client.post('/api/auth/login', json={'email': old_email, 'password': TEST_PASSWORD})
other_token = login.json()['token']
await get_code(self.client, old_email, 'login')
payload = await self.codes(user, ' New.Address@Example.Test ')
response = await self.client.post('/api/auth/email/change', headers=auth(user['token']), json=payload)
self.assertEqual(response.status_code, 200, response.text)
self.assertEqual(response.json()['user']['email'], 'new.address@example.test')
self.assertEqual(response.json()['user']['id'], user['user']['id'])
self.assertEqual((await self.client.get('/api/auth/me', headers=auth(user['token']))).status_code, 200)
self.assertEqual((await self.client.get('/api/auth/me', headers=auth(other_token))).status_code, 401)
self.assertEqual((await self.client.post('/api/auth/login', json={'email': old_email, 'password': TEST_PASSWORD})).status_code, 401)
self.assertEqual((await self.client.post('/api/auth/login', json={'email': 'new.address@example.test', 'password': TEST_PASSWORD})).status_code, 200)
async with self.sessions() as db:
rows = (await db.execute(select(self.m.email_verifications).where(self.m.email_verifications.c.email == old_email, self.m.email_verifications.c.used == 0))).all()
self.assertFalse(rows)
async def test_unauthenticated_same_email_and_occupied_email_rejected(self):
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', '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_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'
r = await self.client.post('/api/auth/email/change', headers=auth(owner['token']), json={**payload, 'new_code': bad})
self.assertEqual(r.status_code, 400)
r = await self.client.post('/api/auth/email/change', headers=auth(owner['token']), json=payload)
self.assertEqual(r.status_code, 200, r.text)
async def test_change_codes_are_bound_to_account_and_cannot_use_generic_bind_codes(self):
owner = await register(self.client, 'scopeowner')
other = await register(self.client, 'scopeother')
payload = await self.codes(owner, 'shared-target@example.test')
other_payload = await self.codes(other, 'shared-target@example.test')
if payload['new_code'] == other_payload['new_code']:
other_payload['new_code'] = '000000' if payload['new_code'] != '000000' else '111111'
r = await self.client.post('/api/auth/email/change', headers=auth(owner['token']), json={**payload, 'new_code': other_payload['new_code']})
self.assertEqual(r.status_code, 400)
generic = await get_code(self.client, 'shared-target@example.test', 'bind')
if generic == payload['new_code']:
generic = '000000' if payload['new_code'] != '000000' else '111111'
r = await self.client.post('/api/auth/email/change', headers=auth(owner['token']), json={**payload, 'new_code': generic})
self.assertEqual(r.status_code, 400)
r = await self.client.post('/api/auth/email/change', headers=auth(owner['token']), json=payload)
self.assertEqual(r.status_code, 200, r.text)
async def test_expired_code_is_rejected(self):
owner = await register(self.client, 'expiredemail')
payload = await self.codes(owner, 'expired-new@example.test')
async with self.sessions() as db:
await db.execute(update(self.m.email_verifications).where(self.m.email_verifications.c.email == payload['email']).values(expires_at=0))
await db.commit()
self.assertEqual((await self.client.post('/api/auth/email/change', headers=auth(owner['token']), json=payload)).status_code, 400)
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['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, '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)