Disallow NaN value for speed key.
The VideoBlock `handle_ajax` is allowing NaN values for speed key and causing videos to not load. Also added a data migration to fix the data for learners. PROD-1148
This commit is contained in:
@@ -8,6 +8,7 @@ StudioViewHandlers are handlers for video descriptor instance.
|
||||
|
||||
import json
|
||||
import logging
|
||||
import math
|
||||
|
||||
import six
|
||||
from django.core.files.base import ContentFile
|
||||
@@ -89,6 +90,11 @@ class VideoStudentViewHandlers(object):
|
||||
if key == 'bumper_last_view_date':
|
||||
value = now()
|
||||
|
||||
if key == 'speed' and math.isnan(value):
|
||||
message = u"Invalid speed value {}, must be a float.".format(value)
|
||||
log.warning(message)
|
||||
return json.dumps({'success': False, 'error': message})
|
||||
|
||||
setattr(self, key, value)
|
||||
|
||||
if key == 'speed':
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.11.29 on 2020-03-11 10:07
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
def fix_nan_value_for_global_speed(apps, schema_editor):
|
||||
XModuleStudentPrefsField = apps.get_model('courseware', 'XModuleStudentPrefsField')
|
||||
XModuleStudentPrefsField.objects.filter(
|
||||
field_name='global_speed', value='NaN'
|
||||
).update(
|
||||
value=1.0
|
||||
)
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('courseware', '0013_auto_20191001_1858'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(fix_nan_value_for_global_speed, reverse_code=migrations.RunPython.noop)
|
||||
]
|
||||
@@ -165,6 +165,24 @@ class TestVideo(BaseTestVideoXBlock):
|
||||
status_codes = {response.status_code for response in responses.values()}
|
||||
self.assertEqual(status_codes.pop(), 404)
|
||||
|
||||
def test_handle_ajax_for_speed_with_nan(self):
|
||||
self.item_descriptor.handle_ajax('save_user_state', {'speed': json.dumps(1.0)})
|
||||
self.assertEqual(self.item_descriptor.speed, 1.0)
|
||||
self.assertEqual(self.item_descriptor.global_speed, 1.0)
|
||||
|
||||
# try to set NaN value for speed.
|
||||
response = self.item_descriptor.handle_ajax(
|
||||
'save_user_state', {'speed': json.dumps(float('NaN'))}
|
||||
)
|
||||
|
||||
self.assertFalse(json.loads(response)['success'])
|
||||
expected_error = u"Invalid speed value nan, must be a float."
|
||||
self.assertEqual(json.loads(response)['error'], expected_error)
|
||||
|
||||
# verify that the speed and global speed are still 1.0
|
||||
self.assertEqual(self.item_descriptor.speed, 1.0)
|
||||
self.assertEqual(self.item_descriptor.global_speed, 1.0)
|
||||
|
||||
def test_handle_ajax(self):
|
||||
|
||||
data = [
|
||||
|
||||
Reference in New Issue
Block a user