Skip to content

Commit

Permalink
fix(x3): encode xml post to SageX3 on utf-8
Browse files Browse the repository at this point in the history
  • Loading branch information
igobranco committed Jul 12, 2024
1 parent 1e74e95 commit 5644807
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
9 changes: 6 additions & 3 deletions apps/billing/services/processor_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@


class SageX3Processor(TransactionProcessorInterface):

ENCODING = "utf-8"

"""
This class is a transaction processor. It means that by implementing the `TransactionProcessorInterface` type,
it can be replaced by another implementation.
Expand Down Expand Up @@ -48,7 +51,7 @@ def send_transaction_to_processor(self) -> dict:
response = requests.post(
url=self.__processor_url,
data=self.data,
headers={"Content-type": "text/xml; charset=UTF-8", "SOAPAction": "''"},
headers={"Content-type": f"text/xml; charset={self.ENCODING}", "SOAPAction": "''"},
auth=(
self.__user_processor_auth,
self.__user_processor_password,
Expand Down Expand Up @@ -89,7 +92,7 @@ def data(self) -> str:
It uses memoization to prevent the generation of data multiple times unnecessary.
"""
if not self.__data:
self.__data = self.__generate_data()
self.__data = self.__generate_data().encode(self.ENCODING, "ignore")
return self.__data

def __generate_data(self) -> str:
Expand Down Expand Up @@ -175,7 +178,7 @@ def __generate_data(self) -> str:
<requestConfig xsi:type="xsd:string">adxwss.beautify=true</requestConfig>
</callContext>
<publicName xsi:type="xsd:string">YWSSIH</publicName>
<objectXml xsi:type="xsd:string"><![CDATA[<?xml version="1.0" encoding="utf-8" ?>
<objectXml xsi:type="xsd:string"><![CDATA[<?xml version="1.0" encoding="{self.ENCODING}" ?>
{objectXML}]]></objectXml>
</wss:save>
</soapenv:Body>
Expand Down
10 changes: 9 additions & 1 deletion apps/billing/tests/test_sagex3_processor_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,21 @@ def test_data_processor_transaction_type_strange(self):

def test_data_processor_client_name(self):
"""
Test the SageX3Processor for email field.
Test the SageX3Processor for client name 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_special_character(self):
"""
Test the SageX3Processor for client name field with special character.
This tests if the xml is encoded on utf-8.
"""
xml = SageX3Processor(TransactionFactory(client_name="Ana Rosário Maria")).data
self.assertIn(b"Ana Ros\xc3\xa1rio Maria", xml)

def test_data_processor_client_name_none(self):
"""
Test the SageX3Processor for email field.
Expand Down
2 changes: 1 addition & 1 deletion apps/billing/tests/test_sagex3_processor_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_send_transaction_to_processor_header_content_type(self, mock_data, mock
SageX3Processor(None).send_transaction_to_processor()
_, kwargs = mock_post.call_args
called_headers = kwargs["headers"]
self.assertEqual("text/xml; charset=UTF-8", called_headers["Content-type"])
self.assertEqual("text/xml; charset=utf-8", called_headers["Content-type"])

@mock.patch("requests.post", return_value=MockResponse(data="", status_code=200))
@mock.patch("apps.billing.services.processor_service.SageX3Processor.data", side_effect=lambda: {"some": "thing"})
Expand Down

0 comments on commit 5644807

Please sign in to comment.