tests.py 6.34 KB
Newer Older
1 2 3
"""
Test for LMS courseware app.
"""
4
from textwrap import dedent
5
from unittest import TestCase
6 7

import mock
8
from django.core.urlresolvers import reverse
9
from nose.plugins.attrib import attr
10
from opaque_keys.edx.keys import CourseKey
Victor Shnayder committed
11

12
from courseware.tests.helpers import LoginEnrollmentTestCase
13
from lms.djangoapps.lms_xblock.field_data import LmsFieldData
14 15
from xmodule.error_module import ErrorDescriptor
from xmodule.modulestore.django import modulestore
16
from xmodule.modulestore.tests.django_utils import TEST_DATA_MIXED_MODULESTORE, ModuleStoreTestCase
17
from xmodule.modulestore.tests.factories import ToyCourseFactory
18 19


20
@attr(shard=1)
21 22 23 24 25
class ActivateLoginTest(LoginEnrollmentTestCase):
    """
    Test logging in and logging out.
    """
    def setUp(self):
26
        super(ActivateLoginTest, self).setUp()
27
        self.setup_user()
Victor Shnayder committed
28

29 30 31
    def test_activate_login(self):
        """
        Test login -- the setup function does all the work.
Victor Shnayder committed
32
        """
33 34 35
        pass

    def test_logout(self):
Victor Shnayder committed
36
        """
37
        Test logout -- setup function does login.
Victor Shnayder committed
38
        """
39 40
        self.logout()

41 42 43 44 45 46 47 48
    def test_request_attr_on_logout(self):
        """
        Test request object after logging out to see whether it
        has 'is_from_log_out' attribute set to true.
        """
        response = self.client.get(reverse('logout'))
        self.assertTrue(getattr(response.wsgi_request, 'is_from_logout', False))  # pylint: disable=no-member

49 50 51 52 53

class PageLoaderTestCase(LoginEnrollmentTestCase):
    """
    Base class that adds a function to load all pages in a modulestore.
    """
54

55
    def check_all_pages_load(self, course_key):
56
        """
57 58
        Assert that all pages in the course load correctly.
        `course_id` is the ID of the course to check.
59
        """
60 61 62 63

        store = modulestore()

        # Enroll in the course before trying to access pages
64
        course = store.get_course(course_key)
65 66 67
        self.enroll(course, True)

        # Search for items in the course
68
        items = store.get_items(course_key)
69 70 71 72

        if len(items) < 1:
            self.fail('Could not retrieve any items from course')

73 74
        # Try to load each item in the course
        for descriptor in items:
75

76 77
            if descriptor.location.category == 'about':
                self._assert_loads('about_course',
78
                                   {'course_id': course_key.to_deprecated_string()},
79
                                   descriptor)
80

81
            elif descriptor.location.category == 'static_tab':
82
                kwargs = {'course_id': course_key.to_deprecated_string(),
83 84
                          'tab_slug': descriptor.location.name}
                self._assert_loads('static_tab', kwargs, descriptor)
85

86
            elif descriptor.location.category == 'course_info':
87
                self._assert_loads('info', {'course_id': course_key.to_deprecated_string()},
88
                                   descriptor)
89

90
            else:
91

92
                kwargs = {'course_id': course_key.to_deprecated_string(),
93
                          'location': descriptor.location.to_deprecated_string()}
94

95 96 97
                self._assert_loads('jump_to', kwargs, descriptor,
                                   expect_redirect=True,
                                   check_content=True)
98 99 100 101 102 103 104 105 106

    def _assert_loads(self, django_url, kwargs, descriptor,
                      expect_redirect=False,
                      check_content=False):
        """
        Assert that the url loads correctly.
        If expect_redirect, then also check that we were redirected.
        If check_content, then check that we don't get
        an error message about unavailable modules.
107
        """
Victor Shnayder committed
108

109 110
        url = reverse(django_url, kwargs=kwargs)
        response = self.client.get(url, follow=True)
Will Daly committed
111

112 113
        if response.status_code != 200:
            self.fail('Status %d for page %s' %
114
                      (response.status_code, descriptor.location))
Will Daly committed
115

116 117
        if expect_redirect:
            self.assertEqual(response.redirect_chain[0][1], 302)
Will Daly committed
118

119
        if check_content:
120 121
            self.assertNotContains(response, "this module is temporarily unavailable")
            self.assertNotIsInstance(descriptor, ErrorDescriptor)
122

123

124
@attr(shard=1)
125
class TestMongoCoursesLoad(ModuleStoreTestCase, PageLoaderTestCase):
126 127 128
    """
    Check that all pages in test courses load properly from Mongo.
    """
129
    MODULESTORE = TEST_DATA_MIXED_MODULESTORE
130

131
    def setUp(self):
132
        super(TestMongoCoursesLoad, self).setUp()
133
        self.setup_user()
134
        self.toy_course_key = ToyCourseFactory.create().id
135

lapentab committed
136 137
    @mock.patch('xmodule.course_module.requests.get')
    def test_toy_textbooks_loads(self, mock_get):
lapentab committed
138 139 140 141 142
        mock_get.return_value.text = dedent("""
            <?xml version="1.0"?><table_of_contents>
            <entry page="5" page_label="ii" name="Table of Contents"/>
            </table_of_contents>
        """).strip()
143
        location = self.toy_course_key.make_usage_key('course', '2012_Fall')
144
        course = self.store.get_item(location)
145
        self.assertGreater(len(course.textbooks), 0)
146

lapentab committed
147

148
@attr(shard=1)
Will Daly committed
149
class TestDraftModuleStore(ModuleStoreTestCase):
150 151
    def test_get_items_with_course_items(self):
        store = modulestore()
152

153
        # fix was to allow get_items() to take the course_id parameter
154
        store.get_items(CourseKey.from_string('abc/def/ghi'), qualifiers={'category': 'vertical'})
155

156 157 158
        # test success is just getting through the above statement.
        # The bug was that 'course_id' argument was
        # not allowed to be passed in (i.e. was throwing exception)
159 160


161
@attr(shard=1)
162 163 164 165 166 167 168 169 170 171 172 173 174
class TestLmsFieldData(TestCase):
    """
    Tests of the LmsFieldData class
    """
    def test_lms_field_data_wont_nest(self):
        # Verify that if an LmsFieldData is passed into LmsFieldData as the
        # authored_data, that it doesn't produced a nested field data.
        #
        # This fixes a bug where re-use of the same descriptor for many modules
        # would cause more and more nesting, until the recursion depth would be
        # reached on any attribute access

        # pylint: disable=protected-access
175 176
        base_authored = mock.Mock()
        base_student = mock.Mock()
177 178 179 180
        first_level = LmsFieldData(base_authored, base_student)
        second_level = LmsFieldData(first_level, base_student)
        self.assertEquals(second_level._authored_data, first_level._authored_data)
        self.assertNotIsInstance(second_level._authored_data, LmsFieldData)