feat: a new account e-mail is confirmed with a code, and the old one is told

Changing the e-mail in Dados da conta took effect at once, with nothing
checking the new address: a typo locked the customer out, and anyone at a
signed-in computer who knew the password could move the account to their own
e-mail and then reset its password. Now the code goes to the new address, the
account changes only once it is entered (same screen and limits as signup,
and only the account that asked can complete it), and the old address gets
"O e-mail da sua conta foi trocado", with the WhatsApp to call if it was not
them. Without e-mail on, it changes at once as before.

Signup and e-mail change share one code check (expiry, five attempts, the
60 s resend wait). Tested: mail_test (wrong password, same and taken e-mail,
wrong code, another account's attempt, resend wait, the notice), the full
suite in a CI-style stack, and the screen in Brave.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-10-14 16:32:46 -03:00
parent 95be624cca
commit 7383e44ff4
9 changed files with 217 additions and 39 deletions

View File

@@ -190,6 +190,40 @@ def signup(tag):
late.call('/account/register/verify', {'signup': expiring['verify'], 'code': code}, expected=410)
def email_change(tag):
"""A new e-mail is the account's only once the code sent to it is entered,
and the old address is told."""
old, new, taken = (f'{n}-{tag}@example.test' for n in ('antigo', 'trocado', 'ocupado'))
owner = Client()
owner.call('/session')
register(owner, {'cnpj': CNPJ, 'zap': '16999990000', 'mail': old}, PASSWORD)
register(Client(), {'cnpj': CNPJ, 'zap': '16999990000', 'mail': taken}, PASSWORD)
owner.call('/account/email', {'email': new, 'password': 'senha-errada-123'}, expected=401)
owner.call('/account/email', {'email': old, 'password': PASSWORD}, expected=422)
owner.call('/account/email', {'email': taken, 'password': PASSWORD}, expected=409)
started = owner.call('/account/email', {'email': new.upper(), 'password': PASSWORD})
assert started['email'] == new and started['verify'], started
assert owner.call('/account/me')['customer']['mail'] == old
with db.connect() as c:
code = code_of(c, new)
wrong = '000000' if code != '000000' else '111111'
assert 'Restam 4' in owner.call('/account/email/verify', {'change': started['verify'], 'code': wrong}, expected=400)['detail']
# Another signed-in customer cannot complete someone else's change.
stranger = Client()
stranger.call('/session')
register(stranger, {'cnpj': CNPJ, 'zap': '16999990000', 'mail': f'outro-{tag}@example.test'}, PASSWORD)
stranger.call('/account/email/verify', {'change': started['verify'], 'code': code}, expected=410)
owner.call('/account/email/resend', {'change': started['verify']}, expected=429)
assert owner.call('/account/email/verify', {'change': started['verify'], 'code': code})['customer']['mail'] == new
assert owner.call('/account/me')['customer']['mail'] == new
owner.call('/account/email/verify', {'change': started['verify'], 'code': code}, expected=410)
Client().call('/account/login', {'email': new, 'password': PASSWORD})
Client().call('/account/login', {'email': old, 'password': PASSWORD}, expected=401)
with db.connect() as c:
[notice] = queued(c, old, 'notice')
assert new in notice['body'] and 'Se não foi você' in notice['body']
def smtp_delivery(tag):
"""The worker's SMTP path, against a server of our own."""
sink = Sink()
@@ -285,9 +319,10 @@ def run():
try:
recovery(tag)
signup(tag)
email_change(tag)
smtp_delivery(tag)
order, quote = alerts(tag)
print('PASS: password recovery by a one-time link, signup confirmed by an e-mailed code, '
print('PASS: password recovery by a one-time link, signup and e-mail changes confirmed by an e-mailed code, '
'SMTP delivery and retry, alerts to who asked for them')
finally:
with db.connect() as c:
@@ -301,6 +336,7 @@ def run():
c.execute('DELETE FROM dtf_local.quotes WHERE id=%s', (quote,))
c.execute("DELETE FROM dtf_local.mail_queue WHERE to_addr LIKE %s OR kind='test'", (f'%-{tag}@example.test',))
c.execute('DELETE FROM dtf_local.account_signups WHERE email LIKE %s', (f'%-{tag}@example.test',))
c.execute('DELETE FROM dtf_local.email_changes WHERE new_email LIKE %s', (f'%-{tag}@example.test',))
c.execute('DELETE FROM dtf_local.accounts WHERE email LIKE %s', (f'%-{tag}@example.test',))
c.execute('DELETE FROM dtf_local.operators WHERE email LIKE %s', (f'%-{tag}@example.test',))
c.execute('DELETE FROM dtf_local.login_attempts')