test_sandboxing.py 1.72 KB
Newer Older
1 2 3 4 5
"""
Tests for sandboxing.py in util app
"""

from django.test import TestCase
6
from opaque_keys.edx.locator import LibraryLocator
7 8
from util.sandboxing import can_execute_unsafe_code
from django.test.utils import override_settings
9
from opaque_keys.edx.locations import SlashSeparatedCourseKey
10 11 12 13 14 15


class SandboxingTest(TestCase):
    """
    Test sandbox whitelisting
    """
16
    @override_settings(COURSES_WITH_UNSAFE_CODE=['edX/full/.*', 'library:v1-edX+.*'])
17 18 19 20
    def test_sandbox_exclusion(self):
        """
        Test to make sure that a non-match returns false
        """
21
        self.assertFalse(can_execute_unsafe_code(SlashSeparatedCourseKey('edX', 'notful', 'empty')))
22
        self.assertFalse(can_execute_unsafe_code(LibraryLocator('edY', 'test_bank')))
23 24 25 26 27 28

    @override_settings(COURSES_WITH_UNSAFE_CODE=['edX/full/.*'])
    def test_sandbox_inclusion(self):
        """
        Test to make sure that a match works across course runs
        """
29 30
        self.assertTrue(can_execute_unsafe_code(SlashSeparatedCourseKey('edX', 'full', '2012_Fall')))
        self.assertTrue(can_execute_unsafe_code(SlashSeparatedCourseKey('edX', 'full', '2013_Spring')))
31
        self.assertFalse(can_execute_unsafe_code(LibraryLocator('edX', 'test_bank')))
32

33
    def test_courselikes_with_unsafe_code_default(self):
34 35 36
        """
        Test that the default setting for COURSES_WITH_UNSAFE_CODE is an empty setting, e.g. we don't use @override_settings in these tests
        """
37 38
        self.assertFalse(can_execute_unsafe_code(SlashSeparatedCourseKey('edX', 'full', '2012_Fall')))
        self.assertFalse(can_execute_unsafe_code(SlashSeparatedCourseKey('edX', 'full', '2013_Spring')))
39
        self.assertFalse(can_execute_unsafe_code(LibraryLocator('edX', 'test_bank')))