test_authz.py 9 KB
Newer Older
1 2 3 4 5 6
"""
Tests authz.py
"""
import mock

from django.test import TestCase
7
from django.contrib.auth.models import User, AnonymousUser
8
from django.core.exceptions import PermissionDenied
9

10 11 12
from student.roles import CourseInstructorRole, CourseStaffRole, CourseCreatorRole
from student.tests.factories import AdminFactory
from student.auth import has_access, add_users, remove_users
13
from opaque_keys.edx.locations import SlashSeparatedCourseKey
14

cahrens committed
15

16 17 18 19
class CreatorGroupTest(TestCase):
    """
    Tests for the course creator group.
    """
cahrens committed
20

21 22 23
    def setUp(self):
        """ Test case setup """
        self.user = User.objects.create_user('testuser', 'test+courses@edx.org', 'foo')
24 25
        self.admin = User.objects.create_user('Mark', 'admin+courses@edx.org', 'foo')
        self.admin.is_staff = True
26 27 28

    def test_creator_group_not_enabled(self):
        """
29
        Tests that CourseCreatorRole().has_user always returns True if ENABLE_CREATOR_GROUP
30 31
        and DISABLE_COURSE_CREATION are both not turned on.
        """
32
        self.assertTrue(has_access(self.user, CourseCreatorRole()))
33 34 35

    def test_creator_group_enabled_but_empty(self):
        """ Tests creator group feature on, but group empty. """
36
        with mock.patch.dict('django.conf.settings.FEATURES', {"ENABLE_CREATOR_GROUP": True}):
37
            self.assertFalse(has_access(self.user, CourseCreatorRole()))
38

39
            # Make user staff. This will cause CourseCreatorRole().has_user to return True.
40
            self.user.is_staff = True
41
            self.assertTrue(has_access(self.user, CourseCreatorRole()))
42 43 44

    def test_creator_group_enabled_nonempty(self):
        """ Tests creator group feature on, user added. """
45
        with mock.patch.dict('django.conf.settings.FEATURES', {"ENABLE_CREATOR_GROUP": True}):
46 47
            add_users(self.admin, CourseCreatorRole(), self.user)
            self.assertTrue(has_access(self.user, CourseCreatorRole()))
48 49 50

            # check that a user who has not been added to the group still returns false
            user_not_added = User.objects.create_user('testuser2', 'test+courses2@edx.org', 'foo2')
51
            self.assertFalse(has_access(user_not_added, CourseCreatorRole()))
52

53 54 55
            # remove first user from the group and verify that CourseCreatorRole().has_user now returns false
            remove_users(self.admin, CourseCreatorRole(), self.user)
            self.assertFalse(has_access(self.user, CourseCreatorRole()))
56 57 58

    def test_course_creation_disabled(self):
        """ Tests that the COURSE_CREATION_DISABLED flag overrides course creator group settings. """
59
        with mock.patch.dict('django.conf.settings.FEATURES',
cahrens committed
60
                             {'DISABLE_COURSE_CREATION': True, "ENABLE_CREATOR_GROUP": True}):
61
            # Add user to creator group.
62
            add_users(self.admin, CourseCreatorRole(), self.user)
63 64

            # DISABLE_COURSE_CREATION overrides (user is not marked as staff).
65
            self.assertFalse(has_access(self.user, CourseCreatorRole()))
66

67
            # Mark as staff. Now CourseCreatorRole().has_user returns true.
68
            self.user.is_staff = True
69 70 71 72 73 74 75 76 77 78
            self.assertTrue(has_access(self.user, CourseCreatorRole()))

            # Remove user from creator group. CourseCreatorRole().has_user still returns true because is_staff=True
            remove_users(self.admin, CourseCreatorRole(), self.user)
            self.assertTrue(has_access(self.user, CourseCreatorRole()))

    def test_add_user_not_authenticated(self):
        """
        Tests that adding to creator group fails if user is not authenticated
        """
79 80 81 82
        with mock.patch.dict(
            'django.conf.settings.FEATURES',
            {'DISABLE_COURSE_CREATION': False, "ENABLE_CREATOR_GROUP": True}
        ):
83 84 85 86
            anonymous_user = AnonymousUser()
            role = CourseCreatorRole()
            add_users(self.admin, role, anonymous_user)
            self.assertFalse(has_access(anonymous_user, role))
87

88 89 90 91
    def test_add_user_not_active(self):
        """
        Tests that adding to creator group fails if user is not active
        """
92 93 94 95
        with mock.patch.dict(
            'django.conf.settings.FEATURES',
            {'DISABLE_COURSE_CREATION': False, "ENABLE_CREATOR_GROUP": True}
        ):
96 97 98
            self.user.is_active = False
            add_users(self.admin, CourseCreatorRole(), self.user)
            self.assertFalse(has_access(self.user, CourseCreatorRole()))
99

100 101 102
    def test_add_user_to_group_requires_staff_access(self):
        with self.assertRaises(PermissionDenied):
            self.admin.is_staff = False
103
            add_users(self.admin, CourseCreatorRole(), self.user)
104 105

        with self.assertRaises(PermissionDenied):
106
            add_users(self.user, CourseCreatorRole(), self.user)
107 108 109 110

    def test_add_user_to_group_requires_active(self):
        with self.assertRaises(PermissionDenied):
            self.admin.is_active = False
111
            add_users(self.admin, CourseCreatorRole(), self.user)
112 113 114

    def test_add_user_to_group_requires_authenticated(self):
        with self.assertRaises(PermissionDenied):
115
            self.admin.is_authenticated = mock.Mock(return_value=False)
116
            add_users(self.admin, CourseCreatorRole(), self.user)
117 118 119 120

    def test_remove_user_from_group_requires_staff_access(self):
        with self.assertRaises(PermissionDenied):
            self.admin.is_staff = False
121
            remove_users(self.admin, CourseCreatorRole(), self.user)
122 123 124 125

    def test_remove_user_from_group_requires_active(self):
        with self.assertRaises(PermissionDenied):
            self.admin.is_active = False
126
            remove_users(self.admin, CourseCreatorRole(), self.user)
127 128 129

    def test_remove_user_from_group_requires_authenticated(self):
        with self.assertRaises(PermissionDenied):
130
            self.admin.is_authenticated = mock.Mock(return_value=False)
131
            remove_users(self.admin, CourseCreatorRole(), self.user)
132

133 134 135 136 137 138 139 140

class CourseGroupTest(TestCase):
    """
    Tests for instructor and staff groups for a particular course.
    """

    def setUp(self):
        """ Test case setup """
141
        self.global_admin = AdminFactory()
142 143
        self.creator = User.objects.create_user('testcreator', 'testcreator+courses@edx.org', 'foo')
        self.staff = User.objects.create_user('teststaff', 'teststaff+courses@edx.org', 'foo')
144
        self.course_key = SlashSeparatedCourseKey('mitX', '101', 'test')
145 146 147 148 149 150

    def test_add_user_to_course_group(self):
        """
        Tests adding user to course group (happy path).
        """
        # Create groups for a new course (and assign instructor role to the creator).
151 152 153 154
        self.assertFalse(has_access(self.creator, CourseInstructorRole(self.course_key)))
        add_users(self.global_admin, CourseInstructorRole(self.course_key), self.creator)
        add_users(self.global_admin, CourseStaffRole(self.course_key), self.creator)
        self.assertTrue(has_access(self.creator, CourseInstructorRole(self.course_key)))
155 156

        # Add another user to the staff role.
157 158 159
        self.assertFalse(has_access(self.staff, CourseStaffRole(self.course_key)))
        add_users(self.creator, CourseStaffRole(self.course_key), self.staff)
        self.assertTrue(has_access(self.staff, CourseStaffRole(self.course_key)))
160 161 162 163 164

    def test_add_user_to_course_group_permission_denied(self):
        """
        Verifies PermissionDenied if caller of add_user_to_course_group is not instructor role.
        """
165 166
        add_users(self.global_admin, CourseInstructorRole(self.course_key), self.creator)
        add_users(self.global_admin, CourseStaffRole(self.course_key), self.creator)
167
        with self.assertRaises(PermissionDenied):
168
            add_users(self.staff, CourseStaffRole(self.course_key), self.staff)
169

170
    def test_remove_user_from_course_group(self):
171 172 173
        """
        Tests removing user from course group (happy path).
        """
174 175
        add_users(self.global_admin, CourseInstructorRole(self.course_key), self.creator)
        add_users(self.global_admin, CourseStaffRole(self.course_key), self.creator)
176

177 178
        add_users(self.creator, CourseStaffRole(self.course_key), self.staff)
        self.assertTrue(has_access(self.staff, CourseStaffRole(self.course_key)))
179

180 181
        remove_users(self.creator, CourseStaffRole(self.course_key), self.staff)
        self.assertFalse(has_access(self.staff, CourseStaffRole(self.course_key)))
182

183 184
        remove_users(self.creator, CourseInstructorRole(self.course_key), self.creator)
        self.assertFalse(has_access(self.creator, CourseInstructorRole(self.course_key)))
185 186 187 188 189

    def test_remove_user_from_course_group_permission_denied(self):
        """
        Verifies PermissionDenied if caller of remove_user_from_course_group is not instructor role.
        """
190
        add_users(self.global_admin, CourseInstructorRole(self.course_key), self.creator)
191
        another_staff = User.objects.create_user('another', 'teststaff+anothercourses@edx.org', 'foo')
192
        add_users(self.global_admin, CourseStaffRole(self.course_key), self.creator, self.staff, another_staff)
193
        with self.assertRaises(PermissionDenied):
194
            remove_users(self.staff, CourseStaffRole(self.course_key), another_staff)