Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added event for subsidy redemption reversal on the bus #227

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, doesn't have to be addressed in this PR, but subsidy_identifier is a really confusing name for this key - as a casual reader of the code, I'd expect a key with that name to point to an instance of subsidy.models.Subsidy :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iloveagent57 the challenge was to come up with a reasonably generic identifier without injecting too much of our policy/budget/subsidy lingo into the event.

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):
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
Loading