Skip to content

Commit

Permalink
Bulk Enroll: Fix the BulkEnrollSerializer courses field to internally…
Browse files Browse the repository at this point in the history
… behave with strings and lists

(cherry picked from commit 43e161f)
  • Loading branch information
bdero committed Jul 20, 2017
1 parent 151d778 commit e3e8dbe
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lms/djangoapps/bulk_enroll/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

class StringListField(serializers.ListField):
def to_internal_value(self, data):
try:
return data[0].split(',')
except IndexError:
if not data:
return []
if isinstance(data, list):
data = data[0]
return data.split(',')


class BulkEnrollmentSerializer(serializers.Serializer):
Expand Down
17 changes: 17 additions & 0 deletions lms/djangoapps/bulk_enroll/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django.test.utils import override_settings
from rest_framework.test import APIRequestFactory, APITestCase, force_authenticate

from bulk_enroll.serializers import BulkEnrollmentSerializer
from bulk_enroll.views import BulkEnrollView
from courseware.tests.helpers import LoginEnrollmentTestCase
from microsite_configuration import microsite
Expand Down Expand Up @@ -74,6 +75,22 @@ def request_bulk_enroll(self, data=None, **extra):
response.render()
return response

def test_course_list_serializer(self):
"""
Test that the course serializer will work when passed a string or list.
Internally, DRF passes the data into the value conversion method as a list instead of
a string, so StringListField needs to work with both.
"""
for key in [self.course_key, [self.course_key]]:
serializer = BulkEnrollmentSerializer(data={
'identifiers': 'percivaloctavius',
'action': 'enroll',
'email_students': False,
'courses': key,
})
self.assertTrue(serializer.is_valid())

def test_non_staff(self):
""" Test that non global staff users are forbidden from API use. """
self.staff.is_staff = False
Expand Down

0 comments on commit e3e8dbe

Please sign in to comment.