test_edi_xml.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from base64 import b64encode
  4. from datetime import datetime
  5. from freezegun import freeze_time
  6. from lxml import etree
  7. from odoo.addons.l10n_es_edi_tbai.models.xml_utils import NS_MAP
  8. from odoo.tests import tagged
  9. from .common import TestEsEdiTbaiCommon
  10. @tagged('post_install', '-at_install', 'post_install_l10n')
  11. class TestEdiTbaiXmls(TestEsEdiTbaiCommon):
  12. @classmethod
  13. def setUpClass(cls):
  14. super().setUpClass()
  15. cls.out_invoice = cls.env['account.move'].create({
  16. 'name': 'INV/01',
  17. 'move_type': 'out_invoice',
  18. 'invoice_date': datetime.now(),
  19. 'partner_id': cls.partner_a.id,
  20. 'invoice_line_ids': [(0, 0, {
  21. 'product_id': cls.product_a.id,
  22. 'price_unit': 1000.0,
  23. 'quantity': 5,
  24. 'discount': 20.0,
  25. 'tax_ids': [(6, 0, cls._get_tax_by_xml_id('s_iva21b').ids)],
  26. })],
  27. })
  28. cls.edi_format = cls.env.ref('l10n_es_edi_tbai.edi_es_tbai')
  29. def test_xml_tree_post(self):
  30. with freeze_time(self.frozen_today):
  31. xml_doc = self.edi_format._get_l10n_es_tbai_invoice_xml(self.out_invoice, cancel=False)[self.out_invoice]['xml_file']
  32. xml_doc.remove(xml_doc.find("Signature", namespaces=NS_MAP))
  33. xml_expected = etree.fromstring(super().L10N_ES_TBAI_SAMPLE_XML_POST)
  34. self.assertXmlTreeEqual(xml_doc, xml_expected)
  35. def test_xml_tree_cancel(self):
  36. self.out_invoice.l10n_es_tbai_post_xml = b64encode(b"""<TicketBAI>
  37. <CabeceraFactura><FechaExpedicionFactura>01-01-2022</FechaExpedicionFactura></CabeceraFactura>
  38. <ds:SignatureValue xmlns:ds="http://www.w3.org/2000/09/xmldsig#">TEXT</ds:SignatureValue>
  39. </TicketBAI>""") # hack to set out_invoice's registration date
  40. xml_doc = self.edi_format._get_l10n_es_tbai_invoice_xml(self.out_invoice, cancel=True)[self.out_invoice]['xml_file']
  41. xml_doc.remove(xml_doc.find("Signature", namespaces=NS_MAP))
  42. xml_expected = etree.fromstring(super().L10N_ES_TBAI_SAMPLE_XML_CANCEL)
  43. self.assertXmlTreeEqual(xml_doc, xml_expected)