Skip to content

Commit

Permalink
fix: add missing SOAPAction HTTP header
Browse files Browse the repository at this point in the history
To SageX3 Soap call add missing `SOAPAction` HTTP header.
fixes #273
  • Loading branch information
igobranco committed Mar 11, 2024
1 parent a2416ba commit 8724333
Show file tree
Hide file tree
Showing 3 changed files with 294 additions and 238 deletions.
2 changes: 1 addition & 1 deletion apps/billing/services/processor_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def send_transaction_to_processor(self) -> dict:
response = requests.post(
url=self.__processor_url,
data=self.data,
headers={"Content-type": "application/xml"},
headers={"Content-type": "application/xml", "SOAPAction": ""},
auth=(
self.__user_processor_auth,
self.__user_processor_password,
Expand Down
258 changes: 258 additions & 0 deletions apps/billing/tests/test_sagex3_processor_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
import xml.etree.ElementTree as ET
from datetime import datetime

from django.test import TestCase, override_settings

from apps.billing.factories import TransactionFactory
from apps.billing.models import Transaction
from apps.billing.services.processor_service import SageX3Processor


class SageX3ProcessDataTest(TestCase):
"""
A test case for the SageX3Processor data property.
"""

@staticmethod
def _get_xml_element_from_transaction(transaction: Transaction) -> ET.Element:
xml = SageX3Processor(transaction).data
root = ET.fromstring(xml) # nosec
object_xml: str = root.findall(".//*/objectXml")[0].text.strip()
return ET.fromstring(object_xml) # nosec

def test_data_processor_transaction_id(self):
"""
Test the SageX3Processor for transaction_id field.
"""
transaction_id = "NAU00653"
object_xml_root: ET.Element = self.__class__._get_xml_element_from_transaction(
TransactionFactory(transaction_id=transaction_id)
)
self.assertEqual(object_xml_root.findtext(".//*/FLD[@NAME='INVREF']"), transaction_id)

def test_data_processor_transaction_date(self):
"""
Test the SageX3Processor for transaction_date field.
"""
transaction_date = datetime.now()
object_xml_root: ET.Element = self.__class__._get_xml_element_from_transaction(
TransactionFactory(transaction_date=transaction_date)
)
self.assertEqual(object_xml_root.findtext(".//*/FLD[@NAME='INVDAT']"), transaction_date.strftime("%Y-%m-%d"))

@override_settings(GEOGRAPHIC_ACTIVITY_VACBPR_FIELD="XPTO")
def test_data_processor_geographic_activity_override(self):
"""
Test the SageX3Processor for geographic activity field.
"""
transaction_date = datetime.now()
object_xml_root: ET.Element = self.__class__._get_xml_element_from_transaction(
TransactionFactory(transaction_date=transaction_date)
)
self.assertEqual(object_xml_root.findtext(".//*/FLD[@NAME='VACBPR']"), "XPTO")

def test_data_processor_geographic_activity_default(self):
"""
Test the SageX3Processor for geographic activity field.
"""
transaction_date = datetime.now()
object_xml_root: ET.Element = self.__class__._get_xml_element_from_transaction(
TransactionFactory(transaction_date=transaction_date)
)
self.assertEqual(object_xml_root.findtext(".//*/FLD[@NAME='VACBPR']"), "CON")

def test_data_processor_vat_identification_country_france(self):
"""
Test the SageX3Processor for vat identification country field for france
"""
object_xml_root: ET.Element = self.__class__._get_xml_element_from_transaction(
TransactionFactory(vat_identification_country="FR")
)
self.assertEqual(object_xml_root.findtext(".//*/FLD[@NAME='YCRY']"), "FR")

def test_data_processor_vat_identification_country_portugal(self):
"""
Test the SageX3Processor for vat identification country field for portugal
"""
object_xml_root: ET.Element = self.__class__._get_xml_element_from_transaction(
TransactionFactory(vat_identification_country="PT")
)
self.assertEqual(object_xml_root.findtext(".//*/FLD[@NAME='YCRY']"), "PT")

def test_data_processor_vat_identification_country_portugal_alpha3(self):
"""
Test the SageX3Processor for vat identification country field for portugal
"""
object_xml_root: ET.Element = self.__class__._get_xml_element_from_transaction(
TransactionFactory(vat_identification_country="PRT")
)
self.assertEqual(object_xml_root.findtext(".//*/FLD[@NAME='YCRY']"), "PT")

def test_data_processor_vat_identification_country_none(self):
"""
Test the SageX3Processor for vat identification country field.
"""
object_xml_root: ET.Element = self.__class__._get_xml_element_from_transaction(
TransactionFactory(vat_identification_country=None)
)
self.assertEqual(object_xml_root.find(".//*/FLD[@NAME='YCRY']").text, None)

def test_data_processor_country_code_portugal(self):
"""
Test the SageX3Processor for country code field.
"""
object_xml_root: ET.Element = self.__class__._get_xml_element_from_transaction(
TransactionFactory(country_code="PT")
)
self.assertEqual(object_xml_root.findtext(".//*/FLD[@NAME='YCRYNAM']"), "PT")

def test_data_processor_country_code_great_britain_alpha3(self):
"""
Test the SageX3Processor for country code field.
"""
object_xml_root: ET.Element = self.__class__._get_xml_element_from_transaction(
TransactionFactory(country_code="GBR")
)
self.assertEqual(object_xml_root.findtext(".//*/FLD[@NAME='YCRYNAM']"), "GB")

def test_data_processor_country_code_none(self):
"""
Test the SageX3Processor for country code field.
"""
object_xml_root: ET.Element = self.__class__._get_xml_element_from_transaction(
TransactionFactory(country_code=None)
)
self.assertEqual(object_xml_root.find(".//*/FLD[@NAME='YCRYNAM']").text, None)

def test_data_processor_postal_code_portugal(self):
"""
Test the SageX3Processor for country code field.
"""
object_xml_root: ET.Element = self.__class__._get_xml_element_from_transaction(
TransactionFactory(postal_code="1249-068")
)
self.assertEqual(object_xml_root.findtext(".//*/FLD[@NAME='YPOSCOD']"), "1249068")

def test_data_processor_postal_code_france(self):
"""
Test the SageX3Processor for country code field.
"""
object_xml_root: ET.Element = self.__class__._get_xml_element_from_transaction(
TransactionFactory(postal_code="93600")
)
self.assertEqual(object_xml_root.findtext(".//*/FLD[@NAME='YPOSCOD']"), "93600")

def test_data_processor_postal_code_none(self):
"""
Test the SageX3Processor for country code field.
"""
object_xml_root: ET.Element = self.__class__._get_xml_element_from_transaction(
TransactionFactory(postal_code=None)
)
self.assertEqual(object_xml_root.find(".//*/FLD[@NAME='YPOSCOD']").text, None)

def test_data_processor_vat_identification_number_corporate(self):
"""
Test the SageX3Processor for country code field.
"""
object_xml_root: ET.Element = self.__class__._get_xml_element_from_transaction(
TransactionFactory(vat_identification_number=503904040)
)
self.assertEqual(object_xml_root.findtext(".//*/FLD[@NAME='YBPIEECNUM']"), "503904040")

def test_data_processor_email(self):
"""
Test the SageX3Processor for email field.
"""
object_xml_root: ET.Element = self.__class__._get_xml_element_from_transaction(
TransactionFactory(email="[email protected]")
)
self.assertEqual(object_xml_root.findtext(".//*/FLD[@NAME='YILINKMAIL']"), "[email protected]")

def test_data_processor_email_none(self):
"""
Test the SageX3Processor for email field.
"""
object_xml_root: ET.Element = self.__class__._get_xml_element_from_transaction(TransactionFactory(email=None))
self.assertEqual(object_xml_root.find(".//*/FLD[@NAME='YILINKMAIL']").text, None)

def test_data_processor_transaction_type_credit(self):
"""
Test the SageX3Processor for transaction_type field.
"""
object_xml_root: ET.Element = self.__class__._get_xml_element_from_transaction(
TransactionFactory(transaction_type="credit")
)
self.assertEqual(object_xml_root.findtext(".//*/FLD[@NAME='YPAM']"), "credit")

def test_data_processor_transaction_type_none(self):
"""
The transaction_type is a required field.
"""
with self.assertRaises(Exception):
self.__class__._get_xml_element_from_transaction(TransactionFactory(transaction_type=None))

def test_data_processor_transaction_type_strange(self):
"""
Test the SageX3Processor for transaction_type field.
"""
object_xml_root: ET.Element = self.__class__._get_xml_element_from_transaction(
TransactionFactory(transaction_type="strange")
)
self.assertEqual(object_xml_root.findtext(".//*/FLD[@NAME='YPAM']"), "strange")

def test_data_processor_client_name(self):
"""
Test the SageX3Processor for email field.
"""
object_xml_root: ET.Element = self.__class__._get_xml_element_from_transaction(
TransactionFactory(client_name="John Snow")
)
self.assertEqual(object_xml_root.findtext(".//*/LST[@NAME='YBPRNAM']/ITM"), "John Snow")

def test_data_processor_client_name_none(self):
"""
Test the SageX3Processor for email field.
"""
object_xml_root: ET.Element = self.__class__._get_xml_element_from_transaction(
TransactionFactory(client_name=None)
)
self.assertEqual(object_xml_root.find(".//*/LST[@NAME='YBPRNAM']/ITM").text, None)

def test_data_processor_address_line_1(self):
"""
Test the SageX3Processor for address_line_1 field.
"""
object_xml_root: ET.Element = self.__class__._get_xml_element_from_transaction(
TransactionFactory(address_line_1="Estrada Nacional nº1")
)
self.assertEqual(object_xml_root.findall(".//*/LST[@NAME='YBPAADDLIG']/ITM")[0].text, "Estrada Nacional nº1")

def test_data_processor_address_line_1_none(self):
"""
Test the SageX3Processor for address_line_1 field.
"""
object_xml_root: ET.Element = self.__class__._get_xml_element_from_transaction(
TransactionFactory(address_line_1=None)
)
self.assertEqual(object_xml_root.find(".//*/LST[@NAME='YBPAADDLIG']/ITM").text, None)

def test_data_processor_address_line_2(self):
"""
Test the SageX3Processor for address_line_2 field.
"""
object_xml_root: ET.Element = self.__class__._get_xml_element_from_transaction(
TransactionFactory(address_line_2="Uma localidade")
)
self.assertEqual(object_xml_root.findall(".//*/LST[@NAME='YBPAADDLIG']/ITM")[1].text, "Uma localidade")

def test_data_processor_address_line_2_none(self):
"""
Test the SageX3Processor for address_line_2 field.
"""
object_xml_root: ET.Element = self.__class__._get_xml_element_from_transaction(
TransactionFactory(address_line_2=None)
)
self.assertFalse(object_xml_root.findall(".//*/LST[@NAME='YBPAADDLIG']/ITM")[1].text)

# TODO test each items
Loading

0 comments on commit 8724333

Please sign in to comment.