Skip to content

Commit

Permalink
feat: Added event for subsidy redemption reversal on the bus
Browse files Browse the repository at this point in the history
  • Loading branch information
irfanuddinahmad committed Apr 5, 2024
1 parent 47f009c commit d87b437
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 7 deletions.
10 changes: 10 additions & 0 deletions enterprise_subsidy/apps/transaction/signals/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
41 changes: 41 additions & 0 deletions enterprise_subsidy/apps/transaction/tests/test_signal_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -16,6 +17,9 @@ class TransactionSignalHandlerTestCase(TestCase):
"""
Tests for the transaction signal handlers
"""
def setUp(self): # pylint: disable=arguments-differ
super().setUp()
self.receiver_called = False

@mock.patch('enterprise_subsidy.apps.api_client.base_oauth.OAuthAPIClient', return_value=mock.MagicMock())
def test_transaction_reversed_signal_handler_catches_event(self, mock_oauth_client):
Expand Down Expand Up @@ -46,3 +50,40 @@ 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):
"""
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.assertEqual(
event_receiver.call_args.kwargs['redemption'].subsidy_identifier,
transaction.subsidy_access_policy_uuid
)
self.assertEqual(
event_receiver.call_args.kwargs['redemption'].content_key,
transaction.content_key
)
self.assertEqual(
event_receiver.call_args.kwargs['redemption'].lms_user_id,
transaction.lms_user_id
)
2 changes: 1 addition & 1 deletion requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion requirements/doc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion requirements/production.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion requirements/quality.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion requirements/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion requirements/validation.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d87b437

Please sign in to comment.