test_import_nostatic.py 5.3 KB
Newer Older
1
#pylint: disable=E1101
2
'''
Chris Dodge committed
3
Tests for importing with no static
4
'''
5 6 7 8 9 10 11 12 13 14

from django.test.client import Client
from django.test.utils import override_settings
from django.conf import settings
from path import path
import copy

from django.contrib.auth.models import User

from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
15
from contentstore.tests.modulestore_config import TEST_MODULESTORE
16

17
from xmodule.modulestore import Location
18
from xmodule.modulestore.django import modulestore
19
from xmodule.contentstore.django import contentstore
20
from xmodule.modulestore.xml_importer import import_from_xml
21
from xmodule.contentstore.content import StaticContent
22
from xmodule.contentstore.django import _CONTENTSTORE
23 24 25 26 27

from xmodule.course_module import CourseDescriptor

from xmodule.exceptions import NotFoundError
from uuid import uuid4
28
from pymongo import MongoClient
29 30

TEST_DATA_CONTENTSTORE = copy.deepcopy(settings.CONTENTSTORE)
31
TEST_DATA_CONTENTSTORE['DOC_STORE_CONFIG']['db'] = 'test_xcontent_%s' % uuid4().hex
32 33


34
@override_settings(CONTENTSTORE=TEST_DATA_CONTENTSTORE, MODULESTORE=TEST_MODULESTORE)
35 36 37
class ContentStoreImportNoStaticTest(ModuleStoreTestCase):
    """
    Tests that rely on the toy and test_import_course courses.
Chris Dodge committed
38
    NOTE: refactor using CourseFactory so they do not.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    """
    def setUp(self):

        settings.MODULESTORE['default']['OPTIONS']['fs_root'] = path('common/test/data')
        settings.MODULESTORE['direct']['OPTIONS']['fs_root'] = path('common/test/data')
        uname = 'testuser'
        email = 'test+courses@edx.org'
        password = 'foo'

        # Create the use so we can log them in.
        self.user = User.objects.create_user(uname, email, password)

        # Note that we do not actually need to do anything
        # for registration if we directly mark them active.
        self.user.is_active = True
        # Staff has access to view all courses
        self.user.is_staff = True

        # Save the data that we've just changed to the db.
        self.user.save()

        self.client = Client()
        self.client.login(username=uname, password=password)

63
    def tearDown(self):
64
        MongoClient().drop_database(TEST_DATA_CONTENTSTORE['DOC_STORE_CONFIG']['db'])
65 66
        _CONTENTSTORE.clear()

67 68
    def load_test_import_course(self):
        '''
69
        Load the standard course used to test imports (for do_import_static=False behavior).
70 71 72 73 74 75 76 77 78 79 80 81 82 83
        '''
        content_store = contentstore()
        module_store = modulestore('direct')
        import_from_xml(module_store, 'common/test/data/', ['test_import_course'], static_content_store=content_store, do_import_static=False, verbose=True)
        course_location = CourseDescriptor.id_to_location('edX/test_import_course/2012_Fall')
        course = module_store.get_item(course_location)
        self.assertIsNotNone(course)

        return module_store, content_store, course, course_location

    def test_static_import(self):
        '''
        Stuff in static_import should always be imported into contentstore
        '''
84
        _, content_store, course, course_location = self.load_test_import_course()
85 86 87 88 89 90 91 92 93 94 95 96 97 98

        # make sure we have ONE asset in our contentstore ("should_be_imported.html")
        all_assets = content_store.get_all_content_for_course(course_location)
        print "len(all_assets)=%d" % len(all_assets)
        self.assertEqual(len(all_assets), 1)

        content = None
        try:
            location = StaticContent.get_location_from_path('/c4x/edX/test_import_course/asset/should_be_imported.html')
            content = content_store.find(location)
        except NotFoundError:
            pass

        self.assertIsNotNone(content)
99

Calen Pennington committed
100 101 102
        # make sure course.static_asset_path is correct
        print "static_asset_path = {0}".format(course.static_asset_path)
        self.assertEqual(course.static_asset_path, 'test_import_course')
103 104 105 106 107 108 109 110 111 112 113

    def test_asset_import_nostatic(self):
        '''
        This test validates that an image asset is NOT imported when do_import_static=False
        '''
        content_store = contentstore()

        module_store = modulestore('direct')
        import_from_xml(module_store, 'common/test/data/', ['toy'], static_content_store=content_store, do_import_static=False, verbose=True)

        course_location = CourseDescriptor.id_to_location('edX/toy/2012_Fall')
114
        module_store.get_item(course_location)
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

        # make sure we have NO assets in our contentstore
        all_assets = content_store.get_all_content_for_course(course_location)
        print "len(all_assets)=%d" % len(all_assets)
        self.assertEqual(len(all_assets), 0)

    def test_no_static_link_rewrites_on_import(self):
        module_store = modulestore('direct')
        import_from_xml(module_store, 'common/test/data/', ['toy'], do_import_static=False, verbose=True)

        handouts = module_store.get_item(Location(['i4x', 'edX', 'toy', 'course_info', 'handouts', None]))
        self.assertIn('/static/', handouts.data)

        handouts = module_store.get_item(Location(['i4x', 'edX', 'toy', 'html', 'toyhtml', None]))
        self.assertIn('/static/', handouts.data)
130 131 132 133 134 135

    def test_tab_name_imports_correctly(self):
        module_store, content_store, course, course_location = self.load_test_import_course()
        print "course tabs = {0}".format(course.tabs)
        self.assertEqual(course.tabs[2]['name'],'Syllabus')