tests.py 4.52 KB
Newer Older
Dave St.Germain committed
1 2 3
"""
Tests for course_info
"""
4 5 6 7

from django.conf import settings

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

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

16 17

class TestAbout(MobileAPITestCase, MobileAuthTestMixin, MobileCourseAccessTestMixin):
18
    """
19
    Tests for /api/mobile/v0.5/course_info/{course_id}/about
20
    """
21 22 23 24 25 26 27 28 29 30 31 32 33
    REVERSE_INFO = {'name': 'course-about-detail', 'params': ['course_id']}

    def verify_success(self, response):
        super(TestAbout, self).verify_success(response)
        self.assertTrue('overview' in response.data)

    def init_course_access(self, course_id=None):
        # override this method since enrollment is not required for the About endpoint.
        self.login()

    def test_about_static_rewrite(self):
        self.login()

34 35 36 37 38 39 40 41
        about_usage_key = self.course.id.make_usage_key('about', 'overview')
        about_module = modulestore().get_item(about_usage_key)
        underlying_about_html = about_module.data

        # check that we start with relative static assets
        self.assertIn('\"/static/', underlying_about_html)

        # but shouldn't finish with any
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
        response = self.api_response()
        self.assertNotIn('\"/static/', response.data['overview'])


class TestUpdates(MobileAPITestCase, MobileAuthTestMixin, MobileEnrolledCourseAccessTestMixin):
    """
    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, [])

    def test_updates_static_rewrite(self):
        self.login_and_enroll()
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

        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
        )
        course_update_data = {
            "id": 1,
            "date": "Some date",
            "content": "<a href=\"/static/\">foo</a>",
            "status": CourseInfoModule.STATUS_VISIBLE
        }

        course_updates.items = [course_update_data]
        modulestore().update_item(course_updates, self.user.id)

76
        response = self.api_response()
77 78 79 80 81 82 83
        content = response.data[0]["content"]  # pylint: disable=maybe-no-member
        self.assertNotIn("\"/static/", content)

        underlying_updates_module = modulestore().get_item(updates_usage_key)
        self.assertIn("\"/static/", underlying_updates_module.items[0]['content'])


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

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

        # use toy course with handouts, and make it mobile_available
94 95
        course_items = import_from_xml(self.store, self.user.id, settings.COMMON_TEST_DATA_ROOT, ['toy'])
        self.course = course_items[0]
96 97 98 99 100 101
        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'])
102 103

    def test_no_handouts(self):
104
        self.login_and_enroll()
105

106 107 108 109 110 111 112 113 114
        # 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()
115 116 117 118 119 120 121

        # 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
122 123
        response = self.api_response()
        self.assertNotIn('\'/static/', response.data['handouts_html'])