Skip to content

Commit

Permalink
Add permissions test
Browse files Browse the repository at this point in the history
  • Loading branch information
tehreem-sadat committed Jan 8, 2025
1 parent 6c012fc commit 003816e
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions lms/djangoapps/course_home_api/tests/test_permissions.py
Original file line number Diff line number Diff line change
@@ -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 True, "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 True, "User with staff access on course should have masquarade access"

0 comments on commit 003816e

Please sign in to comment.