Files
dtf-system/tests/test_cnpj.py
Cauê Faleiros b3c5f63553 feat: customers show by their company's name, taken from the CNPJ registry
The Site asks for e-mail, CNPJ and WhatsApp only, so the Kanban showed an
e-mail where a name belongs and a new Tiny contact was named after it. The
worker now looks up each customer's CNPJ in the public registry (BrasilAPI,
no key) a few at a time, never while a customer waits, and keeps the razão
social and trade name in dtf_local.cnpj_names; a failed lookup is retried a
day later. Cards, the order panel, quotes and the search results show the
trade name (else the razão social) above the e-mail, the search finds
orders and quotes by either name, and a new Tiny contact gets the razão
social as its name and the trade name as fantasia.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 12:19:35 -03:00

39 lines
1.7 KiB
Python

"""The CNPJ registry lookup against a fake BrasilAPI. Runs where httpx is installed."""
import unittest
import httpx
from app import cnpj
def transport(status, body=None):
return httpx.MockTransport(lambda request: httpx.Response(status, json=body or {}))
class CnpjTests(unittest.TestCase):
def test_names_come_from_the_registry_tidied(self):
found = cnpj.fetch('11222333000181', transport(200, {
'razao_social': 'LOJA TESTE COMERCIO LTDA ', 'nome_fantasia': 'Loja Teste'}))
self.assertEqual(found, {'legal_name': 'LOJA TESTE COMERCIO LTDA', 'trade_name': 'Loja Teste'})
self.assertEqual(cnpj.fetch('11222333000181', transport(200, {'razao_social': 'X LTDA', 'nome_fantasia': ''})),
{'legal_name': 'X LTDA', 'trade_name': None})
def test_an_unknown_cnpj_has_no_names_and_an_outage_is_an_error(self):
self.assertEqual(cnpj.fetch('00000000000000', transport(404)), {'legal_name': None, 'trade_name': None})
with self.assertRaises(httpx.HTTPError):
cnpj.fetch('11222333000181', transport(503))
def test_new_tiny_contacts_are_named_after_the_company(self):
from app.tiny import contact_payload
order = {'customer': {'cnpj': '11222333000181', 'mail': 'loja@example.test', 'zap': '16999999999'},
'destination': None}
named = contact_payload(order, {'legal': 'LOJA TESTE LTDA', 'trade': 'Loja Teste'})
self.assertEqual((named['nome'], named['fantasia']), ('LOJA TESTE LTDA', 'Loja Teste'))
plain = contact_payload(order)
self.assertEqual(plain['nome'], 'loja@example.test')
self.assertNotIn('fantasia', plain)
if __name__ == '__main__':
unittest.main()