Skip to content

Commit

Permalink
Clean course_video from invalid course key data - EDUCATOR-765
Browse files Browse the repository at this point in the history
  • Loading branch information
Mushtaq Ali committed Jul 10, 2017
1 parent a03f599 commit f37679e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
17 changes: 11 additions & 6 deletions edxval/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,14 @@ def to_internal_value(self, data):
"""
Convert data into CourseVideo instance and image filename tuple.
"""
if isinstance(data, basestring):
course_id, image = data, None
elif isinstance(data, dict):
(course_id, image), = data.items()
course_id = data
course_video = image = None
if data:
if isinstance(data, dict):
(course_id, image), = data.items()

course_video = CourseVideo(course_id=course_id)
course_video.full_clean(exclude=["video"])
course_video = CourseVideo(course_id=course_id)
course_video.full_clean(exclude=['video'])

return course_video, image

Expand Down Expand Up @@ -157,6 +158,10 @@ def validate(self, data):
except TypeError:
raise serializers.ValidationError("profile field needs to be a profile_name (str)")

# Clean course_video list from any invalid data.
course_videos = [(course_video, image) for course_video, image in data.get('courses', []) if course_video]
data['courses'] = course_videos

return data

def create(self, validated_data):
Expand Down
8 changes: 8 additions & 0 deletions edxval/tests/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,14 @@
subtitles=[SUBTITLE_DICT_SRT],
**VIDEO_DICT_STAR
)
COMPLETE_SET_WITH_SOME_INVALID_COURSE_KEY = dict(
courses=[None, False, '', 'edX/DemoX/Astonomy'],
encoded_videos=[
ENCODED_VIDEO_DICT_STAR
],
subtitles=[SUBTITLE_DICT_SRT],
**VIDEO_DICT_STAR
)
COMPLETE_SET_WITH_OTHER_COURSE_KEYS = dict(
courses=['edX/DemoX/Astonomy', 'edX/DemoX/Zoology'],
encoded_videos=[
Expand Down
14 changes: 14 additions & 0 deletions edxval/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""
Tests for Video Abstraction Layer views
"""
import json
from ddt import ddt, data, unpack

from django.core.urlresolvers import reverse
Expand Down Expand Up @@ -457,6 +458,19 @@ def test_post_video(self):
self.assertEqual(len(video), 1)
self.assertEqual(len(video[0].get("encoded_videos")), 0)

def test_post_video_invalid_course_key(self):
"""
Tests POSTing a new Video with course video list containing some invalid course keys.
"""
url = reverse('video-list')
response = self.client.post(
url, constants.COMPLETE_SET_WITH_SOME_INVALID_COURSE_KEY, format='json'
)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
response = json.loads(response.content)
# Check that invalid course keys have been filtered out.
self.assertEqual(response['courses'], [{u'edX/DemoX/Astonomy': None}])

def test_post_non_latin_client_video_id(self):
"""
Tests POSTing non-latin client_video_id
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def load_requirements(*requirements_paths):

setup(
name='edxval',
version='0.0.16',
version='0.0.17',
author='edX',
url='http://github.com/edx/edx-val',
description='edx-val',
Expand Down

0 comments on commit f37679e

Please sign in to comment.