from datetime import date, datetime from unittest.mock import create_autospec from django.test import TestCase from vbv_lernwelt.core.admin import User from vbv_lernwelt.core.create_default_users import create_default_users from vbv_lernwelt.shop.invoice.abacus import ( AbacusInvoiceCreator, create_customer_xml, create_invoice_xml, render_customer_xml, render_invoice_xml, ) from vbv_lernwelt.shop.invoice.repositories import InvoiceRepository from vbv_lernwelt.shop.models import CheckoutInformation from vbv_lernwelt.shop.tests.factories import CheckoutInformationFactory USER_USERNAME = "testuser" USER_EMAIL = "test@example.com" USER_PASSWORD = "testpassword" class AbacusInvoiceTestCase(TestCase): def setUp(self): create_default_users() def test_create_invoice_xml(self): # set abacus_number before _pat = User.objects.get(username="patrizia.huggel@eiger-versicherungen.ch") _pat.abacus_debitor_number = 60000011 _pat.save() _ignore_checkout_information = CheckoutInformationFactory( user=_pat, abacus_order_id=6_000_000_123 ) feuz = User.objects.get(username="andreas.feuz@eiger-versicherungen.ch") feuz_checkout_info = CheckoutInformationFactory( user=feuz, transaction_id="24021508331287484", ) feuz_checkout_info.created_at = datetime(2024, 2, 15, 8, 33, 12, 0) invoice_xml_filename, invoice_xml_content = create_invoice_xml( feuz_checkout_info ) self.assertEqual( invoice_xml_filename, "myVBV_orde_60000012_20240215083312_6000000124.xml" ) # print(invoice_xml_content) assert "60000012" in invoice_xml_content assert ( "2024-02-15" in invoice_xml_content ) assert ( "6000000124" in invoice_xml_content ) assert ( "24021508331287484" in invoice_xml_content ) assert "2024-02-15" in invoice_xml_content assert ( "Versicherungsvermittler/-in VBV - 2024-02-15 - Feuz Andreas" in invoice_xml_content ) def test_render_invoice_xml(self): invoice_xml = render_invoice_xml( abacus_debitor_number=60000012, abacus_order_id=6000000001, order_date=date(2024, 2, 15), datatrans_transaction_id="24021508331287484", item_description="myVBV Versicherungsvermittler - Lernpfad, 2024-02-15, Skender, Gebhart-Krasniqi", ) # example from Skender expected_xml = """ ORDE Verkaufsauftrag AbaDefault 2022.00 60000012 30 9999 2024-02-15 24021508331287484 6000000001 2024-02-15 1 30202 1 myVBV Versicherungsvermittler - Lernpfad, 2024-02-15, Skender, Gebhart-Krasniqi """ self.maxDiff = None self.assertXMLEqual(expected_xml, invoice_xml) def test_create_customer_xml_with_company_address(self): _pat = User.objects.get(username="patrizia.huggel@eiger-versicherungen.ch") _pat.abacus_debitor_number = 60000011 _pat.save() _ignore_checkout_information = CheckoutInformationFactory( user=_pat, abacus_order_id=6_000_000_123 ) feuz = User.objects.get(username="andreas.feuz@eiger-versicherungen.ch") feuz_checkout_info = CheckoutInformationFactory( user=feuz, transaction_id="24021508331287484", first_name="Andreas", last_name="Feuz", street="Eggersmatt", street_number="32", postal_code="1719", city="Zumholz", country="209", company_name="VBV", company_street="Laupenstrasse", company_street_number="10", company_postal_code="3000", company_city="Bern", company_country="209", ) feuz_checkout_info.created_at = datetime(2024, 2, 15, 8, 33, 12, 0) customer_xml_filename, customer_xml_content = create_customer_xml( checkout_information=feuz_checkout_info ) print(customer_xml_content) print(customer_xml_filename) self.assertEqual("myVBV_debi_60000012.xml", customer_xml_filename) assert "60000012" in customer_xml_content assert ( "andreas.feuz@eiger-versicherungen.ch" in customer_xml_content ) assert "VBV" in customer_xml_content # FIXME refactor country assert "209" in customer_xml_content def test_render_customer_xml(self): customer_xml = render_customer_xml( abacus_debitor_number=60000012, last_name="Gebhart-Krasniqi", first_name="Skender", company_name="VBV", street="Laupenstrasse", house_number="10", zip_code="3000", city="Bern", country="CH", language="de", email="skender.krasniqi@vbv-afa.ch", ) # example from Skender expected_xml = """ DEBI Kunden AbaDefault 2022.00 60000012 CHF 1 NORM 60000012 Gebhart-Krasniqi Skender VBV Laupenstrasse 10 3000 Bern CH de skender.krasniqi@vbv-afa.ch """ self.maxDiff = None self.assertXMLEqual(expected_xml, customer_xml) def test_create_invoice_calls_upload(self): # GIVEN repository_mock = create_autospec(InvoiceRepository) creator = AbacusInvoiceCreator(repository=repository_mock) expected_filename = "test.xml" checkout_information = CheckoutInformation.objects.create( user=self.user, transaction_id="12345", product_sku="001", product_name="Test Product", product_description="Test Product Description", product_price=1000, state="initialized", ) # WHEN creator.create_invoice( checkout_information=checkout_information, filename=expected_filename, ) # THEN repository_mock.upload_invoice.assert_called_once() uploaded_invoice, uploaded_filename = repository_mock.upload_invoice.call_args[ 0 ] assert uploaded_filename == expected_filename assert "12345" in uploaded_invoice assert "1" in uploaded_invoice assert "001" in uploaded_invoice assert "1" in uploaded_invoice assert "Test Product Description" in uploaded_invoice