From 80747f4ccd0aea19884b9baa6bca518d478eaad1 Mon Sep 17 00:00:00 2001 From: irfanuddinahmad Date: Tue, 2 Apr 2024 17:52:23 +0500 Subject: [PATCH] feat: Added event for subsidy redemption reversal on the bus --- .../apps/transaction/signals/handlers.py | 10 ++++++ .../transaction/tests/test_signal_handlers.py | 34 +++++++++++++++++++ requirements/base.txt | 2 +- requirements/dev.txt | 2 +- requirements/doc.txt | 2 +- requirements/production.txt | 2 +- requirements/quality.txt | 2 +- requirements/test.txt | 2 +- requirements/validation.txt | 2 +- 9 files changed, 51 insertions(+), 7 deletions(-) diff --git a/enterprise_subsidy/apps/transaction/signals/handlers.py b/enterprise_subsidy/apps/transaction/signals/handlers.py index c856f0de..53083b9c 100644 --- a/enterprise_subsidy/apps/transaction/signals/handlers.py +++ b/enterprise_subsidy/apps/transaction/signals/handlers.py @@ -5,6 +5,8 @@ import requests from django.dispatch import receiver +from openedx_events.enterprise.data import SubsidyRedemption +from openedx_events.enterprise.signals import SUBSIDY_REDEMPTION_REVERSED from openedx_ledger.signals.signals import TRANSACTION_REVERSED from enterprise_subsidy.apps.api_client.enterprise import EnterpriseApiClient @@ -28,6 +30,14 @@ def listen_for_transaction_reversal(sender, **kwargs): raise ValueError(msg) try: EnterpriseApiClient().cancel_fulfillment(transaction.fulfillment_identifier) + subsidy_redemption = SubsidyRedemption( + subsidy_identifier = transaction.subsidy_access_policy_uuid, + content_key = transaction.content_key, + lms_user_id = transaction.lms_user_id + ) + SUBSIDY_REDEMPTION_REVERSED.send_event( + redemption=subsidy_redemption, + ) except requests.exceptions.HTTPError as exc: error_msg = f"Error canceling platform fulfillment {transaction.fulfillment_identifier}: {exc}" logger.exception(error_msg) diff --git a/enterprise_subsidy/apps/transaction/tests/test_signal_handlers.py b/enterprise_subsidy/apps/transaction/tests/test_signal_handlers.py index 99c4ade4..d5926bc6 100644 --- a/enterprise_subsidy/apps/transaction/tests/test_signal_handlers.py +++ b/enterprise_subsidy/apps/transaction/tests/test_signal_handlers.py @@ -5,6 +5,7 @@ import pytest from django.test import TestCase +from openedx_events.enterprise.signals import SUBSIDY_REDEMPTION_REVERSED from openedx_ledger.signals.signals import TRANSACTION_REVERSED from openedx_ledger.test_utils.factories import LedgerFactory, ReversalFactory, TransactionFactory @@ -46,3 +47,36 @@ def test_transaction_reversed_signal_without_fulfillment_identifier(self, mock_o TRANSACTION_REVERSED.send(sender=self, reversal=reversal) assert mock_oauth_client.return_value.post.call_count == 0 + + def _event_receiver_side_effect(self, **kwargs): # pylint: disable=unused-argument + """ + Used show that the Open edX Event was called by the Django signal handler. + """ + self.receiver_called = True + + @mock.patch('enterprise_subsidy.apps.api_client.base_oauth.OAuthAPIClient', return_value=mock.MagicMock()) + def test_transaction_reversed_signal_handler_produces_redemption_reversal_event(self, mock_oauth_client): + """ + Test that the transaction reversed signal handler produces the redemption reversal event on the bus + """ + mock_oauth_client.return_value.post.return_value = MockResponse({}, 201) + ledger = LedgerFactory() + transaction = TransactionFactory( + ledger=ledger, + quantity=100, + fulfillment_identifier='foobar', + subsidy_access_policy_uuid='d13d16c8-d299-40f8-9bb1-19bf12b2af72' + ) + reversal = ReversalFactory(transaction=transaction) + event_receiver = mock.Mock(side_effect=self._event_receiver_side_effect) + SUBSIDY_REDEMPTION_REVERSED.connect(event_receiver) + TRANSACTION_REVERSED.send(sender=self, reversal=reversal) + self.assertTrue(self.receiver_called) + self.assertDictContainsSubset( + { + subsidy_identifier: transaction.subsidy_access_policy_uuid, + content_key: transaction.content_key, + lms_user_id: transaction.lms_user_id + }, + event_receiver.call_args.kwargs + ) diff --git a/requirements/base.txt b/requirements/base.txt index 266c6069..89b377f7 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -173,7 +173,7 @@ oauthlib==3.2.2 # getsmarter-api-clients # requests-oauthlib # social-auth-core -openedx-events==9.5.2 +openedx-events==9.7.0 # via # -r requirements/base.in # openedx-ledger diff --git a/requirements/dev.txt b/requirements/dev.txt index b230215d..a9770632 100644 --- a/requirements/dev.txt +++ b/requirements/dev.txt @@ -360,7 +360,7 @@ oauthlib==3.2.2 # getsmarter-api-clients # requests-oauthlib # social-auth-core -openedx-events==9.5.2 +openedx-events==9.7.0 # via # -r requirements/validation.txt # openedx-ledger diff --git a/requirements/doc.txt b/requirements/doc.txt index 9f7b4f24..3dbc390d 100644 --- a/requirements/doc.txt +++ b/requirements/doc.txt @@ -344,7 +344,7 @@ oauthlib==3.2.2 # getsmarter-api-clients # requests-oauthlib # social-auth-core -openedx-events==9.5.2 +openedx-events==9.7.0 # via # -r requirements/test.txt # openedx-ledger diff --git a/requirements/production.txt b/requirements/production.txt index 857c9d6e..d6a6e04f 100644 --- a/requirements/production.txt +++ b/requirements/production.txt @@ -211,7 +211,7 @@ oauthlib==3.2.2 # getsmarter-api-clients # requests-oauthlib # social-auth-core -openedx-events==9.5.2 +openedx-events==9.7.0 # via # -r requirements/base.txt # openedx-ledger diff --git a/requirements/quality.txt b/requirements/quality.txt index f2fd53e6..45538249 100644 --- a/requirements/quality.txt +++ b/requirements/quality.txt @@ -323,7 +323,7 @@ oauthlib==3.2.2 # getsmarter-api-clients # requests-oauthlib # social-auth-core -openedx-events==9.5.2 +openedx-events==9.7.0 # via # -r requirements/test.txt # openedx-ledger diff --git a/requirements/test.txt b/requirements/test.txt index 08ebe49d..93f8c823 100644 --- a/requirements/test.txt +++ b/requirements/test.txt @@ -261,7 +261,7 @@ oauthlib==3.2.2 # getsmarter-api-clients # requests-oauthlib # social-auth-core -openedx-events==9.5.2 +openedx-events==9.7.0 # via # -r requirements/base.txt # openedx-ledger diff --git a/requirements/validation.txt b/requirements/validation.txt index 3f62b839..278d9bf6 100644 --- a/requirements/validation.txt +++ b/requirements/validation.txt @@ -415,7 +415,7 @@ oauthlib==3.2.2 # getsmarter-api-clients # requests-oauthlib # social-auth-core -openedx-events==9.5.2 +openedx-events==9.7.0 # via # -r requirements/quality.txt # -r requirements/test.txt