test_access.py 4.93 KB
Newer Older
1
import unittest
Jay Zoldak committed
2
import logging
3
import time
4
from mock import Mock, MagicMock, patch
5 6 7 8

from django.conf import settings
from django.test import TestCase

Jay Zoldak committed
9 10 11 12
from xmodule.course_module import CourseDescriptor
from xmodule.error_module import ErrorDescriptor
from xmodule.modulestore import Location
from xmodule.timeparse import parse_time
13 14
from xmodule.x_module import XModule, XModuleDescriptor
import courseware.access as access
15
from .factories import CourseEnrollmentAllowedFactory
16 17 18 19


class AccessTestCase(TestCase):
    def test__has_global_staff_access(self):
20 21 22 23 24
        u = Mock(is_staff=False)
        self.assertFalse(access._has_global_staff_access(u))

        u = Mock(is_staff=True)
        self.assertTrue(access._has_global_staff_access(u))
25 26

    def test__has_access_to_location(self):
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 52 53 54
        location = Location('i4x://edX/toy/course/2012_Fall')

        self.assertFalse(access._has_access_to_location(None, location,
                                                        'staff', None))
        u = Mock()
        u.is_authenticated.return_value = False
        self.assertFalse(access._has_access_to_location(u, location,
                                                        'staff', None))
        u = Mock(is_staff=True)
        self.assertTrue(access._has_access_to_location(u, location,
                                                       'instructor', None))
        # A user has staff access if they are in the staff group
        u = Mock(is_staff=False)
        g = Mock()
        g.name = 'staff_edX/toy/2012_Fall'
        u.groups.all.return_value = [g]
        self.assertTrue(access._has_access_to_location(u, location,
                                                        'staff', None))
        # A user has staff access if they are in the instructor group
        g.name = 'instructor_edX/toy/2012_Fall'
        self.assertTrue(access._has_access_to_location(u, location,
                                                        'staff', None))

        # A user has instructor access if they are in the instructor group
        g.name = 'instructor_edX/toy/2012_Fall'
        self.assertTrue(access._has_access_to_location(u, location,
                                                        'instructor', None))

Calen Pennington committed
55
        # A user does not have staff access if they are
56 57 58 59 60
        # not in either the staff or the the instructor group
        g.name = 'student_only'
        self.assertFalse(access._has_access_to_location(u, location,
                                                        'staff', None))

Calen Pennington committed
61
        # A user does not have instructor access if they are
62 63 64 65
        # not in the instructor group
        g.name = 'student_only'
        self.assertFalse(access._has_access_to_location(u, location,
                                                        'instructor', None))
66 67

    def test__has_access_string(self):
68 69 70 71 72 73 74 75
        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)

76
    def test__has_access_descriptor(self):
77 78 79
        # TODO: override DISABLE_START_DATES and test the start date branch of the method
        u = Mock()
        d = Mock()
Calen Pennington committed
80
        d.start = time.gmtime(time.time() - 86400)   # make sure the start time is in the past
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

        # 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()
        yesterday = time.gmtime(time.time() - 86400)
        tomorrow = time.gmtime(time.time() + 86400)
        c = Mock(enrollment_start=yesterday, enrollment_end=tomorrow)

        # 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

        c = Mock(enrollment_start=tomorrow, enrollment_end=tomorrow, id='edX/test/2012_Fall')

        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

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

Calen Pennington committed
113
        # TODO:
114
        # Non-staff cannot enroll outside the open enrollment period if not specifically allowed