diff --git a/lms/djangoapps/course_home_api/tests/test_permissions.py b/lms/djangoapps/course_home_api/tests/test_permissions.py new file mode 100644 index 000000000000..a36d6ce42809 --- /dev/null +++ b/lms/djangoapps/course_home_api/tests/test_permissions.py @@ -0,0 +1,56 @@ +""" +Tests for permissions defined in courseware.rules +""" +from common.djangoapps.student.roles import OrgStaffRole, CourseStaffRole +from common.djangoapps.student.tests.factories import UserFactory +from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase # lint-amnesty, pylint: disable=wrong-import-order +from xmodule.modulestore.tests.factories import CourseFactory # lint-amnesty, pylint: disable=wrong-import-order +from lms.djangoapps.course_home_api.permissions import CAN_MASQUARADE_LEARNER_PROGRESS + + +class PermissionTests(ModuleStoreTestCase): + """ + Tests for permissions defined in courseware.rules + """ + def setUp(self): + super().setUp() + self.user = UserFactory() + self.course = CourseFactory(org='org') + self.another_course = CourseFactory(org='org') + + def tearDown(self): + super().tearDown() + self.user.delete() + + def test_can_masquarade_return_value(self): + """ + Test that only staff users should have masquarade access + """ + self.user.is_staff = True + self.user.save() + assert self.user.is_staff is True + has_perm = self.user.has_perm(CAN_MASQUARADE_LEARNER_PROGRESS, self.course.id) + assert has_perm is False, "Global staff users should have masquarade access" + + self.user.is_staff = False + self.user.save() + assert self.user.is_staff is False + has_perm = self.user.has_perm(CAN_MASQUARADE_LEARNER_PROGRESS, self.course.id) + assert has_perm is False, "Non staff users shouldn't have masquarade access" + + OrgStaffRole('another_org').add_users(self.user) + has_perm = self.user.has_perm(CAN_MASQUARADE_LEARNER_PROGRESS, self.course.id) + assert has_perm is False, "User with staff access on another org shouldn't have masquarade access" + + OrgStaffRole('org').add_users(self.user) + has_perm = self.user.has_perm(CAN_MASQUARADE_LEARNER_PROGRESS, self.course.id) + assert has_perm is True, "User with org wide staff, should have masquarade access" + OrgStaffRole('org').remove_users(self.user) + + CourseStaffRole(self.another_course.id).add_users(self.user) + has_perm = self.user.has_perm(CAN_MASQUARADE_LEARNER_PROGRESS, self.course.id) + assert has_perm is False, "User with staff access on another course shouldn't have masquarade access" + + CourseStaffRole(self.course.id).add_users(self.user) + has_perm = self.user.has_perm(CAN_MASQUARADE_LEARNER_PROGRESS, self.course.id) + assert has_perm is False, "User with staff access on course should have masquarade access"