-
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
251 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,18 +6,25 @@ | |
import ddt | ||
from django.test import TestCase | ||
|
||
from enterprise_access.apps.core.models import User | ||
from enterprise_access.apps.core.tests.factories import UserFactory | ||
|
||
from ..api import ( | ||
AllocationException, | ||
_try_populate_assignments_lms_user_id, | ||
allocate_assignments, | ||
cancel_assignments, | ||
get_allocated_quantity_for_configuration, | ||
get_assignment_for_learner, | ||
get_assignments_for_configuration | ||
) | ||
from ..constants import LearnerContentAssignmentStateChoices | ||
from ..models import AssignmentConfiguration | ||
from ..models import AssignmentConfiguration, LearnerContentAssignment | ||
from .factories import LearnerContentAssignmentFactory | ||
|
||
# This is normally much larger (350), but that blows up the test duration. | ||
TEST_USER_EMAIL_READ_BATCH_SIZE = 4 | ||
|
||
|
||
@ddt.ddt | ||
class TestContentAssignmentApi(TestCase): | ||
|
@@ -299,6 +306,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 +374,151 @@ 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 | ||
|
||
@mock.patch( | ||
'enterprise_access.apps.content_assignments.api.USER_EMAIL_READ_BATCH_SIZE', | ||
TEST_USER_EMAIL_READ_BATCH_SIZE, | ||
) | ||
def test_try_populate_assignments_lms_user_id_with_batching(self): | ||
""" | ||
Tests the _try_populate_assignments_lms_user_id() function, especially the read batching feature. | ||
Make 2*N users with unique emails, | ||
Call function on: | ||
2*N assignments with existing emails, | ||
and 0.5*N assignment with new emails, | ||
Where N is the max batch size. | ||
""" | ||
num_users_to_create = 2 * TEST_USER_EMAIL_READ_BATCH_SIZE | ||
existing_users = [UserFactory() for _ in range(num_users_to_create)] | ||
|
||
assignments_for_non_existing_users = [ | ||
LearnerContentAssignmentFactory.create( | ||
assignment_configuration=self.assignment_configuration, | ||
lms_user_id=None, | ||
state=LearnerContentAssignmentStateChoices.ALLOCATED, | ||
) | ||
for _ in range(int(0.5 * TEST_USER_EMAIL_READ_BATCH_SIZE)) | ||
] | ||
assignments_for_existing_users = [ | ||
LearnerContentAssignmentFactory.create( | ||
assignment_configuration=self.assignment_configuration, | ||
learner_email=existing_user.email, | ||
lms_user_id=None, | ||
state=LearnerContentAssignmentStateChoices.ALLOCATED, | ||
) | ||
for existing_user in existing_users | ||
] | ||
# Assemble a list of assignments 2.5 longer than the max batch size. | ||
all_assignments = assignments_for_non_existing_users + assignments_for_existing_users | ||
|
||
_try_populate_assignments_lms_user_id(all_assignments) | ||
|
||
# save() and update all assignments from the db. | ||
for assignment in all_assignments: | ||
assignment.save() | ||
assignment.refresh_from_db() | ||
|
||
# We should not have updated the lms_user_id of the assignment if no user existed. | ||
for assignment in assignments_for_non_existing_users: | ||
assert assignment.lms_user_id is None | ||
|
||
# We should have updated the lms_user_id of the assignment if a user existed. | ||
for assignment in assignments_for_existing_users: | ||
assert assignment.lms_user_id == User.objects.get(email=assignment.learner_email).lms_user_id |
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] |