Commit f37679ed by Mushtaq Ali

Clean course_video from invalid course key data - EDUCATOR-765

parent a03f599e
...@@ -99,13 +99,14 @@ class CourseSerializer(serializers.RelatedField): ...@@ -99,13 +99,14 @@ class CourseSerializer(serializers.RelatedField):
""" """
Convert data into CourseVideo instance and image filename tuple. Convert data into CourseVideo instance and image filename tuple.
""" """
if isinstance(data, basestring): course_id = data
course_id, image = data, None course_video = image = None
elif isinstance(data, dict): if data:
(course_id, image), = data.items() if isinstance(data, dict):
(course_id, image), = data.items()
course_video = CourseVideo(course_id=course_id) course_video = CourseVideo(course_id=course_id)
course_video.full_clean(exclude=["video"]) course_video.full_clean(exclude=['video'])
return course_video, image return course_video, image
...@@ -157,6 +158,10 @@ class VideoSerializer(serializers.ModelSerializer): ...@@ -157,6 +158,10 @@ class VideoSerializer(serializers.ModelSerializer):
except TypeError: except TypeError:
raise serializers.ValidationError("profile field needs to be a profile_name (str)") 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 return data
def create(self, validated_data): def create(self, validated_data):
......
...@@ -310,6 +310,14 @@ COMPLETE_SET_WITH_COURSE_KEY = dict( ...@@ -310,6 +310,14 @@ COMPLETE_SET_WITH_COURSE_KEY = dict(
subtitles=[SUBTITLE_DICT_SRT], subtitles=[SUBTITLE_DICT_SRT],
**VIDEO_DICT_STAR **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( COMPLETE_SET_WITH_OTHER_COURSE_KEYS = dict(
courses=['edX/DemoX/Astonomy', 'edX/DemoX/Zoology'], courses=['edX/DemoX/Astonomy', 'edX/DemoX/Zoology'],
encoded_videos=[ encoded_videos=[
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
""" """
Tests for Video Abstraction Layer views Tests for Video Abstraction Layer views
""" """
import json
from ddt import ddt, data, unpack from ddt import ddt, data, unpack
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
...@@ -457,6 +458,19 @@ class VideoListTest(APIAuthTestCase): ...@@ -457,6 +458,19 @@ class VideoListTest(APIAuthTestCase):
self.assertEqual(len(video), 1) self.assertEqual(len(video), 1)
self.assertEqual(len(video[0].get("encoded_videos")), 0) 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): def test_post_non_latin_client_video_id(self):
""" """
Tests POSTing non-latin client_video_id Tests POSTing non-latin client_video_id
......
...@@ -39,7 +39,7 @@ def load_requirements(*requirements_paths): ...@@ -39,7 +39,7 @@ def load_requirements(*requirements_paths):
setup( setup(
name='edxval', name='edxval',
version='0.0.16', version='0.0.17',
author='edX', author='edX',
url='http://github.com/edx/edx-val', url='http://github.com/edx/edx-val',
description='edx-val', description='edx-val',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment