tests.py 5.11 KB
Newer Older
Dave St.Germain committed
1 2 3
"""
Tests for course_info
"""
4

5
import ddt
6 7 8
from django.conf import settings

from xmodule.html_module import CourseInfoModule
9
from xmodule.modulestore import ModuleStoreEnum
10
from xmodule.modulestore.django import modulestore
11
from xmodule.modulestore.xml_importer import import_course_from_xml
Dave St.Germain committed
12

13
from ..testutils import (
14
    MobileAPITestCase, MobileCourseAccessTestMixin, MobileAuthTestMixin
15
)
Dave St.Germain committed
16

17

18
@ddt.ddt
19
class TestUpdates(MobileAPITestCase, MobileAuthTestMixin, MobileCourseAccessTestMixin):
20 21 22 23 24 25 26 27 28
    """
    Tests for /api/mobile/v0.5/course_info/{course_id}/updates
    """
    REVERSE_INFO = {'name': 'course-updates-list', 'params': ['course_id']}

    def verify_success(self, response):
        super(TestUpdates, self).verify_success(response)
        self.assertEqual(response.data, [])

29 30 31 32 33 34
    @ddt.data(True, False)
    def test_updates(self, new_format):
        """
        Tests updates endpoint with /static in the content.
        Tests both new updates format (using "items") and old format (using "data").
        """
35
        self.login_and_enroll()
36

37
        # create course Updates item in modulestore
38 39 40 41 42 43 44
        updates_usage_key = self.course.id.make_usage_key('course_info', 'updates')
        course_updates = modulestore().create_item(
            self.user.id,
            updates_usage_key.course_key,
            updates_usage_key.block_type,
            block_id=updates_usage_key.block_id
        )
45 46

        # store content in Updates item (either new or old format)
47
        num_updates = 3
48
        if new_format:
49 50 51 52 53 54 55 56 57
            for num in range(1, num_updates + 1):
                course_updates.items.append(
                    {
                        "id": num,
                        "date": "Date" + str(num),
                        "content": "<a href=\"/static/\">Update" + str(num) + "</a>",
                        "status": CourseInfoModule.STATUS_VISIBLE
                    }
                )
58
        else:
59 60 61 62 63
            update_data = ""
            # old format stores the updates with the newest first
            for num in range(num_updates, 0, -1):
                update_data += "<li><h2>Date" + str(num) + "</h2><a href=\"/static/\">Update" + str(num) + "</a></li>"
            course_updates.data = u"<ol>" + update_data + "</ol>"
64 65
        modulestore().update_item(course_updates, self.user.id)

66
        # call API
67
        response = self.api_response()
68 69

        # verify static URLs are replaced in the content returned by the API
70
        self.assertNotIn("\"/static/", response.content)
71

72 73 74 75
        # verify static URLs remain in the underlying content
        underlying_updates = modulestore().get_item(updates_usage_key)
        underlying_content = underlying_updates.items[0]['content'] if new_format else underlying_updates.data
        self.assertIn("\"/static/", underlying_content)
76

77 78
        # verify content and sort order of updates (most recent first)
        for num in range(1, num_updates + 1):
79
            update_data = response.data[num_updates - num]
80 81 82 83
            self.assertEquals(num, update_data['id'])
            self.assertEquals("Date" + str(num), update_data['date'])
            self.assertIn("Update" + str(num), update_data['content'])

84

85
class TestHandouts(MobileAPITestCase, MobileAuthTestMixin, MobileCourseAccessTestMixin):
86 87 88
    """
    Tests for /api/mobile/v0.5/course_info/{course_id}/handouts
    """
89 90
    REVERSE_INFO = {'name': 'course-handouts-list', 'params': ['course_id']}

91
    def setUp(self):
92 93
        super(TestHandouts, self).setUp()

94 95 96 97 98
        # Deleting handouts fails with split modulestore because the handout has no parent.
        # This needs further investigation to determine if it is a bug in the split modulestore.
        # pylint: disable=protected-access
        self.store = modulestore()._get_modulestore_by_type(ModuleStoreEnum.Type.mongo)

99
        # use toy course with handouts, and make it mobile_available
100
        course_items = import_course_from_xml(self.store, self.user.id, settings.COMMON_TEST_DATA_ROOT, ['toy'])
101
        self.course = course_items[0]
102 103 104 105 106 107
        self.course.mobile_available = True
        self.store.update_item(self.course, self.user.id)

    def verify_success(self, response):
        super(TestHandouts, self).verify_success(response)
        self.assertIn('Sample', response.data['handouts_html'])
108 109

    def test_no_handouts(self):
110
        self.login_and_enroll()
111

112 113 114 115 116 117 118 119 120
        # delete handouts in course
        handouts_usage_key = self.course.id.make_usage_key('course_info', 'handouts')
        with self.store.branch_setting(ModuleStoreEnum.Branch.draft_preferred, self.course.id):
            self.store.delete_item(handouts_usage_key, self.user.id)

        self.api_response(expected_response_code=404)

    def test_handouts_static_rewrites(self):
        self.login_and_enroll()
121 122 123 124 125 126 127

        # check that we start with relative static assets
        handouts_usage_key = self.course.id.make_usage_key('course_info', 'handouts')
        underlying_handouts = self.store.get_item(handouts_usage_key)
        self.assertIn('\'/static/', underlying_handouts.data)

        # but shouldn't finish with any
128 129
        response = self.api_response()
        self.assertNotIn('\'/static/', response.data['handouts_html'])