def test_has_access_in_preview_mode_with_group(self): """ Test that a user masquerading as a member of a group sees appropriate content in preview mode. """ # Note about UserPartition and UserPartition Group IDs: these must not conflict with IDs used # by dynamic user partitions. partition_id = MINIMUM_STATIC_PARTITION_ID group_0_id = MINIMUM_STATIC_PARTITION_ID + 1 group_1_id = MINIMUM_STATIC_PARTITION_ID + 2 user_partition = UserPartition( partition_id, 'Test User Partition', '', [Group(group_0_id, 'Group 1'), Group(group_1_id, 'Group 2')], scheme_id='cohort' ) self.course.user_partitions.append(user_partition) self.course.cohort_config = {'cohorted': True} chapter = ItemFactory.create(category="chapter", parent_location=self.course.location) chapter.group_access = {partition_id: [group_0_id]} modulestore().update_item(self.course, ModuleStoreEnum.UserID.test) # User should not be able to preview when masquerading as student (and not in the group above). with patch('lms.djangoapps.courseware.access.get_user_role') as mock_user_role: mock_user_role.return_value = 'student' self.assertFalse( bool(access.has_access(self.global_staff, 'load', chapter, course_key=self.course.id)) ) # Should be able to preview when in staff or instructor role. for mocked_role in ['staff', 'instructor']: with patch('lms.djangoapps.courseware.access.get_user_role') as mock_user_role: mock_user_role.return_value = mocked_role self.assertTrue( bool(access.has_access(self.global_staff, 'load', chapter, course_key=self.course.id)) ) # Now install masquerade group and set staff as a member of that. self.assertEqual(200, masquerade_as_group_member(self.global_staff, self.course, partition_id, group_0_id)) # Can load the chapter since user is in the group. self.assertTrue( bool(access.has_access(self.global_staff, 'load', chapter, course_key=self.course.id)) ) # Move the user to be a part of the second group. self.assertEqual(200, masquerade_as_group_member(self.global_staff, self.course, partition_id, group_1_id)) # Cannot load the chapter since user is in a different group. self.assertFalse( bool(access.has_access(self.global_staff, 'load', chapter, course_key=self.course.id)) )
def ensure_masquerade_as_group_member(self, partition_id, group_id): """ Installs a masquerade for the test_user and test course, to enable the user to masquerade as belonging to the specific partition/group combination. Also verifies that the call to install the masquerade was successful. Arguments: partition_id (int): the integer partition id, referring to partitions already configured in the course. group_id (int); the integer group id, within the specified partition. """ assert 200 == masquerade_as_group_member(self.test_user, self.course, partition_id, group_id)