feat: a new account is confirmed with a code sent to its e-mail
All checks were successful
Build and deploy / Validate source (push) Successful in 17s
Build and deploy / Integration suite on a real stack (push) Successful in 4m25s
Build and deploy / Secret scan and release gate (push) Successful in 9s
Build and deploy / Publish images (push) Successful in 1m22s
All checks were successful
Build and deploy / Validate source (push) Successful in 17s
Build and deploy / Integration suite on a real stack (push) Successful in 4m25s
Build and deploy / Secret scan and release gate (push) Successful in 9s
Build and deploy / Publish images (push) Successful in 1m22s
Anyone could open an account on someone else's e-mail. Now "Criar conta" e-mails a 6-digit code, and the account exists only once it is entered: one big field that takes a paste or the phone's autofill and confirms by itself, "Reenviar código" after 60 s, "Trocar e-mail", and the sign-in box hidden meanwhile. Security: 15 minutes per code, 5 wrong attempts, at most 5 codes, limits per address and per source; the password is hashed at the first step and the code stored only as a hash, its e-mail erased once sent. An address that already has an account gets the same answer on screen and a "você já tem conta" e-mail instead of a code, so the form does not reveal who is registered. Without e-mail (MAIL_ADAPTER=off) the account is created at once, as before. The browser test reads the code from a local-only endpoint that exists only where e-mail stays in the log. Tested: mail_test (wrong, locked, resent, expired codes, existing address, erasure after SMTP), the full suite and the browser test in a CI-style stack, and the screens in Brave on desktop and phone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -16,7 +16,7 @@ from uuid import uuid4
|
||||
|
||||
from app import mail
|
||||
from app.core import db
|
||||
from tests.smoke_test import Client, approved_quote, item_spec, upload_bytes
|
||||
from tests.smoke_test import Client, approved_quote, item_spec, register, upload_bytes
|
||||
|
||||
CNPJ = '11222333000181'
|
||||
PASSWORD = 'senha-antiga-12345'
|
||||
@@ -70,8 +70,7 @@ def recovery(tag):
|
||||
address = f'cliente-{tag}@example.test'
|
||||
customer = Client()
|
||||
assert customer.call('/session')['password_recovery'] is True
|
||||
customer.call('/account/register', {'customer': {'cnpj': CNPJ, 'zap': '16999990000', 'mail': address},
|
||||
'password': PASSWORD})
|
||||
register(customer, {'cnpj': CNPJ, 'zap': '16999990000', 'mail': address}, PASSWORD)
|
||||
# Signed in elsewhere with the old password: the reset signs that out.
|
||||
elsewhere = Client()
|
||||
elsewhere.call('/session')
|
||||
@@ -119,6 +118,78 @@ def recovery(tag):
|
||||
return address
|
||||
|
||||
|
||||
def code_of(c, address):
|
||||
body = c.execute("SELECT body FROM dtf_local.mail_queue WHERE kind='code' AND to_addr=%s ORDER BY id DESC LIMIT 1",
|
||||
(address,)).fetchone()['body']
|
||||
return re.search(r'\b(\d{6})\b', body).group(1)
|
||||
|
||||
|
||||
def signup(tag):
|
||||
"""The account exists only once the code sent to the address is entered."""
|
||||
address = f'novo-{tag}@example.test'
|
||||
customer = {'cnpj': CNPJ, 'zap': '16999990000', 'mail': address}
|
||||
browser = Client()
|
||||
browser.call('/session')
|
||||
started = browser.call('/account/register', {'customer': customer, 'password': PASSWORD})
|
||||
assert started['email'] == address and started['minutes'] == 15 and started['verify'], started
|
||||
assert browser.call('/account/me')['customer'] is None
|
||||
with db.connect() as c:
|
||||
assert not c.execute('SELECT 1 FROM dtf_local.accounts WHERE email=%s', (address,)).fetchone()
|
||||
first = code_of(c, address)
|
||||
# The password is stored hashed from the first step.
|
||||
assert c.execute('SELECT password_hash FROM dtf_local.account_signups WHERE id=%s',
|
||||
(started['verify'],)).fetchone()['password_hash'].startswith('scrypt-v2$')
|
||||
wrong = '000000' if first != '000000' else '111111'
|
||||
attempt = lambda code, expected: browser.call('/account/register/verify', {'signup': started['verify'], 'code': code},
|
||||
expected=expected)
|
||||
assert 'Restam 4' in attempt(wrong, 400)['detail']
|
||||
attempt('12a456', 422)
|
||||
# Another code only after a minute; the old one stops working.
|
||||
browser.call('/account/register/resend', {'signup': started['verify']}, expected=429)
|
||||
with db.connect() as c:
|
||||
c.execute("UPDATE dtf_local.account_signups SET sent_at=now()-interval '2 minutes' WHERE id=%s", (started['verify'],))
|
||||
browser.call('/account/register/resend', {'signup': started['verify']})
|
||||
with db.connect() as c:
|
||||
second = code_of(c, address)
|
||||
if second != first:
|
||||
attempt(first, 400)
|
||||
# Five wrong codes lock it until a new code is asked for.
|
||||
with db.connect() as c:
|
||||
c.execute('UPDATE dtf_local.account_signups SET attempts=5 WHERE id=%s', (started['verify'],))
|
||||
attempt(second, 429)
|
||||
with db.connect() as c:
|
||||
c.execute('UPDATE dtf_local.account_signups SET attempts=0 WHERE id=%s', (started['verify'],))
|
||||
assert attempt(second, 200)['customer']['mail'] == address
|
||||
assert browser.call('/account/me')['customer']['mail'] == address
|
||||
attempt(second, 410)
|
||||
Client().call('/account/login', {'email': address, 'password': PASSWORD})
|
||||
|
||||
# An address that already has an account: the same answer, a notice instead
|
||||
# of a code, and no code opens anything.
|
||||
again = Client()
|
||||
again.call('/session')
|
||||
other = again.call('/account/register', {'customer': customer, 'password': NEW_PASSWORD})
|
||||
assert set(other) == set(started) and other['email'] == address
|
||||
with db.connect() as c:
|
||||
notices = c.execute("SELECT subject FROM dtf_local.mail_queue WHERE kind='account' AND to_addr=%s",
|
||||
(address,)).fetchall()
|
||||
assert len(notices) == 1 and 'já tem conta' in notices[0]['subject']
|
||||
assert code_of(c, address) == second
|
||||
again.call('/account/register/verify', {'signup': other['verify'], 'code': second}, expected=400)
|
||||
Client().call('/account/login', {'email': address, 'password': NEW_PASSWORD}, expected=401)
|
||||
|
||||
# An expired code.
|
||||
late = Client()
|
||||
late.call('/session')
|
||||
expiring = late.call('/account/register', {'customer': {**customer, 'mail': f'tarde-{tag}@example.test'},
|
||||
'password': PASSWORD})
|
||||
with db.connect() as c:
|
||||
code = code_of(c, f'tarde-{tag}@example.test')
|
||||
c.execute("UPDATE dtf_local.account_signups SET expires_at=now()-interval '1 second' WHERE id=%s",
|
||||
(expiring['verify'],))
|
||||
late.call('/account/register/verify', {'signup': expiring['verify'], 'code': code}, expected=410)
|
||||
|
||||
|
||||
def smtp_delivery(tag):
|
||||
"""The worker's SMTP path, against a server of our own."""
|
||||
sink = Sink()
|
||||
@@ -135,17 +206,19 @@ def smtp_delivery(tag):
|
||||
VALUES(%s,%s,%s,%s,now()+interval '1 hour') RETURNING id''',
|
||||
(kind, to, subject, body)).fetchone()['id']
|
||||
for kind, subject, body in (('reset', 'Crie uma nova senha · DTF Dropstar', 'link secreto ção'),
|
||||
('alert', 'Pedido pago', 'olá equipe'))]
|
||||
('alert', 'Pedido pago', 'olá equipe'),
|
||||
('code', '123456 é o seu código', 'código 123456'))]
|
||||
mail.send_pending(db.connect, ids=ids)
|
||||
assert len(sink.messages) == 2, sink.messages
|
||||
assert len(sink.messages) == 3, sink.messages
|
||||
rcpt, first = sink.messages[0]
|
||||
assert to in rcpt[0] and first['Subject'] == 'Crie uma nova senha · DTF Dropstar'
|
||||
assert first['From'] == 'DTF Dropstar <nao-responda@dropstar.test>'
|
||||
assert first.get_content().strip() == 'link secreto ção'
|
||||
with db.connect() as c:
|
||||
sent = {r['kind']: r for r in queued(c, to, 'reset') + queued(c, to, 'alert')}
|
||||
# The recovery's text is erased once sent; an alert keeps its own.
|
||||
sent = {r['kind']: r for r in queued(c, to, 'reset') + queued(c, to, 'alert') + queued(c, to, 'code')}
|
||||
# A recovery link or a signup code is erased once sent; an alert keeps its text.
|
||||
assert sent['reset']['sent_at'] and sent['reset']['body'] == ''
|
||||
assert sent['code']['sent_at'] and sent['code']['body'] == ''
|
||||
assert sent['alert']['sent_at'] and sent['alert']['body'] == 'olá equipe'
|
||||
|
||||
# A server that does not answer: retried later, with the error kept.
|
||||
@@ -211,9 +284,11 @@ def run():
|
||||
c.execute('DELETE FROM dtf_local.login_attempts')
|
||||
try:
|
||||
recovery(tag)
|
||||
signup(tag)
|
||||
smtp_delivery(tag)
|
||||
order, quote = alerts(tag)
|
||||
print('PASS: password recovery by a one-time link, SMTP delivery and retry, alerts to who asked for them')
|
||||
print('PASS: password recovery by a one-time link, signup confirmed by an e-mailed code, '
|
||||
'SMTP delivery and retry, alerts to who asked for them')
|
||||
finally:
|
||||
with db.connect() as c:
|
||||
if order:
|
||||
@@ -225,6 +300,7 @@ def run():
|
||||
c.execute('DELETE FROM dtf_local.payment_intents WHERE quote_id=%s', (quote,))
|
||||
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.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')
|
||||
|
||||
Reference in New Issue
Block a user