Files
dtf-system/tests/label_test.py
Cauê Faleiros 223bd61798 feat: the 10x15 label at Finalizado (Marcus 10/11, point 10)
When an order reaches Finalizado, the Kanban on the computer that opted in
(Integrações > Etiqueta 10x15) prints a 10x15 label rendered by the server:
the Tiny order number in large type (DTF-<n> when there is none), customer,
DTF · N folhas · X m, the delivery line, date and time, the Revisor and a
QR with the number. One automatic print per order (label_prints, claimed
once); Revisor, Expedição, Gestão and the administrators can reprint.
Chrome with --kiosk-printing prints without the dialog.

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

100 lines
4.5 KiB
Python

"""The 10x15 thermal label at Finalizado (Fase 2, front 10).
Run inside the API container: python -m tests.label_test
"""
from urllib.error import HTTPError
from urllib.request import Request
from uuid import uuid4
from psycopg.types.json import Jsonb
from app.core import db
from tests.smoke_test import BASE, Client, with_host
from tests.users_test import PASSWORD, order_fixture
def label_page(client, order_id, expected=200):
"""GET the label as the browser does; returns the HTML (or the error body)."""
request = Request(BASE + '/api/operator/orders/' + order_id + '/label', headers=with_host())
try:
with client.opener.open(request, timeout=30) as response:
assert response.status == expected, (response.status, expected)
assert response.headers.get_content_type() == 'text/html', response.headers
return response.read().decode()
except HTTPError as error:
if error.code != expected:
raise AssertionError((error.code, expected, error.read().decode())) from error
return error.read().decode()
def user(superadmin, tag, role, name):
email = f'etiqueta-{tag}-{role}@example.test'
superadmin.call('/operator/users',
{'name': name, 'email': email, 'role': role, 'password': PASSWORD}, operator=True)
client = Client()
client.call('/session')
client.call('/operator/login', {'email': email, 'password': PASSWORD})
return client
def run():
su = Client()
su.call('/session')
su.call('/operator/me', operator=True) # the default superadmin
tag = uuid4().hex[:6]
revisor_email = f'etiqueta-{tag}-revisor@example.test'
revisor = user(su, tag, 'revisor', 'Revisor Etiqueta')
designer = user(su, tag, 'designer', 'Designer Etiqueta')
expedicao = user(su, tag, 'expedicao', 'Expedição Etiqueta')
owner = uuid4()
with db.connect() as c:
_, order_id = order_fixture(c, owner, 'file', metres='2.5')
snapshot = c.execute('SELECT snapshot FROM dtf_local.orders WHERE id=%s', (order_id,)).fetchone()['snapshot']
snapshot['customer']['name'] = 'Cliente Etiqueta'
snapshot['destination'] = {'recipient': 'Loja Etiqueta', 'street': 'Rua A', 'number': '10',
'complement': '', 'district': 'Centro', 'city': 'Franca',
'state': 'SP', 'postal_code': '14400000'}
snapshot['freight'] = {'service': 'jadlog', 'description': 'Jadlog .Package', 'total_cents': 1500}
c.execute("UPDATE dtf_local.orders SET snapshot=%s, state='fin' WHERE id=%s",
(Jsonb(snapshot), order_id))
c.execute("INSERT INTO dtf_local.movements(order_id,from_state,to_state,operator,reason) "
"VALUES(%s,'rev','fin',%s,'')", (order_id, revisor_email))
number = c.execute('SELECT number FROM dtf_local.orders WHERE id=%s', (order_id,)).fetchone()['number']
oid = str(order_id)
# The first computer claims the automatic print; the second does not.
assert su.call(f'/operator/orders/{oid}/label/claim', {}, operator=True) == {'print': True}
assert su.call(f'/operator/orders/{oid}/label/claim', {}, operator=True) == {'print': False}
# A reprint always prints, on any computer.
assert su.call(f'/operator/orders/{oid}/label/reprint', {}, operator=True) == {'print': True}
# The page is the server's HTML: customer, what the order is, delivery, QR.
page = label_page(revisor, oid)
assert 'Cliente Etiqueta' in page, page
assert 'DTF · 1 folha · 2,50 m' in page, page
assert 'Envio · Jadlog .Package' in page, page
assert 'Revisor Etiqueta' in page, page
assert '<svg' in page, page
# No Tiny receipt: the order's own DTF number, also in the QR.
assert f'DTF-{number}' in page, page
# With the Tiny sale receipt, its number takes the big type and the QR.
with db.connect() as c:
c.execute('''INSERT INTO dtf_local.outbox(event_key,provider,payload,receipt,delivered_at)
VALUES(%s,'tiny',%s,%s,now())''', (f'{oid}:paid:tiny', Jsonb({}),
Jsonb({'tiny_number': '98765'})))
page = label_page(revisor, oid)
assert '98765' in page, page
assert f'DTF-{number}' not in page, page
# Only the label profiles print it: the designer may not, Expedição may.
label_page(designer, oid, expected=403)
label_page(expedicao, oid)
print('PASS: the label page, one automatic print, reprint, Tiny number, designer refused '
'and Expedição allowed')
if __name__ == '__main__':
run()