test_preview.py 1.59 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
"""
Tests for contentstore.views.preview.py
"""
from django.test import TestCase
from django.test.client import RequestFactory

from student.tests.factories import UserFactory

from xmodule.modulestore.tests.factories import CourseFactory, ItemFactory

from contentstore.views.preview import get_preview_fragment


class GetPreviewHtmlTestCase(TestCase):
    """
16
    Tests for get_preview_fragment.
17 18

    Note that there are other existing test cases in test_contentstore that indirectly execute
19
    get_preview_fragment via the xblock RESTful API.
20 21
    """

22
    def test_preview_fragment(self):
23
        """
24 25 26 27
        Test for calling get_preview_html.

        This test used to be specifically about Locators (ensuring that they did not
        get translated to Locations). The test now has questionable value.
28 29 30 31 32 33 34 35 36 37 38 39
        """
        course = CourseFactory.create()
        html = ItemFactory.create(
            parent_location=course.location,
            category="html",
            data={'data': "<html>foobar</html>"}
        )

        request = RequestFactory().get('/dummy-url')
        request.user = UserFactory()
        request.session = {}

40
        # Call get_preview_fragment directly.
41 42 43 44 45
        context = {
            'reorderable_items': set(),
            'read_only': True
        }
        html = get_preview_fragment(request, html, context).content
46 47

        # Verify student view html is returned, and the usage ID is as expected.
48 49
        self.assertRegexpMatches(
            html,
50
            'data-usage-id="i4x://MITx/999/html/html_[0-9]*"'
51 52
        )
        self.assertRegexpMatches(html, '<html>foobar</html>')