"""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 '