feat: an account brings the orders bought without it with its CPF/CNPJ and e-mail
All checks were successful
Build and deploy / Validate source (push) Successful in 8s
Build and deploy / Integration suite on a real stack (push) Successful in 4m44s
Build and deploy / Secret scan and release gate (push) Successful in 10s
Build and deploy / Publish images (push) Successful in 1m22s

At signup (once the e-mailed code confirms the address), on an e-mail
change and at every sign-in, a confirmed account takes over the guest
orders placed with the same CPF/CNPJ and the same e-mail, from any browser,
with their carts and files. A CPF or CNPJ alone, which others may know,
brings nothing; neither does an account whose e-mail was never confirmed.
Accounts already confirmed by a code are marked as such.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-10-16 15:35:07 -03:00
parent 4a2ce72764
commit 1a7736dc8b
4 changed files with 109 additions and 12 deletions

View File

@@ -310,9 +310,64 @@ def alerts(tag):
return order['id'], quote['id']
def guest_order(document, address, made):
"""An order bought without an account, in a browser of its own."""
browser = Client()
browser.call('/session')
uid = upload_bytes(browser, b'GUEST ORDER ARTWORK')
item = item_spec('file', '1.2', 0, uid)
quote = browser.call('/quotes', {'request_key': str(uuid4()), 'items': [item],
'customer': {'cnpj': document, 'zap': '16999990000', 'mail': address},
'freight': {'service': 'pickup'}})
approved_quote(browser, quote, [item])
order = browser.call('/orders/dev-paid', {'quote_id': quote['id']})
made.append((order['id'], quote['id']))
return order['id'], uid
def claimed_orders(tag, made):
"""A confirmed account brings the orders bought without it with its CPF or
CNPJ and e-mail, from any browser; a CPF alone or an unconfirmed e-mail
brings nothing."""
cpf, address = '52998224725', f'comprador-{tag}@example.test'
mine, art = guest_order('529.982.247-25', address.upper(), made)
other_mail, _ = guest_order(cpf, f'outro-{tag}@example.test', made)
other_document, _ = guest_order(CNPJ, address, made)
# Someone who knows the CPF, with an e-mail of their own: nothing.
stranger = Client()
stranger.call('/session')
register(stranger, {'cnpj': cpf, 'zap': '16999990000', 'mail': f'curioso-{tag}@example.test'}, PASSWORD)
assert stranger.call('/customer/orders?page=1&size=10')['items'] == []
# The customer, on another computer: the order comes with its cart and files.
customer = Client()
customer.call('/session')
register(customer, {'cnpj': cpf, 'zap': '16999990000', 'mail': address}, PASSWORD)
listed = lambda client: {e['id'] for e in client.call('/customer/orders?page=1&size=10')['items'] if e['kind'] == 'order'}
assert listed(customer) == {mine}, listed(customer)
customer.call('/customer/orders/' + mine)
with db.connect() as c:
account = c.execute('SELECT id,email_verified FROM dtf_local.accounts WHERE email=%s', (address,)).fetchone()
assert account['email_verified']
assert c.execute('SELECT owner FROM dtf_local.uploads WHERE id=%s', (art,)).fetchone()['owner'] == account['id']
owners = {r['id']: r['owner'] for r in c.execute('SELECT id,owner FROM dtf_local.orders WHERE id=ANY(%s)',
([other_mail, other_document],)).fetchall()}
assert account['id'] not in owners.values()
# Bought later without signing in, elsewhere: it comes at the next sign-in.
later, _ = guest_order(cpf, address, made)
Client().call('/account/login', {'email': address, 'password': PASSWORD})
assert listed(customer) == {mine, later}
# An account whose e-mail was never confirmed brings nothing.
with db.connect() as c:
c.execute('UPDATE dtf_local.accounts SET email_verified=false WHERE id=%s', (account['id'],))
unconfirmed, _ = guest_order(cpf, address, made)
Client().call('/account/login', {'email': address, 'password': PASSWORD})
assert unconfirmed not in listed(customer)
def run():
tag = secrets.token_hex(3)
order = quote = None
made = []
with db.connect() as c:
# The recovery's per-source limit would make a second run within 15 minutes fail.
c.execute('DELETE FROM dtf_local.login_attempts')
@@ -322,16 +377,17 @@ def run():
email_change(tag)
smtp_delivery(tag)
order, quote = alerts(tag)
made.append((order, quote))
claimed_orders(tag, made)
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')
'SMTP delivery and retry, alerts to who asked for them, guest orders brought by CPF/CNPJ and e-mail')
finally:
with db.connect() as c:
if order:
for table in ('movements', 'print_files'):
for order, quote in made:
for table in ('movements', 'print_files', 'order_files'):
c.execute(f'DELETE FROM dtf_local.{table} WHERE order_id=%s', (order,))
c.execute('DELETE FROM dtf_local.outbox WHERE event_key LIKE %s', (f'{order}:%',))
c.execute('DELETE FROM dtf_local.orders WHERE id=%s', (order,))
if quote:
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',))