From 467ff12b61c8f4bd686767524d93a4ac161b8c2e Mon Sep 17 00:00:00 2001 From: Adam Stankiewicz Date: Tue, 3 Sep 2024 12:42:11 -0400 Subject: [PATCH] chore: update --- .../apps/api/v1/tests/test_allocation_view.py | 34 +++++++++++-------- .../apps/content_assignments/api.py | 16 +++++++-- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/enterprise_access/apps/api/v1/tests/test_allocation_view.py b/enterprise_access/apps/api/v1/tests/test_allocation_view.py index 82b1e43b..75c8badd 100644 --- a/enterprise_access/apps/api/v1/tests/test_allocation_view.py +++ b/enterprise_access/apps/api/v1/tests/test_allocation_view.py @@ -443,6 +443,18 @@ def setUpTestData(cls): spend_limit=10000 * 100, ) + # Mock results from the catalog content metadata API endpoint. + cls.mock_catalog_result = { + 'count': 2, + 'results': [ + { + 'key': cls.content_key, + 'parent_content_key': cls.parent_content_key, + 'data': 'things', + }, + ], + } + def setUp(self): super().setUp() self.maxDiff = None @@ -461,14 +473,6 @@ def setUp(self): def delete_assignments(): return self.assignment_configuration.assignments.all().delete() - # Mock results from the catalog content metadata API endpoint. - self.mock_catalog_result = { - 'count': 2, - 'results': [ - {'key': 'course+A', 'data': 'things'}, {'key': 'course+B', 'data': 'stuff'}, - ], - } - self.addCleanup(delete_assignments) @mock.patch.object( @@ -559,6 +563,7 @@ def test_allocate_happy_path_e2e( email='expired@foo.com', lms_user_id=4277 ) + assignment_content_quantity_usd_cents = 12345 LearnerContentAssignmentFactory( assignment_configuration=self.assignment_configuration, learner_email='retired-assignment@foo.com', @@ -569,7 +574,7 @@ def test_allocate_happy_path_e2e( parent_content_key=self.parent_content_key, is_assigned_course_run=True, content_title=self.content_title, - content_quantity=-12345, + content_quantity=-assignment_content_quantity_usd_cents, state=LearnerContentAssignmentStateChoices.EXPIRED, ) @@ -585,7 +590,7 @@ def test_allocate_happy_path_e2e( allocate_payload = { 'learner_emails': ['new@foo.com', 'canceled@foo.com', 'expired@foo.com'], 'content_key': self.content_key, - 'content_price_cents': 123.45 * 100, # policy limit is 100000.00 USD, so this should be well below limit + 'content_price_cents': assignment_content_quantity_usd_cents, # this should be well below limit } response = self.client.post(allocate_url, data=allocate_payload) @@ -595,7 +600,7 @@ def test_allocate_happy_path_e2e( for assignment in self.assignment_configuration.assignments.filter( state=LearnerContentAssignmentStateChoices.ALLOCATED, content_key=self.content_key, - content_quantity=-123.45 * 100, + content_quantity=-assignment_content_quantity_usd_cents ) } self.assertEqual(3, len(allocation_records_by_email)) @@ -611,7 +616,7 @@ def test_allocate_happy_path_e2e( 'parent_content_key': self.parent_content_key, 'is_assigned_course_run': True, 'content_title': self.content_title, - 'content_quantity': -123.45 * 100, + 'content_quantity': -assignment_content_quantity_usd_cents, 'state': LearnerContentAssignmentStateChoices.ALLOCATED, 'transaction_uuid': None, 'uuid': str(foo_record.uuid), @@ -623,7 +628,6 @@ def test_allocate_happy_path_e2e( 'reason': AssignmentAutomaticExpiredReason.NINETY_DAYS_PASSED } }] - self.assertEqual(response_payload['created'], expected_created_records) canceled_record = allocation_records_by_email['canceled@foo.com'] @@ -637,7 +641,7 @@ def test_allocate_happy_path_e2e( 'parent_content_key': self.parent_content_key, 'is_assigned_course_run': True, 'content_title': self.content_title, - 'content_quantity': -123.45 * 100, + 'content_quantity': -assignment_content_quantity_usd_cents, 'state': LearnerContentAssignmentStateChoices.ALLOCATED, 'transaction_uuid': None, 'uuid': str(canceled_record.uuid), @@ -657,7 +661,7 @@ def test_allocate_happy_path_e2e( 'parent_content_key': self.parent_content_key, 'is_assigned_course_run': True, 'content_title': self.content_title, - 'content_quantity': -123.45 * 100, + 'content_quantity': -assignment_content_quantity_usd_cents, 'state': LearnerContentAssignmentStateChoices.ALLOCATED, 'transaction_uuid': None, 'uuid': str(expired_record.uuid), diff --git a/enterprise_access/apps/content_assignments/api.py b/enterprise_access/apps/content_assignments/api.py index 0713e12b..16e54aa9 100644 --- a/enterprise_access/apps/content_assignments/api.py +++ b/enterprise_access/apps/content_assignments/api.py @@ -527,13 +527,23 @@ def _get_content_title(assignment_configuration, content_key): def _get_parent_content_key(assignment_configuration, content_key): """ - Helper to retrieve (from cache) the parent content key of a content_key'ed content_metadata. + Helper to retrieve (from cache) the parent content key of a content_key's content_metadata. + Note: content_key is either a course run key or a course key. Only course run keys have a + parent course key. If content_key is for a course key, this will return the same key. Otherwise, the content_key represents a course run, and this will return the run's parent course key. """ content_metadata = _get_content_summary(assignment_configuration, content_key) - return content_metadata.get('content_key') + metadata_content_key = content_metadata.get('content_key') + + # Check if the assignment's content_key matches the returned content_key. If so, this is a course key + # which has no parent key. + if content_key == metadata_content_key: + return None + + # Otherwise, this is a course run key, so return the parent course key + return metadata_content_key def _get_preferred_course_run_key(assignment_configuration, content_key): @@ -573,7 +583,7 @@ def _create_new_assignments( content_title = _get_content_title(assignment_configuration, content_key) parent_content_key = _get_parent_content_key(assignment_configuration, content_key) preferred_course_run_key = _get_preferred_course_run_key(assignment_configuration, content_key) - is_assigned_course_run = content_key != parent_content_key + is_assigned_course_run = not parent_content_key assignments_to_create = [] for learner_email in learner_emails: