diff --git a/apps/billing/mocks.py b/apps/billing/mocks.py index d3eb849..8a5bd61 100644 --- a/apps/billing/mocks.py +++ b/apps/billing/mocks.py @@ -1,4 +1,5 @@ import json +import sys from random import randint from django.conf import settings @@ -69,7 +70,21 @@ "identifier": "FT 1/1", "ubl_type": "invoice_representation", "hash": "pPikLsRAJEyjGmHsrAVATZuJgba7xgjf7dgJsJ85eEd8ScQbAEwG8ovh0IeP", - } + }, + { + "type": "xml", + "file": "https://ilink.acin.pt/ilinktests-api/file/sadad21dsg435fcjdsfjht43uhfdsncoijdsfds", + "identifier": "FT 1/43324", + "ubl_type": "xml_representation", + "hash": "sadad21dsg435fcjdsfjht43uhfdsncoijdsfds", + }, + { + "type": "txt", + "file": "https://ilink.acin.pt/ilinktests-api/file/5regedggfdgfdfgfdgfdgfdgfdgfdgfdgdgdf432543t", + "identifier": "ANG JJZH6HPJ-00001", + "ubl_type": "qr_code", + "hash": "5regedggfdgfdfgfdgfdgfdgfdgfdgfdgdgdf432543t", + }, ], "amounts": { "tax_amount": "4.86", @@ -518,7 +533,13 @@ def __init__(self, data, status_code): @property def content(self): data = self.data - if not isinstance(data, str): - data = json.JSONEncoder().encode(o=self.data) + # if not isinstance(data, str): + # data = json.JSONEncoder().encode(o=self.data) + if isinstance(data, dict): + data = json.dumps(data).encode(sys.getdefaultencoding()) return data + + @property + def encoding(self): + return sys.getdefaultencoding() diff --git a/apps/billing/services/receipt_host_service.py b/apps/billing/services/receipt_host_service.py index 7476d37..03ae7d6 100644 --- a/apps/billing/services/receipt_host_service.py +++ b/apps/billing/services/receipt_host_service.py @@ -1,5 +1,3 @@ -import json - import requests from django.conf import settings @@ -32,8 +30,7 @@ def get_document(self, document_id: str): }, ) self.__check_status_code(response=response) - response = response.content - response = json.JSONDecoder().decode(response) + response = response.json() document_informations = [ attachment for data in response["response"]["data"] diff --git a/apps/billing/tests/test_receipt_host_service.py b/apps/billing/tests/test_receipt_host_service.py index dbd6982..509a888 100644 --- a/apps/billing/tests/test_receipt_host_service.py +++ b/apps/billing/tests/test_receipt_host_service.py @@ -39,7 +39,6 @@ def test_get_document_success(self, mocked_post): self.api_client.credentials(HTTP_AUTHORIZATION=f"Token {self.token.key}") - link = ILINK_RESPONSE_MOCK["response"]["data"][0]["attachments"][0]["file"] response = self.api_client.get(f"/api/billing/receipt-link/{self.transaction.transaction_id}/") obtained_link = response.data["response"] @@ -49,7 +48,7 @@ def test_get_document_success(self, mocked_post): self.assertTrue(len(response.data.values())) self.assertTrue(isinstance(obtained_link, str)) self.assertTrue(obtained_link is not None and obtained_link.lstrip() != "") - self.assertEqual(link, obtained_link) + self.assertEqual(ILINK_RESPONSE_MOCK["response"]["data"][0]["attachments"][0]["file"], obtained_link) mocked_post.assert_called_once() _, _, kwargs = mocked_post.mock_calls[0] diff --git a/apps/billing/views.py b/apps/billing/views.py index f63aebd..ef6107f 100644 --- a/apps/billing/views.py +++ b/apps/billing/views.py @@ -1,3 +1,5 @@ +import logging + import requests from django.core.exceptions import ObjectDoesNotExist from rest_framework.authentication import TokenAuthentication @@ -11,6 +13,8 @@ from apps.billing.services.receipt_host_service import ReceiptDocumentHost from apps.util.base_views import GeneralPost +log = logging.getLogger(__name__) + class ProcessTransaction(APIView, GeneralPost): """ @@ -60,4 +64,5 @@ def get_receipt_link(request, *args, **kwargs): except ObjectDoesNotExist: return Response({"response": "Transaction not found"}, status=404) except Exception: + log.exception("Not expected error ocurred getting the document") return Response({"response": "A not expected error ocurred getting the document"}, status=500)