test_permissions.py 5.56 KB
Newer Older
1 2 3
"""
Test CRUD for authorization.
"""
4 5
import copy

6
from django.contrib.auth.models import User
7 8

from contentstore.tests.utils import AjaxEnabledTestClient
9
from contentstore.utils import reverse_course_url, reverse_url
10
from student import auth
11 12
from student.roles import CourseInstructorRole, CourseStaffRole, OrgInstructorRole, OrgStaffRole
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
13 14 15 16 17 18 19 20 21 22 23 24


class TestCourseAccess(ModuleStoreTestCase):
    """
    Course-based access (as opposed to access of a non-course xblock)
    """
    def setUp(self):
        """
        Create a staff user and log them in (creating the client).

        Create a pool of users w/o granting them any permissions
        """
25
        super(TestCourseAccess, self).setUp()
26 27

        self.client = AjaxEnabledTestClient()
28
        self.client.login(username=self.user.username, password=self.user_password)
29 30

        # create a course via the view handler which has a different strategy for permissions than the factory
31
        self.course_key = self.store.make_course_key('myu', 'mydept.mycourse', 'myrun')
32
        course_url = reverse_url('course_handler')
33 34
        self.client.ajax_post(
            course_url,
35
            {
36 37
                'org': self.course_key.org,
                'number': self.course_key.course,
38
                'display_name': 'My favorite course',
39
                'run': self.course_key.run,
40
            },
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
        )

        self.users = self._create_users()

    def _create_users(self):
        """
        Create 8 users and return them
        """
        users = []
        for i in range(8):
            username = "user{}".format(i)
            email = "test+user{}@edx.org".format(i)
            user = User.objects.create_user(username, email, 'foo')
            user.is_active = True
            user.save()
            users.append(user)
        return users

    def tearDown(self):
        """
        Reverse the setup
        """
        self.client.logout()
        ModuleStoreTestCase.tearDown(self)

    def test_get_all_users(self):
        """
        Test getting all authors for a course where their permissions run the gamut of allowed group
        types.
        """
71 72
        # first check the course creator.has explicit access (don't use has_access as is_staff
        # will trump the actual test)
73
        self.assertTrue(
74
            CourseInstructorRole(self.course_key).has_user(self.user),
75 76 77
            "Didn't add creator as instructor."
        )
        users = copy.copy(self.users)
78
        # doesn't use role.users_with_role b/c it's verifying the roles.py behavior
79 80
        user_by_role = {}
        # add the misc users to the course in different groups
81
        for role in [CourseInstructorRole, CourseStaffRole, OrgStaffRole, OrgInstructorRole]:
82
            user_by_role[role] = []
83 84 85 86 87
            # Org-based roles are created via org name, rather than course_key
            if (role is OrgStaffRole) or (role is OrgInstructorRole):
                group = role(self.course_key.org)
            else:
                group = role(self.course_key)
88
            # NOTE: this loop breaks the roles.py abstraction by purposely assigning
89
            # users to one of each possible groupname in order to test that has_course_author_access
90
            # and remove_user work
91 92 93
            user = users.pop()
            group.add_users(user)
            user_by_role[role].append(user)
94
            self.assertTrue(auth.has_course_author_access(user, self.course_key), "{} does not have access".format(user))
95 96 97

        course_team_url = reverse_course_url('course_team_handler', self.course_key)
        response = self.client.get_html(course_team_url)
98
        for role in [CourseInstructorRole, CourseStaffRole]:  # Global and org-based roles don't appear on this page
99 100
            for user in user_by_role[role]:
                self.assertContains(response, user.email)
101

102
        # test copying course permissions
103
        copy_course_key = self.store.make_course_key('copyu', 'copydept.mycourse', 'myrun')
104 105 106 107 108
        for role in [CourseInstructorRole, CourseStaffRole, OrgStaffRole, OrgInstructorRole]:
            if (role is OrgStaffRole) or (role is OrgInstructorRole):
                auth.add_users(
                    self.user,
                    role(copy_course_key.org),
109 110
                    *role(self.course_key.org).users_with_role()
                )
111 112 113 114 115 116
            else:
                auth.add_users(
                    self.user,
                    role(copy_course_key),
                    *role(self.course_key).users_with_role()
                )
117 118
        # verify access in copy course and verify that removal from source course w/ the various
        # groupnames works
119
        for role in [CourseInstructorRole, CourseStaffRole, OrgStaffRole, OrgInstructorRole]:
120
            for user in user_by_role[role]:
121 122 123
                # forcefully decache the groups: premise is that any real request will not have
                # multiple objects repr the same user but this test somehow uses different instance
                # in above add_users call
124 125
                if hasattr(user, '_roles'):
                    del user._roles
126

127
                self.assertTrue(auth.has_course_author_access(user, copy_course_key), "{} no copy access".format(user))
128 129 130 131
                if (role is OrgStaffRole) or (role is OrgInstructorRole):
                    auth.remove_users(self.user, role(self.course_key.org), user)
                else:
                    auth.remove_users(self.user, role(self.course_key), user)
132
                self.assertFalse(auth.has_course_author_access(user, self.course_key), "{} remove didn't work".format(user))