Files
dtf-system/tests/order_actions_test.py
Cauê Faleiros 05da6d78ad
All checks were successful
Build and deploy / Validate source (push) Successful in 5s
Build and deploy / Integration suite on a real stack (push) Successful in 2m44s
Build and deploy / Secret scan and release gate (push) Successful in 5s
Build and deploy / Publish images (push) Successful in 54s
feat: discard an unpaid quote, delete a test order, cancel a paid one
Cotações (now subtitled "pedidos ainda não pagos") can discard a quote: it
leaves every list and the search, and a payment for it is refused like an
expired one. A test order can be deleted with its history; its quote is unpaid
again. A paid order is cancelled with a required reason: it leaves the board
for a "Cancelados" chip, keeps its payment and history, can no longer move,
and the customer's account shows it as cancelled. Refunds stay at Mercado
Pago and Tiny is not told; the cancel form says so.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 16:20:27 -03:00

78 lines
4.2 KiB
Python

"""Discarding an unpaid quote, deleting a test order, cancelling a paid one.
Run inside the API container: python -m tests.order_actions_test
"""
from urllib.parse import urlencode
from uuid import uuid4
from psycopg.types.json import Jsonb
from app import payments
from app.core import db
from tests.smoke_test import Client
def run():
client = Client()
owner = uuid4()
mail = f'actions-{uuid4().hex[:8]}@example.test'
approved = {'customer': {'mail': mail}, 'items': [], 'total_cents': 1490}
discard, test_quote, paid_quote, paid_order = uuid4(), uuid4(), uuid4(), uuid4()
test_order = None
try:
with db.connect() as c:
for qid in (discard, test_quote, paid_quote):
c.execute('''INSERT INTO dtf_local.quotes(id,owner,request_key,request_hash,draft,approved,approved_at)
VALUES(%s,%s,%s,'actions-fixture',%s,%s,now())''',
(qid, owner, uuid4(), Jsonb({'customer': {'mail': mail}, 'items': []}), Jsonb(approved)))
c.execute('''INSERT INTO dtf_local.orders(id,quote_id,owner,snapshot,payment) VALUES(%s,%s,%s,%s,%s)''',
(paid_order, paid_quote, owner, Jsonb(approved), Jsonb({'provider': 'mercadopago', 'id': '1'})))
listed = lambda: {q['id'] for q in client.call('/operator/quotes?' + urlencode({'kind': 'approved', 'q': mail}),
operator=True)['quotes']}
assert {str(discard), str(test_quote)} <= listed()
# Discarded: gone from the list, and no payment can make it an order.
client.call(f'/operator/quotes/{discard}/discard', {}, operator=True)
assert str(discard) not in listed()
with db.connect() as c:
try:
payments.approved_quote(c, discard)
raise AssertionError('a discarded quote was payable')
except payments.PaymentRefused as refusal:
assert str(refusal) == 'quote was discarded'
client.call(f'/operator/quotes/{paid_quote}/discard', {}, operator=True, expected=409)
# A test order is deleted, and its quote is unpaid again.
test_order = client.call(f'/operator/quotes/{test_quote}/test-order', {}, operator=True)['id']
assert str(test_quote) not in listed()
client.call(f'/operator/orders/{test_order}/delete-test', {}, operator=True)
with db.connect() as c:
assert not c.execute('SELECT 1 FROM dtf_local.orders WHERE id=%s', (test_order,)).fetchone()
test_order = None
assert str(test_quote) in listed()
# A paid order is cancelled with a reason, kept, and can no longer move.
client.call(f'/operator/orders/{paid_order}/delete-test', {}, operator=True, expected=409)
client.call(f'/operator/orders/{paid_order}/cancel', {'note': 'x'}, operator=True, expected=422)
client.call(f'/operator/orders/{paid_order}/cancel', {'note': 'Cliente desistiu'}, operator=True)
board = client.call('/operator/board', operator=True)
order = next(o for o in board['orders'] if o['id'] == str(paid_order))
assert order['cancelled_at'] and order['cancel_reason'] == 'Cliente desistiu'
client.call(f'/operator/orders/{paid_order}/move', {'state': 'tra', 'version': order['version'], 'reason': ''},
operator=True, expected=409)
client.call(f'/operator/orders/{paid_order}/cancel', {'note': 'de novo'}, operator=True, expected=404)
print('PASS: discard hides and blocks a quote; test orders delete; paid orders cancel and stay')
finally:
with db.connect() as c:
for oid in filter(None, (test_order, paid_order)):
for table in ('movements', 'order_files', 'print_files'):
c.execute(f'DELETE FROM dtf_local.{table} WHERE order_id=%s', (oid,))
c.execute('DELETE FROM dtf_local.outbox WHERE event_key LIKE %s', (f'{oid}:%',))
c.execute('DELETE FROM dtf_local.orders WHERE id=%s', (oid,))
c.execute('DELETE FROM dtf_local.orders WHERE quote_id=ANY(%s)', ([discard, test_quote, paid_quote],))
c.execute('DELETE FROM dtf_local.quotes WHERE id=ANY(%s)', ([discard, test_quote, paid_quote],))
if __name__ == '__main__':
run()