test_access.py 6.55 KB
Newer Older
1 2 3
import courseware.access as access
import datetime

4
from mock import Mock
5 6

from django.test import TestCase
Julia Hansbrough committed
7
from django.test.utils import override_settings
8

9 10
from courseware.tests.factories import UserFactory, CourseEnrollmentAllowedFactory, StaffFactory, InstructorFactory
from student.tests.factories import AnonymousUserFactory
Jay Zoldak committed
11
from xmodule.modulestore import Location
Julia Hansbrough committed
12
from courseware.tests.tests import TEST_DATA_MIXED_MODULESTORE
13
import pytz
14

Julia Hansbrough committed
15

16
# pylint: disable=protected-access
Julia Hansbrough committed
17
@override_settings(MODULESTORE=TEST_DATA_MIXED_MODULESTORE)
18
class AccessTestCase(TestCase):
19 20 21
    """
    Tests for the various access controls on the student dashboard
    """
22

23 24 25 26 27 28 29
    def setUp(self):
        self.course = Location('i4x://edX/toy/course/2012_Fall')
        self.anonymous_user = AnonymousUserFactory()
        self.student = UserFactory()
        self.global_staff = UserFactory(is_staff=True)
        self.course_staff = StaffFactory(course=self.course)
        self.course_instructor = InstructorFactory(course=self.course)
30 31

    def test__has_access_to_location(self):
32 33 34 35 36 37 38
        self.assertFalse(access._has_access_to_location(None, self.course, 'staff', None))

        self.assertFalse(access._has_access_to_location(self.anonymous_user, self.course, 'staff', None))
        self.assertFalse(access._has_access_to_location(self.anonymous_user, self.course, 'instructor', None))

        self.assertTrue(access._has_access_to_location(self.global_staff, self.course, 'staff', None))
        self.assertTrue(access._has_access_to_location(self.global_staff, self.course, 'instructor', None))
39 40

        # A user has staff access if they are in the staff group
41 42
        self.assertTrue(access._has_access_to_location(self.course_staff, self.course, 'staff', None))
        self.assertFalse(access._has_access_to_location(self.course_staff, self.course, 'instructor', None))
43

44 45 46
        # A user has staff and instructor access if they are in the instructor group
        self.assertTrue(access._has_access_to_location(self.course_instructor, self.course, 'staff', None))
        self.assertTrue(access._has_access_to_location(self.course_instructor, self.course, 'instructor', None))
47

48
        # A user does not have staff or instructor access if they are
49
        # not in either the staff or the the instructor group
50 51
        self.assertFalse(access._has_access_to_location(self.student, self.course, 'staff', None))
        self.assertFalse(access._has_access_to_location(self.student, self.course, 'instructor', None))
52 53

    def test__has_access_string(self):
54 55 56 57 58 59 60 61
        u = Mock(is_staff=True)
        self.assertFalse(access._has_access_string(u, 'not_global', 'staff', None))

        u._has_global_staff_access.return_value = True
        self.assertTrue(access._has_access_string(u, 'global', 'staff', None))

        self.assertRaises(ValueError, access._has_access_string, u, 'global', 'not_staff', None)

62
    def test__has_access_descriptor(self):
63 64 65
        # TODO: override DISABLE_START_DATES and test the start date branch of the method
        u = Mock()
        d = Mock()
66
        d.start = datetime.datetime.now(pytz.utc) - datetime.timedelta(days=1)  # make sure the start time is in the past
67 68 69 70 71 72 73

        # Always returns true because DISABLE_START_DATES is set in test.py
        self.assertTrue(access._has_access_descriptor(u, d, 'load'))
        self.assertRaises(ValueError, access._has_access_descriptor, u, d, 'not_load_or_staff')

    def test__has_access_course_desc_can_enroll(self):
        u = Mock()
74 75
        yesterday = datetime.datetime.now(pytz.utc) - datetime.timedelta(days=1)
        tomorrow = datetime.datetime.now(pytz.utc) + datetime.timedelta(days=1)
76
        c = Mock(enrollment_start=yesterday, enrollment_end=tomorrow, enrollment_domain='')
77 78 79 80 81 82 83 84 85

        # User can enroll if it is between the start and end dates
        self.assertTrue(access._has_access_course_desc(u, c, 'enroll'))

        # User can enroll if authenticated and specifically allowed for that course
        # even outside the open enrollment period
        u = Mock(email='test@edx.org', is_staff=False)
        u.is_authenticated.return_value = True

86
        c = Mock(enrollment_start=tomorrow, enrollment_end=tomorrow, id='edX/test/2012_Fall', enrollment_domain='')
87 88 89 90 91 92 93 94 95

        allowed = CourseEnrollmentAllowedFactory(email=u.email, course_id=c.id)

        self.assertTrue(access._has_access_course_desc(u, c, 'enroll'))

        # Staff can always enroll even outside the open enrollment period
        u = Mock(email='test@edx.org', is_staff=True)
        u.is_authenticated.return_value = True

96
        c = Mock(enrollment_start=tomorrow, enrollment_end=tomorrow, id='edX/test/Whenever', enrollment_domain='')
97 98
        self.assertTrue(access._has_access_course_desc(u, c, 'enroll'))

Calen Pennington committed
99
        # TODO:
100
        # Non-staff cannot enroll outside the open enrollment period if not specifically allowed
101 102 103 104

    def test__user_passed_as_none(self):
        """Ensure has_access handles a user being passed as null"""
        access.has_access(None, 'global', 'staff', None)
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

class UserRoleTestCase(TestCase):
    """
    Tests for user roles.
    """
    def setUp(self):
        self.course = Location('i4x://edX/toy/course/2012_Fall')
        self.anonymous_user = AnonymousUserFactory()
        self.student = UserFactory()
        self.global_staff = UserFactory(is_staff=True)
        self.course_staff = StaffFactory(course=self.course)
        self.course_instructor = InstructorFactory(course=self.course)

    def test_user_role_staff(self):
        """Ensure that user role is student for staff masqueraded as student."""
        self.assertEqual(
            'staff',
            access.get_user_role(self.course_staff, self.course.course_id)
        )
        # Masquerade staff
        self.course_staff.masquerade_as_student = True
        self.assertEqual(
            'student',
            access.get_user_role(self.course_staff, self.course.course_id)
        )

    def test_user_role_instructor(self):
        """Ensure that user role is student for instructor masqueraded as student."""
        self.assertEqual(
            'instructor',
            access.get_user_role(self.course_instructor, self.course.course_id)
        )
        # Masquerade instructor
        self.course_instructor.masquerade_as_student = True
        self.assertEqual(
            'student',
            access.get_user_role(self.course_instructor, self.course.course_id)
        )

    def test_user_role_anonymous(self):
        """Ensure that user role is student for anonymous user."""
        self.assertEqual(
            'student',
            access.get_user_role(self.anonymous_user, self.course.course_id)
        )