Skip to content

Commit

Permalink
test: receipt host service
Browse files Browse the repository at this point in the history
Replace `ReceiptDocumentHostForTest` with a override settings configuration.
Improve success test with asserts on iLink GET.
  • Loading branch information
igobranco committed Jun 11, 2024
1 parent 29cf776 commit 04e5b27
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions apps/billing/tests/test_receipt_host_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from unittest import mock

from django.contrib.auth import get_user_model
from django.test import TestCase
from django.test import TestCase, override_settings
from rest_framework.authtoken.models import Token
from rest_framework.test import APIClient

Expand All @@ -12,13 +12,11 @@
from apps.billing.services.receipt_host_service import ReceiptDocumentHost


class ReceiptDocumentHostForTest(ReceiptDocumentHost):
def __init__(self) -> None:
self.__receipt_host_url = "https://receipt-fake.com/"
self.__receipt_entity_public_key = "receipt_entity_public_key"
self.__receipt_bearer_token = "Bearer token"


@override_settings(
RECEIPT_HOST_URL="https://receipt-fake.com/",
RECEIPT_BEARER_TOKEN="receipt_entity_public_key",
RECEIPT_ENTITY_PUBLIC_KEY="Bearer token",
)
class ReceiptDocumentHostTest(TestCase):
def setUp(self) -> None:
"""
Expand All @@ -29,11 +27,11 @@ def setUp(self) -> None:
user = get_user_model().objects.create(username="user_test", password="pwd_test")
self.token = Token.objects.create(user=user)
self.api_client = APIClient()
self.receipt_document_host = ReceiptDocumentHostForTest()
self.receipt_document_host = ReceiptDocumentHost()
self.transaction: Transaction = TransactionFactory.create()

@mock.patch("requests.get", lambda *args, **kargs: MockResponse(data=ILINK_RESPONSE_MOCK, status_code=200))
def test_get_document_success(self):
@mock.patch("requests.get", return_value=MockResponse(data=ILINK_RESPONSE_MOCK, status_code=200))
def test_get_document_success(self, mocked_post):
"""
This test ensures to success getting a file link providing the
transaction_id though the url.
Expand All @@ -53,6 +51,13 @@ def test_get_document_success(self):
self.assertTrue(obtained_link is not None and obtained_link.lstrip() != "")
self.assertEqual(link, obtained_link)

mocked_post.assert_called_once()
_, _, kwargs = mocked_post.mock_calls[0]
self.assertEqual("https://receipt-fake.com/", kwargs.get("url"))
self.assertDictEqual(
{"document_type": "issued", "document_number": self.transaction.document_id}, kwargs.get("params")
)

def test_get_document_transaction_not_found(self):
"""
This test ensures the transaction not found error getting a file link providing wrong
Expand Down Expand Up @@ -123,8 +128,8 @@ def test_get_document_file_not_found(self):
self.assertEqual(response.status_code, 404)
self.assertEqual(response.data["response"], "File not found")

@mock.patch("requests.get", lambda *args, **kwargs: MockResponse(UNAUTHORIZED_ILINK_RESPONSE, status_code=500))
def test_get_document_unauthorized(self):
@mock.patch("requests.get", return_value=MockResponse(UNAUTHORIZED_ILINK_RESPONSE, status_code=500))
def test_get_document_unauthorized(self, mocked_post):
"""
This test ensures that the unauthorized exception is correctly handled.
"""
Expand Down

0 comments on commit 04e5b27

Please sign in to comment.