From 0869cda6c8956769155994bcf5978f250a42e32f Mon Sep 17 00:00:00 2001 From: Ivo Branco Date: Thu, 4 Apr 2024 09:58:24 +0000 Subject: [PATCH] fix: not found receipt link fixes fccn/nau-technical#76 --- nau_extensions/financial_manager.py | 8 ++-- nau_extensions/tests/factories.py | 1 + .../tests/test_financial_manager.py | 47 ++++++++++++++++++- 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/nau_extensions/financial_manager.py b/nau_extensions/financial_manager.py index 29ae138..5626cd8 100644 --- a/nau_extensions/financial_manager.py +++ b/nau_extensions/financial_manager.py @@ -186,6 +186,7 @@ def get_receipt_link(order): receipt_link_url += transaction_id + '/' token = _get_financial_manager_setting(site, "token") try: + logger.info("Get receipt link for transaction id [%s]", transaction_id) response = requests.post( receipt_link_url, headers={"Authorization": token}, @@ -193,9 +194,10 @@ def get_receipt_link(order): ) except Exception as e: # pylint: disable=broad-except logger.exception("Error can't get receipt link for transaction_id [%s] error: [%s]", transaction_id, e) + return None finally: - content = response.content() - logger.info("Get receipt link status: [%d] response: [%s]", response.status_code, content) + logger.info("Received the receipt link status_code: [%d]") if response.status_code == 200: - return content + logger.info("Received the receipt link content: [%s]", response.content) + return response.content return None diff --git a/nau_extensions/tests/factories.py b/nau_extensions/tests/factories.py index 61bff00..0fa1918 100644 --- a/nau_extensions/tests/factories.py +++ b/nau_extensions/tests/factories.py @@ -47,6 +47,7 @@ def json(self): """ return self.json_data + @property def content(self): """ The Json data diff --git a/nau_extensions/tests/test_financial_manager.py b/nau_extensions/tests/test_financial_manager.py index f83172a..9dc6165 100644 --- a/nau_extensions/tests/test_financial_manager.py +++ b/nau_extensions/tests/test_financial_manager.py @@ -456,7 +456,7 @@ def test_send_to_financial_manager_without_basket_billing_information(self): json_data="https://example.com/somereceipt.pdf", status_code=200, )) - def test_get_receipt_link(self, mock_fm_receipt_link): + def test_get_receipt_link_found(self, mock_fm_receipt_link): """ Test the `get_receipt_link` method. """ @@ -489,3 +489,48 @@ def test_get_receipt_link(self, mock_fm_receipt_link): mock_fm_receipt_link.assert_called_once_with(f"https://finacial-manager.example.com/api/billing/receipt-link/{basket.order_number}/", headers={'Authorization': 'a-very-long-token'}, timeout=10) self.assertEqual(link, "https://example.com/somereceipt.pdf") + + @override_settings( + NAU_FINANCIAL_MANAGER={ + "edx": { + "receipt-link-url": "https://finacial-manager.example.com/api/billing/receipt-link/", + "token": "a-very-long-token", + }, + }, + ) + @mock.patch.object(requests, "post", return_value=MockResponse( + status_code=404, + )) + def test_get_receipt_link_not_found(self, mock_fm_receipt_link): + """ + Test the `get_receipt_link` method. + """ + partner = PartnerFactory(short_code="edX") + + site_configuration = SiteConfigurationFactory(partner=partner) + site_configuration.site = SiteFactory(name="openedx") + site = site_configuration.site + + course = CourseFactory( + id="course-v1:edX+DemoX+Demo_Course", + name="edX Demonstration Course", + partner=partner, + ) + honor_product = course.create_or_update_seat("honor", False, 0) + verified_product = course.create_or_update_seat("verified", True, 10) + + owner = UserFactory(email="ecommerce@example.com") + + # create an empty basket so we know what it's inside + basket = create_basket(owner=owner, empty=True, site=site) + basket.add_product(verified_product) + basket.add_product(honor_product) + basket.save() + + # creating an order will mark the card submitted + order = create_order(basket=basket) + + link = get_receipt_link(order) + mock_fm_receipt_link.assert_called_once_with(f"https://finacial-manager.example.com/api/billing/receipt-link/{basket.order_number}/", headers={'Authorization': 'a-very-long-token'}, timeout=10) + + self.assertEqual(link, None)