-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Try to populate lms_user_id on assignments during creation
This is an attempt to cover the case where learners are already logged in at the moment of assignment. This case is a hole left by the work in ENT-7875 which only set out to cover the case where a learner was logged out at the moment of assignment. ENT-7874
- Loading branch information
Showing
4 changed files
with
194 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ | |
import ddt | ||
from django.test import TestCase | ||
|
||
from enterprise_access.apps.core.tests.factories import UserFactory | ||
|
||
from ..api import ( | ||
AllocationException, | ||
allocate_assignments, | ||
|
@@ -15,7 +17,7 @@ | |
get_assignments_for_configuration | ||
) | ||
from ..constants import LearnerContentAssignmentStateChoices | ||
from ..models import AssignmentConfiguration | ||
from ..models import AssignmentConfiguration, LearnerContentAssignment | ||
from .factories import LearnerContentAssignmentFactory | ||
|
||
|
||
|
@@ -299,6 +301,7 @@ def test_allocate_assignments_happy_path(self, mock_get_and_cache_content_metada | |
created_assignment = allocation_results['created'][0] | ||
self.assertEqual(created_assignment.assignment_configuration, self.assignment_configuration) | ||
self.assertEqual(created_assignment.learner_email, '[email protected]') | ||
self.assertEqual(created_assignment.lms_user_id, None) | ||
self.assertEqual(created_assignment.content_key, content_key) | ||
self.assertEqual(created_assignment.content_title, content_title) | ||
self.assertEqual(created_assignment.content_quantity, -1 * content_price_cents) | ||
|
@@ -366,3 +369,99 @@ def test_cancel_assignments_happy_path(self): | |
self.assertEqual(accepted_assignment.state, LearnerContentAssignmentStateChoices.ACCEPTED) | ||
self.assertEqual(cancelled_assignment.state, LearnerContentAssignmentStateChoices.CANCELLED) | ||
self.assertEqual(errored_assignment.state, LearnerContentAssignmentStateChoices.CANCELLED) | ||
|
||
@mock.patch( | ||
'enterprise_access.apps.content_assignments.api.get_and_cache_content_metadata', | ||
return_value=mock.MagicMock(), | ||
) | ||
@ddt.data( | ||
{ | ||
'user_exists': True, | ||
'existing_assignment_state': None, | ||
}, | ||
{ | ||
'user_exists': False, | ||
'existing_assignment_state': None, | ||
}, | ||
{ | ||
'user_exists': True, | ||
'existing_assignment_state': LearnerContentAssignmentStateChoices.ALLOCATED, | ||
}, | ||
{ | ||
'user_exists': False, | ||
'existing_assignment_state': LearnerContentAssignmentStateChoices.ALLOCATED, | ||
}, | ||
{ | ||
'user_exists': True, | ||
'existing_assignment_state': LearnerContentAssignmentStateChoices.ACCEPTED, | ||
}, | ||
{ | ||
'user_exists': False, | ||
'existing_assignment_state': LearnerContentAssignmentStateChoices.ACCEPTED, | ||
}, | ||
{ | ||
'user_exists': True, | ||
'existing_assignment_state': LearnerContentAssignmentStateChoices.CANCELLED, | ||
}, | ||
{ | ||
'user_exists': False, | ||
'existing_assignment_state': LearnerContentAssignmentStateChoices.CANCELLED, | ||
}, | ||
{ | ||
'user_exists': True, | ||
'existing_assignment_state': LearnerContentAssignmentStateChoices.ERRORED, | ||
}, | ||
{ | ||
'user_exists': False, | ||
'existing_assignment_state': LearnerContentAssignmentStateChoices.ERRORED, | ||
}, | ||
) | ||
@ddt.unpack | ||
def test_allocate_assignments_set_lms_user_id( | ||
self, | ||
mock_get_and_cache_content_metadata, | ||
user_exists, | ||
existing_assignment_state, | ||
): | ||
""" | ||
Tests that allocating assignments correctly sets the lms_user_id when a user pre-exists with a matching email. | ||
""" | ||
content_key = 'demoX' | ||
content_title = 'edx: Demo 101' | ||
content_price_cents = 100 | ||
learner_email = '[email protected]' | ||
lms_user_id = 999 | ||
mock_get_and_cache_content_metadata.return_value = { | ||
'title': content_title, | ||
} | ||
|
||
if user_exists: | ||
UserFactory(username='alice', email=learner_email, lms_user_id=lms_user_id) | ||
|
||
assignment = None | ||
if existing_assignment_state: | ||
assignment = LearnerContentAssignmentFactory.create( | ||
assignment_configuration=self.assignment_configuration, | ||
learner_email=learner_email, | ||
lms_user_id=None, | ||
content_key=content_key, | ||
content_title=content_title, | ||
content_quantity=-content_price_cents, | ||
state=existing_assignment_state, | ||
) | ||
|
||
allocate_assignments( | ||
self.assignment_configuration, | ||
[learner_email], | ||
content_key, | ||
content_price_cents, | ||
) | ||
|
||
# Get the latest assignment from the db. | ||
assignment = LearnerContentAssignment.objects.get(learner_email=learner_email) | ||
|
||
# We should have updated the lms_user_id of the assignment IFF a user pre-existed. | ||
if user_exists: | ||
assert assignment.lms_user_id == lms_user_id | ||
else: | ||
assert assignment.lms_user_id is None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
""" | ||
Utility functions for the content_assignments app. | ||
""" | ||
|
||
|
||
def chunks(a_list, chunk_size): | ||
""" | ||
Helper to break a list up into chunks. Returns a generator of lists. | ||
""" | ||
for i in range(0, len(a_list), chunk_size): | ||
yield a_list[i:i + chunk_size] |