test_forms.py 8.26 KB
Newer Older
Nimisha Asthagiri committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
"""
Tests for Course Blocks forms
"""
import ddt
from django.http import Http404, QueryDict
from urllib import urlencode
from rest_framework.exceptions import PermissionDenied

from opaque_keys.edx.locator import CourseLocator
from openedx.core.djangoapps.util.test_forms import FormTestMixin
from student.models import CourseEnrollment
from student.tests.factories import UserFactory, CourseEnrollmentFactory
from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory

from ..forms import BlockListGetForm


@ddt.ddt
20
class TestBlockListGetForm(FormTestMixin, SharedModuleStoreTestCase):
Nimisha Asthagiri committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
    """
    Tests for BlockListGetForm
    """
    FORM_CLASS = BlockListGetForm

    @classmethod
    def setUpClass(cls):
        super(TestBlockListGetForm, cls).setUpClass()

        cls.course = CourseFactory.create()

    def setUp(self):
        super(TestBlockListGetForm, self).setUp()

        self.student = UserFactory.create()
        self.student2 = UserFactory.create()
        self.staff = UserFactory.create(is_staff=True)

        CourseEnrollmentFactory.create(user=self.student, course_id=self.course.id)
        CourseEnrollmentFactory.create(user=self.student2, course_id=self.course.id)

        usage_key = self.course.location
        self.initial = {'requesting_user': self.student}
        self.form_data = QueryDict(
            urlencode({
                'username': self.student.username,
                'usage_key': unicode(usage_key),
            }),
            mutable=True,
        )
        self.cleaned_data = {
52
            'all_blocks': None,
Nimisha Asthagiri committed
53 54 55 56 57 58 59 60 61
            'block_counts': set(),
            'depth': 0,
            'nav_depth': None,
            'return_type': 'dict',
            'requested_fields': {'display_name', 'type'},
            'student_view_data': set(),
            'usage_key': usage_key,
            'username': self.student.username,
            'user': self.student,
lenacom committed
62
            'block_types_filter': set(),
Nimisha Asthagiri committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
        }

    def assert_raises_permission_denied(self):
        """
        Fail unless permission is denied to the form
        """
        with self.assertRaises(PermissionDenied):
            self.get_form(expected_valid=False)

    def assert_raises_not_found(self):
        """
        Fail unless a 404 occurs
        """
        with self.assertRaises(Http404):
            self.get_form(expected_valid=False)

    def assert_equals_cleaned_data(self):
        """
        Check that the form returns the expected data
        """
        form = self.get_form(expected_valid=True)
        self.assertDictEqual(form.cleaned_data, self.cleaned_data)

    def test_basic(self):
        self.assert_equals_cleaned_data()

    #-- usage key

    def test_no_usage_key_param(self):
        self.form_data.pop('usage_key')
        self.assert_error('usage_key', "This field is required.")

    def test_invalid_usage_key(self):
        self.form_data['usage_key'] = 'invalid_usage_key'
        self.assert_error('usage_key', "'invalid_usage_key' is not a valid usage key.")

    def test_non_existent_usage_key(self):
        self.form_data['usage_key'] = self.store.make_course_usage_key(CourseLocator('non', 'existent', 'course'))
        self.assert_raises_permission_denied()

    #-- user

105 106 107 108 109 110 111 112 113 114 115 116
    @ddt.data("True", "true", True)
    def test_no_user_all_blocks_true(self, all_blocks_value):
        self.initial = {'requesting_user': self.staff}

        self.form_data.pop('username')
        self.form_data['all_blocks'] = all_blocks_value
        self.get_form(expected_valid=True)

    @ddt.data("False", "false", False)
    def test_no_user_all_blocks_false(self, all_blocks_value):
        self.initial = {'requesting_user': self.staff}

Nimisha Asthagiri committed
117
        self.form_data.pop('username')
118 119 120 121 122 123 124 125 126 127 128 129
        self.form_data['all_blocks'] = all_blocks_value
        self.assert_error('username', "This field is required unless all_blocks is requested.")

    def test_no_user_all_blocks_none(self):
        self.initial = {'requesting_user': self.staff}

        self.form_data.pop('username')
        self.assert_error('username', "This field is required unless all_blocks is requested.")

    def test_no_user_non_staff(self):
        self.form_data.pop('username')
        self.form_data['all_blocks'] = True
Nimisha Asthagiri committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
        self.assert_raises_permission_denied()

    def test_nonexistent_user_by_student(self):
        self.form_data['username'] = 'non_existent_user'
        self.assert_raises_permission_denied()

    def test_nonexistent_user_by_staff(self):
        self.initial = {'requesting_user': self.staff}
        self.form_data['username'] = 'non_existent_user'
        self.assert_raises_not_found()

    def test_other_user_by_student(self):
        self.form_data['username'] = self.student2.username
        self.assert_raises_permission_denied()

    def test_other_user_by_staff(self):
        self.initial = {'requesting_user': self.staff}
        self.get_form(expected_valid=True)

    def test_unenrolled_student(self):
        CourseEnrollment.unenroll(self.student, self.course.id)
        self.assert_raises_permission_denied()

    def test_unenrolled_staff(self):
        CourseEnrollment.unenroll(self.staff, self.course.id)
        self.initial = {'requesting_user': self.staff}
        self.form_data['username'] = self.staff.username
        self.get_form(expected_valid=True)

    def test_unenrolled_student_by_staff(self):
        CourseEnrollment.unenroll(self.student, self.course.id)
        self.initial = {'requesting_user': self.staff}
162
        self.get_form(expected_valid=True)
Nimisha Asthagiri committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237

    #-- depth

    def test_depth_integer(self):
        self.form_data['depth'] = 3
        self.cleaned_data['depth'] = 3
        self.assert_equals_cleaned_data()

    def test_depth_all(self):
        self.form_data['depth'] = 'all'
        self.cleaned_data['depth'] = None
        self.assert_equals_cleaned_data()

    def test_depth_invalid(self):
        self.form_data['depth'] = 'not_an_integer'
        self.assert_error('depth', "'not_an_integer' is not a valid depth value.")

    #-- nav depth

    def test_nav_depth(self):
        self.form_data['nav_depth'] = 3
        self.cleaned_data['nav_depth'] = 3
        self.cleaned_data['requested_fields'] |= {'nav_depth'}
        self.assert_equals_cleaned_data()

    def test_nav_depth_invalid(self):
        self.form_data['nav_depth'] = 'not_an_integer'
        self.assert_error('nav_depth', "Enter a whole number.")

    def test_nav_depth_negative(self):
        self.form_data['nav_depth'] = -1
        self.assert_error('nav_depth', "Ensure this value is greater than or equal to 0.")

    #-- return_type

    def test_return_type(self):
        self.form_data['return_type'] = 'list'
        self.cleaned_data['return_type'] = 'list'
        self.assert_equals_cleaned_data()

    def test_return_type_invalid(self):
        self.form_data['return_type'] = 'invalid_return_type'
        self.assert_error(
            'return_type',
            "Select a valid choice. invalid_return_type is not one of the available choices."
        )

    #-- requested fields

    def test_requested_fields(self):
        self.form_data.setlist('requested_fields', ['graded', 'nav_depth', 'some_other_field'])
        self.cleaned_data['requested_fields'] |= {'graded', 'nav_depth', 'some_other_field'}
        self.assert_equals_cleaned_data()

    @ddt.data('block_counts', 'student_view_data')
    def test_higher_order_field(self, field_name):
        field_value = {'block_type1', 'block_type2'}
        self.form_data.setlist(field_name, field_value)
        self.cleaned_data[field_name] = field_value
        self.cleaned_data['requested_fields'].add(field_name)
        self.assert_equals_cleaned_data()

    def test_combined_fields(self):
        # add requested fields
        self.form_data.setlist('requested_fields', ['field1', 'field2'])

        # add higher order fields
        block_types_list = {'block_type1', 'block_type2'}
        for field_name in ['block_counts', 'student_view_data']:
            self.form_data.setlist(field_name, block_types_list)
            self.cleaned_data[field_name] = block_types_list

        # verify the requested_fields in cleaned_data includes all fields
        self.cleaned_data['requested_fields'] |= {'field1', 'field2', 'student_view_data', 'block_counts'}
        self.assert_equals_cleaned_data()