Skip to content

Commit

Permalink
fix: Added tests for content assignments in credits_available endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
IrfanUddinAhmad committed Oct 31, 2023
1 parent 39f0e29 commit 7778432
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -538,11 +538,15 @@ def get_assignments_serializer(self, obj):

@extend_schema_field(serializers.IntegerField)
def get_remaining_balance_per_user(self, obj):
"""Returns the remaining balance per user"""
lms_user_id = self.context.get('lms_user_id')
return obj.remaining_balance_per_user(lms_user_id=lms_user_id)
if hasattr(obj, 'remaining_balance_per_user'):
return obj.remaining_balance_per_user(lms_user_id=lms_user_id)
return None

@extend_schema_field(serializers.IntegerField)
def get_remaining_balance(self, obj):
"""Returns the remaining balance for the policy"""
return obj.subsidy_balance()


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
from rest_framework import status
from rest_framework.reverse import reverse

from enterprise_access.apps.content_assignments.tests.factories import AssignmentConfigurationFactory
from enterprise_access.apps.content_assignments.constants import LearnerContentAssignmentStateChoices
from enterprise_access.apps.content_assignments.tests.factories import (
AssignmentConfigurationFactory,
LearnerContentAssignmentFactory
)
from enterprise_access.apps.core.constants import (
SYSTEM_ENTERPRISE_ADMIN_ROLE,
SYSTEM_ENTERPRISE_LEARNER_ROLE,
Expand Down Expand Up @@ -1321,6 +1325,70 @@ def test_credits_available_endpoint(
# with an inactive (i.e., expired, not yet started) subsidy, we should get no records back.
assert len(response_json) == 0

@mock.patch('enterprise_access.apps.subsidy_access_policy.models.get_and_cache_transactions_for_learner')
@mock.patch('enterprise_access.apps.subsidy_access_policy.models.SubsidyAccessPolicy.subsidy_record')
def test_credits_available_endpoint_with_content_assignments(
self,
mock_subsidy_record,
mock_transactions_cache_for_learner # pylint: disable=unused-argument
):
"""
Verify that SubsidyAccessPolicyViewset credits_available returns learner content assignments for assigned
learner credit access policies.
"""
content_key = 'demoX'
content_title = 'edx: Demo 101'
content_price_cents = 100
# Create a pair of AssignmentConfiguration + SubsidyAccessPolicy for the main test customer.
assignment_configuration = AssignmentConfigurationFactory(
enterprise_customer_uuid=self.enterprise_uuid,
)
AssignedLearnerCreditAccessPolicyFactory(
display_name='An assigned learner credit policy, for the test customer.',
enterprise_customer_uuid=self.enterprise_uuid,
active=True,
assignment_configuration=assignment_configuration,
spend_limit=1000000,
)
assignment1 = LearnerContentAssignmentFactory.create(
assignment_configuration=assignment_configuration,
learner_email='alice@foo.com',
lms_user_id=1234,
content_key=content_key,
content_title=content_title,
content_quantity=-content_price_cents,
state=LearnerContentAssignmentStateChoices.ALLOCATED,
)
LearnerContentAssignmentFactory.create(
assignment_configuration=assignment_configuration,
learner_email='bob@foo.com',
lms_user_id=12345,
content_key=content_key,
content_title=content_title,
content_quantity=-content_price_cents,
state=LearnerContentAssignmentStateChoices.ACCEPTED,
)
mock_subsidy_record.return_value = {
'uuid': str(uuid4()),
'title': 'Test Subsidy',
'enterprise_customer_uuid': str(self.enterprise_uuid),
'expiration_datetime': '2030-01-01 12:00:00Z',
'active_datetime': '2020-01-01 12:00:00Z',
'current_balance': '5000',
'is_active': True,
}
self.lms_client_instance.enterprise_contains_learner.return_value = True

query_params = {
'enterprise_customer_uuid': str(self.enterprise_uuid),
'lms_user_id': 1234,
}
response = self.client.get(self.subsidy_access_policy_credits_available_endpoint, query_params)

response_json = response.json()
self.assertEqual(len(response_json[0]['learner_content_assignments']), 1)
self.assertEqual(response_json[0]['learner_content_assignments'][0]['uuid'], str(assignment1.uuid))


@ddt.ddt
class TestSubsidyAccessPolicyCanRedeemView(APITestWithMocks):
Expand Down

0 comments on commit 7778432

Please sign in to comment.