Files
dtf-system/tests/launch_reset_test.py
Cauê Faleiros 8cc35c100f
Some checks failed
Build and deploy / Validate source (push) Successful in 6s
Build and deploy / Publish images (push) Has been cancelled
Build and deploy / Secret scan and release gate (push) Has been cancelled
Build and deploy / Integration suite on a real stack (push) Has been cancelled
fix: the launch cleanup leaves the Site videos' files alone
Marcus, 2026-11-16: the whole Site tab stays. The cleanup marked every
upload expired, the files behind the Site's videos and captions too; it
now skips the uploads site_videos points at, and counts only what it
removes.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-11-16 16:53:21 -03:00

99 lines
5.8 KiB
Python

"""The launch cleanup removes test orders and refuses when real money was paid.
It empties the order tables, so it runs last. Run inside the API container:
python -m tests.launch_reset_test
"""
import tempfile
from pathlib import Path
from uuid import uuid4
from psycopg.types.json import Jsonb
from app.core import db
from ops import launch_reset
def run():
quote, order, owner = uuid4(), uuid4(), uuid4()
with db.connect() as c:
c.execute('''INSERT INTO dtf_local.quotes(id,owner,request_key,request_hash,draft,approved,approved_at)
VALUES(%s,%s,%s,'reset-fixture',%s,%s,now())''',
(quote, owner, uuid4(), Jsonb({'customer': {}, 'items': []}), Jsonb({'items': [], 'total_cents': 1490})))
c.execute("INSERT INTO dtf_local.orders(id,quote_id,owner,snapshot,payment,origin) VALUES(%s,%s,%s,%s,%s,'site')",
(order, quote, owner, Jsonb({'items': [], 'total_cents': 1490}), Jsonb({'provider': 'teste'})))
live = c.execute('''INSERT INTO dtf_local.payment_intents(id,quote_id,provider,provider_payment_id,method,
status,amount_cents,response) VALUES(%s,%s,'mercadopago',%s,'card','approved',1490,%s) RETURNING id''',
(uuid4(), quote, str(uuid4()), Jsonb({'live_mode': True}))).fetchone()['id']
# A Site video's file stays: the whole Site tab is kept (Marcus, 2026-11-16).
video_upload, video_id = uuid4(), None
with db.connect() as c:
taken = {row['id'] for row in c.execute('SELECT id FROM dtf_local.site_videos').fetchall()}
video_id = next(name for name in ('retirada', 'sala', 'nota', 'avulsas', 'subindo', 'png') if name not in taken)
c.execute('''INSERT INTO dtf_local.uploads(id,owner,name,size,object_key,multipart_id,complete,expires_at)
VALUES(%s,%s,'reset-video.mp4',1,%s,'',true,now()+interval '30 days')''',
(video_upload, owner, f'reset-test/{video_upload}.mp4'))
c.execute("INSERT INTO dtf_local.site_videos(id,upload_id) VALUES(%s,%s)", (video_id, video_upload))
lines = []
# Real money paid: nothing happens, even when confirmed.
assert launch_reset.main(['--confirmar', 'APAGAR'], out=lines.append) == 1
assert 'dinheiro real' in lines[-1]
with db.connect() as c:
c.execute("UPDATE dtf_local.payment_intents SET response='{\"live_mode\": false}' WHERE id=%s", (live,))
# Without the word, it only counts.
lines.clear()
assert launch_reset.main([], out=lines.append) == 0
assert 'Nada foi apagado' in lines[-1]
with db.connect() as c:
assert c.execute('SELECT 1 FROM dtf_local.orders WHERE id=%s', (order,)).fetchone()
with db.connect() as c:
before_number = c.execute('SELECT COALESCE(max(number),0) AS n FROM dtf_local.orders').fetchone()['n']
lines.clear()
assert launch_reset.main(['--confirmar', 'APAGAR'], out=lines.append) == 0, lines
with db.connect() as c:
for table in ('orders', 'quotes', 'payment_intents', 'movements', 'outbox'):
assert c.execute(f'SELECT count(*) AS n FROM dtf_local.{table}').fetchone()['n'] == 0, table
assert not c.execute('''SELECT 1 FROM dtf_local.uploads WHERE purged_at IS NULL
AND expires_at>now() AND id<>%s''', (video_upload,)).fetchone()
assert c.execute('SELECT expires_at>now() AS kept FROM dtf_local.uploads WHERE id=%s',
(video_upload,)).fetchone()['kept']
c.execute('DELETE FROM dtf_local.site_videos WHERE id=%s', (video_id,))
c.execute('DELETE FROM dtf_local.uploads WHERE id=%s', (video_upload,))
assert c.execute('SELECT count(*) AS n FROM dtf_local.operators').fetchone()['n'] >= 1
assert any('Apagado.' in line for line in lines) and 'continua de onde parou' in lines[-1], lines
# The numbering is not reset: DTF-1…DTF-15 already exist in Tiny.
with db.connect() as c:
quote = uuid4()
c.execute('''INSERT INTO dtf_local.quotes(id,owner,request_key,request_hash,draft,approved,approved_at)
VALUES(%s,%s,%s,'reset-fixture',%s,%s,now())''',
(quote, owner, uuid4(), Jsonb({'customer': {}, 'items': []}), Jsonb({'items': [], 'total_cents': 1490})))
first = c.execute('''INSERT INTO dtf_local.orders(id,quote_id,owner,snapshot,payment,origin)
VALUES(%s,%s,%s,%s,%s,'site') RETURNING number''', (uuid4(), quote, owner, Jsonb({'items': []}),
Jsonb({'provider': 'teste'}))).fetchone()['number']
assert first > before_number, (first, before_number)
c.execute('DELETE FROM dtf_local.orders WHERE quote_id=%s', (quote,))
c.execute('DELETE FROM dtf_local.quotes WHERE id=%s', (quote,))
# The Kanban folders: only the flow folders are emptied; Estampas stays.
with tempfile.TemporaryDirectory() as temp:
root = Path(temp)
for name in ('1. Arte recebida', '2. Arte conferida', '3. Imprimindo',
'4. Revisão', '5. Finalizado', 'Recibos', 'Revisões'):
(root / name).mkdir()
(root / name / 'card.txt').write_text('x')
(root / 'Estampas').mkdir()
(root / 'Estampas' / '1001.png').write_bytes(b'art')
lines.clear()
assert launch_reset.main(['--root', temp, '--confirmar', 'APAGAR'], out=lines.append) == 0, lines
for name in ('1. Arte recebida', '2. Arte conferida', '3. Imprimindo',
'4. Revisão', '5. Finalizado', 'Recibos', 'Revisões'):
assert list((root / name).iterdir()) == [], name
assert (root / 'Estampas' / '1001.png').read_bytes() == b'art'
print('PASS: launch cleanup refuses real payments, counts first, then removes test orders and quotes, keeping the numbering, the Site videos\' files and the art library')
if __name__ == '__main__':
run()