Skip to content

Commit

Permalink
fix: ilink get document client
Browse files Browse the repository at this point in the history
  • Loading branch information
igobranco committed Jun 12, 2024
1 parent 800d80c commit 331033b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
27 changes: 24 additions & 3 deletions apps/billing/mocks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import sys
from random import randint

from django.conf import settings
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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()
5 changes: 1 addition & 4 deletions apps/billing/services/receipt_host_service.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import json

import requests
from django.conf import settings

Expand Down Expand Up @@ -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"]
Expand Down
3 changes: 1 addition & 2 deletions apps/billing/tests/test_receipt_host_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand All @@ -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]
Expand Down
5 changes: 5 additions & 0 deletions apps/billing/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging

import requests
from django.core.exceptions import ObjectDoesNotExist
from rest_framework.authentication import TokenAuthentication
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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)

0 comments on commit 331033b

Please sign in to comment.