"""A redesign's preview: sent, then approved or rejected, stopping the clock.""" from uuid import uuid4 from psycopg.types.json import Jsonb from app.core import db from tests.smoke_test import Client from tests.users_test import order_fixture def run(): client = Client() client.call('/session') owner = uuid4() quote = order = None try: with db.connect() as c: quote, order = order_fixture(c, owner, 'file') c.execute("""UPDATE dtf_local.orders SET snapshot = snapshot || %s, state='rec', payment=%s WHERE id=%s""", (Jsonb({'redesign': {'arts': 1, 'cents': 1990}}), Jsonb({'provider': 'mercadopago', 'id': str(order)}), order)) path = f'/operator/orders/{order}/redesign-preview' # Nothing to approve before a preview was sent. answer = client.call(path, {'action': 'approved'}, operator=True, expected=409) assert answer['detail'] == 'Nenhuma prévia com o cliente.', answer # The first preview opens. client.call(path, {'action': 'sent'}, operator=True) mine = next(o for o in client.call('/operator/board', operator=True)['orders'] if o['id'] == str(order)) assert len(mine['previews']) == 1 and mine['previews'][0]['number'] == 1, mine['previews'] assert mine['previews'][0]['decided_at'] is None, mine['previews'] # Only one preview waits at a time. answer = client.call(path, {'action': 'sent'}, operator=True, expected=409) assert answer['detail'] == 'Já há uma prévia com o cliente.', answer # Rejected with its reason, spaces tidied. client.call(path, {'action': 'rejected', 'reason': ' motivo teste '}, operator=True) mine = next(o for o in client.call('/operator/board', operator=True)['orders'] if o['id'] == str(order)) preview = mine['previews'][0] assert preview['result'] == 'rejected' and preview['reason'] == 'motivo teste', preview assert preview['decided_at'] and preview['decided_by'], preview # A second preview follows. client.call(path, {'action': 'sent'}, operator=True) mine = next(o for o in client.call('/operator/board', operator=True)['orders'] if o['id'] == str(order)) assert [p['number'] for p in mine['previews']] == [1, 2], mine['previews'] assert mine['previews'][1]['decided_at'] is None, mine['previews'] # Three hours in Arte recebida, two of them waiting for the customer: # the stage clock shows about one. with db.connect() as c: c.execute("UPDATE dtf_local.orders SET created_at=now()-interval '3 hours' WHERE id=%s", (order,)) c.execute("""UPDATE dtf_local.redesign_previews SET sent_at=now()-interval '2 hours' WHERE order_id=%s AND decided_at IS NULL""", (order,)) mine = next(o for o in client.call('/operator/board', operator=True)['orders'] if o['id'] == str(order)) assert abs(mine['state_hours'] - 1) < 0.2, mine['state_hours'] # Approved ends the previews; no new one is sent. client.call(path, {'action': 'approved'}, operator=True) answer = client.call(path, {'action': 'sent'}, operator=True, expected=409) assert answer['detail'] == 'A prévia já foi aprovada.', answer # An order without the redesign has no previews. with db.connect() as c: c.execute("UPDATE dtf_local.orders SET snapshot = snapshot - 'redesign' WHERE id=%s", (order,)) answer = client.call(path, {'action': 'sent'}, operator=True, expected=409) assert answer['detail'] == 'Este pedido não tem Ajuste Fino Redesenho.', answer print('PASS: a redesign preview opens, closes with a reason, reopens, ' 'stops the stage clock and never reopens after approval') finally: if order is not None: with db.connect() as c: c.execute('DELETE FROM dtf_local.orders WHERE id=%s', (order,)) c.execute('DELETE FROM dtf_local.quotes WHERE id=%s', (quote,)) if __name__ == '__main__': run()