Skip to content

Commit

Permalink
Add student_view_data to HTML XBlock (openedx#853)
Browse files Browse the repository at this point in the history
* added student_view_data to HtmlBlock to allow html to be downloadable via the course blocks API
* added tests for html xblock student_view_data
  • Loading branch information
ny0m authored and e-kolpakov committed Aug 28, 2017
1 parent 7ad9718 commit 898a164
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
11 changes: 11 additions & 0 deletions common/lib/xmodule/xmodule/html_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ def student_view(self, _context):
"""
return Fragment(self.get_html())

def student_view_data(self):
"""
Returns a JSON representation of the student_view of this XBlock,
retrievable from the Course Block API.
Since this data is not user-specific, fields like `html` may contain
placeholders like %%USER_ID%%.
"""
return {
'html': self.data
}

def get_html(self):
""" Returns html required for rendering XModule. """

Expand Down
26 changes: 25 additions & 1 deletion common/lib/xmodule/xmodule/tests/test_html_module.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest

import ddt
from mock import Mock

from xblock.field_data import DictFieldData
Expand All @@ -24,6 +24,30 @@ def instantiate_descriptor(**field_data):
)


@ddt.ddt
class HtmlModuleCourseApiTestCase(unittest.TestCase):
"""
Ensure that student_view_data can handle likely input,
and doesn't modify the HTML in any way.
This means that it does NOT protect against XSS, escape HTML tags, etc.
"""
@ddt.data(
'<h1>Some content</h1>', # Valid HTML
'',
None,
'<h1>Some content</h', # Invalid HTML
'<script>alert()</script>', # Does not escape tags
'<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7">', # Images allowed
'short string ' * 100, # May contain long strings
)
def test_common_values(self, html):
descriptor = Mock()
field_data = DictFieldData({'data': html})
module_system = get_test_system()
module = HtmlModule(descriptor, module_system, field_data, Mock())
self.assertEqual(module.student_view_data(), {'html': html})


class HtmlModuleSubstitutionTestCase(unittest.TestCase):
descriptor = Mock()

Expand Down
2 changes: 1 addition & 1 deletion lms/djangoapps/course_api/blocks/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class TestBlocksView(SharedModuleStoreTestCase):
Test class for BlocksView
"""
requested_fields = ['graded', 'format', 'student_view_multi_device', 'children', 'not_a_field', 'due']
BLOCK_TYPES_WITH_STUDENT_VIEW_DATA = ['video', 'discussion']
BLOCK_TYPES_WITH_STUDENT_VIEW_DATA = ['video', 'discussion', 'html']

@classmethod
def setUpClass(cls):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_transform(self):

# verify html data
html_block_key = self.course_key.make_usage_key('html', 'toyhtml')
self.assertIsNone(
self.assertIsNotNone(
self.block_structure.get_transformer_block_field(
html_block_key, StudentViewTransformer, StudentViewTransformer.STUDENT_VIEW_DATA,
)
Expand Down

0 comments on commit 898a164

Please sign in to comment.