test_orphan.py 4.28 KB
Newer Older
1 2 3 4 5
"""
Test finding orphans via the view and django config
"""
import json
from contentstore.tests.utils import CourseTestCase
6
from xmodule.modulestore.django import loc_mapper
7
from student.models import CourseEnrollment
8
from xmodule.modulestore.django import modulestore
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

class TestOrphan(CourseTestCase):
    """
    Test finding orphans via view and django config
    """
    def setUp(self):
        super(TestOrphan, self).setUp()

        runtime = self.course.runtime

        self._create_item('chapter', 'Chapter1', {}, {'display_name': 'Chapter 1'}, 'course', self.course.location.name, runtime)
        self._create_item('chapter', 'Chapter2', {}, {'display_name': 'Chapter 2'}, 'course', self.course.location.name, runtime)
        self._create_item('chapter', 'OrphanChapter', {}, {'display_name': 'Orphan Chapter'}, None, None, runtime)
        self._create_item('vertical', 'Vert1', {}, {'display_name': 'Vertical 1'}, 'chapter', 'Chapter1', runtime)
        self._create_item('vertical', 'OrphanVert', {}, {'display_name': 'Orphan Vertical'}, None, None, runtime)
        self._create_item('html', 'Html1', "<p>Goodbye</p>", {'display_name': 'Parented Html'}, 'vertical', 'Vert1', runtime)
        self._create_item('html', 'OrphanHtml', "<p>Hello</p>", {'display_name': 'Orphan html'}, None, None, runtime)
        self._create_item('static_tab', 'staticuno', "<p>tab</p>", {'display_name': 'Tab uno'}, None, None, runtime)
        self._create_item('about', 'overview', "<p>overview</p>", {}, None, None, runtime)
        self._create_item('course_info', 'updates', "<ol><li><h2>Sep 22</h2><p>test</p></li></ol>", {}, None, None, runtime)

    def _create_item(self, category, name, data, metadata, parent_category, parent_name, runtime):
        location = self.course.location.replace(category=category, name=name)
32 33
        store = modulestore('direct')
        store.create_and_save_xmodule(location, data, metadata, runtime)
34 35 36
        if parent_name:
            # add child to parent in mongo
            parent_location = self.course.location.replace(category=parent_category, name=parent_name)
37
            parent = store.get_item(parent_location)
38
            parent.children.append(location.url())
39
            store.update_item(parent, self.user.id)
40 41 42 43 44

    def test_mongo_orphan(self):
        """
        Test that old mongo finds the orphans
        """
45 46 47
        locator = loc_mapper().translate_location(self.course.location.course_id, self.course.location, False, True)
        orphan_url = locator.url_reverse('orphan/', '')

48 49
        orphans = json.loads(
            self.client.get(
50
                orphan_url,
51 52 53 54 55 56 57 58 59 60
                HTTP_ACCEPT='application/json'
            ).content
        )
        self.assertEqual(len(orphans), 3, "Wrong # {}".format(orphans))
        location = self.course.location.replace(category='chapter', name='OrphanChapter')
        self.assertIn(location.url(), orphans)
        location = self.course.location.replace(category='vertical', name='OrphanVert')
        self.assertIn(location.url(), orphans)
        location = self.course.location.replace(category='html', name='OrphanHtml')
        self.assertIn(location.url(), orphans)
61 62 63 64 65

    def test_mongo_orphan_delete(self):
        """
        Test that old mongo deletes the orphans
        """
66 67 68
        locator = loc_mapper().translate_location(self.course.location.course_id, self.course.location, False, True)
        orphan_url = locator.url_reverse('orphan/', '')
        self.client.delete(orphan_url)
69
        orphans = json.loads(
70
            self.client.get(orphan_url, HTTP_ACCEPT='application/json').content
71 72
        )
        self.assertEqual(len(orphans), 0, "Orphans not deleted {}".format(orphans))
73 74 75 76 77

    def test_not_permitted(self):
        """
        Test that auth restricts get and delete appropriately
        """
78
        test_user_client, test_user = self.create_non_staff_authed_user_client()
79
        CourseEnrollment.enroll(test_user, self.course.location.course_id)
80 81 82
        locator = loc_mapper().translate_location(self.course.location.course_id, self.course.location, False, True)
        orphan_url = locator.url_reverse('orphan/', '')
        response = test_user_client.get(orphan_url)
83
        self.assertEqual(response.status_code, 403)
84
        response = test_user_client.delete(orphan_url)
85
        self.assertEqual(response.status_code, 403)